算力cec结算

This commit is contained in:
hujiabin 2023-08-25 17:53:50 +08:00
parent dd171c7ebf
commit 6f490511a8
10 changed files with 327 additions and 72 deletions

View File

@ -90,21 +90,22 @@ class ComputingPower(object):
['data', _common.ExchangeCrystalNum(), '本周期晶体兑换信息']
]
},
# {
# 'name': 'getRewardHistorys',
# 'desc': '获取奖励历史',
# 'group': 'ComputingPower',
# 'url': 'webapp/index.php?c=ComputingPower&a=getRewardHistorys',
# 'params': [
# _common.ReqHead(),
# ],
# 'response': [
# _common.RspHead(),
# ['info', _common.ComputingPower(), '算力信息']
# ]
# },
{
'name': 'getRewardHistory',
'desc': '获取奖励历史',
'group': 'ComputingPower',
'url': 'webapp/index.php?c=ComputingPower&a=getRewardHistory',
'params': [
_common.ReqHead(),
['star_time', 0, '开始时间,都没传查全部'],
['end_time', 0, '结束时间,都没传查全部'],
],
'response': [
_common.RspHead(),
['!data', [_common.CECRewardHistory()], '算力信息'],
['pending_rewards', '', '剩余总计'],
]
},
#
# {
# 'name': 'getExchangePowerHistorys',

View File

@ -1400,6 +1400,21 @@ class ExchangeCrystalNum(object):
['num4', 0, '本期晶体260004兑换数'],
]
class CECRewardHistory(object):
def __init__(self):
self.fields = [
['hash_rate', 0, '算力数量'],
['cec_num', 0, 'cec数量'],
['state', 0, '领取状态 1全部领取 0反之'],
['week1', 0, '1已领取 反之则是发放的时间戳'],
['week2', 0, '1已领取 反之则是发放的时间戳'],
['week3', 0, '1已领取 反之则是发放的时间戳'],
['week4', 0, '1已领取 反之则是发放的时间戳'],
['create_time', 0, '结算时间'],
['pending_num', 0, '剩余未领取的cec数量'],
]
class Staking(object):
def __init__(self):

View File

