Skip to content

Commit

Permalink
Determine highscore rank for equal score by account create date
Browse files Browse the repository at this point in the history
  • Loading branch information
lanedirt committed Jan 1, 2025
1 parent 18f5426 commit 16b98ec
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions app/Console/Commands/GenerateHighscoreRanks.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ private function updateTypeRank(HighscoreTypeEnum $type): void
{
$rank = 1;
$this->info("Updating highscore ranks for $type->name...");
$query = Highscore::query()->whereHas('player.tech')
->where($type->name, '!=', null);
$query->orderBy($type->name, 'DESC');

// Order by the highscore value in descending order, and by the player creation date in ascending order.
// This ensures that:
// - The highest ranked players are at the top of the list.
// - If two players have the same highscore value, the player who joined the game first will be ranked higher.
$query = Highscore::query()
->join('users', 'highscores.player_id', '=', 'users.id')
->whereHas('player.tech')
->where($type->name, '!=', null);
$query->orderBy($type->name, 'DESC')
->orderBy('users.created_at', 'ASC');

$bar = $this->output->createProgressBar();
$bar->start($query->count());

$query->chunk(200, function ($highscores) use ($type, &$bar, &$rank) {
/** @var \Illuminate\Support\Collection<int, Highscore> $highscores */
foreach ($highscores as $highscore) {
Expand Down

0 comments on commit 16b98ec

Please sign in to comment.