Skip to content

Commit

Permalink
Merge pull request #10406 from nanaya/playlist-top-update
Browse files Browse the repository at this point in the history
Move playlist item high score functions to its model
  • Loading branch information
notbakaneko authored Aug 1, 2023
2 parents 83ad574 + 6103ce5 commit b0af44f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
22 changes: 22 additions & 0 deletions app/Models/Multiplayer/PlaylistItemUserHighScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,30 @@ class PlaylistItemUserHighScore extends Model

protected $table = 'multiplayer_scores_high';

public static function lookupOrDefault(Score $score): static
{
return static::firstOrNew([
'playlist_item_id' => $score->playlist_item_id,
'user_id' => $score->user_id,
], [
'accuracy' => 0,
'pp' => 0,
'total_score' => 0,
]);
}

public function score()
{
return $this->belongsTo(Score::class);
}

public function updateWithScore(Score $score): void
{
$this->update([
'accuracy' => $score->accuracy,
'pp' => $score->pp,
'score_id' => $score->getKey(),
'total_score' => $score->total_score,
]);
}
}
52 changes: 14 additions & 38 deletions app/Models/Multiplayer/UserScoreAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ class UserScoreAggregate extends Model
];
protected $table = 'multiplayer_rooms_high';

public static function getPlaylistItemUserHighScore(Score $score)
{
return PlaylistItemUserHighScore::firstOrNew([
'playlist_item_id' => $score->playlist_item_id,
'user_id' => $score->user_id,
]);
}

public static function lookupOrDefault(User $user, Room $room): static
{
return static::firstOrNew([
Expand All @@ -65,20 +57,6 @@ public static function lookupOrDefault(User $user, Room $room): static
]);
}

public static function updatePlaylistItemUserHighScore(PlaylistItemUserHighScore $highScore, Score $score)
{
if (!$score->passed) {
return;
}

$highScore->total_score = $score->total_score;
$highScore->accuracy = $score->accuracy;
$highScore->pp = $score->pp;
$highScore->score_id = $score->getKey();

$highScore->save();
}

public static function new(User $user, Room $room): self
{
$obj = static::lookupOrDefault($user, $room);
Expand All @@ -98,11 +76,11 @@ public function addScore(Score $score)
return false;
}

$highestScore = static::getPlaylistItemUserHighScore($score);
$highestScore = PlaylistItemUserHighScore::lookupOrDefault($score);

if ($score->total_score > $highestScore->total_score) {
if ($score->passed && $score->total_score > $highestScore->total_score) {
$this->updateUserTotal($score, $highestScore);
static::updatePlaylistItemUserHighScore($highestScore, $score);
$highestScore->updateWithScore($score);
}

return true;
Expand Down Expand Up @@ -193,21 +171,19 @@ public function userRank()

private function updateUserTotal(Score $current, PlaylistItemUserHighScore $prev)
{
if ($current->passed) {
if ($prev->exists) {
$this->total_score -= $prev->total_score;
$this->accuracy -= $prev->accuracy;
$this->pp -= $prev->pp;
$this->completed--;
}

$this->total_score += $current->total_score;
$this->accuracy += $current->accuracy;
$this->pp += $current->pp;
$this->completed++;
$this->last_score_id = $current->getKey();
if ($prev->exists) {
$this->total_score -= $prev->total_score;
$this->accuracy -= $prev->accuracy;
$this->pp -= $prev->pp;
$this->completed--;
}

$this->total_score += $current->total_score;
$this->accuracy += $current->accuracy;
$this->pp += $current->pp;
$this->completed++;
$this->last_score_id = $current->getKey();

$this->save();
}
}

0 comments on commit b0af44f

Please sign in to comment.