From f49f3ef9f9651f79f083100220f46d0444fd11a2 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 31 Mar 2022 08:51:34 +0800 Subject: [PATCH] 1 --- webapp/models/Nft.php | 21 +++++++++++ webapp/services/NftService.php | 64 ++++++++++++++++++++++++++++++---- 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/webapp/models/Nft.php b/webapp/models/Nft.php index 11860f9a..932da042 100644 --- a/webapp/models/Nft.php +++ b/webapp/models/Nft.php @@ -7,6 +7,10 @@ use phpcommon\SqlHelper; class Nft extends BaseModel { + const HERO_TYPE = 0; + const EQUIP_TYPE = 1; + const CHIP_TYPE = 2; + public function getNftList($account) { $nftList = array(); @@ -23,6 +27,23 @@ class Nft extends BaseModel { return $nftList; } + public function getNftListByType($account, $type) + { + $nftList = array(); + SqlHelper::ormSelect( + myself()->_getMarketMysql(), + 't_nft', + array( + 'owner_address' => $account, + 'token_type' => $type, + ), + function ($row) use(&$nftList) { + array_push($nftList, $row); + } + ); + return $nftList; + } + public function getNft($tokenId) { $row = SqlHelper::ormSelectOne( diff --git a/webapp/services/NftService.php b/webapp/services/NftService.php index 001b476b..1396bfbd 100644 --- a/webapp/services/NftService.php +++ b/webapp/services/NftService.php @@ -2,39 +2,89 @@ namespace services; +require_once('models/Nft.php'); + +use models\Nft; + class NftService extends BaseService { - private static $heros = null; - private static $equips = null; - private static $chips = null; + private static $heroList = null; + private static $heroHash = null; + private static $equipList = null; + private static $equipHash = null; + private static $chipList = null; + private static $chipHash = null; public static function isHeroOwner($tokenId) { + self::mustBeHeros(); + $nftDB = getXVal(self::heroHash, $tokenId); + return $nftDb && $nftDB['owner_address'] == myself()->_getOpenId(); } public static function isEquipOwner($tokenId) { - + self::mustBeEquips(); + $nftDb = getXVal(self::equipHash, $tokenId); + return $nftDb && $nftDB['owner_address'] == myself()->_getOpenId(); } public static function isChipOwner($tokenId) { - + self::mustBeChips(); + $nftDb = getXVal(self::chipHash, $tokenId); + return $nftDb && $nftDB['owner_address'] == myself()->_getOpenId(); } public static function getHeros() { - + self::mustBeHeros(); + return self::heroList; } public static function getEquips() { - + self::mustBeEquips(); + return self::equipList; } public static function getChips() { + self::mustBeChips(); + return self::chipList; + } + private static function mustBeHeros() + { + 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; + } + } + } + + 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; + } + } + } + + 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; + } + } } }