282 lines
9.0 KiB
PHP
282 lines
9.0 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/Chip.php');
|
|
require_once('models/Transaction.php');
|
|
require_once('models/BuyRecord.php');
|
|
require_once('models/Chip.php');
|
|
require_once('models/TransactionPrefee.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;
|
|
use models\Chip;
|
|
use models\TransactionPrefee;
|
|
|
|
class BlockChainController extends BaseAuthedController {
|
|
|
|
const TEST_ITEM_ID = 666;
|
|
public function _handlePre()
|
|
{
|
|
parent::_handlePre();
|
|
if (getReqVal('a', '') != 'getTransactionList' && !myself()->_isValidAddress()) {
|
|
die(json_encode(array(
|
|
'errcode' => 501,
|
|
'errmsg' => 'you are not a wallet user'
|
|
)));
|
|
return;
|
|
}
|
|
error_log(json_encode($_REQUEST));
|
|
}
|
|
|
|
public function getTransactionList()
|
|
{
|
|
$trans = array();
|
|
foreach (Transaction::all() as $tran) {
|
|
if (myself()->_getNowTime() - $tran['createtime'] < 24*3600){
|
|
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 reportResult()
|
|
{
|
|
$transId = getReqVal('trans_id', '');
|
|
$result = getReqVal('result', '');
|
|
error_log($result);
|
|
Transaction::reportResult($transId, $result);
|
|
myself()->_rspOk();
|
|
}
|
|
|
|
public function getJumpInfo()
|
|
{
|
|
$transId = getReqVal('trans_id', '');
|
|
if (!$transId){
|
|
myself()->_rspErr(101, 'trans_id paramater error');
|
|
return;
|
|
}
|
|
$tranDb= Transaction::find($transId);
|
|
if (!$tranDb){
|
|
myself()->_rspErr(101, 'trans_id paramater error');
|
|
return;
|
|
}
|
|
$data = Transaction::getJumpInfo($tranDb);
|
|
myself()->_rspData($data);
|
|
}
|
|
|
|
public function getTransactionInfo()
|
|
{
|
|
$transId = getReqVal('trans_id', '');
|
|
if (!$transId){
|
|
myself()->_rspErr(101, 'trans_id paramater error');
|
|
return;
|
|
}
|
|
$tranDb= Transaction::find($transId);
|
|
myself()->_rspData(array(
|
|
'data' => array(
|
|
'trans_id' => $tranDb['trans_id'],
|
|
'item_id' => $tranDb['item_id'],
|
|
'action' => Transaction::getActionDesc($tranDb),
|
|
'status' => Transaction::getStatusDesc($tranDb),
|
|
'time' => $tranDb['createtime'],
|
|
)
|
|
));
|
|
}
|
|
|
|
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()->_getAddress()
|
|
);
|
|
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($uniid);
|
|
if (!$gunDb) {
|
|
myself()->_rspErr(1, 'gun not found');
|
|
return;
|
|
}
|
|
if ($gunDb['token_id']) {
|
|
myself()->_rspErr(1, 'already activated');
|
|
return;
|
|
}
|
|
$tokenId = $gunDb['active_token_id'];
|
|
if (!$tokenId) {
|
|
$tokenId = BuyRecord::genOrderId
|
|
(
|
|
2006,
|
|
phpcommon\BC_FUNC_CREATION,
|
|
myself()->_getNowTime(),
|
|
myself()->_getAddress()
|
|
);
|
|
Gun::Update($gunDb['gun_uniid'],
|
|
array(
|
|
'active_token_id' => $tokenId,
|
|
'active_count' => function () {
|
|
return 'active_count + 1';
|
|
}
|
|
));
|
|
}
|
|
$this->internalActivate721Nft($tokenId, Nft::EQUIP_TYPE, $gunDb['gun_uniid'], $gunDb['gun_id']);
|
|
}
|
|
break;
|
|
case 3:
|
|
{
|
|
$chipDb = Chip::find($uniid);
|
|
if (!$chipDb) {
|
|
myself()->_rspErr(1, 'chip not found');
|
|
return;
|
|
}
|
|
if ($chipDb['token_id']) {
|
|
myself()->_rspErr(1, 'already activated');
|
|
return;
|
|
}
|
|
$tokenId = $chipDb['active_token_id'];
|
|
if (!$tokenId) {
|
|
$tokenId = BuyRecord::genOrderId
|
|
(
|
|
2006,
|
|
phpcommon\BC_FUNC_CREATION,
|
|
myself()->_getNowTime(),
|
|
myself()->_getAddress()
|
|
);
|
|
Chip::Update($chipDb['chip_uniid'],
|
|
array(
|
|
'active_token_id' => $tokenId,
|
|
'active_count' => function () {
|
|
return 'active_count + 1';
|
|
}
|
|
));
|
|
}
|
|
$this->internalActivate721Nft($tokenId, Nft::CHIP_TYPE, $chipDb['chip_uniid'], $chipDb['item_id']);
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
myself()->_rspErr(1, 'type param error');
|
|
return;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
private function internalActivate721Nft($tokenId, $tokenType, $itemUniId, $itemId)
|
|
{
|
|
$params = array(
|
|
'c' => 'BcService',
|
|
'a' => 'activate721Nft',
|
|
'account_id' => myself()->_getAccountId(),
|
|
'session_id' => myself()->_getSessionId(),
|
|
'account' => myself()->_getAddress(),
|
|
'token_id' => $tokenId,
|
|
'token_type' => $tokenType,
|
|
'item_uniid' => $itemUniId,
|
|
'item_id' => $itemId,
|
|
'net_id' => NET_ID
|
|
);
|
|
{
|
|
$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,
|
|
1,
|
|
1
|
|
);
|
|
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)];
|
|
}
|
|
|
|
}
|