From e2add856f029ff178fb51874d2ca53dc21a09eda Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 1 Apr 2022 10:36:36 +0800 Subject: [PATCH] 1 --- webapp/models/Bag.php | 7 ++- webapp/models/Gun.php | 4 +- webapp/models/Hero.php | 4 +- webapp/models/Nft.php | 6 +- webapp/services/NftService.php | 100 ++++++++++++++------------------- 5 files changed, 56 insertions(+), 65 deletions(-) diff --git a/webapp/models/Bag.php b/webapp/models/Bag.php index 4141e345..509fdb48 100644 --- a/webapp/models/Bag.php +++ b/webapp/models/Bag.php @@ -25,6 +25,11 @@ class Bag extends BaseModel { ); if ($row) { $row['item_uniid'] = $row['idx']; + if ($row['account_id'] != myself()->_getAccountId()) { + if (!NftService::isEquipOwner(myself()->_getOpenId(), $row['token_id'])) { + $row = null; + } + } } return $row; } @@ -98,7 +103,7 @@ class Bag extends BaseModel { $cb($row); } ); - foreach (NftService::getChips() as $nftDb) { + foreach (NftService::getChips(myself()->_getOpenId()) as $nftDb) { $row = SqlHelper::ormSelectOne( myself()->_getSelfMysql(), 't_bag', diff --git a/webapp/models/Gun.php b/webapp/models/Gun.php index d51dfbb2..b32ebfc8 100644 --- a/webapp/models/Gun.php +++ b/webapp/models/Gun.php @@ -33,7 +33,7 @@ class Gun extends BaseModel { if ($row) { $row['gun_uniid'] = $row['idx']; if ($row['account_id'] != myself()->_getAccountId()) { - if (!NftService::isEquipOwner($row['token_id'])) { + if (!NftService::isEquipOwner(myself()->_getOpenId(), $row['token_id'])) { $row = null; } } @@ -80,7 +80,7 @@ class Gun extends BaseModel { $cb($row); } ); - foreach (NftService::getEquips() as $nftDb) { + foreach (NftService::getEquips(myself()->_getOpenId()) as $nftDb) { $row = SqlHelper::ormSelectOne( myself()->_getSelfMysql(), 't_gun', diff --git a/webapp/models/Hero.php b/webapp/models/Hero.php index 3ef22dd5..1c4df212 100644 --- a/webapp/models/Hero.php +++ b/webapp/models/Hero.php @@ -35,7 +35,7 @@ class Hero extends BaseModel { if ($row) { $row['hero_uniid'] = $row['idx']; if ($row['account_id'] != myself()->_getAccountId()) { - if (!NftService::isHeroOwner($row['token_id'])) { + if (!NftService::isHeroOwner(myself()->_getOpenId(), $row['token_id'])) { $row = null; } } @@ -66,7 +66,7 @@ class Hero extends BaseModel { $cb($row); } ); - foreach (NftService::getHeros() as $nftDb) { + foreach (NftService::getHeros(myself()->_getOpenId()) as $nftDb) { $row = SqlHelper::ormSelectOne( myself()->_getSelfMysql(), 't_hero', diff --git a/webapp/models/Nft.php b/webapp/models/Nft.php index 2bc5e0e3..3f945e98 100644 --- a/webapp/models/Nft.php +++ b/webapp/models/Nft.php @@ -38,7 +38,7 @@ class Nft extends BaseModel { return self::NONE_TYPE; } - public function getNftList($account) + public static function getNftList($account) { $nftList = array(); SqlHelper::ormSelect( @@ -54,7 +54,7 @@ class Nft extends BaseModel { return $nftList; } - public function getNftListByType($account, $type) + public static function getNftListByType($account, $type) { $nftList = array(); SqlHelper::ormSelect( @@ -71,7 +71,7 @@ class Nft extends BaseModel { return $nftList; } - public function getNft($tokenId) + public static function getNft($tokenId) { $row = SqlHelper::ormSelectOne( myself()->_getMarketMysql(), diff --git a/webapp/services/NftService.php b/webapp/services/NftService.php index 49c44668..11712c0e 100644 --- a/webapp/services/NftService.php +++ b/webapp/services/NftService.php @@ -8,85 +8,71 @@ use models\Nft; class NftService extends BaseService { - private static $heroList = null; - private static $heroHash = null; - private static $equipList = null; - private static $equipHash = null; - private static $chipList = null; - private static $chipHash = null; + private static $userData = array(); + private static $nftCfg = array( + 'hero' => Nft::HERO_TYPE, + 'equip' => Nft::EQUIP_TYPE, + 'chip' => Nft::CHIP_TYPE, + ); - public static function isHeroOwner($tokenId) + public static function isHeroOwner($openId, $tokenId) { - self::mustBeHeros(); - $nftDB = getXVal(self::$heroHash, $tokenId); - return $nftDb && $nftDB['owner_address'] == myself()->_getOpenId(); + return self::internalIsOwner($openId, 'hero', $tokenId); } - public static function isEquipOwner($tokenId) + public static function isEquipOwner($openId, $tokenId) { - self::mustBeEquips(); - $nftDb = getXVal(self::$equipHash, $tokenId); - return $nftDb && $nftDB['owner_address'] == myself()->_getOpenId(); + return self::internalIsOwner($openId, 'equip', $tokenId); } - public static function isChipOwner($tokenId) + public static function isChipOwner($openId, $tokenId) { - self::mustBeChips(); - $nftDb = getXVal(self::$chipHash, $tokenId); - return $nftDb && $nftDB['owner_address'] == myself()->_getOpenId(); + return self::internalIsOwner($openId, 'chip', $tokenId); } - public static function getHeros() + public static function getHeros($openId) { - self::mustBeHeros(); - return self::$heroList; + return self::internalGetList($openId, 'hero'); } - public static function getEquips() + public static function getEquips($openId) { - self::mustBeEquips(); - return self::$equipList; + return self::internalGetList($openId, 'equip'); } - public static function getChips() + public static function getChips($openId) { - self::mustBeChips(); - return self::$chipList; + return self::internalGetList($openId, 'chip'); } - private static function mustBeHeros() + private static function internalGetList($openId, $name) { - if (is_null(self::$heroList)) { - self::$heroList = Nft::GetNftListByType(myself()->_getOpenId(), Nft::HERO_TYPE); - self::$heroHash = array(); - foreach (self::$heroList as $nftDb) { - self::$heroHash[$nftDb['token_id']] = $nftDb; - } - //error_log(json_encode(self::$heroList)); + self::loadNft($openId, $name); + return getXVal(self::$userData[$openid], $name . 'List'); + } + + private static function internalIsOwner($openId, $name, $tokenId) + { + self::loadNft($openId, $name); + $nftHash = getXVal(self::$userData[$openid], $name . 'Hash', array()); + $nftDB = getXVal($nftHash, $tokenId); + return $nftDb && $nftDB['owner_address'] == $openId; + } + + private static function loadNft($openId, $name) + { + if (!in_array($openId, self::$userData)) { + self::$userData[$openId] = array(); } - } - - private static function mustBeEquips() - { - if (is_null(self::$equipList)) { - self::$equipList = Nft::GetNftListByType(myself()->_getOpenId(), Nft::EQUIP_TYPE); - self::$equipHash = array(); - foreach (self::$equipList as $nftDb) { - self::$equipHash[$nftDb['token_id']] = $nftDb; + if (!in_array($name . 'List', self::$userData[$openId])) { + $listName = $name . 'List'; + $hashName = $name . 'Hash'; + $tokenType = self::$nftCfg[$name]; + self::$userData[$openId][$listName] = Nft::GetNftListByType($openId, $tokenType); + self::$userData[$openId][$hashName] = array(); + foreach (self::$userData[$openId][$listName] as $nftDb) { + self::$userData[$openId][$hashName][$nftDb['token_id']] = $nftDb; } - //error_log(json_encode(self::$equipList)); - } - } - - private static function mustBeChips() - { - if (is_null(self::$chipList)) { - self::$chipList = Nft::GetNftListByType(myself()->_getOpenId(), Nft::CHIP_TYPE); - self::$chipHash = array(); - foreach (self::$chipList as $nftDb) { - self::$chipHash[$nftDb['token_id']] = $nftDb; - } - //error_log(json_encode(self::$chipList)); } }