Skip to content

Commit

Permalink
Move playlist item high score functions to its model
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nanaya committed Aug 1, 2023
1 parent 405f6f9 commit c2248b1
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 c2248b1

Please sign in to comment.