407 lines
14 KiB
PHP
407 lines
14 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('phpcommon/bchelper.php');
|
|
|
|
use phpcommon\SqlHelper;
|
|
use models\BoxOrder;
|
|
use models\Nft;
|
|
use models\BuyRecord;
|
|
|
|
const CURRENCY_DECIMALS = 8;
|
|
const PRICE_PAD = '0000000000';
|
|
|
|
const PRESALE_NOT_STARTED = 0;
|
|
const PRESALE_PREPARE = 1;
|
|
const PRESALE_STARTED = 2;
|
|
const PRESALE_SOLD_OUT = 3;
|
|
|
|
class MarketController extends BaseController {
|
|
|
|
private function isTestMode()
|
|
{
|
|
return isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443 && SERVER_ENV == _TEST;
|
|
}
|
|
|
|
public function getPreSaleInfo()
|
|
{
|
|
$account = getReqVal('account', '');
|
|
$currBatchMeta = mt\MarketBatch::getCurrentBatch();
|
|
if (!$currBatchMeta) {
|
|
$presaleInfo = array(
|
|
'batch_id' => 0,
|
|
'countdown' => 0,
|
|
'sold_num' => 0,
|
|
'total_num' => 0,
|
|
'state' => PRESALE_NOT_STARTED,
|
|
'title' => $this->escapeString(mt\Parameter::getVal('pre_sale_not_started_title', '')),
|
|
'hint' => $this->escapeString(mt\Parameter::getVal('pre_sale_not_started_hint', '')),
|
|
'buyable_list' => array(),
|
|
//'buyed' => 0
|
|
);
|
|
myself()->_rspData(array(
|
|
'presale_info' => $presaleInfo
|
|
));
|
|
return;
|
|
}
|
|
|
|
$countdown = max(0, $currBatchMeta['start_time'] - myself()->_getNowTime());
|
|
$soldNum = min(BoxOrder::getSoldNum($currBatchMeta['id']), $currBatchMeta['number_of_props']);
|
|
$totalNum = $currBatchMeta['number_of_props'];
|
|
$buyed = $this->isTestMode() ? 0 : BoxOrder::isBuyed($account, $currBatchMeta['id']);
|
|
$title = '';
|
|
$state = PRESALE_PREPARE;
|
|
if ($countdown > 0) {
|
|
$state = PRESALE_PREPARE;
|
|
} else {
|
|
if ($soldNum >= $totalNum) {
|
|
$title = $this->escapeString($currBatchMeta['end_title']);
|
|
$state = PRESALE_SOLD_OUT;
|
|
} else {
|
|
$title = $this->escapeString($currBatchMeta['begin_title']);
|
|
$state = PRESALE_STARTED;
|
|
}
|
|
}
|
|
$presaleInfo = array(
|
|
'batch_id' => $currBatchMeta['batch_id'],
|
|
'countdown' => $countdown,
|
|
'sold_num' => $soldNum,
|
|
'total_num' => $totalNum,
|
|
'state' => $state,
|
|
'title' => $title,
|
|
'hint' => $this->escapeString($currBatchMeta['hint']),
|
|
'buyable_list' => array(),
|
|
//'buyed' => $buyed
|
|
);
|
|
|
|
if ($this->isTestMode()) {
|
|
foreach(array_keys($presaleInfo) as $key) {
|
|
if (!is_null(getReqVal($key, null))) {
|
|
$presaleInfo[$key] = getReqVal($key, $presaleInfo[$key]);
|
|
}
|
|
}
|
|
}
|
|
|
|
{
|
|
$batchMetas = mt\MarketGoods::getBatchMetas($currBatchMeta['batch_id']);
|
|
if ($batchMetas) {
|
|
foreach ($batchMetas as $meta) {
|
|
$boxId = phpcommon\genBoxId($currBatchMeta['id'], $meta['id'], $meta['item_id']);
|
|
if (!$buyed) {
|
|
if ($currBatchMeta['white_list'] && !mt\WhiteList::inWhiteList($account)) {
|
|
continue;
|
|
}
|
|
array_push($presaleInfo['buyable_list'],
|
|
array(
|
|
'box_id' => $boxId
|
|
));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
myself()->_rspData(array(
|
|
'presale_info' => $presaleInfo
|
|
));
|
|
}
|
|
|
|
public function searchBox()
|
|
{
|
|
$account = 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
|
|
);
|
|
|
|
$currBatchMeta = mt\MarketBatch::getCurrentBatch();
|
|
if (!$currBatchMeta) {
|
|
myself()->_rspData(array(
|
|
'rows' => $rows,
|
|
'page' => $pageInfo,
|
|
));
|
|
return;
|
|
}
|
|
|
|
$batchMetas = mt\MarketGoods::getBatchMetas($currBatchMeta['batch_id']);
|
|
if ($batchMetas) {
|
|
foreach ($batchMetas as $meta) {
|
|
$heroMeta = mt\Hero::get($meta['item_id']);
|
|
$itemMeta = mt\Item::get($meta['item_id']);
|
|
$boxId = phpcommon\genBoxId($currBatchMeta['id'], $meta['id'], $meta['item_id']);
|
|
$currencyMeta = mt\Currency::get($meta['currency_id']);
|
|
if (!$currencyMeta) {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
$originalPrice = $meta['price'] * pow(10, CURRENCY_DECIMALS);
|
|
$discountPrice = $meta['discount'] * 100 > 0 ?
|
|
$originalPrice * $meta['discount'] : $originalPrice;
|
|
$saleBox = array(
|
|
'box_id' => $boxId,
|
|
'item_id' => $meta['item_id'],
|
|
'name' => emptyReplace($heroMeta['name'], 'Hill'),
|
|
'job' => emptyReplace($heroMeta['herotype'], '1'),
|
|
'avatar_url' => 'https://www.cebg.games/res/avatars/' . $itemMeta['nft_image_id'] . '.png',
|
|
'currency_list' => array(
|
|
array(
|
|
'name' => $currencyMeta['name'],
|
|
'original_price' => $originalPrice,
|
|
'discount_price' => $discountPrice,
|
|
'discount_rate' => $meta['discount'],
|
|
'decimals' => CURRENCY_DECIMALS,
|
|
'contract_address' => $currencyMeta['address'],
|
|
)
|
|
)
|
|
);
|
|
++$pageInfo['total'];
|
|
if ($pageInfo['total'] > $pageInfo['per_page'] * ($pageInfo['current_page'] - 1) &&
|
|
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()
|
|
{
|
|
$type = getReqVal('type', '');
|
|
$buyerAddress = getReqVal('buyer_address', '');
|
|
$price = getReqVal('price', '');
|
|
$paymentTokenAddress = getReqVal('payment_token_address', '');
|
|
$nonce = getReqVal('nonce', '');
|
|
$signature = getReqVal('signature', '');
|
|
$gameId = 2006;
|
|
$funcId = 1;
|
|
|
|
$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, CURRENCY_DECIMALS);
|
|
$discountPrice = $goodsMeta['discount'] * 100 > 0 ?
|
|
$originalPrice * $goodsMeta['discount'] : $originalPrice;
|
|
|
|
$discountPrice .= 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 ||
|
|
empty($itemMeta['nft_image_id']) ||
|
|
$itemMeta['nft_image_id'] <= 0 ||
|
|
$itemMeta['nft_image_id'] > 999
|
|
) {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
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 error');
|
|
return;
|
|
}
|
|
|
|
if (!phpcommon\isValidBcTime(myself()->_getNowTime())) {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
|
|
if (!phpcommon\isValidBcFuncId($funcId)) {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
|
|
/*if (!$this->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(),
|
|
$itemMeta['nft_image_id'],
|
|
$buyerAddress);
|
|
$tokenId = $orderId;
|
|
SqlHelper::insert(
|
|
myself()->_getMarketMysql(),
|
|
't_box_order',
|
|
array(
|
|
'batch_idx' => $currBatchMeta['id'],
|
|
'order_id' => $orderId,
|
|
'type' => $type,
|
|
'item_id' => $itemId,
|
|
'state' => 0,
|
|
'buyer_address' => $buyerAddress,
|
|
'token_id' => $tokenId,
|
|
'price' => $price,
|
|
'payment_token_address' => $paymentTokenAddress,
|
|
'nonce' => $nonce,
|
|
'signature' => $signature,
|
|
'done' => 0,
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime()
|
|
)
|
|
);
|
|
myself()->_rspData(array(
|
|
'order_id' => $orderId
|
|
));
|
|
}
|
|
|
|
public function queryOrder()
|
|
{
|
|
$orderId = getReqVal('order_id', '');
|
|
$orderDb = BoxOrder::findByOrderId($orderId);
|
|
if ($orderDb) {
|
|
if (!$orderDb['done']) {
|
|
myself()->_rspData(array(
|
|
'state' => 2
|
|
));
|
|
} else {
|
|
if ($orderDb['state'] == 1) {
|
|
myself()->_rspData(array(
|
|
'state' => 1
|
|
));
|
|
} else {
|
|
myself()->_rspData(array(
|
|
'state' => 3
|
|
));
|
|
}
|
|
}
|
|
} else {
|
|
myself()->_rspData(array(
|
|
'state' => 0
|
|
));
|
|
}
|
|
}
|
|
|
|
public function getNftList()
|
|
{
|
|
$account = getReqVal('account', '');
|
|
$nftDbList = Nft::getNftList($account);
|
|
$nftList = array();
|
|
foreach ($nftDbList as $nftDb) {
|
|
$nft = $this->toNftDto($nftDb);
|
|
array_push($nftList, $nft);
|
|
}
|
|
myself()->_rspData(array(
|
|
'nfts' => $nftList
|
|
));
|
|
}
|
|
|
|
public function getNftDetail()
|
|
{
|
|
$account = getReqVal('account', '');
|
|
$tokenId = getReqVal('token_id', '');
|
|
$nftDb = Nft::getNft($tokenId);
|
|
if (!$nftDb) {
|
|
myself()->_rspErr(1, 'nft not exists');
|
|
return;
|
|
}
|
|
$nft = $this->toNftDto($nftDb);
|
|
myself()->_rspData(array(
|
|
'info' => $nft
|
|
));
|
|
}
|
|
|
|
private function toNftDto($nftDb)
|
|
{
|
|
$nft = array(
|
|
'token_id' => $nftDb['token_id'],
|
|
'owner_address' => $nftDb['owner_address'],
|
|
'owner_name' => $nftDb['owner_name'],
|
|
'item_id' => $nftDb['item_id'],
|
|
'currency_list' => array(),
|
|
'transaction_recrod' => array(),
|
|
'info' => array(
|
|
),
|
|
'mint_time' => $nftDb['createtime']
|
|
);
|
|
$heroMeta = mt\Hero::get($nftDb['item_id']);
|
|
if ($heroMeta) {
|
|
$nft['info']['name'] = $heroMeta['name'];
|
|
$nft['info']['job'] = $heroMeta['herotype'];
|
|
$nft['info']['level'] = 1;
|
|
$nft['info']['quality'] = 1;
|
|
$nft['info']['hp'] = $heroMeta['hp'];
|
|
$nft['info']['speed'] = $heroMeta['move_speed'];
|
|
$nft['info']['atk'] = $heroMeta['damage'];
|
|
$nft['info']['def'] = $heroMeta['defence'];
|
|
$nft['info']['advanced_count'] = 0;
|
|
$nft['info']['lucky'] = 0;
|
|
$nft['info']['success_rate'] = 0;
|
|
}
|
|
return $nft;
|
|
}
|
|
|
|
private function escapeString($str)
|
|
{
|
|
return str_replace("\n", '\n', $str);
|
|
}
|
|
|
|
}
|