256 lines
10 KiB
PHP
256 lines
10 KiB
PHP
<?php
|
|
use phpcommon\SqlHelper;
|
|
|
|
require_once('services/ServerSwitchService.php');
|
|
|
|
require_once('mt/Parameter.php');
|
|
require_once('mt/CircuitTime.php');
|
|
require_once('mt/CircuitReward.php');
|
|
|
|
require_once('models/CircuitReward.php');
|
|
require_once('models/Circuit.php');
|
|
require_once('models/User.php');
|
|
require_once('models/Hero.php');
|
|
require_once('models/HeroSkin.php');
|
|
|
|
use services\ServerSwitchService;
|
|
use models\CircuitReward;
|
|
use models\Circuit;
|
|
use models\User;
|
|
use models\Hero;
|
|
use models\HeroSkin;
|
|
class OutAppCircuitController extends BaseController {
|
|
private $redis_key_circuit_ranking = 'circuit_ranking';
|
|
private $redis_key_circuit_phase_ranking = 'circuit_phase_ranking';
|
|
|
|
public function getCircuitRanking(){
|
|
$currentCircuitMeta = mt\CircuitTime::getCurrentCircuit();
|
|
if (!$currentCircuitMeta){
|
|
$this->_rspErr(1, 'current stage Have not yet started');
|
|
return ;
|
|
}
|
|
$address = getReqVal('address', '');
|
|
if (empty($address)){
|
|
if (SERVER_ENV == _ONLINE) {
|
|
myself()->_rspErr(1, 'param error');
|
|
return;
|
|
}
|
|
}
|
|
$user = User::findByAddress($address);
|
|
if (SERVER_ENV != _ONLINE) {
|
|
$user = User::find('2_2006_I75DCdyQRllgGRYvT0wcGtPR9gbDUZws');
|
|
}
|
|
$info = array(
|
|
'star_time' => strtotime($currentCircuitMeta['start_time']),
|
|
'end_time' => strtotime($currentCircuitMeta['end_time']),
|
|
'ranking' => 0,
|
|
'score' => 0,
|
|
);
|
|
if (!$user){
|
|
} else {
|
|
$redis = $this->_getRedis($this->redis_key_circuit_ranking);
|
|
if (! $redis->exists(CIRCUIT_RANKING_KEY.$this->redis_key_circuit_ranking)){
|
|
$rows = Circuit::getCircuitList($currentCircuitMeta['circuit_season']);
|
|
$sortRows = myself()->arraySort($rows, 'cumulative_score', 'desc');
|
|
$list = $this->_extractRankingInfo($sortRows);
|
|
$redis->set(CIRCUIT_RANKING_KEY.$this->redis_key_circuit_ranking , json_encode($list));
|
|
$redis->pexpire(CIRCUIT_RANKING_KEY.$this->redis_key_circuit_ranking , 10*60*1000);
|
|
}else{
|
|
$listStr = $redis->get(CIRCUIT_RANKING_KEY.$this->redis_key_circuit_ranking);
|
|
$list = emptyReplace(json_decode($listStr, true), array());
|
|
}
|
|
$myInfo = $this->_celMyRankingInfo($list,$user['account_id']);
|
|
$info = array(
|
|
'star_time' => strtotime($currentCircuitMeta['start_time']),
|
|
'end_time' => strtotime($currentCircuitMeta['end_time']),
|
|
'ranking' => $myInfo['ranking'],
|
|
'score' => $myInfo['score'],
|
|
);
|
|
}
|
|
$this->_rspData(array(
|
|
'rank_list' => $list,
|
|
'info' => $info,
|
|
));
|
|
}
|
|
|
|
public function getCircuitPhaseRanking(){
|
|
$currentCircuitMeta = mt\CircuitTime::getCurrentCircuit();
|
|
if (!$currentCircuitMeta){
|
|
$this->_rspErr(1, 'current stage Have not yet started');
|
|
return ;
|
|
}
|
|
$address = getReqVal('address', '');
|
|
if (empty($address)){
|
|
if (SERVER_ENV == _ONLINE) {
|
|
myself()->_rspErr(1, 'param error');
|
|
return;
|
|
}
|
|
}
|
|
$user = User::findByAddress($address);
|
|
if (SERVER_ENV != _ONLINE) {
|
|
$user = User::find('2_2006_I75DCdyQRllgGRYvT0wcGtPR9gbDUZws');
|
|
}
|
|
if (!$user){
|
|
$info = array(
|
|
'star_time' => strtotime($currentStageMeta['start_time']),
|
|
'end_time' => strtotime($currentStageMeta['end_time']),
|
|
'ranking' => 0,
|
|
'score' => 0
|
|
);
|
|
} else {
|
|
$currentStageMeta = mt\CircuitTime::getCurrentStage();
|
|
if (!$currentStageMeta){
|
|
$currentStageMeta = mt\CircuitTime::getPrevStage();
|
|
}
|
|
$redis = $this->_getRedis($this->redis_key_circuit_phase_ranking);
|
|
if (! $redis->exists(CIRCUIT_RANKING_KEY.$this->redis_key_circuit_phase_ranking)){
|
|
$rows = Circuit::getCircuitPhaseList($currentStageMeta['circuit_season'],$currentStageMeta['circuit_phase']);
|
|
$sortRows = myself()->arraySort($rows, 'cumulative_score', 'desc');
|
|
$list = $this->_extractRankingInfo($sortRows);
|
|
$redis->set(CIRCUIT_RANKING_KEY.$this->redis_key_circuit_phase_ranking , json_encode($list));
|
|
$redis->pexpire(CIRCUIT_RANKING_KEY.$this->redis_key_circuit_phase_ranking , 10*60*1000);
|
|
}else{
|
|
$listStr = $redis->get(CIRCUIT_RANKING_KEY.$this->redis_key_circuit_phase_ranking);
|
|
$list = emptyReplace(json_decode($listStr, true), array());
|
|
}
|
|
$myInfo = $this->_celMyRankingInfo($list,$user['account_id']);
|
|
$info = array(
|
|
'star_time' => strtotime($currentStageMeta['start_time']),
|
|
'end_time' => strtotime($currentStageMeta['end_time']),
|
|
'ranking' => $myInfo['ranking'],
|
|
'score' => $myInfo['score'],
|
|
);
|
|
}
|
|
$this->_rspData(array(
|
|
'rank_list' => $list,
|
|
'info' => $info,
|
|
));
|
|
}
|
|
|
|
|
|
public function getCircuitRewardHistory(){
|
|
$address = getReqVal('address', '');
|
|
if (empty($address)){
|
|
myself()->_rspErr(1, 'param error');
|
|
return;
|
|
}
|
|
$user = User::findByAddress($address);
|
|
if (!$user){
|
|
myself()->_rspErr(1, 'user not found');
|
|
return;
|
|
}
|
|
$data = array();
|
|
$currentCircuitMeta = mt\CircuitTime::getCurrentCircuit();
|
|
if ($currentCircuitMeta){
|
|
$metas = mt\CircuitTime::getListBySeason($currentCircuitMeta['circuit_season']);
|
|
foreach ($metas as $meta){
|
|
$info = array(
|
|
'start_time' => strtotime($meta['start_time']),
|
|
'end_time' => strtotime($meta['end_time']),
|
|
'reward' => -1,
|
|
);
|
|
$rewardDb = CircuitReward::find($address,$currentCircuitMeta['circuit_season'],$meta['circuit_phase']);
|
|
if ($rewardDb){
|
|
$info['reward'] = $rewardDb['reward_num'];
|
|
}
|
|
array_push($data,$info);
|
|
}
|
|
}
|
|
$this->_rspData(array('data' => $data));
|
|
}
|
|
|
|
|
|
public function circuitSettlement(){
|
|
$currentStageMeta = \mt\CircuitTime::getCurrentStage();
|
|
$prevStageMeta = \mt\CircuitTime::getPrevStage();
|
|
|
|
if (!$currentStageMeta &&
|
|
$prevStageMeta &&
|
|
$prevStageMeta['is_reward'] > 0 &&
|
|
!CircuitReward::_verifySettlement($prevStageMeta['circuit_season'],$prevStageMeta['circuit_phase'])){
|
|
$rows = Circuit::getCircuitList($prevStageMeta['circuit_season']);
|
|
$sortRows = myself()->arraySort($rows, 'cumulative_score', 'desc');
|
|
if (count($sortRows) > 0){
|
|
$totalWeight = 0;
|
|
$rate = \mt\Parameter::getVal('circuit_reward_rate','');
|
|
$count = floor(count($sortRows) * $rate) ;
|
|
foreach ($sortRows as $k=>$value){
|
|
$ranking = $k+1;
|
|
$rewardWeight = \mt\CircuitReward::getRewardWeight($ranking);
|
|
if ($ranking <= $count){
|
|
$totalWeight += $rewardWeight;
|
|
}
|
|
}
|
|
$cecPool = $prevStageMeta['cec_pool'];
|
|
foreach ($sortRows as $k=>$value){
|
|
$cec = 0;
|
|
$ranking = $k+1;
|
|
$rewardWeight = \mt\CircuitReward::getRewardWeight($ranking);
|
|
if ($ranking <= $count){
|
|
$cec = max(1, floor(($rewardWeight / $totalWeight) * $cecPool));
|
|
}
|
|
$user = User::find($value['account_id']);
|
|
CircuitReward::add(
|
|
$user['account_id'],
|
|
$user['address'],
|
|
$prevStageMeta['circuit_season'],
|
|
$prevStageMeta['circuit_phase'],
|
|
$ranking,
|
|
$cec
|
|
);
|
|
}
|
|
}
|
|
$this->_rspOk();
|
|
return;
|
|
}
|
|
$this->_rspErr(1,'未满足结算条件');
|
|
}
|
|
|
|
private function _extractRankingInfo($data){
|
|
$rankingList = array();
|
|
$ranking = 0;
|
|
foreach ($data as $k=>$row){
|
|
++$ranking;
|
|
$userDb = User::find($row['account_id']);
|
|
$heroDb = Hero::findByAccountId($userDb['account_id'],$userDb['hero_id']);
|
|
$skinDb = HeroSkin::findByAccountId($heroDb['skin_id'], $userDb['account_id']);
|
|
$heroId = $heroDb ? $heroDb['hero_id']:0;
|
|
$skinId = $skinDb ? $skinDb['skin_id']:0;
|
|
$rewardWeight = \mt\CircuitReward::getRewardWeight($ranking);
|
|
if ($userDb){
|
|
$info = array(
|
|
'account_id' => $userDb['account_id'],
|
|
'name' => utf8_encode($userDb['name']),
|
|
'head_id' => $userDb['head_id'],
|
|
'head_frame' => $userDb['head_frame'],
|
|
'hero_id' => $heroId,
|
|
'skin_id' => $skinId,
|
|
'ranking' => $ranking,
|
|
'weight' => $rewardWeight,
|
|
'score' => $row['cumulative_score'],
|
|
'score_boost' => number_format($row['score_boost'], 2, '.', ''),
|
|
);
|
|
array_push($rankingList,$info);
|
|
}
|
|
}
|
|
return $rankingList;
|
|
}
|
|
|
|
private function _celMyRankingInfo($list,$account){
|
|
$myRankedInfo = array(
|
|
'ranking' => 0,
|
|
'score' => 0,
|
|
);
|
|
if (count($list) > 0){
|
|
foreach ($list as $value){
|
|
if ($value['account_id'] == $account){
|
|
$myRankedInfo['ranking'] = $value['ranking'];
|
|
$myRankedInfo['score'] = $value['score'];
|
|
}
|
|
}
|
|
}
|
|
return $myRankedInfo;
|
|
}
|
|
|
|
}
|