game2006api/webapp/controller/OutAppNftController.class.php
2024-07-26 18:02:24 +08:00

563 lines
22 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
use phpcommon\SqlHelper;
require_once('models/Nft.php');
require_once('models/User.php');
require_once('models/Hero.php');
require_once('services/NftService.php');
require_once('mt/NftDesc.php');
require_once('mt/Hero.php');
require_once('mt/EconomyAttribute.php');
use models\Nft;
use models\User;
use models\Hero;
class OutAppNftController extends BaseController {
public function getNftList(){
$address = getReqVal('address', '');
$type = getReqVal('type', '');
if ($type){
$nftList = Nft::getNftListByType($address,$type);
}else{
$nftList = Nft::getNftList($address);
}
$listInfo = array();
if ($nftList){
foreach ($nftList as $nft) {
$image = 'https://www.cebg.games/res/avatars/' . $nft['item_id'] . '.png';
$full_image = 'https://www.cebg.games/res/avatars/full_' . $nft['item_id'] . '.png';
$info = array(
"idx" => $nft['idx'],
"owner_address" => $nft['owner_address'],
"item_id" => $nft['item_id'],
"token_id" => $nft['token_id'],
"token_type" => $nft['token_type'],
"contract_address" => $nft['contract_address'],
'image' => $image,
'full_image' => $full_image,
"details" => array(),
);
array_push($listInfo,$info);
}
}
$this->_rspData(array(
'nfts' => $listInfo,
));
}
public function getWebInfo(){
$channel = getReqVal('channel', '');
$openId = getReqVal('openId', '');
$accountId = BC_POLY_CHANNEL.'_'.$this->_getGameId().'_'.$channel.'_'.$openId;
// $userDb = User::find($accountId);
// if (!$userDb){
// $this->_rspErr(1, 'user not found');
// return;
// }
$info = array(
'loginVal' => 0,
'battleTimes' => 0,
'winTimes' => 0,
'kills' => 0,
'getGoldVal' => 0,
);
$accountBindDb = SqlHelper::ormSelectOne(
myself()->_getMysql($openId),
't_sub_user_bind',
array(
'org_account_id' => $accountId,
)
);
$accountId = $accountBindDb ? $accountBindDb['cur_account_id'] : $accountId ;
$redis = $this->_getRedis($accountId);
$lastActiveTime = $redis->get(LAST_ACTIVE_TIME . $accountId);
if (!empty($lastActiveTime) && $lastActiveTime > myself()->_getNowDaySeconds()){
$info['loginVal'] = 1;
}
// $loginDyn = SqlHelper::ormSelectOne(
// myself()->_getMysql($openId),
// 't_dyndata',
// array(
// 'account_id' => $accountId,
// 'x' => TN_DAILY_LOGINS,
// 'y' => 0,
// )
// );
// if ($loginDyn){
// $info['loginVal'] = $loginDyn['val'];
// if (myself()->_getDaySeconds($loginDyn['modifytime']) < myself()->_getNowDaySeconds()) {
// $info['loginVal'] = 0;
// }
// }
$battleDb = SqlHelper::ormSelectOne(
myself()->_getMysql($openId),
't_battle',
array(
'account_id' => $accountId,
)
);
if ($battleDb){
$hisBattleData = json_decode($battleDb['battle_data'], true);
$todayBattleData = getXVal($hisBattleData, 'today_data', array());
if (myself()->_getDaySeconds(getXVal($todayBattleData, 'modifytime', 0)) == myself()->_getNowDaySeconds()) {
$info['battleTimes'] = getXVal($todayBattleData, "total_battle_times", 0);
$info['winTimes'] = getXVal($todayBattleData, "total_special_win_times", 0);
$info['kills'] = getXVal($todayBattleData, "total_kills_times", 0);
}
}
$getGoldDyn = SqlHelper::ormSelectOne(
myself()->_getMysql($openId),
't_dyndata',
array(
'account_id' => $accountId,
'x' => TN_DAILY_GATHER_GOLD,
'y' => 0,
)
);
if ($getGoldDyn){
$info['getGoldVal'] = $getGoldDyn['val'];
if (myself()->_getDaySeconds($getGoldDyn['modifytime']) < myself()->_getNowDaySeconds()) {
$info['getGoldVal'] = 0;
}
}
$this->_rspData(array(
'info' => $info,
));
}
public function hasAnyNft()
{
$accountAddress = strtolower(getReqVal('account_address', ''));
$ret = services\NftService::hasAnyNft($accountAddress);
error_log('hasAnyNft:' . $accountAddress . ' ret:' . $ret);
$this->_rspData(array(
'has_any_nft' => $ret ? 1 : 0,
));
error_log(2222);
}
public function nftMetaView()
{
$netId = getReqVal('net_id', '');
$tokenType = getReqVal('token_type', '');
$tokenId = getReqVal('token_id', '');
$info = array(
"token_id" => '',
"name" => "",
"description" => "",
"image" => "",
"attributes" => array(),
);
$nftDb = Nft::getNftByNetIdTokenTypeTokenId($netId, $tokenType, $tokenId);
if (empty($nftDb)) {
echo json_encode($info);
die;
}
switch ($nftDb['token_type']) {
case Nft::HERO_TYPE:
case Nft::GCARD_HERO_TYPE:
{
$heroDb = Hero::findByTokenId2($tokenId);
if (!$heroDb){
echo json_encode($info);
die;
}
$heroAttrs = emptyReplace(json_decode($heroDb['wealth_attr'], true), array());
$heroResult = \mt\EconomyAttribute::getAttrValue($heroAttrs);
$wealth = $heroResult['wealth'];
$wealth_rate = $heroResult['wealth_rate'];
$lucky = $heroResult['lucky'];
$lucky_rate = $heroResult['lucky_rate'];
$heroAbility = Hero::abilityInfo($heroDb);
$heroMeta = \mt\Hero::get($heroDb['hero_id']);
$NftMeta = \mt\NftDesc::getByItemId($heroDb['hero_id']);
$itemMeta = \mt\Item::get($heroDb['hero_id']);
$heroAtteMeta = \mt\EconomyAttribute::findByGrade($itemMeta['relationship'],$heroDb['quality']);
$info['token_id'] = $tokenId;
$info['name'] = $heroMeta['name'];
$info['description'] = $NftMeta['desc'];
if ($nftDb['token_type'] == Nft::GCARD_HERO_TYPE) {
$info['image'] = 'https://res2.counterfire.games/nft/video/normal/' . $heroMeta['id'] . '_c' . $this->getRealHeroQuality($heroDb) . '.mp4';
} else {
$info['image'] = 'https://res2.counterfire.games/nft/video/genesis/' . $heroMeta['id'] . '_' . $this->getRealHeroQuality($heroDb) . '.mp4';
}
if ($this->isWingHero($nftDb['token_id'])) {
$info['image'] = 'https://res2.counterfire.games/nft/video/wing/' . $heroMeta['id'] . '_w' . $this->getRealHeroQuality($heroDb) . '.mp4';
}
$info['animation_url'] = "https://res.counterfire.games/nft/video/".
$heroDb['hero_id'].'_'.$this->getRealHeroQuality($heroDb).".mp4";
if ($this->isWingHero($nftDb['token_id'])) {
$info['animation_url'] = "https://res.counterfire.games/nft/video/wing/".
$heroDb['hero_id'].'_w'.$this->getRealHeroQuality($heroDb).".mp4";
}
array_push($info['attributes'],array(
"trait_type" => "Tier",
"value" => intval($this->getRealHeroQuality($heroDb)),
));
array_push($info['attributes'],array(
"trait_type" => "Active Days",
"value" => $heroAtteMeta['validTime'],
));
array_push($info['attributes'],array(
"trait_type" => "Wealth Value",
"value" => floor($wealth * (1+$wealth_rate)),
));
array_push($info['attributes'],array(
"trait_type" => "Luck Value",
"value" => floor($lucky * (1+$lucky_rate)),
));
if ($this->isCloseBox()) {
$this->fillBoxMeta($info);
}
}
break;
case Nft::GOLD_BULLION_TYPE:
{
$nftMeta = \mt\NftDesc::getByItemId($nftDb['item_id']);
$info['token_id'] = $tokenId;
$info['name'] = $nftMeta['name'];
$info['description'] = $nftMeta['desc'];
$info['image'] = "https://res2.counterfire.games/nft/meta/".$nftDb['item_id'].".png";
if ($this->isCloseBox()) {
$this->fillBoxMeta($info);
}
}
break;
}
error_log(json_encode($info));
myself()->_rspData($info);
}
public function nftDetailList()
{
$rspObj = array(
'nfts' => array()
);
$data = file_get_contents('php://input');
$dataJson = json_decode($data, true);
if (count($dataJson['nfts']) > 20) {
myself()->_rspErr(1, 'nfts count error');
return;
}
foreach ($dataJson['nfts'] as $row) {
$netId = $row['net_id'];
$contractAddress = $row['contract_address'];
$tokenId = $row['token_id'];
$nftDb = Nft::getNftByNetIdContractAddressTokenId($netId, $contractAddress, $tokenId);
if (!empty($nftDb)) {
$info = array();
$this->internalNftDetail($netId, $nftDb, $info);
array_push($rspObj['nfts'], $info);
}
}
myself()->_rspData($rspObj);
}
public function nftDetailByContractAddress()
{
$contractAddress = getReqVal('contract_address', '');
$netId = getReqVal('net_id', '');
$tokenId = getReqVal('token_id', '');
$nftDb = Nft::getNftByNetIdContractAddressTokenId($netId, $contractAddress, $tokenId);
$info = array();
error_log(json_encode($_REQUEST));
$this->internalNftDetail($netId, $nftDb, $info);
error_log(json_encode($info));
myself()->_rspData($info);
}
public function nftDetailByTokenType()
{
$netId = getReqVal('net_id', '');
$tokenType = getReqVal('token_type', '');
$tokenId = getReqVal('token_id', '');
$nftDb = Nft::getNftByNetIdTokenTypeTokenId($netId, $tokenType, $tokenId);
$info = array();
error_log(json_encode($_REQUEST));
$this->internalNftDetail($netId, $nftDb, $info);
error_log(json_encode($info));
myself()->_rspData($info);
}
private function internalNftDetail($netId, &$nftDb, &$info)
{
$onSale = $this->nftIsOnSale($nftDb['net_id'], $nftDb['contract_address'], $nftDb['token_id']);
$info = array(
'net_id' => $netId,
'contract_address' => '',
'token_id' => '',
'owner_address' => '',
'meta_url' => '',
'name' => '',
'item_id' => 0,
'type' => 0,
'image' => '',
'on_sale' => $onSale,
'detail' => array(),
'last_lock_time' => 0,
);
if (!empty($nftDb)) {
if ($nftDb['owner_address'] == LOCK_CONTRACT_ADDRESS ||
$nftDb['owner_address'] == ETH_LOCK_CONTRACT_ADDRESS) {
$info['last_lock_time'] = $nftDb['last_lock_time'];
}
$this->internalGetNftDetail($nftDb, $info);
}
}
private function internalGetNftDetail($nftDb, &$info) {
$info['contract_address'] = $nftDb['contract_address'];
$info['token_id'] = $nftDb['token_id'];
$info['owner_address'] = $nftDb['owner_address'];
switch ($nftDb['token_type']) {
case Nft::HERO_TYPE:
case Nft::GCARD_HERO_TYPE:
{
$heroDb = Hero::findByTokenId2($nftDb['token_id']);
if ($heroDb) {
$heroAttrs = emptyReplace(json_decode($heroDb['wealth_attr'], true), array());
$heroResult = \mt\EconomyAttribute::getAttrValue($heroAttrs);
$wealth = $heroResult['wealth'];
$wealth_rate = $heroResult['wealth_rate'];
$lucky = $heroResult['lucky'];
$lucky_rate = $heroResult['lucky_rate'];
$heroAbility = Hero::abilityInfo($heroDb);
$heroMeta = \mt\Hero::get($heroDb['hero_id']);
if ($heroMeta) {
$itemMeta = \mt\Item::get($heroDb['hero_id']);
$heroAtteMeta = \mt\EconomyAttribute::findByGrade($itemMeta['relationship'],$heroDb['quality']);
$info['meta_url'] = NFT_META_URL . '/hero/meta/' . $nftDb['net_id'] . '/' . $nftDb['token_id'];
$info['name'] = $heroMeta['name'];
$info['item_id'] = $heroMeta['id'];
$info['type'] = $nftDb['token_type'];
//$info['image'] = 'https://res2.counterfire.games/nft/meta/' . $heroMeta['id'] . '_' . $this->getRealHeroQuality($heroDb) . '.gif';
if ($nftDb['token_type'] == Nft::GCARD_HERO_TYPE) {
$info['image'] = 'https://res2.counterfire.games/nft/video/normal/' . $heroMeta['id'] . '_c' . $this->getRealHeroQuality($heroDb) . '.mp4';
} else {
$info['image'] = 'https://res2.counterfire.games/nft/video/genesis/' . $heroMeta['id'] . '_' . $this->getRealHeroQuality($heroDb) . '.mp4';
}
if ($this->isWingHero($nftDb['token_id'])) {
$info['image'] = 'https://res2.counterfire.games/nft/video/wing/' . $heroMeta['id'] . '_w' . $this->getRealHeroQuality($heroDb) . '.mp4';
}
$info['detail']['quality'] = $this->getRealHeroQuality($heroDb);
$info['detail']['max_mining_days'] = $heroAtteMeta['validTime'];
$info['detail']['wealth'] = floor($wealth * (1+$wealth_rate));
$info['detail']['lucky'] = floor($lucky * (1+$lucky_rate));
$info['detail']['hp'] = $heroAbility['hp'];
$info['detail']['atk'] = $heroAbility['attack'];
$info['detail']['def'] = $heroAbility['defence'];
$info['detail']['block'] = $heroAbility['block'];
$info['detail']['crit'] = $heroAbility['critical'];
}
}
if ($this->isCloseBox()) {
$this->fillBoxDetail($info);
}
}
break;
case Nft::GOLD_BULLION_TYPE:
{
$nftMeta = \mt\NftDesc::getByItemId($nftDb['item_id']);
if ($nftMeta) {
$info['meta_url'] = NFT_META_URL . '/gold_bullion/meta/' . $nftDb['net_id'] . '/' . $nftDb['token_id'];
$info['name'] = $nftMeta['name'];
$info['item_id'] = $nftMeta['item_id'];
$info['type'] = $nftDb['token_type'];
$info['image'] = 'https://res2.counterfire.games/nft/meta/' . $nftMeta['item_id'] . '.png';
$info['detail']['gold_coins'] = $this->getGoldBullionGoldNum($nftDb['item_id']);
}
if ($this->isCloseBox()) {
$this->fillBoxDetail($info);
}
}
break;
case Nft::FOUNDER_TAG_TYPE:
{
$info['meta_url'] = 'https://nftmint.counterfire.games/nft/202403/' . $nftDb['token_id'];
$info['name'] = 'Founders Tag #1';
$info['item_id'] = 0;
$info['quality'] = $nftDb['quality'];
$info['type'] = $nftDb['token_type'];
$info['image'] = 'https://gateway.pinata.cloud/ipfs/QmNcUxWcNF85mx6aQp7PVNWJ89d9PqMpaHUT9vQTSTatGq';
switch ($nftDb['quality']) {
case 1:
{
$info['image'] = 'https://gateway.pinata.cloud/ipfs/QmNcUxWcNF85mx6aQp7PVNWJ89d9PqMpaHUT9vQTSTatGq';
}
break;
case 2:
{
$info['image'] = 'https://gateway.pinata.cloud/ipfs/QmctTzuks35UTTEQS2rx7fbq8gfQZvHLQL3sjSEZck59es';
}
break;
case 3:
{
$info['image'] = 'https://gateway.pinata.cloud/ipfs/QmfCL6uncbrYnwgZiqcgYcBgL6L2WtNC6m3UTooMLHKxyk';
}
break;
}
}
break;
}
}
private function getGoldBullionGoldNum($itemId)
{
switch ($itemId) {
case V_ITEM_GOLD_BULLION_1W:
{
return 1000;
}
break;
case V_ITEM_GOLD_BULLION_10W:
{
return 10000 * 1;
}
break;
default:
{
return 0;
}
}
}
private function isCloseBox()
{
$tokenId = getReqVal('token_id', '');
if ($tokenId == '6240619010000001') {
return false;
}
$openTime = 1719828000 + 3600 * 24;
if (myself()->_getNowTime() < $openTime) {
if (SERVER_ENV == _ONLINE) {
return true;
}
}
return false;
}
private function fillBoxMeta(&$info)
{
$info['name'] = 'Genesis Hero';
$info['description'] = 'Genesis Hero';
$info['image'] = "https://res2.counterfire.games/nft/box/Gen2_raw.gif";
$info['attributes'] = array();
}
private function fillBoxDetail(&$info)
{
$info = array(
'net_id' => $info['net_id'],
'contract_address' => $info['contract_address'],
'token_id' => $info['token_id'],
'owner_address' => $info['owner_address'],
'meta_url' => $info['meta_url'],
'name' => 'Genesis Hero',
'item_id' => 0,
'type' => $info['type'],
'image' => "https://res2.counterfire.games/nft/box/Gen2_raw.gif",
'on_sale' => $info['on_sale'],
'detail' => array()
);
}
private function nftIsOnSale($netId, $contractAddress, $tokenId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getMarketMysql(),
't_order',
array(
'net_id' => $netId,
'contract_address' => $contractAddress,
'token_id' => $tokenId,
'status' => 'ACTIVE',
)
);
return $row ? 1 : 0;
}
private function getRealHeroQuality($heroDb)
{
$quality = $heroDb['quality'];;
$quality = $quality - 1;
if ($quality <= 0) {
$quality = 1;
}
return $quality;
}
public function getHeroList()
{
$accountId = getReqVal('account_id', '');
$rows = SqlHelper::ormSelect(
myself()->_getMysql($accountId),
't_hero',
array(
'account_id' => $accountId,
)
);
$rspData = array(
'rows' => array()
);
foreach ($rows as $row) {
if ($row['quality'] <= 1) {
continue;
}
$info = array(
'uniid' => '',
'net_id' => 0,
'contract_address' => '',
'token_id' => '',
'owner_address' => '',
'meta_url' => '',
'name' => '',
'item_id' => 0,
'type' => 0,
'image' => '',
'on_sale' => 0,
'detail' => array()
);
$heroDb = $row;
$info['uniid'] = $heroDb['idx'];
$heroAttrs = emptyReplace(json_decode($heroDb['wealth_attr'], true), array());
$heroResult = \mt\EconomyAttribute::getAttrValue($heroAttrs);
$wealth = $heroResult['wealth'];
$wealth_rate = $heroResult['wealth_rate'];
$lucky = $heroResult['lucky'];
$lucky_rate = $heroResult['lucky_rate'];
$heroAbility = Hero::abilityInfo($heroDb);
$heroMeta = \mt\Hero::get($heroDb['hero_id']);
if ($heroMeta) {
$itemMeta = \mt\Item::get($heroDb['hero_id']);
$heroAtteMeta = \mt\EconomyAttribute::findByGrade($itemMeta['relationship'],$heroDb['quality']);
//$info['meta_url'] = NFT_META_URL . '/hero/meta/' . $nftDb['net_id'] . '/' . $nftDb['token_id'];
$info['name'] = $heroMeta['name'];
$info['item_id'] = $heroMeta['id'];
$info['type'] = Nft::HERO_TYPE;
$info['image'] = 'https://res2.counterfire.games/nft/video/normal/' . $heroMeta['id'] . '_c' . $this->getRealHeroQuality($heroDb) . '.mp4';
$info['detail']['quality'] = $this->getRealHeroQuality($heroDb);
$info['detail']['max_mining_days'] = $heroAtteMeta['validTime'];
$info['detail']['wealth'] = floor($wealth * (1+$wealth_rate));
$info['detail']['lucky'] = floor($lucky * (1+$lucky_rate));
$info['detail']['hp'] = $heroAbility['hp'];
$info['detail']['atk'] = $heroAbility['attack'];
$info['detail']['def'] = $heroAbility['defence'];
$info['detail']['block'] = $heroAbility['block'];
$info['detail']['crit'] = $heroAbility['critical'];
}
array_push($rspData['rows'], $info);
}
myself()->_rspData($rspData);
}
private function isWingHero($tokenId)
{
return $tokenId > 6240603010001668 && $tokenId <= 6240603010002168;
}
}