491 lines
16 KiB
PHP
491 lines
16 KiB
PHP
<?php
|
|
|
|
require_once('mt/MarketGoods.php');
|
|
require_once('mt/MarketBatch.php');
|
|
require_once('mt/Item.php');
|
|
require_once('mt/WhiteList.php');
|
|
require_once('mt/Currency.php');
|
|
require_once('mt/Hero.php');
|
|
require_once('mt/Parameter.php');
|
|
|
|
require_once('models/BoxOrder.php');
|
|
require_once('models/Nft.php');
|
|
require_once('models/BuyRecord.php');
|
|
|
|
require_once('services/MarketService.php');
|
|
require_once('services/LuckyBoxService.php');
|
|
require_once('services/ActivateNftService.php');
|
|
|
|
require_once('phpcommon/bchelper.php');
|
|
|
|
use phpcommon\SqlHelper;
|
|
use models\BoxOrder;
|
|
use models\Nft;
|
|
use models\BuyRecord;
|
|
use services\MarketService;
|
|
use services\LuckyBoxService;
|
|
use services\ActivateNftService;
|
|
|
|
class MarketController extends BaseController {
|
|
|
|
public function getPreSaleInfo()
|
|
{
|
|
$account = strtolower(getReqVal('account', ''));
|
|
$presaleInfo = MarketService::getPreSaleInfo($account);
|
|
if (MarketService::isTestMode()) {
|
|
foreach(array_keys($presaleInfo) as $key) {
|
|
if (!is_null(getReqVal($key, null))) {
|
|
$presaleInfo[$key] = getReqVal($key, $presaleInfo[$key]);
|
|
}
|
|
}
|
|
}
|
|
myself()->_rspData(array(
|
|
'presale_info' => $presaleInfo
|
|
));
|
|
}
|
|
|
|
public function searchBox()
|
|
{
|
|
$account = strtolower(getReqVal('account', ''));
|
|
$page = getReqVal('page', 1);
|
|
$type = getReqVal('type', 0);
|
|
$sort = getReqVal('sort', '');
|
|
|
|
$perPage = 10000;
|
|
$rows = array();
|
|
$pageInfo = array(
|
|
'total' => 0,
|
|
'count' => 0,
|
|
'per_page' => $perPage,
|
|
'current_page' => $page,
|
|
'total_pages' => 0
|
|
);
|
|
$startPos= $pageInfo['per_page'] * ($pageInfo['current_page'] - 1);
|
|
|
|
$currBatchMeta = mt\MarketBatch::getCurrentBatch();
|
|
if ($currBatchMeta) {
|
|
$batchMetas = mt\MarketGoods::getBatchMetas($currBatchMeta['batch_id']);
|
|
if ($batchMetas) {
|
|
foreach ($batchMetas as $meta) {
|
|
$saleBox = $this->fillPresaleBox($currBatchMeta, $meta);
|
|
if ($saleBox) {
|
|
++$pageInfo['total'];
|
|
if ($pageInfo['total'] > $startPos &&
|
|
count($rows) < $pageInfo['per_page']) {
|
|
array_push($rows, $saleBox);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$pageInfo['count'] = count($rows);
|
|
$pageInfo['total_pages'] = ceil($pageInfo['total'] / $pageInfo['per_page']);
|
|
myself()->_rspData(array(
|
|
'rows' => $rows,
|
|
'page' => $pageInfo,
|
|
));
|
|
}
|
|
|
|
public function buyBox()
|
|
{
|
|
$token = getReqVal('token', '');
|
|
$type = getReqVal('type', '');
|
|
$rawBuyerAddress = getReqVal('buyer_address', '');
|
|
$buyerAddress = strtolower($rawBuyerAddress);
|
|
$price = getReqVal('price', '');
|
|
$paymentTokenAddress = getReqVal('payment_token_address', '');
|
|
$nonce = getReqVal('nonce', '');
|
|
$signature = getReqVal('signature', '');
|
|
$netId = getReqVal('net_id', '');
|
|
$gameId = 2006;
|
|
$funcId = MarketService::FUNCID_PRESALE;
|
|
if (!MarketService::isValidToken($buyerAddress, $token)) {
|
|
myself()->_rspErr(100, 'invalid token');
|
|
return;
|
|
}
|
|
MarketService::buyBoxVerifySignature(
|
|
$buyerAddress,
|
|
$type,
|
|
$paymentTokenAddress,
|
|
$price,
|
|
$nonce,
|
|
$signature);
|
|
|
|
$batchIdx = 0;
|
|
$idx = 0;
|
|
$itemId = 0;
|
|
if (!phpcommon\extractBoxId($type, $batchIdx, $idx, $itemId)) {
|
|
myself()->_rspErr(2, 'type parameter error');
|
|
return;
|
|
}
|
|
|
|
if (empty($type) ||
|
|
empty($buyerAddress) ||
|
|
empty($price) ||
|
|
empty($paymentTokenAddress) ||
|
|
empty($signature) ||
|
|
empty($nonce)) {
|
|
myself()->_rspErr(2, 'parameter error');
|
|
return;
|
|
}
|
|
|
|
$currBatchMeta = mt\MarketBatch::getCurrentBatch();
|
|
if (!$currBatchMeta) {
|
|
myself()->_rspErr(500, 'server internal error1');
|
|
return;
|
|
}
|
|
if ($batchIdx != $currBatchMeta['id']) {
|
|
myself()->_rspErr(500, 'server internal error2');
|
|
return;
|
|
}
|
|
$goodsMeta = mt\MarketGoods::getOnSaleGoods($currBatchMeta['batch_id'], $idx, $itemId);
|
|
if (!$goodsMeta) {
|
|
myself()->_rspErr(500, 'server internal error3');
|
|
return;
|
|
}
|
|
if ($currBatchMeta['white_list'] && !mt\WhiteList::inWhiteList($buyerAddress)) {
|
|
myself()->_rspErr(500, 'not white list user');
|
|
return;
|
|
}
|
|
$originalPrice = $goodsMeta['price'] * pow(10, MarketService::CURRENCY_DECIMALS);
|
|
$discountPrice = $goodsMeta['discount'] * 100 > 0 ?
|
|
$originalPrice * $goodsMeta['discount'] : $originalPrice;
|
|
|
|
$discountPrice .= MarketService::PRICE_PAD;
|
|
error_log('price:' . $price . ' discountPrice:' . $discountPrice);
|
|
if (!$discountPrice || strcmp($price, $discountPrice) != 0) {
|
|
myself()->_rspErr(500, 'price error');
|
|
return;
|
|
}
|
|
$itemMeta = mt\Item::get($itemId);
|
|
if (!$itemMeta) {
|
|
myself()->_rspErr(500, 'server internal error11');
|
|
return;
|
|
}
|
|
$currencyMeta = mt\Currency::get($goodsMeta['currency_id']);
|
|
if (!$currencyMeta || $currencyMeta['address'] != $paymentTokenAddress) {
|
|
myself()->_rspErr(500, 'currency error');
|
|
return;
|
|
}
|
|
|
|
if (!phpcommon\isValidBcGameId($gameId)) {
|
|
myself()->_rspErr(500, 'server internal error12');
|
|
return;
|
|
}
|
|
|
|
if (!phpcommon\isValidBcTime(myself()->_getNowTime())) {
|
|
myself()->_rspErr(500, 'server internal error13');
|
|
return;
|
|
}
|
|
|
|
if (!phpcommon\isValidBcFuncId($funcId)) {
|
|
myself()->_rspErr(500, 'server internal error14');
|
|
return;
|
|
}
|
|
|
|
if (!MarketService::isTestMode() &&
|
|
BoxOrder::isBuyed($buyerAddress, $currBatchMeta['id'])) {
|
|
myself()->_rspErr(1, 'account can only choose 1 hero to purchase');
|
|
return;
|
|
}
|
|
$orderId = BuyRecord::genOrderId($gameId,
|
|
$funcId,
|
|
myself()->_getNowTime(),
|
|
$buyerAddress);
|
|
|
|
$fieldsKv = array(
|
|
'game_id' => $gameId,
|
|
'func_id' => $funcId,
|
|
'batch_idx' => $currBatchMeta['id'],
|
|
'type' => $type,
|
|
'raw_buyer_address' => $rawBuyerAddress,
|
|
'buyer_address' => $buyerAddress,
|
|
'price' => $price,
|
|
'payment_token_address' => $paymentTokenAddress,
|
|
'nonce' => $nonce,
|
|
'signature' => $signature,
|
|
'net_id' => $netId,
|
|
'done' => 0,
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime()
|
|
);
|
|
$items = array();
|
|
MarketService::openBox($itemMeta, $items);
|
|
for ($i = 1; $i <= BoxOrder::MAX_NFT_NUM; ++$i) {
|
|
if ($i <= count($items)) {
|
|
$tokenId = phpcommon\setOrderIdSubIdx($orderId, $i);
|
|
$fieldsKv['bc_mint_tokenid' . $i] = $tokenId;
|
|
$fieldsKv['bc_mint_need' . $i] = $items[$i - 1]['need'];
|
|
$fieldsKv['bc_mint_itemid' . $i] = $items[$i - 1]['item_id'];
|
|
$fieldsKv['bc_mint_token_type' . $i] = $items[$i - 1]['token_type'];
|
|
}
|
|
}
|
|
/*if (MarketService::isTestMode()) {
|
|
$fieldsKv['bc_paid'] = 1;
|
|
}*/
|
|
$fieldsKv['order_id'] = $orderId;
|
|
SqlHelper::insert(
|
|
myself()->_getMarketMysql(),
|
|
't_box_order',
|
|
$fieldsKv
|
|
);
|
|
myself()->_rspData(array(
|
|
'order_id' => $orderId
|
|
));
|
|
}
|
|
|
|
public function queryOrder()
|
|
{
|
|
$token = getReqVal('token', '');
|
|
$account = strtolower(getReqVal('account', ''));
|
|
$orderId = getReqVal('order_id', '');
|
|
if (!MarketService::isValidToken($account, $token)) {
|
|
myself()->_rspErr(100, 'invalid token');
|
|
return;
|
|
}
|
|
|
|
$orderDb = BoxOrder::findByOrderId($orderId);
|
|
if ($orderDb) {
|
|
if (!$orderDb['done']) {
|
|
myself()->_rspData(array(
|
|
'state' => 2
|
|
));
|
|
} else {
|
|
if ($orderDb['bc_paid'] == 1) {
|
|
myself()->_rspData(array(
|
|
'state' => 1
|
|
));
|
|
} else {
|
|
myself()->_rspData(array(
|
|
'state' => 3
|
|
));
|
|
}
|
|
}
|
|
} else {
|
|
myself()->_rspData(array(
|
|
'state' => 0
|
|
));
|
|
}
|
|
}
|
|
|
|
public function getNftList()
|
|
{
|
|
$account = strtolower(getReqVal('account', ''));
|
|
$token = getReqVal('token', '');
|
|
$page = getReqVal('page', 0);
|
|
if (MarketService::isTestMode()) {
|
|
} else {
|
|
if (!MarketService::isValidToken($account, $token)) {
|
|
myself()->_rspErr(100, 'invalid token');
|
|
return;
|
|
}
|
|
}
|
|
$nftDbList = array();
|
|
$pagination = array();
|
|
SqlHelper::rawQueryPage(
|
|
myself()->_getMarketMysql(),
|
|
'SELECT * FROM t_nft WHERE owner_address=:owner_address',
|
|
array(
|
|
':owner_address' => $account
|
|
),
|
|
array(
|
|
'page' => $page,
|
|
'perPage' => 8,
|
|
'filter' => array(
|
|
'data' => $_REQUEST,
|
|
'fields' => array(
|
|
array(
|
|
'name' => 'type',
|
|
'field_name' => 'token_type',
|
|
'cond' => '=',
|
|
'ignore_empty' => true
|
|
),
|
|
array(
|
|
'name' => 'state',
|
|
'field_name' => 'token_state',
|
|
'cond' => '=',
|
|
'ignore_empty' => true
|
|
),
|
|
)
|
|
),
|
|
'handle' => function ($row) use(&$nftDbList) {
|
|
#error_log(json_encode($row));
|
|
array_push($nftDbList, $row);
|
|
}
|
|
),
|
|
$pagination
|
|
);
|
|
$nftList = array();
|
|
foreach ($nftDbList as $nftDb) {
|
|
$nft = Nft::toDto($nftDb);
|
|
if ($nft) {
|
|
array_push($nftList, $nft);
|
|
}
|
|
}
|
|
myself()->_rspData(array(
|
|
'nfts' => $nftList,
|
|
'page' => $pagination
|
|
));
|
|
}
|
|
|
|
public function getNftDetail()
|
|
{
|
|
$account = strtolower(getReqVal('account', ''));
|
|
$token = getReqVal('token', '');
|
|
$tokenId = getReqVal('token_id', '');
|
|
if (!MarketService::isValidToken($account, $token)) {
|
|
myself()->_rspErr(100, 'invalid token');
|
|
return;
|
|
}
|
|
|
|
$nftDb = Nft::getNft($tokenId);
|
|
if (!$nftDb) {
|
|
myself()->_rspErr(1, 'nft not exists');
|
|
return;
|
|
}
|
|
$nft = Nft::toDto($nftDb);
|
|
myself()->_rspData(array(
|
|
'info' => $nft
|
|
));
|
|
}
|
|
|
|
public function getNonce()
|
|
{
|
|
$account = strtolower(getReqVal('account', ''));
|
|
$token = getReqVal('token', '');
|
|
$nonce = uniqid() . '_' . myself()->_getNowTime();
|
|
myself()->_rspData(array(
|
|
'state' => MarketService::isValidToken($account, $token) ? 1 : 0,
|
|
'nonce' => $nonce,
|
|
));
|
|
}
|
|
|
|
public function tokenAuth()
|
|
{
|
|
$account = strtolower(getReqVal('account', ''));
|
|
$token = getReqVal('token', '');
|
|
if (!MarketService::isValidToken($account, $token)) {
|
|
myself()->_rspErr(1, 'invalid token');
|
|
return;
|
|
}
|
|
myself()->_rspData(array(
|
|
'account' => $account
|
|
));
|
|
}
|
|
|
|
public function auth()
|
|
{
|
|
$account = strtolower(getReqVal('account', ''));
|
|
$tips = getReqVal('tips', '') ;
|
|
$nonce = getReqVal('nonce', '');
|
|
$signature = getReqVal('signature', '');
|
|
MarketService::auth($account, $tips, $nonce, $signature);
|
|
}
|
|
|
|
private function fillPresaleBox($batchMeta, $goodsMeta)
|
|
{
|
|
$currencyMeta = mt\Currency::get($goodsMeta['currency_id']);
|
|
if (!$currencyMeta) {
|
|
return null;
|
|
}
|
|
$boxId = phpcommon\genBoxId($batchMeta['id'],
|
|
$goodsMeta['id'],
|
|
$goodsMeta['item_id']);
|
|
|
|
$itemMeta = mt\Item::get($goodsMeta['item_id']);
|
|
$originalPrice = $goodsMeta['price'] * pow(10, MarketService::CURRENCY_DECIMALS);
|
|
$discountPrice = $goodsMeta['discount'] * 100 > 0 ?
|
|
$originalPrice * $goodsMeta['discount'] : $originalPrice;
|
|
$name = '';
|
|
$job = 0;
|
|
{
|
|
$heroMeta = mt\Hero::get($goodsMeta['item_id']);
|
|
if ($heroMeta) {
|
|
$name = emptyReplace($heroMeta['name'], 'Hill');
|
|
$job = emptyReplace($heroMeta['herotype'], '1');
|
|
}
|
|
}
|
|
$saleBox = array(
|
|
'box_id' => $boxId,
|
|
'item_id' => $goodsMeta['item_id'],
|
|
'name' => $name,
|
|
'job' => $job,
|
|
'avatar_url' => 'https://www.cebg.games/res/avatars/' . 501 . '.png',
|
|
'currency_list' => array(
|
|
array(
|
|
'name' => $currencyMeta['name'],
|
|
'original_price' => $originalPrice,
|
|
'discount_price' => $discountPrice,
|
|
'discount_rate' => $goodsMeta['discount'],
|
|
'decimals' => MarketService::CURRENCY_DECIMALS,
|
|
'contract_address' => $currencyMeta['address'],
|
|
)
|
|
));
|
|
return $saleBox;
|
|
}
|
|
|
|
public function openLuckyBox()
|
|
{
|
|
$token = getReqVal('token', '');
|
|
$account = getReqVal('account', '');
|
|
$tokenId = getReqVal('token_id', '');
|
|
$netId = getReqVal('net_id', '');
|
|
$account = strtolower(getReqVal('account', ''));
|
|
$token = getReqVal('token', '');
|
|
if (!MarketService::isValidToken($account, $token)) {
|
|
myself()->_rspErr(1, 'invalid token');
|
|
return;
|
|
}
|
|
LuckyBoxService::open($account, $tokenId, $netId);
|
|
}
|
|
|
|
public function queryLuckyBox()
|
|
{
|
|
$this->queryLuckyBoxResult();
|
|
}
|
|
|
|
public function queryLuckyBoxResult()
|
|
{
|
|
$token = getReqVal('token', '');
|
|
$account = getReqVal('account', '');
|
|
$tokenId = getReqVal('token_id', '');
|
|
$netId = getReqVal('net_id', '');
|
|
$account = strtolower(getReqVal('account', ''));
|
|
if (!MarketService::isValidToken($account, $token)) {
|
|
myself()->_rspErr(1, 'invalid token');
|
|
return;
|
|
}
|
|
LuckyBoxService::queryResult($account, $tokenId, $netId);
|
|
}
|
|
|
|
public function activateNft()
|
|
{
|
|
$token = getReqVal('token', '');
|
|
$account = getReqVal('account', '');
|
|
$tokenId = getReqVal('token_id', '');
|
|
$netId = getReqVal('net_id', '');
|
|
$account = strtolower(getReqVal('account', ''));
|
|
if (!MarketService::isValidToken($account, $token)) {
|
|
myself()->_rspErr(1, 'invalid token');
|
|
return;
|
|
}
|
|
ActivateNftService::activate($account, $tokenId, $netId);
|
|
}
|
|
|
|
public function queryActivateResult()
|
|
{
|
|
$token = getReqVal('token', '');
|
|
$account = getReqVal('account', '');
|
|
$tokenId = getReqVal('token_id', '');
|
|
$netId = getReqVal('net_id', '');
|
|
$account = strtolower(getReqVal('account', ''));
|
|
if (!MarketService::isValidToken($account, $token)) {
|
|
myself()->_rspErr(1, 'invalid token');
|
|
return;
|
|
}
|
|
ActivateNftService::queryResult($account, $tokenId, $netId);
|
|
}
|
|
|
|
}
|