game2006api/webapp/controller/ComputingPowerController.class.php
hujiabin 49e46637fc 1
2023-11-15 15:03:25 +08:00

514 lines
20 KiB
PHP

<?php
require_once('mt/HashRate.php');
require_once('mt/HashRateCommon.php');
require_once('mt/HashRateTask.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('services/NumberService.php');
require_once('models/ComputingPower.php');
require_once('models/CrystalRecord.php');
require_once('models/RewardsCec.php');
require_once('models/Staking.php');
require_once('models/HashRateTask.php');
require_once('models/Bag.php');
use models\ComputingPower;
use models\CrystalRecord;
use models\RewardsCec;
use models\Staking;
use models\HashRateTask;
use models\Bag;
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(
"period_begin" => 0,
"period_end" => 0,
"await_time" => 0,
"assignable_cec" => 0,
"cec_pool" => 0,
"total_exchange_hash_rate" => 0,
"total_hash_rate" => 0,
);
$reward = array(
"cec" => 0,
"point" => 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){
$ownerNum = ComputingPower::getOwnedBH($lastMeta['id']);
$totalNum = ComputingPower::getTotalBH($lastMeta['id']);
$target = \services\NumberService::ceilEx(min($totalNum / $lastMeta['cec_pool'] , 1),6);
if ($totalNum == 0) {
$ratio = 0;
}else{
$ratio = $ownerNum/$totalNum;
}
$reward['cec'] = \services\NumberService::ceilEx($currentMeta['cec_pool'] * $target * $ratio,2);
if ($nextMeta){
$period_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{
$period_state = 2;
}
}
if ($currentMeta){
$period_state = 0;
$ownerNum = ComputingPower::getOwnedBH($currentMeta['id']);
$totalNum = ComputingPower::getTotalBH($currentMeta['id']);
$curr_period['period_begin'] = strtotime($currentMeta['start_time']);
$curr_period['period_end'] = strtotime($currentMeta['end_time']);
$curr_period['assignable_cec'] = min($currentMeta['cec_pool'],$totalNum);
$curr_period['cec_pool'] = $currentMeta['cec_pool'];
$curr_period['total_exchange_hash_rate'] = $ownerNum;
$curr_period['total_hash_rate'] = $totalNum;
$curr_period['await_time'] = strtotime($currentMeta['end_time']) - myself()->_getNowTime();
$target = \services\NumberService::ceilEx(min($totalNum / $currentMeta['cec_pool'] , 1),6);
if ($totalNum == 0) {
$ratio = 0;
}else{
$ratio = $ownerNum/$totalNum;
}
$reward['cec'] = \services\NumberService::ceilEx($currentMeta['cec_pool'] * $target * $ratio,2);
$reward['point'] = $ownerNum;
}
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']);
}
$crystalDb = Bag::find(ComputingPower::CRYSTAL_NEW);
$info = array(
'period_state' => $period_state,
'curr_period' => $curr_period,
'curr_reward' => $reward,
'last_period' => $last_period,
'listing_state' => LISTING_SWITCH,
'owned_cec' => RewardsCec::getTotalCecNum(),
'owned_total_hash_rate' =>ComputingPower::getMyTotalBH(),
'owned_total_crystal' =>$crystalDb ? $crystalDb['item_num'] : 0,
);
$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);
error_log('exchangeCrystal:'.json_encode($recordDb));
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),
));
}
public function exchangeCrystalInfo(){
$hashRateService = new \services\HashRateService();
$crystalInfo = $hashRateService->getCrystalValue();
$this->_rspData(array(
'crystalInfo' => $crystalInfo
));
}
public function claimCrystal(){
$hashRateService = new \services\HashRateService();
$crystalInfo = $hashRateService->getCrystalValue();
if ($crystalInfo['totalNum'] < 1){
$this->_rspErr(1, 'No crystals available at this time');
return ;
}
foreach ($crystalInfo['playing'] as $val){
if ($val['chaim_num'] < 1){
continue;
}
$hashRateMt = \mt\HashRateTask::find($val['task_id']);
switch ($val['task_id']){
case HashRateTask::RANK_TYPE :{
$chaimUpTotal = $val['chaim_num'] / $hashRateMt['reward'];
HashRateTask::updateKv($val['task_id'],array(
'claim_total' => function () use($chaimUpTotal) {
return "claim_total + ${chaimUpTotal}";
},
'modifytime' => myself()->_getNowTime()
));
}
break;
default : {
if ($val['task_id'] == HashRateTask::BATTLE_TIME_TYPE){
$chaimTotal = $val['chaim_num'] / $hashRateMt['reward'] * $hashRateMt['target'] * 1000;
}else{
$chaimTotal = $val['chaim_num'] / $hashRateMt['reward'] * $hashRateMt['target'];
}
HashRateTask::incDailyReward($val['task_id'],$val['chaim_num'],$chaimTotal);
}
}
}
foreach ($crystalInfo['staking'] as $val){
if ($val['chaim_num'] > 0){
HashRateTask::updateKv($val['task_id'],array(
'claim_day' => 1,
'modifytime' => myself()->_getNowTime()
));
}
}
foreach ($crystalInfo['consuming'] as $val){
if ($val['chaim_num'] > 0){
$hashRateMt = \mt\HashRateTask::find($val['task_id']);
$chaimTotal = $val['chaim_num'] / $hashRateMt['reward'] * $hashRateMt['target'] * \services\HashRateService::EXCHANGE_RATE;
myself()->_incV(TN_TOTAL_DIAMOND_CONSUME,-1,$chaimTotal);
}
}
$items = array(
array(
'item_id' => ComputingPower::CRYSTAL_NEW,
'item_num' => $crystalInfo['totalNum'],
)
);
$propertyChgService = new services\PropertyChgService();
$awardService = new services\AwardService();
$this->_addItems($items,$awardService,$propertyChgService);
CrystalRecord::addCrystalRecord(ComputingPower::CRYSTAL_NEW,$crystalInfo['totalNum']);
$this->_rspData(array(
'award' => $awardService->toDto(),
'property_chg' => $propertyChgService->toDto(),
));
}
public function exchangePowerNew(){
$num = getReqVal('num', 0);
$currentMeta = \mt\HashRateCommon::getCurrentPeriod();
if (!$currentMeta){
$this->_rspErr(1, 'kindly await');
return ;
}
if (!$num){
$this->_rspErr(1, 'exchange none');
return ;
}
//校验是否兑换上限
{
}
$BH = $num;
$costItems = array();
array_push($costItems,array(
'item_id' => ComputingPower::CRYSTAL_NEW,
'item_num' => $num
));
$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' =>$num,
'item_num2' => 0,
'item_num3' => 0,
'item_num4' => 0,
'total_num' => $BH,
)
);
$this->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
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;
}
}
}