461 lines
14 KiB
PHP
461 lines
14 KiB
PHP
<?php
|
|
|
|
|
|
namespace models;
|
|
|
|
require_once('services/NftService.php');
|
|
require_once('services/FormulaService.php');
|
|
|
|
require_once('mt/Item.php');
|
|
require_once('mt/EconomyAttribute.php');
|
|
require_once('mt/ChipAttribute.php');
|
|
|
|
|
|
use mt;
|
|
use phpcommon\SqlHelper;
|
|
use services\NftService;
|
|
use services\FormulaService;
|
|
|
|
class Chip extends BaseModel
|
|
{
|
|
const GETED_STATE = 0;
|
|
const FREE_STATE = 1;
|
|
|
|
const CHIP_LV_MAX = 3;
|
|
const CHIP_QUALITY_MAX = 5;
|
|
|
|
public static function find($chipUniId)
|
|
{
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getSelfMysql(),
|
|
't_chip',
|
|
array(
|
|
'idx' => $chipUniId,
|
|
)
|
|
);
|
|
if ($row) {
|
|
$row['chip_uniid'] = $row['idx'];
|
|
if ($row['account_id'] != myself()->_getAccountId()) {
|
|
// if (NftService::isChipOwner(myself()->_getAddress(), $row['token_id']) <= 0) {
|
|
$row = null;
|
|
// }
|
|
}
|
|
}
|
|
return $row;
|
|
}
|
|
|
|
public static function findEx($chipUniId)
|
|
{
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getSelfMysql(),
|
|
't_chip',
|
|
array(
|
|
'idx' => $chipUniId,
|
|
)
|
|
);
|
|
return $row;
|
|
}
|
|
|
|
|
|
public static function update2($chipUniId, $fieldsKv){
|
|
if (self::find($chipUniId)) {
|
|
SqlHelper::update
|
|
(myself()->_getSelfMysql(),
|
|
't_chip',
|
|
array(
|
|
'idx' => $chipUniId,
|
|
),
|
|
$fieldsKv
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
public static function findByTokenId($tokenId)
|
|
{
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getMysql($tokenId),
|
|
't_chip',
|
|
array(
|
|
'token_id' => $tokenId
|
|
)
|
|
);
|
|
if ($row) {
|
|
$row['chip_uniid'] = $row['idx'];
|
|
if (NftService::isChipOwner(myself()->_getAddress(), $tokenId) <= 0) {
|
|
$row = null;
|
|
}
|
|
}
|
|
return $row;
|
|
}
|
|
|
|
public static function findByTokenId2($tokenId)
|
|
{
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getMysql($tokenId),
|
|
't_chip',
|
|
array(
|
|
'token_id' => $tokenId,
|
|
)
|
|
);
|
|
return $row;
|
|
}
|
|
|
|
public static function getChipList($cb)
|
|
{
|
|
SqlHelper::ormSelect(
|
|
myself()->_getSelfMysql(),
|
|
't_chip',
|
|
array(
|
|
'account_id' => myself()->_getAccountId()
|
|
),
|
|
function ($row) use($cb) {
|
|
$cb($row);
|
|
}
|
|
);
|
|
foreach (NftService::getChips(myself()->_getAddress()) as $nftDb) {
|
|
if (! $nftDb['deleted']){
|
|
$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::addNftChip($itemMeta, $nftDb['token_id']);
|
|
// $row = SqlHelper::ormSelectOne(
|
|
// myself()->_getSelfMysql(),
|
|
// 't_chip',
|
|
// array(
|
|
// 'token_id' => $nftDb['token_id'],
|
|
// )
|
|
// );
|
|
// }
|
|
// }
|
|
if ($row){
|
|
if (! $row['activate']){
|
|
self::activateChip($row);
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getSelfMysql(),
|
|
't_chip',
|
|
array(
|
|
'token_id' => $nftDb['token_id'],
|
|
)
|
|
);
|
|
}
|
|
$cb($row);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static function activateChip($row){
|
|
$itemMeta = mt\Item::get($row['item_id']);
|
|
if (!$itemMeta) {
|
|
return;
|
|
}
|
|
$randAttr = array();
|
|
$fieldsKv = array(
|
|
// 'item_id' => $itemMeta['id'],
|
|
// 'item_num' => 1,
|
|
// 'state' => self::GETED_STATE,
|
|
// 'rand_attr' => json_encode($randAttr),
|
|
// 'chip_grade' => 1,
|
|
// 'chip_type' => $itemMeta['sub_type'],
|
|
'activate' => 1,
|
|
'modifytime' => myself()->_getNowTime()
|
|
);
|
|
self::update($row['token_id'],$fieldsKv);
|
|
}
|
|
|
|
public static function toDto($row)
|
|
{
|
|
// $todayGetGold = $row['today_get_gold'];
|
|
// $lastGetGoldTime = $row['last_get_gold_time'];
|
|
// if (myself()->_getDaySeconds($lastGetGoldTime) > myself()->_getNowDaySeconds()) {
|
|
// $todayGetGold = 0;
|
|
// }
|
|
$rand_attr = emptyReplace(json_decode($row['rand_attr'], true), array());
|
|
$attribute = emptyReplace(json_decode($row['wealth_attr'], true), array());
|
|
|
|
$dto = array(
|
|
'idx'=> $row['idx'],
|
|
'chip_uniid'=> $row['idx'],
|
|
'token_id'=> $row['token_id'],
|
|
'item_id'=> $row['item_id'],
|
|
'chip_grade'=> $row['chip_grade'],
|
|
'chip_type'=> $row['chip_type'],
|
|
'state'=> $row['state'],
|
|
'inlay_state'=> $row['inlay_state'],
|
|
'rand_attr'=> $rand_attr,
|
|
'quality'=> $row['quality'],
|
|
'attribute' => $attribute,
|
|
'is_old' => $row['is_old'],
|
|
);
|
|
$dto['chip_name'] = mt\Item::get($row['item_id'])?mt\Item::get($row['item_id'])['name']:'XXX';
|
|
// $nft_address = '';
|
|
// $contract = ContractConfig::find(ContractConfig::ERC721);
|
|
// if ($dto['token_id']){
|
|
// $nft_address = $contract ? $contract['chip'] : "";
|
|
// }
|
|
// $dto['nft_address'] = $nft_address;
|
|
$dto['tags'] = '';
|
|
return $dto;
|
|
}
|
|
|
|
public static function addFreeChip($itemMeta)
|
|
{
|
|
|
|
return self::internalAddItem(
|
|
myself()->_getSelfMysql(),
|
|
$itemMeta,
|
|
myself()->_getAccountId(),
|
|
null,
|
|
self::FREE_STATE);
|
|
}
|
|
|
|
public static function addChip($itemMeta)
|
|
{
|
|
|
|
return self::internalAddItem(
|
|
myself()->_getSelfMysql(),
|
|
$itemMeta,
|
|
myself()->_getAccountId(),
|
|
null,
|
|
self::GETED_STATE);
|
|
}
|
|
|
|
public static function addNftChip($itemMeta,$tokenId)
|
|
{
|
|
|
|
return self::internalAddItem(
|
|
myself()->_getMysql($tokenId),
|
|
$itemMeta,
|
|
null,
|
|
$tokenId,
|
|
self::GETED_STATE);
|
|
}
|
|
|
|
public static function addSyntheticChip($itemMeta,$quality)
|
|
{
|
|
|
|
return self::internalAddItem(
|
|
myself()->_getSelfMysql(),
|
|
$itemMeta,
|
|
myself()->_getAccountId(),
|
|
null,
|
|
self::FREE_STATE,
|
|
$quality);
|
|
}
|
|
|
|
public static function internalAddItem($conn, $itemMeta, $accountId, $tokenId,$state,$quality=1)
|
|
{
|
|
$chipAttrMeta = \mt\ChipAttribute::get($itemMeta['id']);
|
|
$attribute = \mt\EconomyAttribute::getAttribute($chipAttrMeta['economyAttribute'],$quality);
|
|
{
|
|
$randAttr = array();
|
|
// $randMeta = mt\BattleRandAttribute::getByWeight($chipAttrMeta['battleAttribute'],$quality);
|
|
// if ($randMeta){
|
|
// $randAttr = mt\BattleRandAttribute::getRandAttr($randMeta);
|
|
// }
|
|
for ($i=1;$i<=$quality;$i++){
|
|
$randMeta = mt\BattleRandAttribute::getByWeight($chipAttrMeta['battleAttribute'],$i);
|
|
if ($randMeta) {
|
|
$result = mt\BattleRandAttribute::getRandAttr($randMeta);
|
|
if ($result){
|
|
foreach ($result as $value){
|
|
if (isset($randAttr[$value['attr_id']])){
|
|
$randAttr[$value['attr_id']]['val'] += $value['val'];
|
|
}else{
|
|
$randAttr[$value['attr_id']] = $value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$randAttr = array_values($randAttr);
|
|
$fieldsKv = array(
|
|
'item_id' => $itemMeta['id'],
|
|
'item_num' => 1,
|
|
'state' => $state,
|
|
'rand_attr' => json_encode($randAttr),
|
|
'chip_grade' => 1,
|
|
'inlay_state' => 0,
|
|
'chip_type' => $itemMeta['sub_type'],
|
|
'activate' => 1,
|
|
'quality' => $quality,
|
|
'wealth_attr' => json_encode($attribute),
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime()
|
|
);
|
|
if ($accountId) {
|
|
$fieldsKv['account_id'] = $accountId;
|
|
}
|
|
if ($tokenId) {
|
|
$fieldsKv['token_id'] = $tokenId;
|
|
}
|
|
|
|
SqlHelper::insert
|
|
($conn,
|
|
't_chip',
|
|
$fieldsKv
|
|
);
|
|
myself()->_addTgLog("addChipItem",array(
|
|
'item_id'=>$itemMeta['id'],
|
|
'item_num'=>1,
|
|
));
|
|
return true;
|
|
}
|
|
|
|
public static function getChipByTokenId($token_ids){
|
|
return SqlHelper::ormSelectOne(
|
|
myself()->_getMysql(''),
|
|
'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 updateInlayState($chip_unnid,$status){
|
|
SqlHelper::update
|
|
(myself()->_getSelfMysql(),
|
|
't_chip',
|
|
array(
|
|
'idx' => $chip_unnid,
|
|
),
|
|
array(
|
|
'inlay_state' => $status
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
// public static function updateRandAttr($accountId,$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)
|
|
// ];
|
|
// SqlHelper::update(
|
|
// myself()->_getMysql($accountId),
|
|
// 't_chip',
|
|
// array(
|
|
// 'token_id' => $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){
|
|
$row = self::getChipByTokenId($val);
|
|
if ($row){
|
|
$chip = self::toDto($row);
|
|
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(array_filter($chipIdsArr)) == 4){
|
|
$min = 15;
|
|
foreach (array_filter($chipIdsArr) as $val){
|
|
$chip = self::getChipByTokenId($val);
|
|
if ($chip){
|
|
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::getChipByTokenId($val);
|
|
if ($chip) {
|
|
$lucky = FormulaService::getChipLuckyValue($chip['chip_grade']);
|
|
$MaxStrength+=FormulaService::Hero_Chip_NFT_Maximum_Physical_Strength($chip['chip_grade'],$lucky)*FormulaService::Hero_Chip_PSA_Value($chip)*FormulaService::Hero_Chip_GAC_PS_Value($chip['chip_grade']);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case 2:{
|
|
foreach ($chipIdsArr as $val){
|
|
$chip = self::getChipByTokenId($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)*FormulaService::Weapon_Chip_GAC_PS_Value($chip['chip_grade']);
|
|
}
|
|
}
|
|
break;
|
|
default:{
|
|
$MaxStrength = 0;
|
|
}
|
|
}
|
|
return $MaxStrength;
|
|
}
|
|
|
|
}
|