294 lines
9.9 KiB
PHP
294 lines
9.9 KiB
PHP
<?php
|
|
|
|
require_once('mt/BcShopGoods.php');
|
|
require_once('mt/BcShopBatch.php');
|
|
require_once('mt/Item.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('phpcommon/bchelper.php');
|
|
|
|
use phpcommon\SqlHelper;
|
|
use models\BoxOrder;
|
|
use models\Nft;
|
|
use models\BuyRecord;
|
|
use services\MarketService;
|
|
|
|
class BcShopController extends BaseController {
|
|
|
|
public function search()
|
|
{
|
|
$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
|
|
);
|
|
$startPos= $pageInfo['per_page'] * ($pageInfo['current_page'] - 1);
|
|
|
|
$currBatchMeta = mt\BcShopBatch::getCurrentBatch();
|
|
if ($currBatchMeta) {
|
|
$batchMetas = mt\BcShopGoods::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 buy()
|
|
{
|
|
$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_SHOP;
|
|
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 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 (!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 = 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
|
|
));
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|