game2006api/webapp/controller/MarketController.class.php
aozhiwei c758a4a4a4 1
2022-01-27 19:20:54 +08:00

342 lines
11 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');
require_once('models/BuyRecord.php');
require_once('phpcommon/bchelper.php');
use phpcommon\SqlHelper;
use models\BoxOrder;
use models\Nft;
use models\BuyRecord;
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']);
$boxId = phpcommon\genBoxId($currBatchMeta['batch_id'], $meta['id'], $meta['item_id']);
$currencyMeta = mt\Currency::get($meta['currency_id']);
if (!$currencyMeta) {
myself()->_rspErr(500, 'server internal error');
return;
}
$saleBox = array(
'box_id' => $boxId,
'item_id' => $meta['item_id'],
'name' => emptyReplace($heroMeta['name'], 'Hill'),
'job' => emptyReplace($heroMeta['herotype'], '1'),
'currency_list' => array(
array(
'name' => $currencyMeta['name'],
'original_price' => $meta['price'],
'discount_price' => $meta['discount'] > 0 ?
$meta['price'] * ($meta['discount'] / 100) : $meta['price'],
'discount_rate' => $meta['discount'],
'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;
$batchId = 0;
$idx = 0;
$itemId = 0;
if (!phpcommon\extractBoxId($type, $batchId, $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 error');
return;
}
if ($batchId != $currBatchMeta['batch_id']) {
myself()->_rspErr(500, 'server internal error');
return;
}
if (!mt\MarketGoods::isOnSaleItem($batchId, $idx, $itemId)) {
myself()->_rspErr(500, 'server internal 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['batch_id'])) {
myself()->_rspErr(1, 'account can only choose 1 hero to purchase');
return;
}*/
$orderId = BuyRecord::genOrderId($gameId, $funcId, myself()->_getNowTime(), $buyerAddress);
$tokenId = $orderId;
if ($this->isTestMode()) {
$itemId = $type;
SqlHelper::insert
(myself()->_getMarketMysql(),
't_box_order',
array(
'batch_id' => $currBatchMeta['batch_id'],
'game_id' => $gameId,
'order_id' => $orderId,
'type' => $type,
'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 {
$itemId = $type;
SqlHelper::insert
(myself()->_getMarketMysql(),
't_box_order',
array(
'batch_id' => $currBatchMeta['batch_id'],
'order_id' => $orderId,
'type' => $type,
'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()
{
$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;
}
}