game2006api/webapp/controller/ComputingPowerController.class.php
2023-08-23 04:15:52 +08:00

262 lines
9.3 KiB
PHP

<?php
require_once('mt/HashRate.php');
require_once('mt/HashRateCommon.php');
require_once('mt/Item.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');
use models\ComputingPower;
use models\CrystalRecord;
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'] = ( $rowDb ? $rowDb['total_num'] : 0 ) + $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']);
}
$info = array(
'period_state' => $period_state,
'curr_period' => $curr_period,
'last_period' => $last_period,
'listing_state' => LISTING_SWITCH,
'owned_cec' => 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);
$itemNum = getReqVal('item_num', 0);
$itemMeta = \mt\Item::get($itemId);
if (!$itemMeta || $itemMeta['type'] != \mt\Item::CRYSTAL_TYPE || !$itemNum){
$this->_rspErr(1, 'param error');
return ;
}
$items = array(
array(
'item_id' => $itemId,
'item_num' => $itemNum,
)
);
$propertyChgService = new services\PropertyChgService();
$awardService = new services\AwardService();
$this->_addItems($items,$awardService,$propertyChgService);
CrystalRecord::addCrystalRecord($itemId,$itemNum);
$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 _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;
}
}
}