feat(比赛): 优化团队排名算法并更新个人排名逻辑

- 新增团队排名算法,根据比赛得分和胜负情况计算排名
- 更新个人排名查询条件,排除 status 为 -1 的参与者
This commit is contained in:
2025-06-01 18:46:12 +08:00
parent 3feb90619c
commit 19bfe5e992

View File

@@ -440,7 +440,34 @@ class Game extends Base
$this->error('比赛不存在');
}
$match = GameMatch::field('*,GREATEST(scoreA,scoreB) as score')->where('game_id', $game['id'])->order(['level' => 'desc', 'score' => 'desc'])->select();
$this->success('Success', $match);
$teams = [];
$rank = 1;
foreach ($match as $m) {
$win = json_decode($m['winner'] ?? '', true);
if (empty($win)) { // 未决出胜负的按暂时得分 高的队伍排名靠前
$teamA = json_decode($m['teamA'] ?? '', true);
$teamB = json_decode($m['teamB'] ?? '', true);
if (($m['scoreA'] >= $m['scoreB'])) {
$teamA['rank'] = $rank;
$teamB['rank'] = $rank + 1;
$teams[] = $teamA;
$teams[] = $teamB;
$rank += 2;
} else {
$teamB['rank'] = $rank;
$teamA['rank'] = $rank + 1;
$teams[] = $teamB;
$teams[] = $teamA;
$rank += 2;
}
} else {
$win['rank'] = $rank;
$rank++;
$teams[] = $win;
}
}
$this->success('Success', $teams);
}
// 个人排名
@@ -452,7 +479,7 @@ class Game extends Base
$this->error('比赛不存在');
}
$list = Participant::where('game_id', $game['id'])->order('score', 'desc')->select();
$list = Participant::where('game_id', $game['id'])->where('status', '>', -1)->order('score', 'desc')->select();
$rank = 1;
foreach ($list as &$l) {
$l['rank'] = $rank;