556 lines
18 KiB
PHP
556 lines
18 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', '');
|
|
error_log($result);
|
|
Transaction::reportResult($transId, $result);
|
|
myself()->_rspOk();
|
|
}
|
|
|
|
public function evolve721Nft()
|
|
{
|
|
$type = getReqVal('type', '');
|
|
$tokenId1 = getReqVal('token_id1', '');
|
|
$tokenId2 = getReqVal('token_id2', '');
|
|
switch ($type) {
|
|
case 1:
|
|
{
|
|
$nft1 = Hero::findByTokenId($tokenId1);
|
|
$nft2 = Hero::findByTokenId($tokenId2);
|
|
if (!$nft1 || $nft2) {
|
|
myself()->_rspErr(1, 'token paramater error');
|
|
return;
|
|
}
|
|
$this->internalBcCall(
|
|
array(
|
|
'c' => 'BcService',
|
|
'a' => 'evolve721Nft',
|
|
'account_id' => myself()->_getAccountId(),
|
|
'session_id' => myself()->_getSessionId(),
|
|
'account' => myself()->_getOpenId(),
|
|
'type' => $type,
|
|
'token_id1' => $tokenId1,
|
|
'token_id2' => $tokenId2
|
|
),
|
|
array(
|
|
'action' => Transaction::EVOLVE_721_ACTION_TYPE,
|
|
'tokenId' => '',
|
|
'itemType' => $type,
|
|
'itemUniId' => 0,
|
|
'itemId' => 0
|
|
)
|
|
);
|
|
}
|
|
break;
|
|
case 2:
|
|
{
|
|
$nft1 = Gun::findByTokenId($tokenId1);
|
|
$nft2 = Gun::findByTokenId($tokenId2);
|
|
if (!$nft1 || $nft2) {
|
|
myself()->_rspErr(1, 'token paramater error');
|
|
return;
|
|
}
|
|
$this->internalBcCall(
|
|
array(
|
|
'c' => 'BcService',
|
|
'a' => 'evolve721Nft',
|
|
'account_id' => myself()->_getAccountId(),
|
|
'session_id' => myself()->_getSessionId(),
|
|
'account' => myself()->_getOpenId(),
|
|
'type' => $type,
|
|
'token_id1' => $tokenId1,
|
|
'token_id2' => $tokenId2
|
|
),
|
|
array(
|
|
'action' => Transaction::EVOLVE_721_ACTION_TYPE,
|
|
'tokenId' => '',
|
|
'itemType' => $type,
|
|
'itemUniId' => 0,
|
|
'itemId' => 0
|
|
)
|
|
);
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
myself()->_rspErr(1, 'type param error');
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function evolveChip()
|
|
{
|
|
$tokenId1 = getReqVal('token_id1', '');
|
|
$tokenIds = explode('|', getReqVal('token_ids', ''));
|
|
|
|
if (in_array($tokenId1, $tokenIds) || count($tokenIds) <= 0) {
|
|
myself()->_rspErr(1, 'token_ids paramater error');
|
|
return;
|
|
}
|
|
|
|
$this->internalBcCall(
|
|
array(
|
|
'c' => 'BcService',
|
|
'a' => 'evolveChip',
|
|
'account_id' => myself()->_getAccountId(),
|
|
'session_id' => myself()->_getSessionId(),
|
|
'account' => myself()->_getOpenId(),
|
|
'token_id' => $tokenId,
|
|
'token_ids' => $tokenIds
|
|
),
|
|
array(
|
|
'action' => Transaction::EVOLVE_CHIP_ACTION_TYPE,
|
|
'tokenId' => $tokenId1,
|
|
'itemType' => 0,
|
|
'itemUniId' => 0,
|
|
'itemId' => 0
|
|
)
|
|
);
|
|
}
|
|
|
|
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, $itemMeta, $num);
|
|
}
|
|
break;
|
|
case mt\Item::CHIP_BOX_TYPE:
|
|
{
|
|
$this->internalOpenChipBox($itemDb, $itemMeta, $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 = '';
|
|
|
|
{
|
|
$heros = array();
|
|
$guns = array();
|
|
$specHeros = array();
|
|
$specGuns = array();
|
|
mt\Item::groupFragment($tokenIds, $heros, $guns, $specHeros, $specGuns);
|
|
if (count($tokenIds) != (
|
|
count($heros) + count($guns) + count($specHeros) + count($specGuns)
|
|
)) {
|
|
myself()->_rspErr(101, 'token_ids paramater error');
|
|
return;
|
|
}
|
|
if (count($specHeros) + count($specGuns) > 1) {
|
|
myself()->_rspErr(101, 'token_ids paramater error');
|
|
return;
|
|
}
|
|
if (count($heros) > 0 && count($guns) > 0) {
|
|
myself()->_rspErr(101, 'token_ids paramater error');
|
|
return;
|
|
}
|
|
if (count($heros) > 0) {
|
|
if (count($heros) != 8 || count($specGuns) > 0) {
|
|
myself()->_rspErr(101, 'token_ids paramater error');
|
|
return;
|
|
}
|
|
} else if (count($guns) > 0) {
|
|
if (count($guns) != 8 || count($specHeros) > 0) {
|
|
myself()->_rspErr(101, 'token_ids paramater error');
|
|
return;
|
|
}
|
|
} else {
|
|
myself()->_rspErr(101, 'token_ids paramater 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)
|
|
);
|
|
{
|
|
error_log(3333333);
|
|
$url = self::getWeb3ServiceUrl();
|
|
$response = '';
|
|
if (!phpcommon\HttpClient::get
|
|
($url,
|
|
$params,
|
|
$response)) {
|
|
error_log(444444);
|
|
myself()->_rspErr(500, 'server internal error');
|
|
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']
|
|
));
|
|
error_log(5555555555);
|
|
} else {
|
|
error_log(22222222222222);
|
|
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, $itemMeta, $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, $itemMeta, $num) {
|
|
$tokenId = $itemMeta['include_item_id'];
|
|
$itemId = $itemMeta['include_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' => $itemId,
|
|
'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'],
|
|
$itemId
|
|
);
|
|
myself()->_rspData(array(
|
|
'trans_id' => $transId,
|
|
'params' => $rspObj['params']
|
|
));
|
|
} else {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function internalBcCall($params, $transParams) {
|
|
$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,
|
|
$transParams['action'],
|
|
$transParams['tokenId'],
|
|
$transParams['tokenType'],
|
|
$transParams['itemUniId'],
|
|
$transParams['itemId']
|
|
);
|
|
myself()->_rspData(array(
|
|
'trans_id' => $transId,
|
|
'params' => $rspObj['params']
|
|
));
|
|
} else {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
die();
|
|
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)];
|
|
}
|
|
|
|
}
|