hujiabin 219897fb6c 1
2023-03-09 16:56:08 +08:00

413 lines
12 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 mt\ChipAttr;
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;
public static function find($chipUniId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_chip',
array(
'idx' => $chipUniId,
)
);
if ($row) {
if ($row['account_id'] != myself()->_getAccountId()) {
if (NftService::getChipBlance(myself()->_getOpenId(), $row['token_id']) <= 0) {
$row = null;
}
}
}
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::getChipBlance(myself()->_getOpenId(), $tokenId) <= 0) {
$row = null;
}
}
return $row;
}
public static function getChipList($cb)
{
SqlHelper::ormSelect(
myself()->_getSelfMysql(),
't_chip',
array(
'account_id' => myself()->_getAccountId()
),
function ($row) use($cb) {
$cb($row);
}
);
$sql = "select * from t_nft1155 where owner_address=:owner_address and token_id>10000000 and balance>0";
$whereKv =array(
'owner_address' => myself()->_getOpenId(),
);
foreach (myself()->_getMarketMysql()->execQuery($sql,$whereKv) 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::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());
$chipMeta = mt\ChipAttr::getAttrByItemId($row['item_id']);
if ($chipMeta){
array_unshift($rand_attr,array(
'attr_id'=>$chipMeta['attr_id'],
'val' => $chipMeta['lv'.$row['chip_grade']]
));
}
$dto = array(
'idx'=> $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,
);
$dto['chip_name'] = mt\Item::get($row['item_id'])?mt\Item::get($row['item_id'])['name']:'XXX';
$nft_address = '';
if ($dto['token_id']){
$nft_address = SERVER_ENV == _ONLINE ? '0x73482411443E87CAC124C12A10B34e9Aaa2De168' : '0x26b4AFb60d6C903165150C6F0AA14F8016bE4aec';
}
$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 internalAddItem($conn, $itemMeta, $accountId, $tokenId,$state)
{
if (!$itemMeta) {
return;
}
if (myself()->_isVirtualItem($itemMeta['id'])) {
return;
}
$chipMeta = ChipAttr::getAttrByItemId($itemMeta['id']);
if (!$chipMeta){
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);
// }
$randAttr = array();
$fieldsKv = array(
'item_id' => $itemMeta['id'],
'item_num' => 1,
'state' => $state,
'rand_attr' => json_encode($randAttr),
'chip_grade' => 1,
'chip_type' => $itemMeta['sub_type'],
'activate' => 1,
'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()->_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($idx,$page){
$row = self::find($idx);
if ($row){
$inlayState = array_filter (explode("|",$row['inlay_state']) );
if (in_array($page,$inlayState)){
unset($inlayState[array_search($page,$inlayState)]);
}else{
array_push($inlayState,$page);
}
self::update2($idx,array(
'inlay_state' => implode('|',$inlayState)
));
}
}
// 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;
}
}