aozhiwei f242f30c86 1
2022-01-06 19:13:13 +08:00

161 lines
4.6 KiB
PHP

<?php
namespace models;
require_once('mt/Gun.php');
require_once('mt/GunLevel.php');
require_once('mt/GunQuality.php');
require_once('models/GunSkin.php');
use mt;
use phpcommon\SqlHelper;
class Gun extends BaseModel {
const GETED_STATE = 0;
const TRY_STATE = 1;
const NO_LOCK = 1;
const LEVEL_LOCK = 1;
const QUALITY_LOCK = 2;
public static function find($gunUniId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_gun',
array(
'account_id' => myself()->_getAccountId(),
'idx' => $gunUniId,
)
);
if ($row) {
$row['gun_uniid'] = $row['idx'];
}
return $row;
}
public static function getValidGun($gunId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_gun',
array(
'account_id' => myself()->_getAccountId(),
'gun_id' => $gunId,
'state' => self::GETED_STATE,
)
);
if ($row) {
$row['gun_uniid'] = $row['idx'];
}
return $row;
}
public static function toDto($row)
{
$attr = emptyReplace(json_decode($row['rand_attr'], true), array());
$lockType = 0;
$lockTime = 0;
if ($row['lock_type'] != 0 && $row['unlock_time'] - myself()->_getNowTime() > 0) {
$lockType = $row['lock_type'];
$lockTime = $row['unlock_time'] - myself()->_getNowTime();
}
$tradeLocktime = max(0, $row['unlock_trade_time'] - myself()->_getNowTime());
$dto = array(
'gun_uniid' => $row['idx'],
'gun_id' => $row['gun_id'],
'gun_lv' => $row['gun_lv'],
'gun_tili' => $row['gun_tili'],
'state' => $row['state'],
'quality' => $row['quality'],
'attr' => $attr,
'try_count' => $row['try_count'],
'lock_type' => $lockType,
'lock_time' => $lockTime,
'trade_locktime' => $tradeLocktime,
);
return $dto;
}
public static function addGun($gunMeta)
{
$realGunMeta = mt\Gun::get($gunMeta['id']);
$randAttr = array();
{
$initQualityMeta = mt\GunQuality::getByQuality(1);
if ($initQualityMeta) {
$randAttr = mt\GunQuality::getRandAttr($initQualityMeta);
}
}
SqlHelper::insert(
myself()->_getSelfMysql(),
't_gun',
array(
'account_id' => myself()->_getAccountId(),
'gun_id' => $gunMeta['id'],
'gun_lv' => 1,
'quality' => 1,
'state' => self::GETED_STATE,
'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 addTryGun($gunMeta, $tryCount)
{
$realGunMeta = mt\Gun::get($gunMeta['id']);
$randAttr = array();
{
$initQualityMeta = mt\GunQuality::getByQuality(1);
if ($initQualityMeta) {
$randAttr = mt\GunQuality::getRandAttr($initQualityMeta);
}
}
SqlHelper::upsert(
myself()->_getSelfMysql(),
't_gun',
array(
'account_id' => myself()->_getAccountId(),
'gun_id' => $gunMeta['id']
),
array(
),
array(
'account_id' => myself()->_getAccountId(),
'gun_id' => $gunMeta['id'],
'gun_lv' => 1,
'quality' => 1,
'gun_tili' => $realGunMeta ? $realGunMeta['tili'] : 0,
'state' => self::TRY_STATE,
'try_count' => $tryCount,
'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 update($gunUniId, $fieldsKv)
{
SqlHelper::update
(myself()->_getSelfMysql(),
't_gun',
array(
'account_id' => myself()->_getAccountId(),
'idx' => $gunUniId,
),
$fieldsKv
);
}
}