380 lines
11 KiB
PHP
380 lines
11 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/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/BcOrder.php');
|
|
require_once('models/Mall.php');
|
|
|
|
require_once('services/AwardService.php');
|
|
require_once('services/PropertyChgService.php');
|
|
require_once('services/BlockChainService.php');
|
|
|
|
use phpcommon\SqlHelper;
|
|
use models\Hero;
|
|
use models\Gun;
|
|
use models\Nft;
|
|
use models\Transaction;
|
|
use models\BuyRecord;
|
|
use models\Chip;
|
|
use models\BcOrder;
|
|
use models\Mall;
|
|
|
|
use services\BlockChainService;
|
|
|
|
class BlockChainController extends BaseAuthedController {
|
|
|
|
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'] && $heroDb['activate']) {
|
|
myself()->_rspErr(1, 'already activated');
|
|
return;
|
|
}
|
|
/*
|
|
if ($heroDb['state'] == 1) {
|
|
myself()->_rspErr(1, 'cant mint');
|
|
return;
|
|
}*/
|
|
$tokenId = $heroDb['token_id'];
|
|
if (!$tokenId) {
|
|
$tokenId = BuyRecord::genOrderId
|
|
(
|
|
GAME_ID,
|
|
phpcommon\BC_FUNC_CREATION,
|
|
myself()->_getNowTime(),
|
|
myself()->_getAddress()
|
|
);
|
|
Hero::safeUpdateTokenId($heroDb['hero_uniid'], $tokenId);
|
|
$heroDb = Hero::find($uniid);
|
|
if (!$heroDb) {
|
|
myself()->_rspErr(1, 'hero not found');
|
|
return;
|
|
}
|
|
if ($heroDb['token_id'] != $tokenId) {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
}
|
|
$this->internalActivate721Nft($tokenId, Nft::HERO_TYPE, $heroDb['hero_uniid'], $heroDb['hero_id']);
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
myself()->_rspErr(1, 'type param error');
|
|
return;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function mintGoldBullion()
|
|
{
|
|
$type = getReqVal('type', 0);
|
|
$itemId = 0;
|
|
switch ($type) {
|
|
case 1:
|
|
{
|
|
$itemId = V_ITEM_GOLD_BULLION_1W;
|
|
}
|
|
break;
|
|
case 2:
|
|
{
|
|
$itemId = V_ITEM_GOLD_BULLION_10W;
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
|
'trans_req' => $rspObj['trans_req']
|
|
));
|
|
} else {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function _buyDiamond()
|
|
{
|
|
return;
|
|
error_log(json_encode($_REQUEST));
|
|
$num = getReqVal('num', 0);
|
|
$tokenType = getReqVal('token_type', '');
|
|
if (!is_numeric($num)) {
|
|
myself()->_rspErr(1, "num is invalid, {$num}");
|
|
return;
|
|
}
|
|
if ($num <= 0) {
|
|
myself()->_rspErr(1, "num is invalid, {$num}");
|
|
return;
|
|
}
|
|
if (!in_array(
|
|
$tokenType,
|
|
array(
|
|
BlockChainService::CURRENCY_CEG,
|
|
BlockChainService::CURRENCY_USDC,
|
|
BlockChainService::CURRENCY_USDT,
|
|
)
|
|
)) {
|
|
myself()->_rspErr(1, "token_type error");
|
|
return;
|
|
}
|
|
$rate = 1;
|
|
if (in_array(
|
|
$tokenType,
|
|
array(
|
|
BlockChainService::CURRENCY_USDC,
|
|
BlockChainService::CURRENCY_USDT,
|
|
)
|
|
)) {
|
|
$rate = 10;
|
|
}
|
|
$jinDu = BlockChainService::getCurrencyDecimals($tokenType);
|
|
if ($jinDu === false) {
|
|
myself()->_rspErr(1, "token_type error");
|
|
return;
|
|
}
|
|
$currency = $tokenType;
|
|
$price = BlockChainService::formatCurrencyEx($num, $jinDu);
|
|
|
|
$itemId = V_ITEM_DIAMOND;
|
|
$itemCount = $num * $rate;
|
|
|
|
$rspObj = BlockChainService::gameItemMallBuy(
|
|
Transaction::BUY_GOODS_ACTION_TYPE,
|
|
$currency,
|
|
$price,
|
|
$itemId,
|
|
$itemCount
|
|
);
|
|
|
|
BcOrder::upsert($rspObj['trans_id'], array(
|
|
'item_id' => $itemId,
|
|
'item_num' => $itemCount,
|
|
'order_type' => BcOrder::SPEC_ORDER_TYPE,
|
|
'price' => $num,
|
|
'currency_name' => $currency,
|
|
'ext_data' => json_encode(array(
|
|
'mode' => BcOrder::SHOP_BUY_MODE_NORMAL,
|
|
)),
|
|
));
|
|
|
|
error_log(json_encode(
|
|
array(
|
|
'trans_id' => $rspObj['trans_id'],
|
|
'params' => $rspObj['params'],
|
|
'item_id' => $itemId,
|
|
'item_num' => $itemCount
|
|
)
|
|
));
|
|
myself()->_rspData(
|
|
array(
|
|
'trans_id' => $rspObj['trans_id'],
|
|
'params' => $rspObj['params'],
|
|
'item_id' => $itemId,
|
|
'item_num' => $itemCount
|
|
)
|
|
);
|
|
}
|
|
|
|
public function _buyMallProduct()
|
|
{
|
|
return;
|
|
$address = myself()->_getAddress();
|
|
if (empty($address)) {
|
|
myself()-_rspErr(1, 'address not found');
|
|
return;
|
|
}
|
|
|
|
$goodsUuid = getReqVal('goods_uuid', '');
|
|
$price = getReqVal('price', '');
|
|
if (empty($price)) {
|
|
myself()-_rspErr(1, 'price not found');
|
|
return;
|
|
}
|
|
if (!is_numeric($price)) {
|
|
myself()-_rspErr(1, 'price not number');
|
|
return;
|
|
}
|
|
|
|
$goodsDb = Mall::findByGoodsUuid($goodsUuid);
|
|
if (!$goodsDb) {
|
|
myself()-_rspErr(1, 'goods not found, idx:');
|
|
return;
|
|
}
|
|
if ($goodsDb['status'] != Mall::PENDING_STATE) {
|
|
myself()-_rspErr(1, 'cant buy');
|
|
return;
|
|
}
|
|
|
|
$rspObj = BlockChainService::gameItemMarketBuy(
|
|
Transaction::BUY_GOODS_FROM_MARKET_ACTION_TYPE,
|
|
$goodsDb['seller_address'],
|
|
$goodsDb['currency'],
|
|
$goodsDb['price'],
|
|
$goodsDb['item_id'],
|
|
$goodsDb['item_num'],
|
|
$goodsDb['order_id']
|
|
);
|
|
|
|
Mall::buy($goodsUuid, $address);
|
|
|
|
$this->_rspData(array(
|
|
'trans_id' => $rspObj['trans_id'],
|
|
'order_id' => $goodsDb['order_id'],
|
|
'params' => $rspObj['params'],
|
|
));
|
|
}
|
|
|
|
private static function getWeb3ServiceUrl()
|
|
{
|
|
$web3ServiceCluster = require_once('../config/web3service.cluster.php');
|
|
return $web3ServiceCluster[rand() % count($web3ServiceCluster)];
|
|
}
|
|
|
|
}
|