345 lines
11 KiB
PHP
345 lines
11 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');
|
|
require_once('services/FormulaService.php');
|
|
|
|
use mt;
|
|
use phpcommon\SqlHelper;
|
|
use services\NftService;
|
|
use services\FormulaService;
|
|
|
|
class Gun extends BaseModel {
|
|
|
|
const GETED_STATE = 0;
|
|
const TRY_STATE = 1;
|
|
|
|
const NO_LOCK = 0;
|
|
const LEVEL_LOCK = 1;
|
|
const QUALITY_LOCK = 2;
|
|
const SEND_LOCK = 3;
|
|
|
|
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 findByAccountId($accountId, $gunUniId)
|
|
{
|
|
return self::internalFind($accountId, $gunUniId);
|
|
}
|
|
|
|
public static function findByUniId($gunUniId)
|
|
{
|
|
return self::internalFind(myself()->_getAccountId(), $gunUniId);
|
|
}
|
|
|
|
private static function internalFind($accountId, $gunUniId)
|
|
{
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getMysql($accountId),
|
|
't_gun',
|
|
array(
|
|
'account_id' => $accountId,
|
|
'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 getRawPveCegUpLimit()
|
|
{
|
|
$cegUpLimit = 0;
|
|
self::getGunList(function ($row) use(&$cegUpLimit) {
|
|
$gunDto = self::toDto($row);
|
|
$cegUpLimit += $gunDto['raw_pve_ceg_uplimit'];
|
|
});
|
|
return $cegUpLimit;
|
|
}
|
|
|
|
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);
|
|
}
|
|
$todayGetGold = $row['today_get_gold'];
|
|
$lastGetGoldTime = $row['last_get_gold_time'];
|
|
if (myself()->_getDaySeconds($lastGetGoldTime) < myself()->_getNowDaySeconds()) {
|
|
$todayGetGold = 0;
|
|
}
|
|
$todayPveGetCeg = $row['today_pve_get_ceg'];
|
|
$lastPveGetCegTime = $row['last_pve_get_ceg_time'];
|
|
if (myself()->_getDaySeconds($lastPveGetCegTime) <
|
|
myself()->_getNowDaySeconds()) {
|
|
$todayPveGetCeg = 0;
|
|
}
|
|
$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'],
|
|
'ceg_uplimit' => 0,
|
|
'pve_ceg_uplimit' => 0,
|
|
'raw_pve_ceg_uplimit' => 0,
|
|
'attr' => $attr,
|
|
'try_count' => $row['try_count'],
|
|
'today_get_gold' => $todayGetGold,
|
|
'last_get_gold_time' => $lastGetGoldTime,
|
|
'today_pve_get_ceg' => $todayPveGetCeg,
|
|
'last_pve_get_ceg_time' => $lastPveGetCegTime,
|
|
'lock_type' => $lockType,
|
|
'unlock_time' => $unlockTime,
|
|
'unlock_lefttime' => max(0,
|
|
$unlockTime - myself()->_getNowTime()),
|
|
'unlock_trade_time' => $row['unlock_trade_time'],
|
|
);
|
|
$dto['ceg_uplimit'] = FormulaService::getWeaponPvpDailyCegUpLimit($dto);
|
|
$dto['raw_pve_ceg_uplimit'] =
|
|
FormulaService::getWeaponPveDailyCegUpLimit($dto);
|
|
$dto['pve_ceg_uplimit'] = round($dto['raw_pve_ceg_uplimit'] * 0.9);
|
|
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'] ? $gunMeta['init_durability'] : 0,
|
|
'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}";
|
|
}
|
|
));
|
|
}
|
|
|
|
public static function newGainGold($gunDto, $addGold)
|
|
{
|
|
$newGold = min($gunDto['ceg_uplimit'],
|
|
$gunDto['today_get_gold'] + $addGold);
|
|
$finalyAddGold = max(0, $newGold - $gunDto['today_get_gold']);
|
|
if ($finalyAddGold > 0) {
|
|
self::update($gunDto['gun_uniid'],
|
|
array(
|
|
'today_get_gold' => $newGold,
|
|
'last_get_gold_time' => myself()->_getNowTime()
|
|
));
|
|
}
|
|
return $finalyAddGold;
|
|
}
|
|
|
|
public static function pveGainGold($gunDto, $count)
|
|
{
|
|
if ($count <= 0) {
|
|
return 0;
|
|
}
|
|
$finalyAddGold = self::calcPveGainGold($gunDto, $count);
|
|
if ($finalyAddGold > 0) {
|
|
self::update($gunDto['gun_uniid'],
|
|
array(
|
|
'today_pve_get_ceg' => $gunDto['today_pve_get_ceg'] + $finalyAddGold,
|
|
'last_pve_get_ceg_time' => myself()->_getNowTime()
|
|
));
|
|
}
|
|
return $finalyAddGold;
|
|
}
|
|
|
|
public static function calcPveGainGold($gunDto, $count)
|
|
{
|
|
if ($count <= 0) {
|
|
return 0;
|
|
}
|
|
$newGold = min($gunDto['pve_ceg_uplimit'],
|
|
$gunDto['today_pve_get_ceg'] +
|
|
round($gunDto['pve_ceg_uplimit'] / $count));
|
|
$finalyAddGold = max(0, $newGold - $gunDto['today_pve_get_ceg']);
|
|
return $finalyAddGold;
|
|
}
|
|
|
|
}
|