255 lines
7.3 KiB
PHP
255 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace models;
|
|
|
|
require_once('mt/GunLevel.php');
|
|
require_once('mt/GunQuality.php');
|
|
require_once('mt/Item.php');
|
|
require_once('models/GunSkin.php');
|
|
require_once('services/NftService.php');
|
|
|
|
use mt;
|
|
use phpcommon\SqlHelper;
|
|
use services\NftService;
|
|
|
|
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(
|
|
'idx' => $gunUniId,
|
|
)
|
|
);
|
|
if ($row) {
|
|
$row['gun_uniid'] = $row['idx'];
|
|
if ($row['account_id'] != myself()->_getAccountId()) {
|
|
if (!NftService::isEquipOwner(myself()->_getOpenId(), $row['token_id'])) {
|
|
$row = null;
|
|
}
|
|
}
|
|
}
|
|
return $row;
|
|
}
|
|
|
|
public static function findByUniId($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)
|
|
{
|
|
$gunList = array();
|
|
self::getGunList(function ($row) use($gunId, &$gunList) {
|
|
if ($row['gun_id'] == $gunId && $row['state'] == self::GETED_STATE) {
|
|
array_push($gunList, self::toDto($row));
|
|
}
|
|
});
|
|
return !empty($gunList) ? $gunList[0] : null;
|
|
}
|
|
|
|
public static function getGunList($cb)
|
|
{
|
|
SqlHelper::ormSelect(
|
|
myself()->_getSelfMysql(),
|
|
't_gun',
|
|
array(
|
|
'account_id' => myself()->_getAccountId()
|
|
),
|
|
function ($row) use($cb) {
|
|
$cb($row);
|
|
}
|
|
);
|
|
foreach (NftService::getEquips(myself()->_getOpenId()) as $nftDb) {
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getSelfMysql(),
|
|
't_gun',
|
|
array(
|
|
'token_id' => $nftDb['token_id'],
|
|
)
|
|
);
|
|
if (!$row) {
|
|
$itemMeta = mt\Item::get($nftDb['item_id']);
|
|
if ($itemMeta) {
|
|
self::addNftGun($itemMeta, $nftDb['token_id']);
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getSelfMysql(),
|
|
't_gun',
|
|
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'];
|
|
}
|
|
$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'],
|
|
'durability' => $row['durability'],
|
|
'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)
|
|
{
|
|
return self::internalAddGun(
|
|
myself()->_getSelfMysql(),
|
|
$gunMeta,
|
|
myself()->_getAccountId(),
|
|
null);
|
|
}
|
|
|
|
public static function addNftGun($gunMeta, $tokenId)
|
|
{
|
|
return self::internalAddGun(
|
|
myself()->_getMysql($tokenId),
|
|
$gunMeta,
|
|
null,
|
|
$tokenId);
|
|
}
|
|
|
|
private static function internalAddGun($conn, $gunMeta, $accountId, $tokenId)
|
|
{
|
|
$randAttr = array();
|
|
{
|
|
$initQualityMeta = mt\GunQuality::getByQuality(1);
|
|
if ($initQualityMeta) {
|
|
$randAttr = mt\GunQuality::getRandAttr($initQualityMeta);
|
|
}
|
|
}
|
|
$fieldsKv = array(
|
|
'gun_id' => $gunMeta['id'],
|
|
'gun_lv' => 1,
|
|
'quality' => 1,
|
|
'state' => self::GETED_STATE,
|
|
'durability' => $gunMeta['init_durability'],
|
|
'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(
|
|
$conn,
|
|
't_gun',
|
|
$fieldsKv
|
|
);
|
|
}
|
|
|
|
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,
|
|
'durability' => $gunMeta['init_durability'],
|
|
'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)
|
|
{
|
|
if (self::find($gunUniId)) {
|
|
SqlHelper::update
|
|
(myself()->_getSelfMysql(),
|
|
't_gun',
|
|
array(
|
|
'idx' => $gunUniId,
|
|
),
|
|
$fieldsKv
|
|
);
|
|
}
|
|
}
|
|
|
|
public static function addDurability($gunUniId, $val)
|
|
{
|
|
self::update($gunUniId,
|
|
array(
|
|
'durability' => function () use ($val) {
|
|
return "durability + ${val}";
|
|
}
|
|
));
|
|
}
|
|
|
|
}
|