This commit is contained in:
aozhiwei 2022-04-01 10:36:36 +08:00
parent 6ed02245ef
commit e2add856f0
5 changed files with 56 additions and 65 deletions

View File

@ -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',

View File

@ -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',

View File

@ -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',

View File

@ -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(),

View File

@ -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));
}
}