From c2248b116608678715a098bd1b62668074d7a1e7 Mon Sep 17 00:00:00 2001 From: nanaya Date: Tue, 25 Jul 2023 16:21:30 +0900 Subject: [PATCH] Move playlist item high score functions to its model Also put score pass and total score together instead of two separate locations. Additionally add default values for PlaylistItemUserHighScore because otherwise the new score will be compared with null. --- .../Multiplayer/PlaylistItemUserHighScore.php | 22 ++++++++ app/Models/Multiplayer/UserScoreAggregate.php | 52 +++++-------------- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/app/Models/Multiplayer/PlaylistItemUserHighScore.php b/app/Models/Multiplayer/PlaylistItemUserHighScore.php index 33e2ce1fb90..947130ea4df 100644 --- a/app/Models/Multiplayer/PlaylistItemUserHighScore.php +++ b/app/Models/Multiplayer/PlaylistItemUserHighScore.php @@ -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, + ]); + } } diff --git a/app/Models/Multiplayer/UserScoreAggregate.php b/app/Models/Multiplayer/UserScoreAggregate.php index 38dbe095e19..f0d68ecd52a 100644 --- a/app/Models/Multiplayer/UserScoreAggregate.php +++ b/app/Models/Multiplayer/UserScoreAggregate.php @@ -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([ @@ -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); @@ -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; @@ -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(); } }