hujiabin 4efd335bcc 1
2022-09-20 12:01:04 +08:00

489 lines
17 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;
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()->_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) {
if (! $nftDb['deleted']){
$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']);
$baseAttr=[];
$attrPro=[];
if ($itemMeta) {
$baseAttr = mt\Equip::getGunBaseAttrs($itemMeta['relationship']);
$attrPro1=[];
if ($row['gun_lv']>1){
$attrPro1 = self::getAttrProByLevel($row,$baseAttr,$attr);
}
$attrPro2=[];
if ($row['quality']>1){
$attrPro2 = self::getAttrProByQuality($row,$baseAttr);
}
$attrPro = self::mergeAttrPro($baseAttr,$attrPro1,$attrPro2);
}
$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']);
$dto = array(
'gun_uniid' => $row['idx'],
'gun_id' => $row['gun_id'],
'gun_lv' => $row['gun_lv'],
'state' => $row['state'],
'quality' => $row['quality'],
'lucky' => strval($gunLucky),
'durability' => $row['durability'],
'ceg_uplimit' => 0,
'pve_ceg_uplimit' => 0,
'raw_pve_ceg_uplimit' => 0,
'attr_base' => $baseAttr,
'attr_pro' => $attrPro,
'attr_chip' => Chip::getChipAttr($row['chip_ids'])['attr_chip'],
'chip_core' => Chip::getChipAttr($row['chip_ids'])['chip_core'],
'try_count' => $row['try_count'],
'current_pvp_get_ceg' => $todayGetGold,
'last_pvp_get_ceg_time' => $lastGetGoldTime,
'current_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'],
'chip_ids' => $row['chip_ids'],
'chip_strength_sum' => strval(Chip::getChipMaxStrength($row['chip_ids'],2)), //计算ceg上限所需参数
'offer_reward_state' => 0,
);
$dto['durability_max'] = strval(round(FormulaService::Weapon_NFT_Maximum_Durability($dto['quality'],$dto['lucky']),3));
$dto['pvp_ceg_uplimit'] = strval( round(FormulaService::getWeaponPvpDailyCegUpLimit($dto),2) );
$dto['pve_ceg_uplimit'] = strval( round(FormulaService::getWeaponPveDailyCegUpLimit($dto),2) );
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' => 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,
'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 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(
'current_pvp_get_ceg' => $newGold,
'last_pvp_get_ceg_time' => myself()->_getNowTime()
));
}
return $finalyAddGold;
}
public static function gainGoldPve($gunDto, $count)
{
$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(
'current_pve_get_ceg' => $newGold,
'last_pve_get_ceg_time' => myself()->_getNowTime()
));
}
return $finalyAddGold;
}
private static function getAttrProByLevel($row,$baseAttr,$attr){
$attrPro1 = [];
$itemMeta = mt\Item::get($row['gun_id']);
if ($itemMeta){
$coefficient_level = mt\GunLevel::getCoefficientByLevel($row['gun_lv'],$itemMeta['relationship']);
foreach ($baseAttr as $val){
$coef_level = mt\GunLevel::getByCoefficient($coefficient_level,$val['attr_id']);
foreach ($attr as $v){
//狙击抢和散弹枪不用提升弹夹数量
if ($val['attr_id'] == $v['attr_id'] && $val['attr_id'] == kHAT_Volume ){
if (in_array($itemMeta['id'],self::$limit_the_cartridge_gun)){
array_push($attrPro1,[
'attr_id' => $val['attr_id'],
'type'=> $val['type'],
'val' => strval(1)
]);
}else{
array_push($attrPro1,[
'attr_id' => $val['attr_id'],
'type'=> $val['type'],
'val' => strval($val['val']*pow($v['val'],$coef_level['val'])),
]);
}
}
//射速升级提升公式
if ($val['attr_id'] == $v['attr_id'] && $val['attr_id'] == kHAT_FireRate){
array_push($attrPro1,[
'attr_id' => $val['attr_id'],
'type'=> $val['type'],
'val' => strval($val['val']/pow($v['val'],$coef_level['val']))
]);
}
if ($val['attr_id'] == $v['attr_id'] && $val['attr_id'] != kHAT_FireRate && $val['attr_id'] != kHAT_Volume){
array_push($attrPro1,[
'attr_id' => $val['attr_id'],
'type'=> $val['type'],
'val' => strval($val['val']*pow($v['val'],$coef_level['val'])),
]);
}
}
}
}
return $attrPro1;
}
private static function getAttrProByQuality($row,$baseAttr){
$attrPro2 = [];
$itemMeta = mt\Item::get($row['gun_id']);
if ($itemMeta) {
$qualityMeta = mt\GunQuality::getByQuality($row['quality']);
$coefficient_quality = mt\GunQuality::getCoefficientByQuality($row['quality'], $itemMeta['relationship']);
foreach ($baseAttr as $val) {
$coef_quality = mt\HeroQuality::getByCoefficient($coefficient_quality, $val['attr_id']);
if ($coef_quality){
//射速升级提升公式
if ($val['attr_id'] == kHAT_FireRate){
array_push($attrPro2,[
'attr_id' => $val['attr_id'],
'type'=> $val['type'],
'val' => strval($val['val']/pow($qualityMeta['promote_val'] , $coef_quality['val']))
]);
}
if ($val['attr_id'] == kHAT_Volume){
if (in_array($itemMeta['id'],self::$limit_the_cartridge_gun)){
array_push($attrPro2,[
'attr_id' => $val['attr_id'],
'type'=> $val['type'],
'val' => strval(1)
]);
}else{
array_push($attrPro2, [
'attr_id' => $val['attr_id'],
'type' => $val['type'],
'val' => strval($val['val'] * pow($qualityMeta['promote_val'], $coef_quality['val'])),
]);
}
}
if($val['attr_id'] != kHAT_FireRate && $val['attr_id'] != kHAT_Volume){
array_push($attrPro2, [
'attr_id' => $val['attr_id'],
'type' => $val['type'],
'val' => strval($val['val'] * pow($qualityMeta['promote_val'], $coef_quality['val'])),
]);
}
}
}
}
return $attrPro2;
}
private static function mergeAttrPro($baseAttr,$attrPro1,$attrPro2){
$newAttr = [];
$newAttrPro = [];
if ($attrPro1 && !$attrPro2){
$newAttrPro = $attrPro1;
}
if (!$attrPro1 && $attrPro2){
$newAttrPro = $attrPro2;
}
if ($attrPro1 && $attrPro2){
foreach ($attrPro1 as $value){
foreach ($attrPro2 as $val){
if ($value['attr_id'] == $val['attr_id']){
array_push($newAttr,
[
'attr_id' => $value['attr_id'],
'type' => $value['type'],
'val' => $value['val']+$val['val'],
]);
}
}
}
if ($newAttr){
foreach ($baseAttr as $value){
foreach ($newAttr as $val){
if ($value['attr_id'] == $val['attr_id']){
array_push($newAttrPro,
[
'attr_id' => $value['attr_id'],
'type' => $value['type'],
'val' => strval($val['val']-$value['val'])
]);
}
}
}
}
}
return $newAttrPro;
}
}