game2006api/webapp/controller/OutAppPlanetController.class.php
aozhiwei ccd0cfc55a 1
2023-09-01 13:54:26 +08:00

94 lines
2.7 KiB
PHP

<?php
require_once('models/Transaction.php');
require_once('services/BlockChainService.php');
use phpcommon\SqlHelper;
use models\Transaction;
use services\BlockChainService;
class OutAppPlanetController extends BaseController {
public function buy()
{
$netId = getReqVal('net_id', '');
$address = getReqVal('address', '');
$num = getReqVal('num', '');
if ($netId != NET_ID) {
myself()->_rspErr(1, 'net_id error');
return;
}
if ($num < 1) {
myself()->_rspErr(1, 'num error');
return;
}
$decimals = BlockChainService::getCurrencyDecimals(BlockChainService::CURRENCY_USDT);
if ($decimals === false) {
myself()->_rspErr(1, 'deciamls error');
return;
}
$ids = array();
$period = 1;
$data = $this->load($period);
{
$rows = myself()->_getMarketMysql()->execQuery
(
'SELECT * FROM t_nft WHERE token_type=:token_type AND creator_address = owner_address AND token_id >= :begin_token_id AND token_id <= :end_token_id;',
array(
':token_type' => Nft::PLANET_TYPE,
':begin_token_id' => 0,
':end_token_id' => 0
)
);
$nftList = array();
foreach ($rows as $row) {
if (!array_key_exists($row['token_id'], $data)) {
array_push($nftList, $row['token_id']);
}
}
shuffle($nftList);
if (count($nftList) < $num) {
myself()->_rspErr(2, 'num error');
return;
}
for ($i = 0; $i < $num; ++$i) {
array_push($ids, $nftList[$i]);
$data[$nftList] = myself()->_getNowTime();
}
}
$price = BlockChainService::formatCurrencyEx(1 * count($ids), $decimals);
$rspObj = BlockChainService::beNftMallTransBuyPlanet(
Transaction::BUY_OUTAPP_PLANET_ACTION_TYPE,
$address,
$ids,
BlockChainService::CURRENCY_USDT,
$price
);
$this->save($period, $data);
$this->_rspData(array(
'trans_id' => $rspObj['trans_id'],
'params' => $rspObj['params'],
));
}
private function load($period)
{
$rawData = myself()->_getRedis('')->get(PLANET_BUY_KEY . $period);
if (empty($rawData)) {
return array();
}
return json_decode($rawData, true);
}
private function save($period, $data)
{
myself()->_getRedis('')->get(PLANET_BUY_KEY . $period, json_encode($data));
}
}