Skip to content

Commit

Permalink
ensure newest winners are first in AotW API response
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras committed Jan 7, 2025
1 parent a5ee010 commit 88aeb39
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions public/API/API_GetAchievementOfTheWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@

$playerAchievements = $eventAchievement->achievement->playerAchievements()
->with('user')
->orderBy(DB::raw('IFNULL(unlocked_hardcore_at, unlocked_at)'))
->orderByDesc(DB::raw('IFNULL(unlocked_hardcore_at, unlocked_at)')) // newest winners first
->limit(500)
->get();
$numWinners = $playerAchievements->count();
Expand Down Expand Up @@ -135,7 +135,7 @@
$unlocks = $unlocks->filter(fn ($unlock) => Carbon::parse($unlock['DateAwarded'])->gte($startAt));
}

// reverse order so newest winners are last
// sort so newest winners are first
$unlocks->sortByDesc('DateAwarded');
}

Expand Down
25 changes: 16 additions & 9 deletions tests/Feature/Api/V1/AchievementOfTheWeekTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public function testGetAchievementOfTheWeekEventAchievement(): void
/** @var Achievement $achievement2 */
$achievement2 = Achievement::factory()->published()->create(['GameID' => $game->ID]);
$now = Carbon::now();
$time1 = $now->copy()->startOfSecond();
$this->addSoftcoreUnlock($this->user, $achievement1, $time1);
$time2 = $time1->copy()->subMinutes(5);
$time1 = $now->clone()->startOfSecond();
$this->addHardcoreUnlock($this->user, $achievement1, $time1);
$time2 = $time1->clone()->subMinutes(5);
$this->addSoftcoreUnlock($user2, $achievement1, $time2);
$time3 = $time2->copy()->addMinutes(5);
$time3 = $time2->clone()->addMinutes(10);
$this->addHardcoreUnlock($user3, $achievement1, $time3);

$staticData = StaticData::factory()->create([
Expand Down Expand Up @@ -86,7 +86,7 @@ public function testGetAchievementOfTheWeekEventAchievement(): void
'ID' => $game->ID, // source achievement game
],
'StartAt' => $ev->active_from->jsonSerialize(),
'TotalPlayers' => 1, // only the hardcore unlock applies (would normally include people who have unlocked any AotW)
'TotalPlayers' => 2, // only the hardcore unlock applies (event achievements can't technically be unlocked in softcore)
'Unlocks' => [
[
'User' => $user3->User,
Expand All @@ -95,9 +95,16 @@ public function testGetAchievementOfTheWeekEventAchievement(): void
'HardcoreMode' => 1,
'DateAwarded' => $time3->jsonSerialize(),
],
[
'User' => $this->user->User,
'RAPoints' => $this->user->RAPoints,
'RASoftcorePoints' => $this->user->RASoftcorePoints,
'HardcoreMode' => 1,
'DateAwarded' => $time1->jsonSerialize(),
],
],
'UnlocksCount' => 1,
'UnlocksHardcoreCount' => 1,
'UnlocksCount' => 2,
'UnlocksHardcoreCount' => 2,
]);
}

Expand All @@ -113,9 +120,9 @@ public function testGetAchievementOfTheWeekStaticData(): void
$now = Carbon::now();
$time1 = $now->startOfSecond();
$this->addSoftcoreUnlock($this->user, $achievement, $time1);
$time2 = $time1->copy()->subMinutes(5);
$time2 = $time1->clone()->subMinutes(5);
$this->addSoftcoreUnlock($user2, $achievement, $time2);
$time3 = $time2->copy()->addMinutes(10);
$time3 = $time2->clone()->addMinutes(10);
$this->addHardcoreUnlock($user3, $achievement, $time3);

// fallback to static data until AotW data is populated
Expand Down

0 comments on commit 88aeb39

Please sign in to comment.