From a037de92f4a464a47c81e72d647ec02244e14738 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 30 Mar 2022 17:10:41 +0800 Subject: [PATCH] hero ok --- webapp/controller/HeroController.class.php | 13 +--- webapp/models/Hero.php | 91 ++++++++++++++-------- webapp/services/NftService.php | 20 +++++ 3 files changed, 80 insertions(+), 44 deletions(-) create mode 100644 webapp/services/NftService.php diff --git a/webapp/controller/HeroController.class.php b/webapp/controller/HeroController.class.php index d8a73318..571e566e 100644 --- a/webapp/controller/HeroController.class.php +++ b/webapp/controller/HeroController.class.php @@ -25,16 +25,9 @@ class HeroController extends BaseAuthedController { public function heroList() { $heroList = array(); - SqlHelper::ormSelect( - $this->_getSelfMysql(), - 't_hero', - array( - 'account_id' => $this->_getAccountId() - ), - function ($row) use(&$heroList) { - array_push($heroList, Hero::toDto($row)); - } - ); + Hero::getHeroList(function ($row) use(&$heroList) { + array_push($heroList, Hero::toDto($row)); + }); $this->_rspData(array( 'hero_list' => $heroList )); diff --git a/webapp/models/Hero.php b/webapp/models/Hero.php index a02cc677..49620651 100644 --- a/webapp/models/Hero.php +++ b/webapp/models/Hero.php @@ -7,6 +7,7 @@ require_once('mt/HeroLevel.php'); require_once('mt/HeroQuality.php'); require_once('mt/AttrHelper.php'); require_once('models/HeroSkin.php'); +require_once('services/NftService.php'); use mt; use phpcommon\SqlHelper; @@ -26,31 +27,55 @@ class Hero extends BaseModel { myself()->_getSelfMysql(), 't_hero', array( - 'account_id' => myself()->_getAccountId(), 'idx' => $heroUniId, ) ); if ($row) { $row['hero_uniid'] = $row['idx']; + if ($row['account_id'] != myself()->_getAccountId()) { + if (!services\NftService::isHeroOwner($row['token_id'])) { + $row = null; + } + } } return $row; } public static function getValidHero($heroId) { - $row = SqlHelper::ormSelectOne( - myself()->_getSelfMysql(), + $heroList = array(); + Hero::getHeroList(function ($row) use($heroId, &$heroList) { + if ($row['hero_id'] == $heroId && $row['state'] == self::GETED_STATE) { + array_push($heroList, Hero::toDto($row)); + } + }); + return !empty($heroList) ? $heroList[0] : null; + } + + public static function getHeroList($cb) + { + SqlHelper::ormSelect( + $this->_getSelfMysql(), 't_hero', array( - 'account_id' => myself()->_getAccountId(), - 'hero_id' => $heroId, - 'state' => self::GETED_STATE, - ) + 'account_id' => $this->_getAccountId() + ), + function ($row) { + $cb($row); + } ); - if ($row) { - $row['hero_uniid'] = $row['idx']; + foreach (services\NftService::getHeros() as $nftDb) { + $row = SqlHelper::ormSelectOne( + myself()->_getSelfMysql(), + 't_hero', + array( + 'token_id' => $nftDb['token_id'], + ) + ); + if ($row) { + $cb($row); + } } - return $row; } public static function toDto($row) @@ -68,6 +93,7 @@ class Hero extends BaseModel { mt\AttrHelper::mergeAttr($attr, $baseAttr); } $dto = array( + 'token_id' => $row['token_id'], 'hero_uniid' => $row['idx'], 'hero_id' => $row['hero_id'], 'hero_lv' => $row['hero_lv'], @@ -181,37 +207,34 @@ class Hero extends BaseModel { public static function update($heroUniId, $fieldsKv) { - SqlHelper::update - (myself()->_getSelfMysql(), - 't_hero', - array( - 'account_id' => myself()->_getAccountId(), - 'idx' => $heroUniId, - ), - $fieldsKv - ); + if (self::find($heroUniId)) { + SqlHelper::update + (myself()->_getSelfMysql(), + 't_hero', + array( + 'idx' => $heroUniId, + ), + $fieldsKv + ); + } } public static function randHero(&$heroUniId, &$heroId) { $heroUniId = 0; $heroId = 0; - $rows = SqlHelper::select( - myself()->_getSelfMysql(), - 't_hero', - array( - 'idx', - 'hero_id' - ), - array( - 'account_id' => myself()->_getAccountId(), - 'state' => self::GETED_STATE - ) - ); - $key = array_rand($rows, 1); + + $heroList = array(); + Hero::getHeroList(function ($row) use($heroId, &$heroList) { + if ($row['state'] == self::GETED_STATE) { + array_push($heroList, Hero::toDto($row)); + } + }); + + $key = array_rand($heroList, 1); if (!is_null($key)) { - $heroUniId = $rows[$key]['idx']; - $heroId = $rows[$key]['hero_id']; + $heroUniId = $heroList[$key]['idx']; + $heroId = $heroList[$key]['hero_id']; } } diff --git a/webapp/services/NftService.php b/webapp/services/NftService.php new file mode 100644 index 00000000..82ff1cb6 --- /dev/null +++ b/webapp/services/NftService.php @@ -0,0 +1,20 @@ +