This commit is contained in:
aozhiwei 2022-03-30 17:10:41 +08:00
parent 1a44f98abc
commit a037de92f4
3 changed files with 80 additions and 44 deletions

View File

@ -25,16 +25,9 @@ class HeroController extends BaseAuthedController {
public function heroList() public function heroList()
{ {
$heroList = array(); $heroList = array();
SqlHelper::ormSelect( Hero::getHeroList(function ($row) use(&$heroList) {
$this->_getSelfMysql(),
't_hero',
array(
'account_id' => $this->_getAccountId()
),
function ($row) use(&$heroList) {
array_push($heroList, Hero::toDto($row)); array_push($heroList, Hero::toDto($row));
} });
);
$this->_rspData(array( $this->_rspData(array(
'hero_list' => $heroList 'hero_list' => $heroList
)); ));

View File

@ -7,6 +7,7 @@ require_once('mt/HeroLevel.php');
require_once('mt/HeroQuality.php'); require_once('mt/HeroQuality.php');
require_once('mt/AttrHelper.php'); require_once('mt/AttrHelper.php');
require_once('models/HeroSkin.php'); require_once('models/HeroSkin.php');
require_once('services/NftService.php');
use mt; use mt;
use phpcommon\SqlHelper; use phpcommon\SqlHelper;
@ -26,31 +27,55 @@ class Hero extends BaseModel {
myself()->_getSelfMysql(), myself()->_getSelfMysql(),
't_hero', 't_hero',
array( array(
'account_id' => myself()->_getAccountId(),
'idx' => $heroUniId, 'idx' => $heroUniId,
) )
); );
if ($row) { if ($row) {
$row['hero_uniid'] = $row['idx']; $row['hero_uniid'] = $row['idx'];
if ($row['account_id'] != myself()->_getAccountId()) {
if (!services\NftService::isHeroOwner($row['token_id'])) {
$row = null;
}
}
} }
return $row; return $row;
} }
public static function getValidHero($heroId) public static function getValidHero($heroId)
{ {
$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' => $this->_getAccountId()
),
function ($row) {
$cb($row);
}
);
foreach (services\NftService::getHeros() as $nftDb) {
$row = SqlHelper::ormSelectOne( $row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(), myself()->_getSelfMysql(),
't_hero', 't_hero',
array( array(
'account_id' => myself()->_getAccountId(), 'token_id' => $nftDb['token_id'],
'hero_id' => $heroId,
'state' => self::GETED_STATE,
) )
); );
if ($row) { if ($row) {
$row['hero_uniid'] = $row['idx']; $cb($row);
}
} }
return $row;
} }
public static function toDto($row) public static function toDto($row)
@ -68,6 +93,7 @@ class Hero extends BaseModel {
mt\AttrHelper::mergeAttr($attr, $baseAttr); mt\AttrHelper::mergeAttr($attr, $baseAttr);
} }
$dto = array( $dto = array(
'token_id' => $row['token_id'],
'hero_uniid' => $row['idx'], 'hero_uniid' => $row['idx'],
'hero_id' => $row['hero_id'], 'hero_id' => $row['hero_id'],
'hero_lv' => $row['hero_lv'], 'hero_lv' => $row['hero_lv'],
@ -181,37 +207,34 @@ class Hero extends BaseModel {
public static function update($heroUniId, $fieldsKv) public static function update($heroUniId, $fieldsKv)
{ {
if (self::find($heroUniId)) {
SqlHelper::update SqlHelper::update
(myself()->_getSelfMysql(), (myself()->_getSelfMysql(),
't_hero', 't_hero',
array( array(
'account_id' => myself()->_getAccountId(),
'idx' => $heroUniId, 'idx' => $heroUniId,
), ),
$fieldsKv $fieldsKv
); );
} }
}
public static function randHero(&$heroUniId, &$heroId) public static function randHero(&$heroUniId, &$heroId)
{ {
$heroUniId = 0; $heroUniId = 0;
$heroId = 0; $heroId = 0;
$rows = SqlHelper::select(
myself()->_getSelfMysql(), $heroList = array();
't_hero', Hero::getHeroList(function ($row) use($heroId, &$heroList) {
array( if ($row['state'] == self::GETED_STATE) {
'idx', array_push($heroList, Hero::toDto($row));
'hero_id' }
), });
array(
'account_id' => myself()->_getAccountId(), $key = array_rand($heroList, 1);
'state' => self::GETED_STATE
)
);
$key = array_rand($rows, 1);
if (!is_null($key)) { if (!is_null($key)) {
$heroUniId = $rows[$key]['idx']; $heroUniId = $heroList[$key]['idx'];
$heroId = $rows[$key]['hero_id']; $heroId = $heroList[$key]['hero_id'];
} }
} }

View File

@ -0,0 +1,20 @@
<?php
namespace services;
class NftService extends BaseService {
private static $heros = null;
private static $equips = null;
private static $chips = null;
public static function isHeroOwner($tokenId)
{
}
public static function getHeros()
{
}
}