aozhiwei a7e8ed5917 1
2022-01-13 14:18:53 +08:00

160 lines
4.5 KiB
PHP

<?php
namespace models;
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 = 0;
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;
$unlockTime = 0;
if ($row['lock_type'] != 0 && $row['unlock_time'] - myself()->_getNowTime() > 0) {
$lockType = $row['lock_type'];
$unlockTime = $row['unlock_time'];
}
$itemMeta = mt\Item::get($row['gun_id']);
if ($itemMeta) {
$baseAttr = mt\Item::getBaseAttrs($itemMeta);
mt\AttrHelper::mergeAttr($attr, $baseAttr);
}
$dto = array(
'gun_uniid' => $row['idx'],
'gun_id' => $row['gun_id'],
'gun_lv' => $row['gun_lv'],
'state' => $row['state'],
'quality' => $row['quality'],
'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 addGun($gunMeta)
{
$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)
{
$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,
'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
);
}
}