game2006api/webapp/controller/StakingController.class.php
aozhiwei 798f78c82f 1
2023-09-05 14:27:51 +08:00

318 lines
9.8 KiB
PHP

<?php
require_once("mt/Staking.php");
require_once("models/Nft.php");
require_once("models/Staking.php");
use models\Nft;
use models\Staking;
class StakingController extends BaseAuthedController {
const PLANET_TYPE = 1;
const BADGE_TYPE = 2;
const CEC_TYPE = 3;
public function _handlePre()
{
parent::_handlePre();
}
public function info()
{
$info = array(
'pool_size' => 8000 * 10000 - Staking::getAllStakingValue(),
'total_staking_value' => '0',
'daily_staking_value' => '0',
'planet' => array(
'type' => self::PLANET_TYPE,
'is_open' => 1
),
'badge' => array(
'type' => self::BADGE_TYPE,
'is_open' => 1
),
'cec' => array(
'type' => self::CEC_TYPE,
'is_open' => 0
),
);
$rows = Staking::all(myself()->_getAddress());
$this->fillStakingInfo($info, 'planet', $rows);
$this->fillStakingInfo($info, 'badge', $rows);
$this->fillStakingInfo($info, 'cec', $rows);
myself()->_rspData($info);
}
private function fillStakingInfo(&$info, $fieldName, $rows)
{
$stackingInfo = &$info[$fieldName];
$stackingInfo['staked_quant'] = 0;
$stackingInfo['claim_rewards'] = 0;
$stackingInfo['daily_rewards'] = 0;
$stackingInfo['staking_value'] = 0;
$stackingInfo['maturity_quant'] = 0;
foreach ($rows as $row) {
$stakingMeta = mt\Staking::get($row['item_id']);
if ($this->isTypeNft($stackingInfo['type'], $row) && $stakingMeta) {
$stakingDto = Staking::toDto($row);
if ($stakingDto['status'] == Staking::STAKING_STATUS) {
if ($stakingDto['remain_days'] <= 0) {
$stackingInfo['maturity_quant'] += $stakingDto['stacked_num'];
} else {
$stackingInfo['daily_rewards'] += $stakingDto['daily_rewards'];
$info['daily_staking_value'] += $stakingDto['daily_rewards'];
}
$stackingInfo['staked_quant'] += $stakingDto['stacked_num'];
$stackingInfo['staking_value'] += $stakingDto['cec_value'];
$info['total_staking_value'] += $stakingDto['cec_value'] + $stakingDto['total_rewards'];
} else {
$stackingInfo['claim_rewards'] += $stakingDto['total_rewards'];
}
}
}
}
public function staking()
{
$type = getReqVal('type', '');
$stackedQuant = 0;
$items = array();
$rows = Staking::all(myself()->_getAddress());
foreach ($rows as $row) {
if ($row['status'] == Staking::STAKING_STATUS) {
if ($this->isTypeNft($type, $row)) {
array_push($items, Staking::toDto($row));
++$stackedQuant;
}
}
}
myself()->_rspData(array(
'type' => $type,
'stacked_quant' => $stackedQuant,
'items' => $items
));
}
public function history()
{
$items = array();
$fromTime = getReqVal('from_time', '');
$toTime = getReqVal('to_time', '');
$rows = Staking::all(myself()->_getAddress());
foreach ($rows as $row) {
if ($row['status'] == Staking::REDEEM_STATUS) {
array_push($items, Staking::toDto($row));
}
}
myself()->_rspData(array(
'items' => $items
));
}
public function redeemPreview()
{
$transId = getReqVal('trans_id', 0);
$rows = Staking::all(myself()->_getAddress());
$cegRewards = '0';
foreach ($rows as $row) {
$stakingDto = Staking::toDto($row);
if ($stakingDto && $stakingDto['trans_id'] == $transId) {
if ($stakingDto['remain_days'] <= 0) {
$cegRewards = $stakingDto['total_rewards'];
} else {
$cegRewards = $stakingDto['total_rewards'] * 0.25;
}
break;
}
}
myself()->_rspData(array(
'ceg_rewards' => $cegRewards
));
}
public function stakingPreview()
{
$checkList = array();
{
$tokenIdList = explode('|', getReqVal('token_id_list', ''));
$contractAddressList = explode('|', strtolower(getReqVal('contract_address_list', '')));
$netId = getReqVal('net_id', '');
if (count($tokenIdList) <= 0) {
myself()->_rspErr(1, 'paramater error');
return;
}
if (count($tokenIdList) != count($contractAddressList)) {
myself()->_rspErr(1, 'paramater error');
return;
}
for ($i = 0; $i < count($tokenIdList); ++$i) {
if (array_key_exists($tokenIdList[$i], $checkList)) {
myself()->_rspErr(1, 'paramater error');
return;
}
$checkList[$tokenIdList[$i]] = array(
'token_id' => $tokenIdList[$i],
'contract_address' => $contractAddressList[$i],
'net_id' => $netId
);
}
}
$confirmedNfts = array();
{
$nftList = Nft::getNftList(myself()->_getAddress());
foreach ($nftList as $nft) {
if (array_key_exists($nft['token_id'], $checkList)) {
$c = $checkList[$nft['token_id']];
if ($c['contract_address'] == $nft['contract_address'] &&
$c['net_id'] == $nft['net_id']) {
array_push($confirmedNfts,
$nft);
}
}
}
}
error_log(json_encode($_REQUEST));
error_log(json_encode($checkList));
error_log(json_encode($confirmedNfts));
if (count($checkList) != count($confirmedNfts)) {
myself()->_rspErr(3, 'paramater error');
return;
}
$nftValue = 0;
$items = array();
$lastTimeArr = null;
foreach ($confirmedNfts as $nft) {
$stakingMeta = mt\Staking::get($nft['item_id']);
if (!$stakingMeta) {
myself()->_rspErr(4, 'config error');
return;
}
$nftValue += $stakingMeta['stake_value'];
if ($lastTimeArr) {
if (count($lastTimeArr) != count($stakingMeta['stake_time_arr'])) {
myself()->_rspErr(5, 'config error');
return;
}
for ($i = 0; $i < count($lastTimeArr); ++$i) {
if ($lastTimeArr[$i] != $stakingMeta['stake_time_arr'][$i]) {
myself()->_rspErr(5, 'config error');
return;
}
}
}
$lastTimeArr = $stakingMeta['stake_time_arr'];
}
foreach ($lastTimeArr as $time) {
$days = intval($time / 30);
switch ($days) {
case 1:
{
array_push(
$items,
array(
'time' => $days * 30,
'interest' => 1
)
);
}
break;
case 3:
{
array_push(
$items,
array(
'time' => $days * 30,
'interest' => 5
)
);
}
break;
case 6:
{
array_push(
$items,
array(
'time' => $days * 30,
'interest' => 15
)
);
}
break;
case 12:
{
array_push(
$items,
array(
'time' => $days * 30,
'interest' => 40
)
);
}
break;
case 24:
{
array_push(
$items,
array(
'time' => $days * 30,
'interest' => 100
)
);
}
break;
default:
{
myself()->_rspErr(10, 'config error');
return;
}
break;
}
}
myself()->_rspData(array(
'nft_value' => $nftValue,
'items' => $items
));
}
private function isTypeNft($type, $dbInfo)
{
switch ($type) {
case self::PLANET_TYPE:
{
if (in_array($dbInfo['token_type'], array(
Nft::PLANET_TYPE
))) {
return true;
}
}
break;
case self::BADGE_TYPE:
{
if (SERVER_ENV != _ONLINE) {
if (in_array($dbInfo['token_type'], array(
Nft::HONOR1_TYPE
))) {
return true;
}
} else {
if (in_array($dbInfo['token_type'], array(
Nft::GENESIS_TYPE
))) {
return true;
}
}
}
break;
case self::CEC_TYPE:
{
}
break;
}
return false;
}
}