279 lines
9.2 KiB
PHP
279 lines
9.2 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('models/BoxOrder.php');
|
|
require_once('models/Nft.php');
|
|
|
|
use phpcommon\SqlHelper;
|
|
use models\BoxOrder;
|
|
use models\Nft;
|
|
|
|
class MarketController extends BaseController {
|
|
|
|
private function isTestMode()
|
|
{
|
|
return isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443 && SERVER_ENV == _TEST;
|
|
}
|
|
|
|
public function getPreSaleInfo()
|
|
{
|
|
$currBatchMeta = mt\MarketBatch::getCurrentBatch();
|
|
if (!$currBatchMeta) {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
|
|
$presaleInfo = array(
|
|
'batch_id' => $currBatchMeta['batch_id'],
|
|
'countdown' => max(0, $currBatchMeta['_start_time_utc'] - myself()->_getNowTime()),
|
|
'sold_num' => min(BoxOrder::getSoldNum($currBatchMeta['batch_id']), $currBatchMeta['number_of_props']),
|
|
'total_num' => $currBatchMeta['number_of_props'],
|
|
'state' => 2,
|
|
'title' => '',
|
|
'hint' => str_replace("\n", '\n', $currBatchMeta['hint']),
|
|
'buyed' => $this->isTestMode() ? 0 : BoxOrder::isBuyed($account, $currBatchMeta['batch_id'])
|
|
);
|
|
|
|
if ($this->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 = getReqVal('account', '');
|
|
$page = getReqVal('page', 1);
|
|
$type = getReqVal('type', 0);
|
|
$sort = getReqVal('sort', '');
|
|
|
|
$currBatchMeta = mt\MarketBatch::getCurrentBatch();
|
|
if (!$currBatchMeta) {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
|
|
$perPage = 10000;
|
|
$rows = array();
|
|
$pageInfo = array(
|
|
'total' => 0,
|
|
'count' => 0,
|
|
'per_page' => $perPage,
|
|
'current_page' => $page,
|
|
'total_pages' => 0
|
|
);
|
|
|
|
$batchMetas = mt\MarketGoods::getBatchMetas($currBatchMeta['batch_id']);
|
|
if ($batchMetas) {
|
|
foreach ($batchMetas as $meta) {
|
|
$heroMeta = mt\Hero::get($meta['item_id']);
|
|
$saleBox = array(
|
|
'box_id' => $meta['item_id'],
|
|
'item_id' => $meta['item_id'],
|
|
'name' => emptyReplace($heroMeta['name'], 'Hill'),
|
|
'job' => emptyReplace($heroMeta['herotype'], '1'),
|
|
'currency_list' => array(
|
|
array(
|
|
'name' => 'BNB',
|
|
'original_price' => 100,
|
|
'discount_price' => 80,
|
|
'discount_rate' => 80,
|
|
'contract_address' => '0xCfEB869F69431e42cdB54A4F4f105C19C080A601',
|
|
)
|
|
)
|
|
);
|
|
++$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;
|
|
|
|
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 error');
|
|
return;
|
|
}
|
|
|
|
/*if (!$this->isTestMode() && BoxOrder::isBuyed($buyerAddress, $currBatchMeta['batch_id'])) {
|
|
myself()->_rspErr(1, 'account can only choose 1 hero to purchase');
|
|
return;
|
|
}*/
|
|
|
|
if ($this->isTestMode()) {
|
|
$orderId = myself()->_getNowTime();
|
|
$tokenId = $orderId;
|
|
$itemId = $type;
|
|
SqlHelper::insert
|
|
(myself()->_getMarketMysql(),
|
|
't_box_order',
|
|
array(
|
|
'batch_id' => $currBatchMeta['batch_id'],
|
|
'game_id' => $gameId,
|
|
'order_id' => $orderId,
|
|
'item_id' => $itemId,
|
|
'state' => 1,
|
|
'buyer_address' => $buyerAddress,
|
|
'token_id' => $tokenId,
|
|
'price' => $price,
|
|
'payment_token_address' => $paymentTokenAddress,
|
|
'nonce' => $nonce,
|
|
'signature' => $signature,
|
|
'done' => 1,
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime()
|
|
)
|
|
);
|
|
SqlHelper::insert
|
|
(myself()->_getMarketMysql(),
|
|
't_nft',
|
|
array(
|
|
'token_id' => $tokenId,
|
|
'game_id' => $gameId,
|
|
'item_id' => $itemId,
|
|
'owner_id' => $buyerAddress,
|
|
'owner_address' => $buyerAddress,
|
|
'owner_name' => '',
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime()
|
|
)
|
|
);
|
|
} else {
|
|
$orderId = myself()->_getNowTime();
|
|
$tokenId = $orderId;
|
|
$itemId = $type;
|
|
SqlHelper::insert
|
|
(myself()->_getMarketMysql(),
|
|
't_box_order',
|
|
array(
|
|
'batch_id' => $currBatchMeta['batch_id'],
|
|
'order_id' => $orderId,
|
|
'item_id' => $itemId,
|
|
'state' => 0,
|
|
'buyer_address' => $buyerAddress,
|
|
'token_id' => '',
|
|
'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()
|
|
{
|
|
myself()->_rspData(array(
|
|
'state' => 1
|
|
));
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|