467 lines
14 KiB
PHP
467 lines
14 KiB
PHP
<?php
|
|
|
|
require_once('mt/Parameter.php');
|
|
require_once('mt/Item.php');
|
|
require_once('mt/Drop.php');
|
|
require_once('mt/Hero.php');
|
|
|
|
require_once('models/Bag.php');
|
|
require_once('models/Hero.php');
|
|
require_once('models/Gun.php');
|
|
require_once('models/Nft.php');
|
|
require_once('models/Transaction.php');
|
|
require_once('models/BuyRecord.php');
|
|
|
|
require_once('services/AwardService.php');
|
|
require_once('services/PropertyChgService.php');
|
|
|
|
require_once('services/NameService.php');
|
|
|
|
use phpcommon\SqlHelper;
|
|
use models\Bag;
|
|
use models\Hero;
|
|
use models\Gun;
|
|
use models\Nft;
|
|
use models\Transaction;
|
|
use models\BuyRecord;
|
|
|
|
class BlockChainController extends BaseAuthedController {
|
|
|
|
public function _handlePre()
|
|
{
|
|
parent::_handlePre();
|
|
if (getReqVal('a', '') != 'getTransactionList' &&
|
|
myself()->_getChannel() != BC_CHANNEL) {
|
|
die(json_encode(array(
|
|
'errcode' => 501,
|
|
'errmsg' => 'you are not a wallet user'
|
|
)));
|
|
return;
|
|
}
|
|
}
|
|
|
|
public function getTransactionList()
|
|
{
|
|
$trans = array();
|
|
foreach (Transaction::all() as $tran) {
|
|
array_push(
|
|
$trans,
|
|
array(
|
|
'trans_id' => $tran['trans_id'],
|
|
'item_id' => $tran['item_id'],
|
|
'action' => Transaction::getActionDesc($tran),
|
|
'status' => Transaction::getStatusDesc($tran),
|
|
'time' => $tran['createtime'],
|
|
)
|
|
);
|
|
}
|
|
$this->_rspData(array(
|
|
'transactions' => $trans
|
|
));
|
|
}
|
|
|
|
public function active721Nft()
|
|
{
|
|
$type = getReqVal('type', 0);
|
|
$uniid = getReqVal('uniid', 0);
|
|
switch ($type) {
|
|
case 1:
|
|
{
|
|
$heroDb = Hero::find($uniid);
|
|
if (!$heroDb) {
|
|
myself()->_rspErr(1, 'hero not found');
|
|
return;
|
|
}
|
|
if ($heroDb['token_id']) {
|
|
myself()->_rspErr(1, 'already activated');
|
|
return;
|
|
}
|
|
$tokenId = $heroDb['active_token_id'];
|
|
if (!$tokenId) {
|
|
$tokenId = BuyRecord::genOrderId
|
|
(
|
|
2006,
|
|
phpcommon\BC_FUNC_CREATION,
|
|
myself()->_getNowTime(),
|
|
myself()->_getOpenId()
|
|
);
|
|
Hero::Update($heroDb['hero_uniid'],
|
|
array(
|
|
'active_token_id' => $tokenId,
|
|
'active_count' => function () {
|
|
return 'active_count + 1';
|
|
}
|
|
));
|
|
}
|
|
$this->internalActivate721Nft($tokenId, Nft::HERO_TYPE, $heroDb['hero_uniid'], $heroDb['hero_id']);
|
|
}
|
|
break;
|
|
case 2:
|
|
{
|
|
$gunDb = Gun::find($uuid);
|
|
if (!$gunDb) {
|
|
myself()->_rspErr(1, 'gun not found');
|
|
return;
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
myself()->_rspErr(1, 'type param error');
|
|
return;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function reportResult()
|
|
{
|
|
$transId = getReqVal('trans_id', '');
|
|
$result = getReqVal('result', '');
|
|
Transaction::reportResult($transId, $result);
|
|
myself()->_rspOk();
|
|
}
|
|
|
|
public function evolve721Nft()
|
|
{
|
|
|
|
}
|
|
|
|
public function evolveChip()
|
|
{
|
|
|
|
}
|
|
|
|
public function mintShardBatchUser()
|
|
{
|
|
$itemUniId = getReqVal('item_uniid', 0);
|
|
$num = getReqVal('num', 0);
|
|
$itemDb = Bag::findByUniId($itemUniId);
|
|
if ($num <= 0) {
|
|
myself()->_rspErr(1, 'num paramater error');
|
|
return;
|
|
}
|
|
if (!$itemDb || $itemDb['item_num'] < $num) {
|
|
myself()->_rspErr(1, 'item not enough');
|
|
return;
|
|
}
|
|
$itemMeta = mt\Item::get($itemDb['item_id']);
|
|
switch ($itemMeta['type']) {
|
|
case mt\Item::FRAGMENT_BOX_TYPE:
|
|
{
|
|
$this->internalOpenFragmentBox($itemDb, $num);
|
|
}
|
|
break;
|
|
case mt\Item::CHIP_BOX_TYPE:
|
|
{
|
|
$this->internalOpenChipBox($itemDb, $num);
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
myself()->_rspErr(1, 'item type error');
|
|
return;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function shardMixByUser()
|
|
{
|
|
$tokenIds = explode('|', getReqVal('token_ids', ''));
|
|
$itemId = getReqVal('item_id', '');
|
|
|
|
$tokenId = '';
|
|
$tokenType = '';
|
|
|
|
if (count($tokenIds) <= 0) {
|
|
myself()->_rspErr(1, 'token_ids paramaer error');
|
|
return;
|
|
}
|
|
|
|
$part = 0;
|
|
$hasSpecHero = false;
|
|
$hasSpecGun = false;
|
|
$specCount = 0;
|
|
foreach ($tokenIds as $itemId) {
|
|
$itemMeta = mt\Item::get($itemId);
|
|
if (!$itemMeta || !mt\Item::isFragment($itemMeta)) {
|
|
myself()->_rspErr(1, 'token_ids paramaer error');
|
|
return;
|
|
}
|
|
if ($part == 0) {
|
|
if (!($itemMeta['sub_type'] == 3 ||
|
|
$itemMeta['sub_type'] == 4)) {
|
|
$part = $itemMeta['sub_type'];
|
|
} else {
|
|
if ($itemMeta['sub_type'] == 3) {
|
|
$hasSpecHero = true;
|
|
}
|
|
if ($itemMeta['sub_type'] == 4) {
|
|
$hasGunHero = true;
|
|
}
|
|
++$specCount;
|
|
}
|
|
} else {
|
|
if ($part != $itemMeta['sub_type']) {
|
|
myself()->_rspErr(1, 'token_ids paramaer error');
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
if ($specCount > 1) {
|
|
myself()->_rspErr(1, 'token_ids paramaer error');
|
|
return;
|
|
}
|
|
if ($part == 1) {
|
|
if ($hasSpecGun) {
|
|
myself()->_rspErr(1, 'token_ids paramaer error');
|
|
return;
|
|
}
|
|
$tokenType = Nft::HERO_TYPE;
|
|
if (!mt\Item::canMixHero($tokenIds)) {
|
|
myself()->_rspErr(1, 'token_ids paramaer error');
|
|
return;
|
|
}
|
|
if ($hasSpecHero) {
|
|
$itemMeta = mt\Item::get($itemId);
|
|
if (!$itemMeta || $itemMeta['type'] != mt\Item::HERO_TYPE) {
|
|
myself()->_rspErr(1, 'token_ids paramaer error');
|
|
return;
|
|
}
|
|
}
|
|
} else if ($part == 2) {
|
|
if ($hasSpecHero) {
|
|
myself()->_rspErr(1, 'token_ids paramaer error');
|
|
return;
|
|
}
|
|
$tokenType = Nft::EQUIP_TYPE;
|
|
if (!mt\Item::canMixGun($tokenIds)) {
|
|
myself()->_rspErr(1, 'token_ids paramaer error');
|
|
return;
|
|
}
|
|
if ($hasSpecGun) {
|
|
$itemMeta = mt\Item::get($itemId);
|
|
if (!$itemMeta || $itemMeta['type'] != mt\Item::GUN_TYPE) {
|
|
myself()->_rspErr(1, 'token_ids paramaer error');
|
|
return;
|
|
}
|
|
}
|
|
} else {
|
|
myself()->_rspErr(1, 'token_ids paramaer error');
|
|
return;
|
|
}
|
|
|
|
$tokenId = BuyRecord::genOrderId
|
|
(
|
|
2006,
|
|
phpcommon\BC_FUNC_CREATION,
|
|
myself()->_getNowTime(),
|
|
myself()->_getOpenId()
|
|
);
|
|
|
|
$params = array(
|
|
'c' => 'BcService',
|
|
'a' => 'shardMixByUser',
|
|
'account_id' => myself()->_getAccountId(),
|
|
'session_id' => myself()->_getSessionId(),
|
|
'account' => myself()->_getOpenId(),
|
|
'token_id' => $tokenId,
|
|
'token_type' => $tokenType,
|
|
'item_id' => $itemId,
|
|
'token_ids' => implode('|', $tokenIds)
|
|
);
|
|
{
|
|
$url = self::getWeb3ServiceUrl();
|
|
$response = '';
|
|
if (!phpcommon\HttpClient::get
|
|
($url,
|
|
$params,
|
|
$response)) {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
die();
|
|
return;
|
|
}
|
|
error_log($response);
|
|
$rspObj = json_decode($response, true);
|
|
if ($rspObj['errcode'] == 0) {
|
|
$transId = $rspObj['trans_id'];
|
|
Transaction::add(
|
|
$transId,
|
|
Transaction::SHARD_MIX_BY_USER_ACTION_TYPE,
|
|
$tokenId,
|
|
$tokenType,
|
|
0,
|
|
$itemId
|
|
);
|
|
myself()->_rspData(array(
|
|
'trans_id' => $transId,
|
|
'params' => $rspObj['params']
|
|
));
|
|
} else {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function internalActivate721Nft($tokenId, $tokenType, $itemUniId, $itemId)
|
|
{
|
|
$params = array(
|
|
'c' => 'BcService',
|
|
'a' => 'activate721Nft',
|
|
'account_id' => myself()->_getAccountId(),
|
|
'session_id' => myself()->_getSessionId(),
|
|
'account' => myself()->_getOpenId(),
|
|
'token_id' => $tokenId,
|
|
'token_type' => $tokenType,
|
|
'item_uniid' => $itemUniId,
|
|
'item_id' => $itemId
|
|
);
|
|
{
|
|
$url = self::getWeb3ServiceUrl();
|
|
$response = '';
|
|
if (!phpcommon\HttpClient::get
|
|
($url,
|
|
$params,
|
|
$response)) {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
die();
|
|
return;
|
|
}
|
|
error_log($response);
|
|
$rspObj = json_decode($response, true);
|
|
if ($rspObj['errcode'] == 0) {
|
|
$transId = $rspObj['trans_id'];
|
|
Transaction::add(
|
|
$transId,
|
|
Transaction::MINT_721_ACTION_TYPE,
|
|
$tokenId,
|
|
$tokenType,
|
|
$itemUniId,
|
|
$itemId
|
|
);
|
|
myself()->_rspData(array(
|
|
'trans_id' => $transId,
|
|
'params' => $rspObj['params']
|
|
));
|
|
} else {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function internalOpenChipBox($itemDb, $num) {
|
|
$tokenId = BuyRecord::genOrderId
|
|
(
|
|
2006,
|
|
phpcommon\BC_FUNC_CREATION,
|
|
myself()->_getNowTime(),
|
|
myself()->_getOpenId()
|
|
);
|
|
$params = array(
|
|
'c' => 'BcService',
|
|
'a' => 'activate1155Nft',
|
|
'account_id' => myself()->_getAccountId(),
|
|
'session_id' => myself()->_getSessionId(),
|
|
'account' => myself()->_getOpenId(),
|
|
'token_id' => $tokenId,
|
|
'item_uniid' => $itemDb['item_uniid'],
|
|
'item_id' => $itemDb['item_id'],
|
|
'num' => $num,
|
|
);
|
|
{
|
|
$url = self::getWeb3ServiceUrl();
|
|
$response = '';
|
|
if (!phpcommon\HttpClient::get
|
|
($url,
|
|
$params,
|
|
$response)) {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
die();
|
|
return;
|
|
}
|
|
error_log($response);
|
|
$tokenType = Nft::CHIP_TYPE;
|
|
$rspObj = json_decode($response, true);
|
|
if ($rspObj['errcode'] == 0) {
|
|
$transId = $rspObj['trans_id'];
|
|
Transaction::add(
|
|
$transId,
|
|
Transaction::MINT_1155_ACTION_TYPE,
|
|
$tokenId,
|
|
$tokenType,
|
|
$itemUniId,
|
|
$itemId
|
|
);
|
|
myself()->_rspData(array(
|
|
'trans_id' => $transId,
|
|
'params' => $rspObj['params']
|
|
));
|
|
} else {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function internalOpenFragmentBox($itemDb, $num) {
|
|
$tokenId = $itemDb['item_id'];
|
|
$params = array(
|
|
'c' => 'BcService',
|
|
'a' => 'mintShardBatchUser',
|
|
'account_id' => myself()->_getAccountId(),
|
|
'session_id' => myself()->_getSessionId(),
|
|
'account' => myself()->_getOpenId(),
|
|
'token_id' => $tokenId,
|
|
'item_uniid' => $itemDb['item_uniid'],
|
|
'item_id' => $itemDb['item_id'],
|
|
'num' => $num
|
|
);
|
|
{
|
|
$url = self::getWeb3ServiceUrl();
|
|
$response = '';
|
|
if (!phpcommon\HttpClient::get
|
|
($url,
|
|
$params,
|
|
$response)) {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
die();
|
|
return;
|
|
}
|
|
error_log($response);
|
|
$tokenType = Nft::FRAGMENT_TYPE;
|
|
$rspObj = json_decode($response, true);
|
|
if ($rspObj['errcode'] == 0) {
|
|
$transId = $rspObj['trans_id'];
|
|
Transaction::add(
|
|
$transId,
|
|
Transaction::MINT_SHARD_BATCH_ACTION_TYPE,
|
|
$tokenId,
|
|
$tokenType,
|
|
$itemDb['item_uniid'],
|
|
$itemDb['item_id']
|
|
);
|
|
myself()->_rspData(array(
|
|
'trans_id' => $transId,
|
|
'params' => $rspObj['params']
|
|
));
|
|
} else {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static function getWeb3ServiceUrl()
|
|
{
|
|
if (SERVER_ENV == _TEST) {
|
|
return 'http://127.0.0.1:7672/webapp/index.php';
|
|
}
|
|
$web3ServiceCluster = require_once('../config/web3service.cluster.php');
|
|
return $web3ServiceCluster[rand() % count($web3ServiceCluster)];
|
|
}
|
|
|
|
}
|