127 lines
3.9 KiB
PHP
127 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace services;
|
|
|
|
require_once('phpcommon/bchelper.php');
|
|
|
|
require_once('models/Nft.php');
|
|
require_once('models/LuckyBox.php');
|
|
require_once('models/BuyRecord.php');
|
|
require_once('models/BoxOpenedEvent.php');
|
|
|
|
use phpcommon;
|
|
use models\Nft;
|
|
use models\LuckyBox;
|
|
use models\BuyRecord;
|
|
use models\BoxOpenedEvent;
|
|
|
|
class LuckyBoxService {
|
|
|
|
public static function open($account, $tokenId, $netId)
|
|
{
|
|
$nftDb = Nft::getNft($tokenId);
|
|
if (!$nftDb) {
|
|
myself()->_rspErr(2, 'box not found');
|
|
return;
|
|
}
|
|
if ($nftDb['token_type'] != Nft::BLIND_BOX_TYPE) {
|
|
myself()->_rspErr(2, 'paramater error');
|
|
return;
|
|
}
|
|
$luckyDb = LuckyBox::find($tokenId);
|
|
if (!$luckyDb) {
|
|
$tokenIds = array();
|
|
for ($i = 0; $i < 3; ++$i) {
|
|
$newTokenId = BuyRecord::genOrderId(2006,
|
|
phpcommon\BC_FUNC_OPEN_BOX,
|
|
time(),
|
|
$account);
|
|
if (!phpcommon\isValidOrderId($newTokenId)) {
|
|
myself()->_rspErr(2, 'server internal error');
|
|
return;
|
|
}
|
|
array_push($tokenIds, $newTokenId);
|
|
}
|
|
LuckyBox::add($tokenId, $account, $tokenIds);
|
|
}
|
|
$luckyDb = LuckyBox::find($tokenId);
|
|
if (!$luckyDb) {
|
|
myself()->_rspErr(2, 'server internal error3');
|
|
return;
|
|
}
|
|
{
|
|
$params = array(
|
|
'c' => 'BcService',
|
|
'a' => 'openBoxSignature',
|
|
'account' => $account,
|
|
'box_token_id' => $tokenId,
|
|
'token_id1' => $luckyDb['token_id1'],
|
|
'token_id2' => $luckyDb['token_id2'],
|
|
'token_id3' => $luckyDb['token_id3'],
|
|
);
|
|
$url = self::getWeb3ServiceUrl();
|
|
$response = '';
|
|
if (!phpcommon\HttpClient::get
|
|
($url,
|
|
$params,
|
|
$response)) {
|
|
phpcommon\sendError(500, 'server internal error');
|
|
die();
|
|
return;
|
|
}
|
|
error_log(json_encode(array(
|
|
'_REQUEST' => $_REQUEST,
|
|
'params' => $params,
|
|
'response' => $response
|
|
)));
|
|
$json = json_decode($response, true);
|
|
myself()->_rspData(array(
|
|
'nonce' => $json['nonce'],
|
|
'signature' => $json['signature'],
|
|
'tokenIds' => array(
|
|
$luckyDb['token_id1'],
|
|
$luckyDb['token_id2'],
|
|
$luckyDb['token_id3'],
|
|
)
|
|
));
|
|
}
|
|
}
|
|
|
|
public static function queryResult($account, $tokenId, $netId)
|
|
{
|
|
$eventDb = BoxOpenedEvent::find($tokenId);
|
|
if (!$eventDb) {
|
|
myself()->_rspData(array(
|
|
'state' => 0,
|
|
'nfts'=> array()
|
|
));
|
|
return;
|
|
}
|
|
$nfts = array();
|
|
for ($i = 1; $i <= 3; ++$i) {
|
|
if ($eventDb['token_id' . $i]) {
|
|
$nftDb = Nft::getNft($eventDb['token_id' . $i]);
|
|
if (!$nftDb) {
|
|
myself()->_rspData(array(
|
|
'state' => 0,
|
|
'nfts'=> array()
|
|
));
|
|
return;
|
|
}
|
|
array_push($nfts, Nft::toDto($nftDb));
|
|
}
|
|
}
|
|
myself()->_rspData(array(
|
|
'state' => 1,
|
|
'nfts'=> $nfts
|
|
));
|
|
}
|
|
|
|
private static function getWeb3ServiceUrl()
|
|
{
|
|
$web3ServiceCluster = require_once('../config/web3service.cluster.php');
|
|
return $web3ServiceCluster[rand() % count($web3ServiceCluster)];
|
|
}
|
|
|
|
}
|