hujiabin 8f019f5964 1
2022-09-20 10:23:16 +08:00

332 lines
10 KiB
PHP

<?php
namespace models;
require_once('services/NftService.php');
require_once('services/FormulaService.php');
require_once('mt/Item.php');
require_once('mt/ChipAttr.php');
use mt;
use phpcommon\SqlHelper;
use services\NftService;
use services\FormulaService;
class Chip extends BaseModel
{
public static function all($type)
{
$chipList = array();
self::getChipList(function ($row) use(&$chipList,$type) {
if ($row['item_num'] > 0 && $row['chip_type'] == $type) {
array_push($chipList, $row);
}
});
return $chipList;
}
public static function getChipList($cb)
{
$nft = SqlHelper::ormSelect(
myself()->_getMarketMysql(),
't_nft',
array(
'owner_address' => myself()->_getOpenId(),
'token_type' =>Nft::CHIP_TYPE,
'deleted' => 0
)
);
foreach ($nft as $nftDb) {
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_chip',
array(
'token_id' => $nftDb['token_id'],
)
);
if (!$row) {
$itemMeta = mt\Item::get($nftDb['item_id']);
if ($itemMeta) {
self::addChip($nftDb['item_id'], $nftDb['token_id']);
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_chip',
array(
'token_id' => $nftDb['token_id'],
)
);
}
}
if ($row) {
$cb($row);
}
}
}
public static function toDto($row)
{
$todayGetGold = $row['today_get_gold'];
$lastGetGoldTime = $row['last_get_gold_time'];
if (myself()->_getDaySeconds($lastGetGoldTime) > myself()->_getNowDaySeconds()) {
$todayGetGold = 0;
}
$attr_pool = emptyReplace(json_decode($row['rand_attr'], true), array());
$attrs = array();
foreach ($attr_pool as $key=>$val){
if ($val){
$attr = mt\ChipAttr::getAttrById($val['attr_id'],$val['attr_type']);
$newAttr = [
'attr_id' => $attr['attr_id'],
'type' => 2,
'val' => $attr['lv'.$row['chip_grade']]*$val['attr_pool_number'],
// 'chip_name' => $attr['chip_name'],
'chip_type' => $attr['chip_type'],
'attr_num' => $attr['lv'.$row['chip_grade']],
'attr_pool_num' => $val['attr_pool_number'],
];
array_push($attrs,$newAttr);
}
}
$row['rand_attr'] = $attrs;
$row['today_get_gold'] = $todayGetGold;
$row['last_get_gold_time'] = $lastGetGoldTime;
$row['belong_to_item_id'] = self::belongsToWhere($row);
return $row;
}
public static function belongsToWhere($row){
if ($row['chip_type'] == 1){
$heroDb = array();
Hero::getHeroList(function ($hero) use ($row,&$heroDb) {
if ($hero['chip_ids'] && strstr($hero['chip_ids']."|",strval($row['idx']."|")) ){
$heroDb = $hero;
}
});
if($heroDb){
return $heroDb['hero_id'];
}else{
return 0;
}
}
if ($row['chip_type'] == 2){
$gunDb = array();
Gun::getGunList(function ($gun) use($row,&$gunDb) {
if ($gun['chip_ids'] && strstr($gun['chip_ids']."|",strval($row['idx']."|")) ){
$gunDb = $gun;
}
});
if($gunDb){
return $gunDb['gun_id'];
}else{
return 0;
}
}
}
public static function addChip($itemId, $tokenId)
{
return self::internalAddItem(
myself()->_getMysql($tokenId),
$itemId,
1,
null,
$tokenId);
}
public static function internalAddItem($conn, $itemId, $itemNum, $accountId, $tokenId)
{
$grade = rand(1,4);//随机一个等级
if (myself()->_isVirtualItem($itemId)) {
return;
}
if ($itemNum <= 0) {
return;
}
$itemMeta = mt\Item::get($itemId);
if (!$itemMeta) {
return;
}
$randAttr = array();
if (mt\Item::isRoleChipItem($itemMeta)) {
$randAttr = mt\ChipAttr::generateAttrRandom($itemMeta,$grade);
}
if (mt\Item::isGunChipItem($itemMeta)) {
$randAttr = mt\ChipAttr::generateAttrRandom($itemMeta,$grade);
}
$fieldsKv = array(
'item_id' => $itemId,
'item_num' => 1,
'rand_attr' => json_encode($randAttr),
'chip_grade' => $grade,
'chip_type' => $itemMeta['sub_type'],
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime()
);
if ($accountId) {
$fieldsKv['account_id'] = $accountId;
}
if ($tokenId) {
$fieldsKv['token_id'] = $tokenId;
}
SqlHelper::insert
($conn,
't_chip',
$fieldsKv
);
}
public static function getChipByTokenId($token_ids){
return SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_chip',
array(
'token_id' => $token_ids,
)
);
}
public static function getChipByIdx($idx){
return SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_chip',
array(
'idx' => $idx,
)
);
}
public static function update($token_id, $fieldsKv){
SqlHelper::update
(myself()->_getSelfMysql(),
't_chip',
array(
'token_id' => $token_id,
),
$fieldsKv
);
}
public static function updateChipInlayState($token_id,$state){
return SqlHelper::update(
myself()->_getSelfMysql(),
't_chip',
['token_id' => $token_id],
['inlay_state'=>$state]
);
}
public static function getChipInlay($type){
return SqlHelper::ormSelect(
myself()->_getSelfMysql(),
't_chip',
array(
'chip_type' => $type,
'supper_state' => 0,
'inlay_state' => 0,
)
);
}
public static function deleteChip($whereKv){
SqlHelper::update(
myself()->_getMarketMysql(),
't_nft',
$whereKv,
['deleted'=>1]
);
}
public static function updateRandAttr($chip){
$attr_pool = mt\ChipAttr::generateOneAttr($chip['chip_type']);
$rand_attr = emptyReplace(json_decode($chip['rand_attr'], true), array());
array_push($rand_attr,$attr_pool);
$fieldsKv = [
'rand_attr'=>json_encode($rand_attr)
];
self::update($chip['token_id'],$fieldsKv);
}
public static function getChipAttr($chip_ids){
$data = ['attr_chip'=>[],'chip_core'=>[]];
if (! $chip_ids) {
return $data;
}
$chipAttr = [];
$chipIdsArr = explode('|',$chip_ids);
foreach ($chipIdsArr as $val){
$chip = self::toDto(self::getChipByIdx($val));
foreach ($chip['rand_attr'] as $v){
array_push($chipAttr,$v);
}
}
$item = [];
foreach ($chipAttr as $k=>$v){
if (!isset($item[$v['attr_id']])){
$item[$v['attr_id']] = $v;
}else{
$item[$v['attr_id']]['val']+= $v['val'];
}
}
$data['attr_chip'] = $item;
$chipCore = [];
if (count($chipIdsArr) == 4){
$min = 15;
foreach ($chipIdsArr as $val){
$chip = self::getChipByIdx($val);
if ($chip['chip_grade']<$min){
$min = $chip['chip_grade'];
}
}
$chipCoreList = getMetaTable('chipCore@chipCore.php');
foreach ($chipCoreList as $val){
if ($val['chip_core_type']==1 && $val['chip_core_lv']<=$min){
array_push($chipCore,$val);
}
}
$data['chip_core'] = $chipCore;
}else{
$data['chip_core'] = [];
}
return $data;
}
public static function getChipMaxStrength($chip_ids,$type){
$MaxStrength = 0;
if (! $chip_ids) {
return $MaxStrength;
}
$chipIdsArr = explode('|',$chip_ids);
switch ($type){
case 1:{
foreach ($chipIdsArr as $val){
$chip = self::getChipByIdx($val);
$lucky = FormulaService::getChipLuckyValue($chip['chip_grade']);
$MaxStrength+=FormulaService::Hero_Chip_NFT_Maximum_Physical_Strength($chip['chip_grade'],$lucky)*FormulaService::Hero_Chip_PSA_Value($chip['chip_grade']);
}
}
break;
case 2:{
foreach ($chipIdsArr as $val){
$chip = self::getChipByIdx($val);
$lucky = FormulaService::getChipLuckyValue($chip['chip_grade']);
$MaxStrength+=FormulaService::Hero_Chip_NFT_Maximum_Physical_Strength($chip['chip_grade'],$lucky)*FormulaService::Weapon_Chip_DA_Value($chip['chip_grade']);
}
}
break;
default:{
$MaxStrength = 0;
}
}
return $MaxStrength;
}
}