game2006api/webapp/controller/MarketController.class.php
songliang f4d1bbe2ed ...
2023-07-07 15:34:47 +08:00

1933 lines
64 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/Hero.php');
require_once('models/Gun.php');
require_once('models/Chip.php');
require_once('models/Fragment.php');
require_once('models/BuyRecord.php');
require_once('services/MarketService.php');
require_once('services/LuckyBoxService.php');
require_once('services/ActivateNftService.php');
require_once('services/BlockChainService.php');
require_once('phpcommon/bchelper.php');
// use phpcommon as phpcommon;
use models\BcOrder;
use phpcommon\SqlHelper;
use models\BoxOrder;
use models\Nft;
use models\Hero;
use models\Gun;
use models\Chip;
use models\Fragment;
use models\BuyRecord;
use services\MarketService;
use services\LuckyBoxService;
use services\ActivateNftService;
use models\Transaction;
class MarketController extends BaseAuthedController
{
public function _handlePre()
{
if (
getReqVal('a', '') != 'eventSellOrder' &&
getReqVal('a', '') != 'eventBuyOrder' &&
getReqVal('a', '') != 'eventCancelOrder' &&
getReqVal('a', '') != 'eventPriceUpdateOrder'
) {
parent::_handlePre();
}
}
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;
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);
}
public function getPhase3Box()
{
$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;
}
$rows = array();
myself()->_rspData(array(
'rows' => $rows
));
}
public function openPhase3Box()
{
$token = getReqVal('token', '');
$account = getReqVal('account', '');
$tokenId = getReqVal('token_id', '');
$netId = getReqVal('net_id', '');
$boxId = getReqVal('box_id', '');
$account = strtolower(getReqVal('account', ''));
if (!MarketService::isValidToken($account, $token)) {
myself()->_rspErr(1, 'invalid token');
return;
}
myself()->_rspOk();
}
public function queryPhase3Box()
{
$token = getReqVal('token', '');
$account = getReqVal('account', '');
$tokenId = getReqVal('token_id', '');
$netId = getReqVal('net_id', '');
$boxId = getReqVal('box_id', '');
$account = strtolower(getReqVal('account', ''));
if (!MarketService::isValidToken($account, $token)) {
myself()->_rspErr(1, 'invalid token');
return;
}
myself()->_rspOk();
}
private function getNftListByAccountAndType($account, $type, $order_method, $order_asc, $job, $search, $lv, $quality, $durability)
{
$sortByLevel = function ($a, $b) use ($order_asc) {
return ($order_asc == 1 ? 1 : -1) * ($b['detail']['hero_lv'] - $a['detail']['hero_lv']);
};
$sortByGunLv = function ($a, $b) use ($order_asc) {
return ($order_asc == 1 ? 1 : -1) * ($b['detail']['gun_lv'] - $a['detail']['gun_lv']);
};
$sortByTili = function ($a, $b) use ($order_asc) {
return ($order_asc == 1 ? 1 : -1) * ($b['detail']['hero_tili'] - $a['detail']['hero_tili']);
};
$sortByStar = function ($a, $b) use ($order_asc) {
return ($order_asc == 1 ? 1 : -1) * ($b['detail']['quality'] - $a['detail']['quality']);
};
$sortByDurability = function ($a, $b) use ($order_asc) {
return ($order_asc == 1 ? 1 : -1) * ($b['detail']['durability_max'] - $a['detail']['durability_max']);
};
$sortByPower = function ($a, $b) use ($order_asc) {
return ($order_asc == 1 ? 1 : -1) * ($b['detail']['strength'] - $a['detail']['strength']);
};
$sortByGrade = function ($a, $b) use ($order_asc) {
return ($order_asc == 1 ? 1 : -1) * ($b['detail']['chip_grade'] - $a['detail']['chip_grade']);
};
$sortByTokenId = function ($a, $b) use ($order_asc) {
return ($order_asc == 1 ? 1 : -1) * ($b['token_id'] - $a['token_id']);
};
$nfts = array();
switch ($type) {
case Nft::NONE_TYPE: {
$rows = Nft::getNftListByType($account, $type);
$rows = array_merge($rows, $this->listMySelledNfts($account, $type));
foreach ($rows as &$row) {
$nftDb = Nft::findNftByOwner($account, $row['token_id']);
if (empty($nftDb)) {
$nftDb = Nft::getNft($row['token_id']);
}
// $row['info'] = Nft::toDto($nftDb);
// $row['detail'] = Hero::toDtoInfo(Hero::findByTokenId2($row['token_id']));
// if (in_array($row['info']['info']['job'], $job) == false) continue;
// if ($row['detail']['hero_lv'] < $lv) continue;
// if ($row['detail']['quality'] < $quality) continue;
// if ($row['detail']['hero_tili'] < $durability) continue;
if (count($search) > 0) {
$searchLower = array_map('strtolower', $search);
if (!(in_array(strtolower($row['detail']['hero_name']), $searchLower) || in_array(strtolower($row['detail']['token_id']), $searchLower))) continue;
}
// $row['detail'] = $this->appendChipsInfo($row['detail']);
array_push($nfts, $row);
}
switch ($order_method) {
case 1:
usort($nfts, $sortByLevel);
break;
case 2:
usort($nfts, $sortByTili);
break;
case 3:
usort($nfts, $sortByStar);
break;
}
}
break;
case Nft::HERO_TYPE: {
$rows = Nft::getNftListByType($account, $type);
$rows = array_merge($rows, $this->listMySelledNfts($account, $type));
foreach ($rows as &$row) {
$nftDb = Nft::findNftByOwner($account, $row['token_id']);
if (empty($nftDb)) {
$nftDb = Nft::getNft($row['token_id']);
}
$row['info'] = Nft::toDto($nftDb);
$row['detail'] = Hero::toDtoInfo(Hero::findByTokenId2($row['token_id']));
if (!empty($job)) {
if (in_array($row['info']['info']['job'], $job) == false) continue;
}
if ($row['detail']['hero_lv'] < $lv) continue;
if ($row['detail']['quality'] < $quality) continue;
if ($row['detail']['hero_tili'] < $durability) continue;
if (count($search) > 0) {
$searchLower = array_map('strtolower', $search);
if (!(in_array(strtolower($row['detail']['hero_name']), $searchLower) || in_array(strtolower($row['detail']['token_id']), $searchLower))) continue;
}
$row['detail'] = $this->appendChipsInfo($row['detail']);
array_push($nfts, $row);
}
switch ($order_method) {
case 1:
usort($nfts, $sortByLevel);
break;
case 2:
usort($nfts, $sortByTili);
break;
case 3:
usort($nfts, $sortByStar);
break;
}
}
break;
case Nft::EQUIP_TYPE: {
$rows = Nft::getNftListByType($account, $type);
$rows = array_merge($rows, $this->listMySelledNfts($account, $type));
foreach ($rows as &$row) {
$nftDb = Nft::findNftByOwner($account, $row['token_id']);
if (empty($nftDb)) {
$nftDb = Nft::getNft($row['token_id']);
}
$row['info'] = Nft::toDto($nftDb);
$row['detail'] = Gun::toDtoInfo(Gun::findByTokenId2($row['token_id']));
if ($row['detail']['gun_lv'] < $lv) continue;
if ($row['detail']['quality'] < $quality) continue;
if ($row['detail']['durability'] < $durability) continue;
if (count($search) > 0) {
$searchLower = array_map('strtolower', $search);
if (!(in_array(strtolower($row['detail']['gun_name']), $searchLower) || in_array(strtolower($row['detail']['token_id']), $searchLower))) continue;
}
$row['detail'] = $this->appendChipsInfo($row['detail']);
array_push($nfts, $row);
}
switch ($order_method) {
case 1:
usort($nfts, $sortByGunLv);
break;
case 2:
usort($nfts, $sortByDurability);
break;
case 3:
usort($nfts, $sortByStar);
break;
}
}
break;
case Nft::CHIP_TYPE: {
$rows = Nft::getNft1155List($account, $type);
$rows = array_merge($rows, $this->listMySelledNfts($account, $type));
foreach ($rows as &$row) {
$row['detail'] = Chip::toDto(Chip::getChipByTokenId($row['token_id']));
if (!in_array($row['detail']['chip_type'], $job))
continue;
if ($row['detail']['chip_grade'] < $lv) continue;
if (count($search) > 0) {
$searchLower = array_map('strtolower', $search);
if (!(in_array(strtolower($row['detail']['chip_name']), $searchLower) || in_array(strtolower($row['detail']['token_id']), $searchLower))) continue;
}
array_push($nfts, $row);
}
switch ($order_method) {
case 1:
usort($nfts, $sortByGrade);
break;
case 2:
usort($nfts, $sortByPower);
break;
}
}
break;
case Nft::FRAGMENT_TYPE: {
$rows = Nft::getNft1155List($account, $type);
$rows = array_merge($rows, $this->listMySelledNfts($account, $type));
foreach ($rows as &$row) {
$nftDb = Nft::findNftByOwner($account, $row['token_id']);
if (empty($nftDb)) {
$nftDb = Nft::getNft($row['token_id']);
}
$row['detail'] = $this->getNftGameData($nftDb);
if (!in_array($row['detail']['type'], $job))
continue;
if (count($search) > 0) {
$searchLower = array_map('strtolower', $search);
if (!(in_array(strtolower($row['detail']['name']), $searchLower) || in_array(strtolower($row['detail']['token_id']), $searchLower))) continue;
}
array_push($nfts, $row);
}
usort($nfts, $sortByTokenId);
}
break;
default: {
}
}
return $nfts;
}
public function listSellNfts()
{
$account = strtolower(getReqVal('account', ''));
$token = getReqVal('token', '');
$start = getReqVal('start', 0);
$page_size = getReqVal('page_size', 10);
$order_method = getReqVal('order_method', 0);
$order_asc = getReqVal('order_asc', 1);
$type = getReqVal('type', 1);
$job_filters = getReqVal('job_filters', '');
$job_filter_array = explode('|', $job_filters);
$search_filters = getReqVal('search_filters', '');
$search_filter_array = explode('|', $search_filters);
$lv_filter = getReqVal('lv_filter', 0);
$quality_filter = getReqVal('quality_filter', 0);
$durability_filter = getReqVal('durability_filter', 0);
$price_filter = getReqVal('price_filter', '');
$price_filter_array = explode('|', $price_filter);
$job_filter_fn = function ($f) {
$str = '';
$arr = array();
foreach ($f as $v) {
if (!empty($v)) {
array_push($arr, 'c_job=\'' . $v . '\' ');
}
}
if (count($arr) > 0) {
$str = implode('OR ', $arr);
$str = 'AND (' . $str . ') ';
}
return $str;
};
$price_filter_fn = function ($f) {
if (count($f) == 2) {
$low = $f[0];
$top = $f[1];
return 'AND s_price>=' . $low . ' AND s_price<=' . $top . ' ';
}
return '';
};
$lv_filter_fn = function ($f) {
$f = (int) $f;
return 'AND c_lv>=' . $f . ' ';
};
$quality_filter_fn = function ($f) {
$f = (int) $f;
return 'AND c_quality>=' . $f . ' ';
};
$durability_filter_fn = function ($f) {
$f = (int) $f;
return 'AND c_durability>=' . $f . ' ';
};
$search_filter_fn = function ($f) {
$str = '';
$arr_options = array();
foreach ($f as $v) {
if (!empty($v)) {
array_push($arr_options, 'c_name=\'' . $v . '\' OR token_id=\'' . $v . '\' ');
}
}
if (count($arr_options) > 0) {
$str = implode('OR ', $arr_options);
$str = 'AND (' . $str . ') ';
}
return $str;
};
$order_fn = function ($method, $asc) {
switch ($method) {
case 2:
return 'ORDER BY s_price ' . ($asc == 0 ? 'ASC' : 'DESC') . ' ';
break;
case 3:
return 'ORDER BY c_quality ' . ($asc == 0 ? 'ASC' : 'DESC') . ' ';
break;
case 4:
return 'ORDER BY c_lv ' . ($asc == 0 ? 'ASC' : 'DESC') . ' ';
break;
case 5:
return 'ORDER BY c_durability ' . ($asc == 0 ? 'ASC' : 'DESC') . ' ';
break;
// 所有其他不正常的排序都执行最新上架
case 1:
default:
return 'ORDER BY createtime ' . ($asc == 0 ? 'ASC' : 'DESC') . ' ';
break;
}
return '';
};
$conn = myself()->_getMarketMysql('');
$counts = $conn->execQuery(
'SELECT count(*) as count FROM t_market_store ' .
'WHERE token_type=:token_type AND status=0 ' .
$job_filter_fn($job_filter_array) .
$lv_filter_fn($lv_filter) .
$quality_filter_fn($quality_filter) .
$durability_filter_fn($durability_filter) .
$price_filter_fn($price_filter_array) .
$search_filter_fn($search_filter_array) .
$order_fn($order_method, $order_asc),
array(
':token_type' => $type,
)
);
$total = $counts[0]['count'];
$page_end = $start + $page_size;
if ($page_end > $total) {
$page_end = $total;
$start = $total - 1;
$start = intval($start / $page_size) * $page_size;
if ($start < 0) $start = 0;
}
$rows = $conn->execQuery(
'SELECT * FROM t_market_store ' .
'WHERE token_type=:token_type AND status=0 ' .
$job_filter_fn($job_filter_array) .
$lv_filter_fn($lv_filter) .
$quality_filter_fn($quality_filter) .
$durability_filter_fn($durability_filter) .
$price_filter_fn($price_filter_array) .
$search_filter_fn($search_filter_array) .
$order_fn($order_method, $order_asc) .
'LIMIT ' . $start . ',' . $page_size,
array(
':token_type' => $type,
)
);
$nfts = array();
for ($x = $start; $x < $page_end; $x++) {
$row = $rows[$x % $page_size];
$nftDb = Nft::getNft($row['token_id']);
if (!$nftDb) {
$nftDb = Nft::findNftByOwner($account, $row['token_id']);
// 0x768b5faed6dc69816f33377d214ffaf00dcdd0cf
if (!$nftDb) {
$nftDb = Nft::findNftByOwner('0xfc628dd79137395f3c9744e33b1c5de554d94882', $row['token_id']);
if (!$nftDb) {
if ($row['item_id']) {
} else {
myself()->_rspErr(1, 'nft not exists');
return;
}
}
}
}
$row['info'] = $nftDb ? Nft::toDto($nftDb) : null;
$row['detail'] = $nftDb ? $this->getNftGameData($nftDb) : null;
array_push($nfts, $row);
}
$this->_rspData(array(
"total" => $total,
"start" => $start,
"page_size" => $page_size,
'nfts' => $nfts,
));
}
public function listMyNfts()
{
$account = strtolower(getReqVal('account', ''));
$token = getReqVal('token', '');
$start = getReqVal('start', 0);
$page_size = getReqVal('page_size', 10);
$order_method = getReqVal('order_method', 0);
$order_asc = getReqVal('order_asc', 1);
$type = getReqVal('type', 1);
$job_filters = getReqVal('job_filters', '');
$address = $this->_getAddress();
if ($address != $account) {
$this->_rspErr(1, 'account not match');
return;
}
if (empty($job_filters)) {
$job_filter_array = array();
} else {
$job_filter_array = explode('|', $job_filters);
}
$search_filters = getReqVal('search_filters', '');
if ($search_filters != '') {
$search_filter_array = explode('|', $search_filters);
} else {
$search_filter_array = array();
}
$lv_filter = getReqVal('lv_filter', 0);
$quality_filter = getReqVal('quality_filter', 0);
$durability_filter = getReqVal('durability_filter', 0);
$rows = $this->getNftListByAccountAndType($account, $type, $order_method, $order_asc, $job_filter_array, $search_filter_array, $lv_filter, $quality_filter, $durability_filter);
$total = count($rows);
$page_end = $start + $page_size;
if ($page_end > $total) {
$page_end = $total;
$start = $total - 1;
$start = intval($start / $page_size) * $page_size;
if ($start < 0) $start = 0;
}
$nfts = array();
for ($x = $start; $x < $page_end; $x++) {
$row = $rows[$x];
// $this->attach_market_selling($row);
array_push($nfts, $row);
}
$this->_rspData(array(
"total" => $total,
"start" => $start,
"page_size" => $page_size,
'nfts' => $nfts,
));
}
public function sell()
{
$self = myself();
if (!$self) {
$this->_rspErr(500, 'internal error, no self');
return;
}
$account = strtolower(getReqVal('account', ''));
$token = getReqVal('token', '');
$nft_token = getReqVal('nft_token', '');
$item_id = getReqVal('item_id', '');
$s_price = getReqVal('s_price', '');
$amount = getReqVal('amount', 1);
$payment_token_address = getReqVal('payment_token_address', '');
$nonce = getReqVal('nonce', '');
$signature = getReqVal('signature', '');
$net_id = getReqVal('net_id', '');
$conn = $self->_getMarketMysql('');
$nftDb = null;
$c_name = null;
$c_job = null;
$c_lv = null;
$c_quality = null;
$c_durability = null;
$c_type = null;
$c_id = null;
if ($nft_token) {
$nftDb = Nft::findNftByOwner($account, $nft_token);
$nftDetail = Nft::toDto($nftDb);
$detail = $this->getNftGameData($nftDb);
$c_name = $nftDetail['info']['name'];
$c_job = isset($nftDetail['info']['job']) ? $nftDetail['info']['job']
: (isset($detail['chip_type']) ? $detail['chip_type']
: (isset($detail['type']) ? $detail['type']
: 0));
$c_lv = @$detail['gun_lv'] | @$detail['hero_lv'] | @$detail['chip_grade'];
$c_quality = isset($nftDetail['info']['quality']) ? $nftDetail['info']['quality'] : 0;
$c_durability = isset($nftDetail['info']['durability']) ? $nftDetail['info']['durability'] : (isset($detail['hero_tili']) ? $detail['hero_tili'] : 0);
$c_type = isset($detail['type']) ? $detail['type'] : 0;
$c_id = $nftDetail['item_id'];
} else {
if (!$this->decItems($account, $item_id, $amount)) {
$this->_rspErr(1, 'item not enough, item_id:' . $item_id);
return;
}
$itemMeta = mt\Item::get($item_id);
$c_name = $itemMeta['name'];
$c_job = 0;
$c_lv = 0;
$c_quality = $itemMeta['quality'];
$c_durability = 0;
$c_type = 0;
$c_id = $item_id;
}
$r = SqlHelper::insert(
$conn,
't_market_store',
array(
'token_id' => $nft_token,
'item_id' => $item_id,
'owner_address' => $account,
'token_type' => 0,
'amount' => $amount,
'createtime' => $self->_getNowTime(),
'modifytime' => $self->_getNowTime(),
's_price' => $s_price,
'c_name' => $c_name,
'c_job' => $c_job,
'c_lv' => $c_lv,
'c_quality' => $c_quality,
'c_durability' => $c_durability,
'c_type' => $c_type,
'c_id' => $c_id,
)
);
$this->_rspOk();
}
public function sellCancel()
{
$account = strtolower(getReqVal('account', ''));
$idx = getReqVal('idx', '');
$address = $this->_getAddress();
if ($address != $account) {
$this->_rspErr(1, 'not your goods, idx:' . $idx);
return;
}
$goods = $this->getGoodsByIdx($idx);
if (!$goods) {
$this->_rspErr(1, 'goods not found, idx:' . $idx);
return;
}
if ($goods['owner_address'] != $account) {
$this->_rspErr(1, 'not your goods, idx:' . $idx);
return;
}
if (!$this->addItems($address, $goods['item_id'], $goods['amount'])) {
$this->_rspErr(1, "cancel failed, add item failed, idx:" . $idx);
return;
}
$conn = $this->_getMarketMysql('');
$r = SqlHelper::update(
$conn,
't_market_store',
array(
'idx' => $idx,
),
array(
'status' => 1,
'modifytime' => $this->_getNowTime(),
)
);
$this->_rspOk();
}
public function sellUpdatePrice()
{
$account = strtolower(getReqVal('account', ''));
$idx = getReqVal('idx', '');
$s_price = getReqVal('s_price', '');
$address = $this->_getAddress();
if ($address != $account) {
$this->_rspErr(1, 'not your goods, idx:' . $idx);
return;
}
$goods = $this->getGoodsByIdx($idx);
if (!$goods) {
$this->_rspErr(1, 'goods not found, idx:' . $idx);
return;
}
if ($goods['owner_address'] != $account) {
$this->_rspErr(1, 'not your goods, idx:' . $idx);
return;
}
$conn = $this->_getMarketMysql('');
$r = SqlHelper::update(
$conn,
't_market_store',
array(
'idx' => $idx,
),
array(
's_price' => $s_price,
'modifytime' => $this->_getNowTime(),
)
);
$this->_rspOk();
}
public function buy()
{
$account = strtolower(getReqVal('account', ''));
$idx = getReqVal('idx', '');
$goods = $this->getGoodsByIdx($idx);
if (!$goods) {
$this->_rspErr(1, 'goods not found, idx:' . $idx);
return;
}
$response = services\BlockChainService::gameItemMallBuy(
Transaction::BUY_GOODS_FROM_MARKET_ACTION_TYPE,
$goods['s_price'],
$goods['item_id'],
$goods['amount']
);
if ($response['code'] != 0) {
$this->_rspErr(1, 'buy failed, code:' . $response['code'] . ', msg:' . $response['msg']);
return;
}
if (!$this->markOrderBuyStatus($idx)) {
$this->_rspErr(1, 'buy failed, update order status failed, idx:' . $idx);
return;
}
$item_id = $goods['item_id'];
$item_count = $goods['amount'];
BcOrder::upsert($response['trans_id'], array(
'item_id' => $item_id,
'item_num' => $item_count,
'order_type' => 1,
'ext_data' => json_encode(array(
'mode' => SHOP_BUY_MODE_NORMAL,
)),
));
$this->_rspData(array(
'block_chain' => $response,
));
}
private function sellMyNft()
{
$account = strtolower(getReqVal('account', ''));
$token = getReqVal('token', '');
$nft_token = getReqVal('nft_token', '');
$s_price = getReqVal('s_price', '');
$amount = getReqVal('amount', 1);
$payment_token_address = getReqVal('payment_token_address', '');
$nonce = getReqVal('nonce', '');
$signature = getReqVal('signature', '');
$net_id = getReqVal('net_id', '');
$conn = myself()->_getMarketMysql('');
$nftDb = Nft::findNftByOwner($account, $nft_token);
$nftDetail = Nft::toDto($nftDb);
$detail = $this->getNftGameData($nftDb);
$r = SqlHelper::insert(
$conn,
't_market_store',
array(
'token_id' => $nft_token,
'owner_address' => $nftDetail['owner_address'],
'token_type' => $nftDetail['type'],
'amount' => $amount,
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime(),
's_price' => $s_price,
'c_name' => $nftDetail['info']['name'],
'c_job' => isset($nftDetail['info']['job']) ? $nftDetail['info']['job']
: (isset($detail['chip_type']) ? $detail['chip_type']
: (isset($detail['type']) ? $detail['type']
: 0)),
'c_lv' => @$detail['gun_lv'] | @$detail['hero_lv'] | @$detail['chip_grade'],
'c_quality' => isset($nftDetail['info']['quality']) ? $nftDetail['info']['quality'] : 0,
'c_durability' => isset($nftDetail['info']['durability']) ? $nftDetail['info']['durability'] : (isset($detail['hero_tili']) ? $detail['hero_tili'] : 0),
'c_type' => isset($detail['type']) ? $detail['type'] : 0,
'c_id' => $nftDetail['item_id'],
)
);
$this->_rspOk();
}
public function buyNft()
{
$account = strtolower(getReqVal('account', ''));
$token = getReqVal('token', '');
$nft_token = getReqVal('nft_token', '');
$payment_token_address = getReqVal('payment_token_address', '');
$nonce = getReqVal('nonce', '');
$signature = getReqVal('signature', '');
$net_id = getReqVal('net_id', '');
$conn = myself()->_getMarketMysql('');
$conn->execScript('DELETE FROM t_market_store WHERE ' . 'token_id=\'' . $nft_token . '\'');
$this->_rspOk();
}
public function getSupportedCurrencyTypes()
{
$types = array();
if (SERVER_ENV == _ONLINE) {
array_push($types, array(
'name' => 'USDT',
'address' => '0xc22Ffa318051d8aF4E5f2E2732d7049486fcE093',
));
} else {
array_push($types, array(
'name' => 'USDT',
'address' => '0xc22Ffa318051d8aF4E5f2E2732d7049486fcE093',
));
}
$this->_rspData(array(
'list' => $types,
));
}
public function getTransactionRecord()
{
$account = strtolower(getReqVal('account', ''));
$type = getReqVal('type', 0);
$start = getReqVal('start', 0);
$page_size = getReqVal('page_size', 10);
$conn = myself()->_getMarketMysql('');
$type_filter_fn = function ($f) {
if ($f == 0) {
return '';
} else {
return 'AND type=' . $f;
}
};
$counts = $conn->execQuery(
'SELECT count(*) as count FROM t_market_transaction_record ' .
'WHERE (seller=:account OR buyer=:account) ' .
$type_filter_fn($type) .
' ORDER BY createtime DESC',
array(
':account' => $account,
)
);
$total = $counts[0]['count'];
$page_end = $start + $page_size;
if ($page_end > $total) {
$page_end = $total;
$start = $total - 1;
$start = intval($start / $page_size) * $page_size;
if ($start < 0) $start = 0;
}
$rows = $conn->execQuery(
'SELECT * FROM t_market_transaction_record ' .
'WHERE (seller=:account OR buyer=:account) ' .
$type_filter_fn($type) .
' ORDER BY createtime DESC ' .
'LIMIT ' . $start . ',' . $page_size,
array(
':account' => $account,
)
);
$this->_rspData(array(
"total" => $total,
"start" => $start,
"page_size" => $page_size,
'nfts' => $rows,
));
}
private function addTransactionRecord($record)
{
$conn = myself()->_getMarketMysql('');
$r = SqlHelper::insert(
$conn,
't_market_transaction_record',
$record
);
if (!$r) {
$this->_rspErr(2, 'unknown error, orderId=' . $record['orderid']);
}
}
public function eventSellOrder()
{
$tokenId = getReqVal('tokenId', '');
$owner = strtolower(getReqVal('owner', ''));
$nftToken = getReqVal('nftToken', '');
$amount = getReqVal('amount', 0);
$orderId = getReqVal('orderId', '');
$currency = getReqVal('currency', '');
$price = getReqVal('price', '');
error_log(
"eventSellOrder:" . json_encode(
array(
'tokenId' => $tokenId,
'owner' => $owner,
'nftToken' => $nftToken,
'amount' => $amount,
'orderId' => $orderId,
'currency' => $currency,
'price' => $price,
),
JSON_PRETTY_PRINT
)
);
$conn = myself()->_getMarketMysql('');
// 1. check order status
$chk = SqlHelper::selectOne($conn, 't_market_store', array('status'), array('o_link' => $orderId));
if (!empty($chk)) {
$this->_rspErr(1, 'repeat sell order, orderId=' . $orderId);
return;
}
// 2. insert sell order to t_market_store
$nftDb = Nft::findNftByOwner($owner, $tokenId);
if (empty($nftDb)) {
$nftDb = Nft::getNft($tokenId);
}
$nftDetail = Nft::toDto($nftDb);
$detail = $this->getNftGameData($nftDb);
$r = SqlHelper::insert(
$conn,
't_market_store',
array(
'token_id' => $tokenId,
'o_link' => $orderId,
'nft_token' => $nftToken,
'status' => 0,
'owner_address' => $owner,
'token_type' => $nftDetail['type'],
'amount' => $amount,
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime(),
's_currency' => $currency,
's_price' => $price,
'c_name' => $nftDetail['info']['name'],
'c_job' => isset($nftDetail['info']['job']) ? $nftDetail['info']['job']
: (isset($detail['chip_type']) ? $detail['chip_type']
: (isset($detail['type']) ? $detail['type']
: 0)),
'c_lv' => @$detail['gun_lv'] | @$detail['hero_lv'] | @$detail['chip_grade'],
'c_quality' => isset($nftDetail['info']['quality']) ? $nftDetail['info']['quality'] : 0,
'c_durability' => isset($nftDetail['info']['durability']) ? $nftDetail['info']['durability'] : (isset($detail['hero_tili']) ? $detail['hero_tili'] : 0),
'c_type' => isset($detail['type']) ? $detail['type'] : 0,
'c_id' => $nftDetail['item_id'],
)
);
if (!$r) {
$this->_rspErr(2, 'unknown error, orderId=' . $orderId);
}
$this->_rspOk();
}
public function eventBuyOrder()
{
$tokenId = getReqVal('tokenId', '');
$orderId = getReqVal('orderId', '');
$nftToken = getReqVal('nftToken', '');
$amount = getReqVal('amount', 0);
$seller = strtolower(getReqVal('seller', ''));
$buyer = strtolower(getReqVal('buyer', ''));
$erc20 = getReqVal('erc20', '');
$price = getReqVal('price', '');
error_log(
"eventBuyOrder:" . json_encode(
array(
'tokenId' => $tokenId,
'orderId' => $orderId,
'nftToken' => $nftToken,
'amount' => $amount,
'seller' => $seller,
'buyer' => $buyer,
'erc20' => $erc20,
'price' => $price,
),
JSON_PRETTY_PRINT
)
);
$conn = myself()->_getMarketMysql('');
// 1. check order status
$chk = SqlHelper::selectOne($conn, 't_market_store', array('status', 'idx', 'c_name', 'token_type'), array('o_link' => $orderId));
if (empty($chk)) {
$this->_rspErr(1, 'not found order, orderId=' . $orderId);
return;
}
if ($chk['status'] == '0') {
$r = SqlHelper::update(
$conn,
't_market_store',
array(
'o_link' => $orderId,
),
array(
'status' => 2,
)
);
if ($r) {
// 增加交易记录
$record = array(
'createtime' => myself()->_getNowTime(),
'orderid' => $chk['idx'],
'o_link' => $orderId,
'seller' => $seller,
'buyer' => $buyer,
'tokenid' => $tokenId,
'amount' => $amount,
'name' => $chk['c_name'],
'type' => $chk['token_type'],
);
$this->addTransactionRecord($record);
$this->_rspOk();
return;
}
}
$this->_rspErr(1, 'order status error, order=' . $orderId);
}
public function eventCancelOrder()
{
$orderId = getReqVal('orderId', '');
$nftToken = getReqVal('nftToken', '');
$tokenId = getReqVal('tokenId', '');
error_log(
"eventCancelOrder:" . json_encode(
array(
'orderId' => $orderId,
'nftToken' => $nftToken,
'tokenId' => $tokenId,
),
JSON_PRETTY_PRINT
)
);
$conn = myself()->_getMarketMysql('');
// 1. check order status
$chk = SqlHelper::selectOne($conn, 't_market_store', array('status'), array('o_link' => $orderId));
if (empty($chk)) {
$this->_rspErr(1, 'not found order, orderId=' . $orderId);
return;
}
if ($chk['status'] == '0') {
$r = SqlHelper::update(
$conn,
't_market_store',
array(
'o_link' => $orderId,
),
array(
'status' => 1,
)
);
if ($r) {
$this->_rspOk();
return;
}
}
$this->_rspErr(1, 'order status error, order=' . $orderId);
}
public function eventPriceUpdateOrder()
{
$orderId = getReqVal('orderId', '');;
$nftToken = getReqVal('nftToken', '');
$tokenId = getReqVal('tokenId', '');
$priceOld = getReqVal('priceOld', '');
$price = getReqVal('price', '');
error_log(
"eventPriceUpdateOrder:" . json_encode(
array(
'orderId' => $orderId,
'nftToken' => $nftToken,
'tokenId' => $tokenId,
'priceOld' => $priceOld,
'price' => $price,
),
JSON_PRETTY_PRINT
)
);
$conn = myself()->_getMarketMysql('');
// 1. check order status
$chk = SqlHelper::selectOne($conn, 't_market_store', array('status'), array('o_link' => $orderId));
if (empty($chk)) {
$this->_rspErr(1, 'not found order, orderId=' . $orderId);
return;
}
if ($chk['status'] == '0') {
$r = SqlHelper::update(
$conn,
't_market_store',
array(
'o_link' => $orderId,
),
array(
's_price' => $price,
)
);
if ($r) {
$this->_rspOk();
return;
}
}
$this->_rspErr(1, 'price update failed, orderId=' . $orderId);
}
private function getNftGameData($nftRowInfo)
{
$t = $nftRowInfo['token_type'];
$token_id = $nftRowInfo['token_id'];
switch ($t) {
case Nft::HERO_TYPE: {
return $this->appendChipsInfo(Hero::toDtoInfo(Hero::findByTokenId2($token_id)));
}
break;
case Nft::EQUIP_TYPE: {
return $this->appendChipsInfo(Gun::toDtoInfo(Gun::findByTokenId2($token_id)));
}
break;
case Nft::CHIP_TYPE: {
return Chip::toDto(Chip::getChipByTokenId($token_id));
}
break;
case Nft::FRAGMENT_TYPE: {
return Fragment::ToDto($nftRowInfo);
}
break;
default: {
}
break;
}
return array('unknown' => 'unknown game data type, cannot find data');
}
private function appendChipsInfo($detail)
{
$detail['chips_info'] = array();
if (!empty($detail['chip_ids'])) {
$chips = explode('|', $detail['chip_ids']);
foreach ($chips as $chip) {
$chip_info = "";
if (!empty($chip)) {
$chip_info = Chip::toDto(Chip::getChipByTokenId($chip));
}
array_push($detail['chips_info'], $chip_info);
}
}
return $detail;
}
private function attach_market_selling(&$row)
{
$conn = myself()->_getMarketMysql('');
$rows = $conn->execQuery(
'SELECT * FROM t_market_store ' .
'WHERE token_id=:token_id AND owner_address=:owner_address AND status=:status',
array(
':token_id' => $row['token_id'],
':owner_address' => $row['owner_address'],
':status' => 0,
)
);
$count = 0;
$link_array = array();
foreach ($rows as $r) {
$count += $r['amount'];
array_push($link_array, $r['o_link']);
}
$row['o_link'] = implode('|', $link_array);
$row['selling'] = $count;
}
private function listMySelledNfts($account, $type)
{
// error_log('listMySelledNfts ' . $account . ' ' . $type);
$conn = myself()->_getMarketMysql('');
$rows = $conn->execQuery(
'SELECT * FROM t_market_store ' .
'WHERE owner_address=:account AND token_type=:token_type AND status=0 ',
array(
':account' => $account,
':token_type' => $type,
)
);
return $rows;
}
private function addItems($address, $item_id, $amount)
{
$self = myself();
if (!$self) {
return false;
}
$r = $this->addItem($address, $item_id, $amount);
if (!$r) {
return false;
}
return true;
}
private function decItems($address, $item_id, $amount)
{
$self = myself();
if (!$self) {
return false;
}
$userInfo = $this->getUserInfo($address, array('gold'));
$count = $this->getItemCount($item_id, $userInfo);
if ($count < $amount) {
return false;
}
$r = $this->decItem($address, $item_id, $amount);
if (!$r) {
return false;
}
return true;
}
private function addItem($address, $item_id, $amount)
{
$self = myself();
if (!$self) {
return false;
}
switch ($item_id) {
case V_ITEM_GOLD: {
$r = $this->addGold($address, $amount);
if (!$r) {
return false;
}
}
break;
}
return true;
}
private function decItem($address, $item_id, $amount)
{
$self = myself();
if (!$self) {
return false;
}
switch ($item_id) {
case V_ITEM_GOLD: {
$r = $this->decGold($address, $amount);
if (!$r) {
return false;
}
}
break;
}
return true;
}
private function addGold($address, $amount)
{
$self = myself();
if (!$self) {
return false;
}
$r = $this->updateUserInfo($address, array(
'gold' => function () use ($amount) {
return "gold + ${amount}";
}
));
if (!$r) {
return false;
}
return true;
}
private function decGold($address, $amount)
{
$self = myself();
if (!$self) {
return false;
}
$userInfo = $this->getUserInfo($address, array('gold'));
$count = $this->getItemCount(V_ITEM_GOLD, $userInfo);
if ($count < $amount) {
return false;
}
$r = $this->updateUserInfo($address, array(
'gold' => function () use ($amount) {
return "GREATEST(0, gold - ${amount})";
},
'consume_gold' => function () use ($amount) {
return "consume_gold + ${amount}";
}
));
if (!$r) {
return false;
}
return true;
}
private function updateUserInfo($address, $fieldKv)
{
$self = myself();
if (!$self) {
return false;
}
$r = SqlHelper::update(
$self->_getMysql(''),
't_user',
array(
'address' => $address,
),
$fieldKv
);
if (!$r) {
return false;
}
return true;
}
private function getCostItem($address, $item_id)
{
$self = myself();
if (!$self) {
return null;
}
$userInfo = $this->getUserInfo($address, array('gold'));
$count = $this->getItemCount($item_id, $userInfo);
return array(
'item_id' => $item_id,
'item_amount' => $count
);
}
private function getItemCount($item_id, $userInfo)
{
switch ($item_id) {
case V_ITEM_GOLD: {
return $userInfo['gold'];
}
}
return "0";
}
private function getUserInfo($address, $fields)
{
$self = myself();
if (!$self) {
return null;
}
// $account_id = $this->getAccountId($address);
// if (!$account_id) {
// return null;
// }
$conn = $self->_getMysql($address);
$row = SqlHelper::selectOne(
$conn,
't_user',
$fields,
array(
'address' => $address
)
);
if (empty($row)) {
return null;
}
return $row;
}
private function getAccountId($address)
{
$self = myself();
if (!$self) {
return null;
}
$row = SqlHelper::selectOne(
$self->_getMysql($address),
't_user',
array('account_id'),
array(
'address' => $address
)
);
return $row['account_id'];
}
private function normalizeWeb3Price($price)
{
$bn1 = phpcommon\bnInit($price * pow(10, 8));
$bn2 = phpcommon\bnInit('1000000000000000000');
$ret_price = phpcommon\bnDiv(phpcommon\bnMul($bn1, $bn2), pow(10, 8));
// error_log('normalizeWeb3Price: ' . $ret_price . ' ' . $price * pow(10, 8));
return phpcommon\bnToStr($ret_price);
}
private function getGoodsByIdx($idx)
{
$self = myself();
if (!$self) {
return null;
}
$row = SqlHelper::selectOne(
$self->_getMarketMysql(''),
't_market_store',
array('item_id', 'amount', 's_price', 'owner_address'),
array(
'idx' => $idx,
'status' => 0,
)
);
if (!$row) {
return null;
}
if (!$row['item_id']) {
return null;
}
return $row;
}
private function markOrderBuyStatus($idx)
{
$self = myself();
if (!$self) {
return false;
}
$r = SqlHelper::update(
$self->_getMarketMysql(''),
't_market_store',
array(
'idx' => $idx,
),
array(
'status' => 3,
'buytime' => $self->_getNowTime(),
)
);
if (!$r) {
return false;
}
return true;
}
}