@ -8,7 +8,7 @@ function add(name) {
function init() {
add('season');
//add('dailyTask');
//add('hourlyTask');
add('hourlyTask');
add('rankings');
}

View File

@ -8,15 +8,26 @@ class HourlyTask {
async start() {
console.log('HourlyTask Start ... ');
while (true) {
await this.doComputingPower()
await this.doRewardsCEC()
// await this.doComputingPower()
const now = new Date();
// 计算距离下一个整点的秒数
const sleepTime = 60 * 60 - (now.getMinutes() * 60 + now.getSeconds()) ;
const sleepTime = 60 * 60 - (now.getMinutes() * 60 + now.getSeconds()) + 60;
console.log('HourlyTask.sleepTime:' + sleepTime, new Date(), sleepTime /60);
await utils.sleep(sleepTime * 1000);
}
}
async doRewardsCEC(){
let url = '';
if (utils.isOnlineEnv()) {
url = 'https://game2006api-test.cebggame.com/webapp/index.php?&c=DailyRequest&a=computingPowerRewards';
} else {
url = 'https://game2006api-test.kingsome.cn/webapp/index.php?&c=DailyRequest&a=computingPowerRewards';
}
await http.get(url,null)
}
async doComputingPower(){
try {
const {err, conn} = await app.getDbConn("GameDb20060");

View File

@ -8,9 +8,11 @@ require_once('services/PropertyChgService.php');
require_once('models/ComputingPower.php');
require_once('models/CrystalRecord.php');
require_once('models/RewardsCec.php');
use models\ComputingPower;
use models\CrystalRecord;
use models\RewardsCec;
use mt\HashRate;
use phpcommon\SqlHelper;
class ComputingPowerController extends BaseAuthedController
@ -34,6 +36,7 @@ class ComputingPowerController extends BaseAuthedController
"period_end" => 0,
"reward" => 0,
);
$lastMeta = \mt\HashRateCommon::getLastPeriod();
$currentMeta = \mt\HashRateCommon::getCurrentPeriod();
$nextMeta = \mt\HashRateCommon::getNextPeriod();
@ -60,8 +63,8 @@ class ComputingPowerController extends BaseAuthedController
$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'];
// $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;
}
@ -69,6 +72,8 @@ class ComputingPowerController extends BaseAuthedController
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['reward_cec'];
}
@ -77,7 +82,7 @@ class ComputingPowerController extends BaseAuthedController
'curr_period' => $curr_period,
'last_period' => $last_period,
'listing_state' => LISTING_SWITCH,
'owned_cec' => 0,
'owned_cec' => RewardsCec::getTotalCecNum(),
);
$this->_rspData(array(
'info' => $info
@ -123,12 +128,13 @@ class ComputingPowerController extends BaseAuthedController
break;
case ComputingPower::CRYSTAL2 : {
$consumeCost = myself()->_getV(TN_TOTAL_DIAMOND_CONSUME,0);
$crystalNum = max(0,floor($consumeCost/$param[0]));
$crystalNum = max(0,floor($consumeCost/ \services\HashRateService::EXCHANGE_RATE/$param[0]));
}
break;
case ComputingPower::CRYSTAL3 : {
$consumeCost = myself()->_getV(TN_TOTAL_CEG_CONSUME,0);
$crystalNum = max(0,floor($consumeCost/$param[0]));
$crystalNum = max(0,floor($consumeCost/ \services\HashRateService::EXCHANGE_RATE/$param[0]));
}
}
if ($crystalNum < 1){
@ -279,7 +285,24 @@ class ComputingPowerController extends BaseAuthedController
}
public function _verificationCeiling($period,$meta,$num1,$num2,$num3,$num4){
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 : {

View File

@ -6,15 +6,20 @@ require_once('mt/StarLevel.php');
require_once('mt/BattlePass.php');
require_once('mt/Rank.php');
require_once('mt/RankSeason.php');
require_once('mt/HashRateCommon.php');
require_once('models/RealtimeData.php');
require_once('models/Season.php');
require_once('models/RankingSetRecord.php');
require_once('models/ComputingPower.php');
require_once('models/RewardsCec.php');
use phpcommon\SqlHelper;
use models\RealtimeData;
use models\Season;
use models\RankingSetRecord;
use models\ComputingPower;
use models\RewardsCec;
class DailyRequestController extends BaseAuthedController {
private $awardService = null;
@ -142,4 +147,25 @@ class DailyRequestController extends BaseAuthedController {
$this->_rspOk();
}
/**
* 算力周期cec计算
*/
public function computingPowerRewards(){
$currentMeta = \mt\HashRateCommon::getLatestPeriod();
if (\mt\HashRateCommon::checkAwaitTime()){
$temp = ComputingPower::getAccountGroup($currentMeta['id']);
if (! $temp){
error_log("computingPowerRewards : No one involved");
die;
}
foreach ($temp as $row){
if ( ! RewardsCec::findByAccount($row['account_id'],$currentMeta['id'])){
RewardsCec::celCecReward($row['account_id'],$currentMeta);
error_log("computingPowerRewards : ACCOUNT_ID_{$row['account_id']} CEC Reward record completion");
}
}
}
$this->_rspOk();
}
}

View File

@ -13,12 +13,15 @@ class ComputingPower extends BaseModel
const CRYSTAL4 = 260004;
//获取我的算力
public static function getOwnedBH($period){
public static function getOwnedBH($period,$account = null){
if (!$account){
$account = myself()->_getAccountId();
}
$rows = SqlHelper::ormSelect(
myself()->_getSelfMysql(),
myself()->_getMysql($account),
't_power_exchange_record',
array(
'account_id'=>myself()->_getAccountId(),
'account_id'=>$account,
'period' => $period
)
);
@ -33,17 +36,24 @@ class ComputingPower extends BaseModel
//获取全服总算力
public static function getTotalBH($period){
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_computing_power_period',
$rows = SqlHelper::select(
myself()->_getMysql(''),
't_power_exchange_record',
array(
"period",
"total_num"
),
array(
'period' => $period
)
);
if (!$row){
$row = null;
$total_num = 0;
if ($rows){
foreach ($rows as $row){
$total_num += $row['total_num'];
}
}
return $row;
return $total_num;
}
public static function findByPeriod($period){
@ -91,5 +101,11 @@ class ComputingPower extends BaseModel
);
}
public static function getAccountGroup($period){
$sql = "select account_id,`period` from t_power_exchange_record where `period` = $period group by account_id";
$rows = myself()->_getMysql('')->execQuery($sql);
return $rows;
}
}

View File

@ -0,0 +1,137 @@
<?php
namespace models;
use mt;
use phpcommon\SqlHelper;
use phpcommon;
class RewardsCec extends BaseModel
{
public static function getTotalCecNum(){
$rows = SqlHelper::ormSelect(
myself()->_getSelfMysql(),
't_hash_rate_reward',
array(
'account_id'=>myself()->_getAccountId(),
)
);
$totalCecNum = 0;
if ($rows){
foreach ($rows as $row){
$totalCecNum += $row['reward_cec'];
}
}
return $totalCecNum;
}
public static function getHistoryByTime($starTime,$endTime){
if ($starTime || $endTime){
$sql = "select * from t_hash_rate_reward where account_id=:account and createtime>:starTime and createtime<:endTime";
$rows = myself()->_getSelfMysql()->execQuery(
$sql,
array(
'account' => myself()->_getAccountId(),
'starTime' => $starTime,
'endTime' => $endTime,
)
);
}else{
$sql = "select * from t_hash_rate_reward where account_id=:account";
$rows = myself()->_getSelfMysql()->execQuery(
$sql,
array(
'account' => myself()->_getAccountId(),
)
);
}
if (!$rows){
$rows =array();
}
return $rows;
}
public static function toDto($row){
if (!$row){
return;
}
$week1 = phpcommon\getMondaySeconds($row['createtime']);
$week2 = $week1 + 60*60*24*7 ;
$week3 = $week1 + 60*60*24*14 ;
$week4 = $week1 + 60*60*24*21 ;
$state = $row['reward1'] && $row['reward2'] && $row['reward3'] && $row['reward4'] ? 1 : 0;
$pendingNum = $row['reward_cec'];
if ($row['reward1']){
$pendingNum -= $pendingNum*0.25;
}elseif ($row['reward2']){
$pendingNum -= $pendingNum*0.5;
}elseif ($row['reward3']){
$pendingNum -= $pendingNum*0.75;
}elseif ($row['reward4']){
$pendingNum = 0;
}
return array(
'hash_rate' => $row['power'],
'cec_num' => $row['reward_cec'],
'state' => $state,
'week1' => $row['reward1'] ? $row['reward1'] : $week1,
'week2' => $row['reward2'] ? $row['reward2'] : $week2,
'week3' => $row['reward3'] ? $row['reward3'] : $week3,
'week4' => $row['reward4'] ? $row['reward4'] : $week4,
'create_time' => $row['createtime'],
'pending_num' => strval($pendingNum),
);
}
public static function findByAccount($account,$period){
$row = SqlHelper::ormSelectOne(
myself()->_getMysql($account),
't_hash_rate_reward',
array(
'account_id'=>$account,
'period' => $period
)
);
if (!$row){
$row = null;
}
return $row;
}
public static function celCecReward($account,$hashMeta){
$ownerNum = ComputingPower::getOwnedBH($hashMeta['id'],$account); //13.7
$totalNum = ComputingPower::getTotalBH($hashMeta['id']) + $hashMeta['hashrate_add']; //25.9
$target = min($totalNum / $hashMeta['target'] , 1); // 25.9/10000
$ratio = $ownerNum/$totalNum; // 13.7/25.9
$cecNum = $hashMeta['cec_pool'] * $target * $ratio; //10000*(25.9/10000)*(13.7/25.9)
$address = User::findByAddress($account);
SqlHelper::upsert(
myself()->_getMysql($account),
't_hash_rate_reward',
array(
'account_id'=>$account,
'period'=>$hashMeta['id'],
),
array(
),
array(
'account_id'=>$account,
'address'=>$address,
'period'=>$hashMeta['id'],
'power'=>$ownerNum,
'reward_cec' => $cecNum,
'reward1' => 0,
'reward2' => 0,
'reward3' => 0,
'reward4' => 0,
'createtime'=>myself()->_getNowTime(),
'modifytime'=>myself()->_getNowTime(),
)
);
}
}

View File

@ -10,6 +10,25 @@ class HashRateCommon {
return getXVal(self::getMetaList(), $id, null);
}
public static function getLatestPeriod(){
$period = self::getMetaList() ? self::getMetaList()[1] : array();
foreach (self::getMetaList() as $key => $meta) {
if (myself()->_getNowTime() >= strtotime($meta['start_time'])) {
$period = $meta;
}
}
return $period;
}
public static function checkAwaitTime(){
$period = self::getLatestPeriod();
// print_r(myself()->_getNowTime() . "======". strtotime($period['end_time']));
if ($period && myself()->_getNowTime() > strtotime($period['end_time'])){
return true;
}
return false;
}
public static function getCurrentPeriod()
{
foreach (self::getMetaList() as $meta) {
@ -23,34 +42,25 @@ class HashRateCommon {
public static function getNextPeriod()
{
$metaList = self::getMetaList();
foreach ($metaList as $key => $meta) {
if ($key == 1 && myself()->_getNowTime() <= strtotime($meta['start_time'])) {
return $meta;
}
if ($key > 1 &&
myself()->_getNowTime() >= strtotime($metaList[$key-1]['end_time']) &&
myself()->_getNowTime() <= strtotime($metaList[$key]['start_time'])) {
return $meta;
$next = array();
foreach (self::getMetaList() as $meta){
if (myself()->_getNowTime() < strtotime($meta['start_time'])){
$next = $meta;
break;
}
}
return array();
return $next;
}
public static function getLastPeriod()
{
$metaList = self::getMetaList();
$count = count($metaList);
foreach ($metaList as $key => $meta) {
if (myself()->_getNowTime() >= strtotime($metaList[$key]['end_time']) &&
myself()->_getNowTime() <= strtotime($metaList[$key+1]['start_time'])) {
return $meta;
}
if ($key == $count && myself()->_getNowTime() >= strtotime($meta['end_time'])) {
return $meta;
$last = array();
foreach (self::getMetaList() as $meta){
if (myself()->_getNowTime() > strtotime($meta['end_time'])){
$last = $meta;
}
}
return array();
return $last;
}

View File

@ -21,17 +21,25 @@ class RankSeason
public static function getLastSeason()
{
$metaList = self::getMetaList();
$count = count($metaList);
foreach ($metaList as $key => $meta) {
if ($key == $count && myself()->_getNowTime() >= strtotime($meta['end_time'])) {
return $meta;
}elseif (myself()->_getNowTime() >= strtotime($metaList[$key]['end_time']) &&
myself()->_getNowTime() <= strtotime($metaList[$key+1]['start_time'])) {
return $meta;
// $metaList = self::getMetaList();
// $count = count($metaList);
// foreach ($metaList as $key => $meta) {
// if ($key == $count && myself()->_getNowTime() >= strtotime($meta['end_time'])) {
// return $meta;
// }elseif (myself()->_getNowTime() >= strtotime($metaList[$key]['end_time']) &&
// myself()->_getNowTime() <= strtotime($metaList[$key+1]['start_time'])) {
// return $meta;
// }
// }
// return array();
$lastSeason = array();
foreach (self::getMetaList() as $meta){
if (myself()->_getNowTime() > strtotime($meta['end_time'])){
$lastSeason = $meta;
}
}
return array();
return $lastSeason;
}
public static function getCurrentSeason()
@ -47,18 +55,26 @@ class RankSeason
public static function getNextCurrentSeason()
{
$metaList = self::getMetaList();
foreach ($metaList as $key => $meta) {
if ($key == 1 && myself()->_getNowTime() <= strtotime($meta['start_time'])) {
return $meta;
}
if ($key > 1 &&
myself()->_getNowTime() >= strtotime($metaList[$key-1]['end_time']) &&
myself()->_getNowTime() <= strtotime($metaList[$key]['start_time'])) {
return $meta;
// $metaList = self::getMetaList();
// foreach ($metaList as $key => $meta) {
// if ($key == 1 && myself()->_getNowTime() <= strtotime($meta['start_time'])) {
// return $meta;
// }
// if ($key > 1 &&
// myself()->_getNowTime() >= strtotime($metaList[$key-1]['end_time']) &&
// myself()->_getNowTime() <= strtotime($metaList[$key]['start_time'])) {
// return $meta;
// }
// }
// return null;
$next = array();
foreach (self::getMetaList() as $meta){
if (myself()->_getNowTime() < strtotime($meta['start_time'])){
$next = $meta;
break;
}
}
return null;
return $next;
}
public static function getSeasonByTime($time)