game2006api/webapp/controller/ComputingPowerController.class.php
hujiabin a186e56f3e 1
2023-09-05 18:44:10 +08:00

368 lines
14 KiB
PHP

<?php
require_once('mt/HashRate.php');
require_once('mt/HashRateCommon.php');
require_once('mt/Item.php');
require_once('mt/Parameter.php');
require_once('services/HashRateService.php');
require_once('services/AwardService.php');
require_once('services/PropertyChgService.php');
require_once('models/ComputingPower.php');
require_once('models/CrystalRecord.php');
require_once('models/RewardsCec.php');
require_once('models/Staking.php');
use models\ComputingPower;
use models\CrystalRecord;
use models\RewardsCec;
use models\Staking;
use mt\HashRate;
use mt\Parameter;
use phpcommon\SqlHelper;
class ComputingPowerController extends BaseAuthedController
{
public function info(){
$period_state = -1 ; //-1:算力周期未开始 0:正常周期 1:等待周期 2:维护周期
$curr_period = array(
// "current_state" => -1, //-1:算力周期未开始 0:正常周期 1:等待周期 2:维护周期
"period_begin" => 0,
"period_end" => 0,
"await_time" => 0,
"assignable_cec" => 0,
"total_target" => 0,
"total_exchange_hash_rate" => 0,
"total_hash_rate" => 0,
// "refresh_remain_time" => 0,
);
$last_period = array(
"period_begin" => 0,
"period_end" => 0,
"reward" => 0,
);
$lastMeta = \mt\HashRateCommon::getLastPeriod();
$currentMeta = \mt\HashRateCommon::getCurrentPeriod();
$nextMeta = \mt\HashRateCommon::getNextPeriod();
if (!$currentMeta && $lastMeta){
if ($nextMeta){
$period_state = 1;
// $curr_period['current_state'] = 1;
$curr_period['period_begin'] = strtotime($nextMeta['start_time']);
$curr_period['period_end'] = strtotime($nextMeta['end_time']);
$curr_period['await_time'] = strtotime($nextMeta['start_time']) - myself()->_getNowTime();
}else{
// $curr_period['current_state'] = 2;
$period_state = 2;
}
}
if ($currentMeta){
$period_state = 0;
// $curr_period['current_state'] = 0;
$curr_period['period_begin'] = strtotime($currentMeta['start_time']);
$curr_period['period_end'] = strtotime($currentMeta['end_time']);
$curr_period['assignable_cec'] = $currentMeta['cec_pool'];
$curr_period['total_target'] = $currentMeta['target'];
$curr_period['total_exchange_hash_rate'] = ComputingPower::getOwnedBH($currentMeta['id']);
// $rowDb = ComputingPower::getTotalBH($currentMeta['id']);
$curr_period['total_hash_rate'] = ComputingPower::getTotalBH($currentMeta['id']) + $currentMeta['hashrate_add'];
// $curr_period['refresh_remain_time'] = $rowDb ? $rowDb['modifytime'] + 3600 - myself()->_getNowTime() : 0;
}
if ($lastMeta){
$last_period['period_begin'] = strtotime($lastMeta['start_time']);
$last_period['period_end'] = strtotime($lastMeta['end_time']);
$cecDb = RewardsCec::findByAccount(myself()->_getAccountId(),$lastMeta['id']);
$last_period['reward'] = $cecDb ? $cecDb['reward_cec'] : 0;
$last_period['last_hash_rate'] = ComputingPower::getOwnedBH($lastMeta['id']);
}
$info = array(
'period_state' => $period_state,
'curr_period' => $curr_period,
'last_period' => $last_period,
'listing_state' => LISTING_SWITCH,
'owned_cec' => RewardsCec::getTotalCecNum(),
'owned_total_hash_rate' =>ComputingPower::getMyTotalBH(),
);
$this->_rspData(array(
'info' => $info
));
}
public function getCrystalUi(){
$crystalList = array();
$itemMetas = \mt\Item::getMetaListByType(\mt\Item::CRYSTAL_TYPE);
$hashRateService = new \services\HashRateService();
foreach ($itemMetas as $meta){
$crystalDto = array();
$hashRateService->getCrystalDto($meta,$crystalDto);
array_push($crystalList,$crystalDto);
}
$this->_rspData(array(
'data' => $crystalList
));
}
public function exchangeCrystal(){
$itemId = getReqVal('item_id', 0);
$itemMeta = \mt\Item::get($itemId);
if (!$itemMeta || $itemMeta['type'] != \mt\Item::CRYSTAL_TYPE ){
$this->_rspErr(1, 'param error');
return ;
}
$hashRateMeta = HashRate::findHash($itemId);
if (!$hashRateMeta){
$this->_rspErr(1, 'param error');
return ;
}
$param = explode('|',$hashRateMeta['parameter']);
$paramEd = explode(':',$param[0]);
$paramEx = explode(':',$param[1]);
$crystalNum = 0;
switch ($itemId){
case ComputingPower::CRYSTAL1 : {
$killsNum = myself()->_getV(TN_TOTAL_KILLS_NUM,0);
$killsNumEd = myself()->_getV(TN_TOTAL_KILLS_NUM,-1);
$killsNumEx = $killsNum - $killsNumEd;
$starNum = myself()->_getV(TN_TOTAL_STAR_NUM,0);
$starNumEd = myself()->_getV(TN_TOTAL_STAR_NUM,-1);
$starNumEx = $starNum - $starNumEd ;
$crystalNum = max(0,floor($killsNumEx / $paramEd[0]) * $paramEd[1] +
floor($starNumEx / $paramEx[0]) * $paramEx[1] );
}
break;
case ComputingPower::CRYSTAL2 : {
$consumeCost = myself()->_getV(TN_TOTAL_DIAMOND_CONSUME,0);
$consumeCostEd = myself()->_getV(TN_TOTAL_DIAMOND_CONSUME,-1);
$consumeCostEx = $consumeCost - $consumeCostEd ;
$crystalNum = max(0,floor($consumeCostEx / \services\HashRateService::EXCHANGE_RATE / $paramEd[0]) * $paramEd[1]);
}
break;
case ComputingPower::CRYSTAL3 : {
$consumeCost = myself()->_getV(TN_TOTAL_CEG_CONSUME,0);
$consumeCostEd = myself()->_getV(TN_TOTAL_CEG_CONSUME,-1);
$consumeCostEx = $consumeCost - $consumeCostEd;
$crystalNum = max(0,floor($consumeCostEx / \services\HashRateService::EXCHANGE_RATE / $paramEd[0]) * $paramEd[1]);
}
break;
case ComputingPower::CRYSTAL4 : {
$recordDb = CrystalRecord::getNewestRecordOne($itemId);
if ($recordDb && myself()->_getNowDaySeconds() == myself()->_getDaySeconds($recordDb['createtime'])){
$this->_rspErr(1, 'There are no available crystals');
return ;
}
// $rate = Parameter::getVal('cec_exchange_rate',10);
$cecVal = Staking::getStakingTotalValue();
$crystalNum = max(0,floor($cecVal / $paramEd[0]) * $paramEd[1]);
}
}
if ($crystalNum < 1){
$this->_rspErr(1, 'There are no available crystals');
return ;
}
$items = array(
array(
'item_id' => $itemId,
'item_num' => $crystalNum,
)
);
$propertyChgService = new services\PropertyChgService();
$awardService = new services\AwardService();
$this->_addItems($items,$awardService,$propertyChgService);
CrystalRecord::addCrystalRecord($itemId,$crystalNum);
switch ($itemId) {
case ComputingPower::CRYSTAL1 : {
$killsNum = myself()->_getV(TN_TOTAL_KILLS_NUM,0);
$killsNumEd = myself()->_getV(TN_TOTAL_KILLS_NUM,-1);
$starNum = myself()->_getV(TN_TOTAL_STAR_NUM,0);
$starNumEd = myself()->_getV(TN_TOTAL_STAR_NUM,-1);
//floor($killsNumEx / $paramEd[0]) * $paramEd[1] 222 100:3 222/100=2 2*3=6 6/3=2 2*100
$newKillsNum = floor(($killsNum-$killsNumEd)/$paramEd[0]) * $paramEd[0];
myself()->_incV(TN_TOTAL_KILLS_NUM,-1,$newKillsNum);
$newStarNum = floor(($starNum-$starNumEd)/$paramEx[0]) * $paramEx[0];
myself()->_incV(TN_TOTAL_STAR_NUM,-1,$newStarNum);
}
break;
case ComputingPower::CRYSTAL2 : {
myself()->_incV(TN_TOTAL_DIAMOND_CONSUME,-1,$crystalNum / $paramEd[1] * $paramEd[0] * \services\HashRateService::EXCHANGE_RATE);
}
break;
case ComputingPower::CRYSTAL3 : {
myself()->_incV(TN_TOTAL_CEG_CONSUME,-1,$crystalNum / $paramEd[1] * $paramEd[0] * \services\HashRateService::EXCHANGE_RATE);
}
break;
}
$this->_rspData(array(
'award' => $awardService->toDto(),
'property_chg' => $propertyChgService->toDto(),
));
}
public function getExchangeCrystalRecord(){
$crystalRecordList = array();
CrystalRecord::getCrystalRecordList(function ($row) use (&$crystalRecordList) {
array_push($crystalRecordList,array(
'item_id' => $row['item_id'],
'item_num' => $row['item_num'],
'createtime' => $row['createtime'],
));
});
$this->_rspData(array(
'data' => $crystalRecordList,
));
}
public function exchangeUplimit(){
$currentMeta = \mt\HashRateCommon::getCurrentPeriod();
if (!$currentMeta){
$this->_rspErr(1, 'kindly await');
return ;
}
$list = ComputingPower::findByPeriod($currentMeta['id']);
$this->_rspData(array(
'data' => $list,
));
}
public function exchangePower(){
$num1 = getReqVal('num1', 0);
$num2 = getReqVal('num2', 0);
$num3 = getReqVal('num3', 0);
$num4 = getReqVal('num4', 0);
$currentMeta = \mt\HashRateCommon::getCurrentPeriod();
if (!$currentMeta){
$this->_rspErr(1, 'kindly await');
return ;
}
if (!$num1 && !$num2 && !$num3 && !$num4 ){
$this->_rspErr(1, 'exchange none');
return ;
}
$BH = 0 ;
$costItems = array();
if ($num1 > 0){
$meta = \mt\HashRate::findHash(ComputingPower::CRYSTAL1);
$this->_verificationCeiling($currentMeta['id'],$meta,$num1,$num2,$num3,$num4);
$BH += $num1 * $meta['amount_bh'];
array_push($costItems,array(
'item_id' => ComputingPower::CRYSTAL1,
'item_num' => $num1
));
}
if ($num2 > 0){
$meta = \mt\HashRate::findHash(ComputingPower::CRYSTAL2);
$this->_verificationCeiling($currentMeta['id'],$meta,$num1,$num2,$num3,$num4);
$BH += $num2 * $meta['amount_bh'];
array_push($costItems,array(
'item_id' => ComputingPower::CRYSTAL2,
'item_num' => $num2
));
}
if ($num3 > 0){
$meta = \mt\HashRate::findHash(ComputingPower::CRYSTAL3);
$this->_verificationCeiling($currentMeta['id'],$meta,$num1,$num2,$num3,$num4);
$BH += $num3 * $meta['amount_bh'];
array_push($costItems,array(
'item_id' => ComputingPower::CRYSTAL3,
'item_num' => $num3
));
}
if ($num4 > 0){
$meta = \mt\HashRate::findHash(ComputingPower::CRYSTAL4);
$this->_verificationCeiling($currentMeta['id'],$meta,$num1,$num2,$num3,$num4);
$BH += $num4 * $meta['amount_bh'];
array_push($costItems,array(
'item_id' => ComputingPower::CRYSTAL4,
'item_num' => $num4
));
}
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
$this->_decItems($costItems);
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addBagChg();
ComputingPower::addPowerRecord(
array(
'period' => $currentMeta['id'],
'item_num1' => $num1,
'item_num2' => $num2,
'item_num3' => $num3,
'item_num4' => $num4,
'total_num' => $BH,
)
);
$this->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
public function getRewardHistory(){
$starTime = getReqVal('star_time', 0);
$endTime = getReqVal('end_time', 0);
$historyDb = RewardsCec::getHistoryByTime($starTime,$endTime);
$historyList = array();
$pendingNum = 0;
foreach ($historyDb as $row){
$historyDto = RewardsCec::toDto($row);
$pendingNum += $historyDto['pending_num'];
array_push($historyList,$historyDto);
}
$this->_rspData(array(
'data' => $historyList,
'pending_rewards' => strval($pendingNum),
));
}
private function _verificationCeiling($period,$meta,$num1,$num2,$num3,$num4){
$list = ComputingPower::findByPeriod($period);
switch ($meta['item_id']){
case ComputingPower::CRYSTAL1 : {
if ($list['num1'] + $num1 > $meta['week_max']){
$this->_rspErr(1, 'Micro crystal exchange ceiling');
die ;
}
}
break;
case ComputingPower::CRYSTAL2 : {
if ($list['num2'] + $num2 > $meta['week_max']){
$this->_rspErr(1, 'Standard crystal exchange ceiling');
die ;
}
}
break;
case ComputingPower::CRYSTAL3 : {
if ($list['num3'] + $num3 > $meta['week_max']){
$this->_rspErr(1, 'Large crystal exchange ceiling');
die ;
}
}
break;
case ComputingPower::CRYSTAL4 : {
if ($list['num4'] + $num4 > $meta['week_max']){
$this->_rspErr(1, 'Giant crystal exchange ceiling');
die ;
}
}
break;
}
}
}