Skip to content

Commit

Permalink
Merge pull request ppy#11360 from nanaya/percentile-index
Browse files Browse the repository at this point in the history
Fix off by one percentile score
  • Loading branch information
notbakaneko authored Jul 25, 2024
2 parents 7400ab9 + 1735d00 commit 44ef777
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/Models/DailyChallengeUserStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public static function calculate(CarbonImmutable $date): void
$count = $highScores->count();
// these variables are only used if there's anything in the array
if ($count > 0) {
$top50p = $highScores[(int) ($count * 0.5)]->total_score;
$top10p = $highScores[(int) ($count * 0.1)]->total_score;
$top50p = $highScores[max(0, (int) ($count * 0.5) - 1)]->total_score;
$top10p = $highScores[max(0, (int) ($count * 0.1) - 1)]->total_score;
}
$highScoresByUserId = [];
foreach ($highScores as $highScore) {
Expand Down
35 changes: 35 additions & 0 deletions tests/Models/DailyChallengeUserStatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,39 @@ public function testCalculateNoPlaysOverAWeekBreaksWeeklyStreak(): void
$this->assertSame(3, $stats->weekly_streak_best);
$this->assertTrue($lastWeeklyStreak->equalTo($stats->last_weekly_streak));
}

public function testCalculatePercentile(): void
{
$playTime = static::startOfWeek();
$playlistItem = static::preparePlaylistItem($playTime);

$totalScores = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
$scoreLinks = [];
foreach ($totalScores as $totalScore) {
$scoreLink = $scoreLinks[] = ScoreLink::factory()->completed([
'passed' => true,
'total_score' => $totalScore,
])->create([
'playlist_item_id' => $playlistItem,
]);
UserScoreAggregate::new($scoreLink->user, $playlistItem->room)->save();
}

$this->expectCountChange(fn () => DailyChallengeUserStats::count(), 10);

DailyChallengeUserStats::calculate($playTime);

foreach ($scoreLinks as $i => $scoreLink) {
[$count10p, $count50p] = match (true) {
// 100
$i === 9 => [1, 0],
// 60 - 90
$i >= 5 => [0, 1],
default => [0, 0],
};
$stats = DailyChallengeUserStats::find($scoreLink->user_id);
$this->assertSame($count10p, $stats->top_10p_placements, "i: {$i}");
$this->assertSame($count50p, $stats->top_50p_placements, "i: {$i}");
}
}
}

0 comments on commit 44ef777

Please sign in to comment.