aozhiwei 0856cd5a4b 1
2022-04-01 14:50:20 +08:00

303 lines
8.9 KiB
PHP

<?php
namespace models;
require_once('mt/Hero.php');
require_once('mt/HeroLevel.php');
require_once('mt/HeroQuality.php');
require_once('mt/AttrHelper.php');
require_once('mt/Item.php');
require_once('models/HeroSkin.php');
require_once('services/NftService.php');
use mt;
use phpcommon\SqlHelper;
use services\NftService;
class Hero extends BaseModel {
const GETED_STATE = 0;
const TRY_STATE = 1;
const NO_LOCK = 0;
const LEVEL_LOCK = 1;
const QUALITY_LOCK = 2;
public static function find($heroUniId)
{
return self::internalFind(myself()->_getAccountId(), $heroUniId);
}
public static function findByAccountId($accountId, $heroUniId)
{
return self::internalFind($accountId, $heroUniId);
}
private static function internalFind($accountId, $heroUniId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getMysql($accountId),
't_hero',
array(
'idx' => $heroUniId,
)
);
if ($row) {
$row['hero_uniid'] = $row['idx'];
if ($row['account_id'] != $accountId) {
$openId = phpcommon\extractOpenId($accountId);
if (!NftService::isHeroOwner($opneId, $row['token_id'])) {
$row = null;
}
}
}
return $row;
}
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(
myself()->_getSelfMysql(),
't_hero',
array(
'account_id' => myself()->_getAccountId()
),
function ($row) use($cb) {
$cb($row);
}
);
foreach (NftService::getHeros(myself()->_getOpenId()) as $nftDb) {
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_hero',
array(
'token_id' => $nftDb['token_id'],
)
);
if (!$row) {
$itemMeta = mt\Item::get($nftDb['item_id']);
if ($itemMeta) {
self::addNftHero($itemMeta, $nftDb['token_id']);
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_hero',
array(
'token_id' => $nftDb['token_id'],
)
);
}
}
if ($row) {
$cb($row);
}
}
}
public static function toDto($row)
{
$attr = emptyReplace(json_decode($row['rand_attr'], true), array());
$lockType = 0;
$unlockTime = 0;
if ($row['lock_type'] != 0 && $row['unlock_time'] - myself()->_getNowTime() > 0) {
$lockType = $row['lock_type'];
$unlockTime = $row['unlock_time'];
}
$heroMeta = mt\Hero::get($row['hero_id']);
if ($heroMeta) {
$baseAttr = mt\Hero::getHeroAttr($heroMeta);
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'],
'hero_tili' => $row['hero_tili'],
'state' => $row['state'],
'skin_id' => $row['skin_id'],
'quality' => $row['quality'],
'skill_lv1' => $row['skill_lv1'],
'skill_lv2' => $row['skill_lv2'],
'attr' => $attr,
'try_count' => $row['try_count'],
'lock_type' => $lockType,
'unlock_time' => $unlockTime,
'unlock_trade_time' => $row['unlock_trade_time'],
);
return $dto;
}
public static function addHero($heroMeta)
{
return self::internalAddHero(
myself()->_getSelfMysql(),
$heroMeta,
myself()->_getAccountId(),
null);
}
public static function addNftHero($heroMeta, $tokenId)
{
return self::internalAddHero(
myself()->_getMysql($tokenId),
$heroMeta,
null,
$tokenId);
}
public static function internalAddHero($conn, $heroMeta, $accountId, $tokenId)
{
$realHeroMeta = mt\Hero::get($heroMeta['id']);
$randAttr = array();
{
$initQualityMeta = mt\HeroQuality::getByQuality(1);
if ($initQualityMeta) {
$randAttr = mt\HeroQuality::getRandAttr($initQualityMeta);
}
}
$fieldsKv = array(
'hero_id' => $heroMeta['id'],
'hero_lv' => 1,
'quality' => 1,
'hero_tili' => $realHeroMeta ? $realHeroMeta['tili'] : 0,
'state' => self::GETED_STATE,
'skill_lv1' => 1,
'skill_lv2' => 1,
'rand_attr' => json_encode($randAttr),
'lock_type' => self::NO_LOCK,
'unlock_time' => 0,
'unlock_trade_time' => 0,
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime()
);
if ($accountId) {
$fieldsKv['account_id'] = $accountId;
}
if ($tokenId) {
$fieldsKv['token_id'] = $tokenId;
}
SqlHelper::insert(
myself()->_getSelfMysql(),
't_hero',
$fieldsKv
);
}
public static function addTryHero($heroMeta, $tryCount)
{
$realHeroMeta = mt\Hero::get($heroMeta['id']);
$randAttr = array();
{
$initQualityMeta = mt\HeroQuality::getByQuality(1);
if ($initQualityMeta) {
$randAttr = mt\HeroQuality::getRandAttr($initQualityMeta);
}
}
SqlHelper::upsert(
myself()->_getSelfMysql(),
't_hero',
array(
'account_id' => myself()->_getAccountId(),
'hero_id' => $heroMeta['id']
),
array(
),
array(
'account_id' => myself()->_getAccountId(),
'hero_id' => $heroMeta['id'],
'hero_lv' => 1,
'quality' => 1,
'hero_tili' => $realHeroMeta ? $realHeroMeta['tili'] : 0,
'state' => self::TRY_STATE,
'try_count' => $tryCount,
'skill_lv1' => 1,
'skill_lv2' => 1,
'rand_attr' => json_encode($randAttr),
'lock_type' => self::NO_LOCK,
'unlock_time' => 0,
'unlock_trade_time' => 0,
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime()
)
);
}
public static function takeonSkin($heroUniId, $skinId)
{
self::update($heroUniId, array(
'skin_id' => $skinId,
'modifytime' => myself()->_getNowTime()
));
}
public static function upgradeSkill($heroUniId, $skillIdx)
{
if (!in_array($skillIdx, array(0, 1))) {
return;
}
$fieldName = 'skill_lv' . ($skillIdx + 1);
self::update($heroUniId, array(
$fieldName => function () use($fieldName) {
return "${fieldName} + 1";
},
'modifytime' => myself()->_getNowTime()
));
}
public static function update($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;
$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 = $heroList[$key]['idx'];
$heroId = $heroList[$key]['hero_id'];
}
}
public static function addTili($heroUniId, $tili)
{
self::update($heroUniId,
array(
'hero_tili' => function () use($tili) {
return "hero_tili + ${tili}";
},
));
}
}