2024-08-09 11:53:49 +08:00

591 lines
19 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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('models/ChipPlugin.php');
require_once('models/User.php');
require_once('services/NftService.php');
require_once('services/FormulaService.php');
use mt;
use phpcommon;
use phpcommon\SqlHelper;
use services\NftService;
use services\FormulaService;
use models\ChipPlugin;
class Gun extends BaseModel {
const GETED_STATE = 0;
const FREE_STATE = 1;
const NO_LOCK = 0;
const LEVEL_LOCK = 1;
const QUALITY_LOCK = 2;
const SEND_LOCK = 3;
const COST_LOCK = 4;
private static $limit_the_cartridge_gun = [70003,70004];
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()->_getAddress(), $row['token_id'])) {
$row = null;
}
}
}
return $row;
}
public static function findEx($gunUniId){
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_gun',
array(
'idx' => $gunUniId,
)
);
if ($row) {
if ($row['account_id'] != myself()->_getAccountId()) {
$row = null;
}
}
return $row;
}
public static function findByTokenId($tokenId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getMysql($tokenId),
't_gun',
array(
'token_id' => $tokenId,
)
);
if ($row) {
$row['gun_uniid'] = $row['idx'];
if (!NftService::isEquipOwner(myself()->_getAddress(), $row['token_id'])) {
$row = null;
}
}
return $row;
}
public static function findByTokenId2($tokenId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getMysql($tokenId),
't_gun',
array(
'token_id' => $tokenId,
)
);
return $row;
}
public static function findByAccountId($accountId, $gunUniId)
{
return self::internalFind($accountId, User::findUserAddress($accountId), $gunUniId);
}
public static function findByUniId($gunUniId)
{
return self::internalFind(myself()->_getAccountId(),myself()->_getAddress(), $gunUniId);
}
private static function internalFind($accountId, $address,$gunUniId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getMysql($accountId),
't_gun',
array(
'idx' => $gunUniId,
)
);
if ($row) {
$row['gun_uniid'] = $row['idx'];
if ($row['account_id'] != $accountId) {
$openId = $address;
if (!NftService::isEquipOwner($openId, $row['token_id'])) {
$row = null;
}
}
}
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) {
$row['tags'] = '';
$cb($row);
}
);
foreach (NftService::getEquips(myself()->_getAddress()) as $nftDb) {
if (! $nftDb['deleted']){
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_gun',
array(
'token_id' => $nftDb['token_id'],
)
);
//将NFT表的数据同步到中心化英雄表 (以后可能删除)
// 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){
if ( ! $row['activate']){
self::activateGun($row);
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_gun',
array(
'token_id' => $nftDb['token_id'],
)
);
}
$row['tags'] = $nftDb['tags'];
$cb($row);
}
}
}
}
private static function activateGun($row){
$itemMeta = mt\Item::get($row['gun_id']);
if (!$itemMeta) {
return;
}
// $randAttr = array();
// {
// $initQualityMeta = mt\GunQuality::getByQuality(1);
// if ($initQualityMeta) {
// $randAttr = mt\GunQuality::getRandAttr($initQualityMeta);
// }
// }
$fieldsKv = array(
// 'gun_lv' => 1,
// 'quality' => 1,
// 'state' => self::GETED_STATE,
// 'durability' => FormulaService::Weapon_NFT_Maximum_Durability(1,FormulaService::Weapon_Advanced_Lucky_Value(1)),
// 'rand_attr' => json_encode($randAttr),
'lock_type' => self::NO_LOCK,
'unlock_time' => 0,
'unlock_trade_time' => 0,
'activate' => 1,
'modifytime' => myself()->_getNowTime()
);
self::updateByTokenId($row['token_id'],$fieldsKv);
}
public static function toDto($row)
{
$attr = emptyReplace(json_decode($row['rand_attr'], true), array());
if (!$attr){
$itemMeta = mt\Item::get($row['gun_id']);
$attr = mt\Equip::getGunBaseAttrs($itemMeta['relationship']);
}
// $lockType = 0;
// $unlockTime = 0;
// //if ($row['lock_type'] != 0 && $row['unlock_time'] - myself()->_getNowTime() > 0) {
{
$lockType = $row['lock_type'];
$unlockTime = $row['unlock_time'];
}
$heroIds =array();
$dto = array(
'idx' => $row['idx'],
'token_id' => $row['token_id'],
'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,
'rand_attr' => $attr,
'try_count' => $row['try_count'],
'lock_type' => $lockType,
'unlock_time' => $unlockTime,
'unlock_lefttime' => max(0,
$unlockTime - myself()->_getNowTime()),
'unlock_trade_time' => $row['unlock_trade_time'],
'offer_reward_state' => 0,
'tags' => isset($row['tags'])?$row['tags']:'',
'hero_ids' => $heroIds,
);
// $nft_address = '';
// $contract = ContractConfig::find(ContractConfig::ERC721);
// if ($row['token_id']){
// $nft_address = $contract ? $contract['gun'] : "";
// }
//
// $dto['nft_address'] = $nft_address;
return $dto;
}
public static function addFreeGun($gunMeta)
{
return self::internalAddGun(
myself()->_getSelfMysql(),
$gunMeta,
myself()->_getAccountId(),
null,
self::FREE_STATE);
}
public static function addGun($gunMeta)
{
return self::internalAddGun(
myself()->_getSelfMysql(),
$gunMeta,
myself()->_getAccountId(),
null,
self::GETED_STATE);
}
public static function addNftGun($gunMeta, $tokenId)
{
return self::internalAddGun(
myself()->_getMysql($tokenId),
$gunMeta,
null,
$tokenId,
self::GETED_STATE);
}
private static function internalAddGun($conn, $gunMeta, $accountId, $tokenId,$state)
{
$randAttr = array();
$fieldsKv = array(
'gun_id' => $gunMeta['id'],
'gun_lv' => 1,
'quality' => 1,
'state' => $state,
// 'durability' => FormulaService::Weapon_NFT_Maximum_Durability(1,FormulaService::Weapon_Advanced_Lucky_Value(1)),
'rand_attr' => json_encode($randAttr),
'lock_type' => self::NO_LOCK,
'unlock_time' => 0,
'unlock_trade_time' => 0,
'activate' => 1,
'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::FREE_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 updateByTokenId($tokenId, $fieldsKv)
{
if (self::findByTokenId($tokenId)) {
SqlHelper::update
(myself()->_getSelfMysql(),
't_gun',
array(
'token_id' => $tokenId,
),
$fieldsKv
);
}
}
public static function addDurability($gunUniId, $val)
{
self::update($gunUniId,
array(
'durability' => function () use ($val) {
return "durability + ${val}";
}
));
}
public static function gainGoldPvp($gunDto, $addGold)
{
$newGold = min($gunDto['pvp_ceg_uplimit'],
$gunDto['current_pvp_get_ceg'] + $addGold);
$finalyAddGold = max(0, $newGold - $gunDto['current_pvp_get_ceg']);
if ($finalyAddGold > 0) {
self::update($gunDto['gun_uniid'],
array(
'today_get_gold' => $newGold * 100,
'last_get_gold_time' => myself()->_getNowTime()
));
}
return $finalyAddGold;
}
public static function gainGoldPve($gunDto, $addGold)
{
$newGold = min($gunDto['pve_ceg_uplimit'],
$gunDto['current_pve_get_ceg'] + $addGold);
$finalyAddGold = max(0, $newGold - $gunDto['current_pve_get_ceg']);
if ($finalyAddGold > 0) {
self::update($gunDto['gun_uniid'],
array(
'today_pve_get_ceg' => $newGold * 100,
'last_pve_get_ceg_time' => myself()->_getNowTime()
));
}
return $finalyAddGold;
}
public static function calcMissionGainGold($gunDto, $count)
{
if ($count <= 0) {
return 0;
}
$newGold = min($gunDto['mission_ceg_uplimit'],
$gunDto['current_mission_get_ceg'] +
$gunDto['mission_ceg_uplimit'] / $count);
$finalyAddGold = max(0, $newGold - $gunDto['current_mission_get_ceg']);
return $finalyAddGold;
}
public static function gainGoldMission($gunDto, $addGold)
{
$newGold = min($gunDto['mission_ceg_uplimit'],
$gunDto['current_mission_get_ceg'] + $addGold);
$finalyAddGold = max(0, $newGold - $gunDto['current_mission_get_ceg']);
if ($finalyAddGold > 0) {
self::update($gunDto['gun_uniid'],
array(
'today_mission_get_ceg' => $newGold * 100,
'last_mission_get_ceg_time' => myself()->_getNowTime()
));
}
return $finalyAddGold;
}
public static function calcPveGainGold($gunDto, $count)
{
return 0;
}
public static function getFreeGun(){
$gun = array();
Gun::getGunList(function ($row) use (&$gun) {
if (!$row['token_id']) {
$gun = $row;
}
});
return $gun;
}
public static function toDtoInfo($row){
$attr = emptyReplace(json_decode($row['rand_attr'], true), array());
$itemMeta = mt\Item::get($row['gun_id']);
if (!$attr){
$attr = mt\Equip::getGunBaseAttrs($itemMeta['relationship']);
}
$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;
}
$gunLucky = \services\FormulaService::Weapon_Advanced_Lucky_Value($row['quality']);
$info = array(
'idx' => $row['idx'],
'token_id' => $row['token_id'],
'gun_uniid' => $row['idx'],
'gun_name' => $itemMeta['name'],
'gun_id' => $row['gun_id'],
'gun_lv' => $row['gun_lv'],
'state' => $row['state'],
'quality' => $row['quality'],
'lucky' => strval($gunLucky),
'durability' => $row['durability'],
'rand_attr' => $attr,
'current_pvp_get_ceg' => $todayGetGold / 100,
'current_pve_get_ceg' => $todayPveGetCeg / 100,
'chip_ids' => $row['chip_ids'],
'chip_strength_sum' => strval(Chip::getChipMaxStrength($row['chip_ids'],2)), //计算ceg上限所需参数
'labour' => $row['labour'],
);
$info['durability_max'] = strval(round(FormulaService::Weapon_NFT_Maximum_Durability($info['quality'],$info['lucky']),3));
$info['pvp_ceg_uplimit'] = strval( round(FormulaService::getWeaponPvpDailyCegUpLimit($info),2) );
$info['pve_ceg_uplimit'] = strval( round(FormulaService::getWeaponPveDailyCegUpLimit($info),2) );
return $info;
}
public static function LvUpAddAttr($gunDb,$type){
if ($gunDb['gun_lv'] == 1){
$itemMeta = mt\Item::get($gunDb['gun_id']);
$baseAttr = mt\Equip::getGunBaseAttrs($itemMeta['relationship']);
}else{
$baseAttr = emptyReplace(json_decode($gunDb['rand_attr'], true), array());
}
$gunLevelMeta = \mt\GunLevel::find($gunDb['gun_id'],$gunDb['gun_lv']+1);
$attr = \mt\GunLevel::addRandAttr($gunLevelMeta,$type);
$randAttr = array();
foreach ($baseAttr as $val){
foreach ($attr as $v){
if ($val['attr_id'] == $v['attr_id'] && $v['attr_id'] == kHAT_Atk) {
//攻击力:上一级攻击力 *1+提升幅度*随机数)
$temp = $val['val'] * (1 + $gunLevelMeta['promote'] * $v['val']);
array_push($randAttr, array(
'attr_id' => $v['attr_id'],
'val' => $temp,
));
}else if ($val['attr_id'] == $v['attr_id'] && in_array($v['attr_id'] ,array(kHAT_ReloadTime,kHAT_FireRate))) {
//射速 装弹时间
//上级数值-表格数据
$temp = $val['val'] - $v['val'];
array_push($randAttr, array(
'attr_id' => $v['attr_id'],
'val' => $temp,
));
}else if($val['attr_id'] == $v['attr_id'] && in_array($v['attr_id'] ,array(kHAT_Volume,kHAT_BulletSpeed,kHAT_ShotRange))){
//子弹数 子弹飞行速度 射程 暴击率 暴击增伤
//上级数值+表格数据
$temp = $val['val'] + $v['val'];
array_push($randAttr, array(
'attr_id' => $v['attr_id'],
'val' => $temp,
));
}
}
}
foreach ($baseAttr as &$val){
foreach ($randAttr as $v){
if ($val['attr_id'] == $v['attr_id']){
$val['val'] = $v['val'];
}
}
}
return $baseAttr;
}
}