aozhiwei 45c161a6e9 1
2022-01-12 19:10:35 +08:00

219 lines
6.4 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('models/HeroSkin.php');
use mt;
use phpcommon\SqlHelper;
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)
{
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_hero',
array(
'account_id' => myself()->_getAccountId(),
'idx' => $heroUniId,
)
);
if ($row) {
$row['hero_uniid'] = $row['idx'];
}
return $row;
}
public static function getValidHero($heroId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_hero',
array(
'account_id' => myself()->_getAccountId(),
'hero_id' => $heroId,
'state' => self::GETED_STATE,
)
);
if ($row) {
$row['hero_uniid'] = $row['idx'];
}
return $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(
'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)
{
$realHeroMeta = mt\Hero::get($heroMeta['id']);
$randAttr = array();
{
$initQualityMeta = mt\HeroQuality::getByQuality(1);
if ($initQualityMeta) {
$randAttr = mt\HeroQuality::getRandAttr($initQualityMeta);
}
}
SqlHelper::insert(
myself()->_getSelfMysql(),
't_hero',
array(
'account_id' => myself()->_getAccountId(),
'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()
)
);
}
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)
{
SqlHelper::update
(myself()->_getSelfMysql(),
't_hero',
array(
'account_id' => myself()->_getAccountId(),
'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);
if (!is_null($key)) {
$heroUniId = $rows[$key]['idx'];
$heroId = $rows[$key]['hero_id'];
}
}
}