Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read & store score total without mods on submission #11211

Merged
merged 5 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/Models/Solo/Score.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ public static function createFromJsonOrExplode(array $params): static
'maximum_statistics' => $params['maximum_statistics'] ?? [],
'mods' => $params['mods'] ?? [],
'statistics' => $params['statistics'] ?? [],
'total_score_without_mods' => $params['total_score_without_mods'] ?? null,
];
unset(
$params['maximum_statistics'],
$params['mods'],
$params['statistics'],
$params['total_score_without_mods'],
);

$score = new static($params);
Expand Down Expand Up @@ -106,6 +108,7 @@ public static function extractParams(array $rawParams, ScoreToken|MultiplayerSco
'rank:string',
'statistics:array',
'total_score:int',
'total_score_without_mods:int',
]);

$params['maximum_statistics'] ??= [];
Expand Down Expand Up @@ -270,6 +273,14 @@ public function assertCompleted(): void
throw new InvariantException('Invalid total_score.');
}

// unsigned int (no data type enforcement as this goes into the json, but just to match total_score)
if (
$this->data->totalScoreWithoutMods !== null
&& ($this->data->totalScoreWithoutMods < 0 || $this->data->totalScoreWithoutMods > 4294967295)
) {
throw new InvariantException('Invalid total_score_without_mods.');
}

foreach (['max_combo', 'passed'] as $field) {
if (!present($this->$field)) {
throw new InvariantException("field missing: '{$field}'");
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Solo/ScoreData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ScoreData implements Castable, JsonSerializable
public ScoreDataStatistics $maximumStatistics;
public array $mods;
public ScoreDataStatistics $statistics;
public ?int $totalScoreWithoutMods;

public function __construct(array $data)
{
Expand All @@ -39,6 +40,7 @@ public function __construct(array $data)
$this->maximumStatistics = new ScoreDataStatistics($data['maximum_statistics'] ?? []);
$this->mods = $mods;
$this->statistics = new ScoreDataStatistics($data['statistics'] ?? []);
$this->totalScoreWithoutMods = $data['total_score_without_mods'] ?? null;
}

public static function castUsing(array $arguments)
Expand Down Expand Up @@ -67,6 +69,7 @@ public function jsonSerialize(): array
'maximum_statistics' => $this->maximumStatistics,
'mods' => $this->mods,
'statistics' => $this->statistics,
'total_score_without_mods' => $this->totalScoreWithoutMods,
];
}
}
1 change: 1 addition & 0 deletions resources/js/interfaces/solo-score-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface SoloScoreJsonAttributesBase {
started_at: string | null;
statistics: Partial<Record<SoloScoreStatisticsAttribute, number>>;
total_score: number;
total_score_without_mods: number | null;
user_id: number;
}

Expand Down
82 changes: 82 additions & 0 deletions tests/Models/Solo/ScoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Tests\Models\Solo;

use App\Exceptions\InvariantException;
use App\Models\Solo\Score;
use stdClass;
use Tests\TestCase;
Expand Down Expand Up @@ -165,4 +166,85 @@ public function testWeightedPpWithoutWeight(): void

$this->assertNull($score->weightedPp());
}

public function testNegativeTotalScoreIsNotAccepted()
{
$this->expectException(InvariantException::class);
$this->expectExceptionMessage('Invalid total_score.');

Score::createFromJsonOrExplode([
'accuracy' => 1,
'beatmap_id' => 1,
'ended_at' => json_time(now()),
'max_combo' => 100,
'mods' => [],
'passed' => true,
'rank' => 'S',
'ruleset_id' => 1,
'statistics' => ['Great' => 10, 'SmallTickHit' => 1],
'total_score' => -1000,
'user_id' => 1,
]);
}

public function testNegativeAccuracyIsNotAccepted()
{
$this->expectException(InvariantException::class);
$this->expectExceptionMessage('Invalid accuracy.');

Score::createFromJsonOrExplode([
'accuracy' => -1,
'beatmap_id' => 1,
'ended_at' => json_time(now()),
'max_combo' => 100,
'mods' => [],
'passed' => true,
'rank' => 'S',
'ruleset_id' => 1,
'statistics' => ['Great' => 10, 'SmallTickHit' => 1],
'total_score' => 1000,
'user_id' => 1,
]);
}

public function testAccuracyAboveOneIsNotAccepted()
{
$this->expectException(InvariantException::class);
$this->expectExceptionMessage('Invalid accuracy.');

Score::createFromJsonOrExplode([
'accuracy' => 2,
'beatmap_id' => 1,
'ended_at' => json_time(now()),
'max_combo' => 100,
'mods' => [],
'passed' => true,
'rank' => 'S',
'ruleset_id' => 1,
'statistics' => ['Great' => 10, 'SmallTickHit' => 1],
'total_score' => 1000,
'user_id' => 1,
]);
}

public function testNegativeTotalScoreWithoutModsIsNotAccepted()
{
$this->expectException(InvariantException::class);
$this->expectExceptionMessage('Invalid total_score_without_mods.');

Score::createFromJsonOrExplode([
'accuracy' => 1,
'beatmap_id' => 1,
'ended_at' => json_time(now()),
'max_combo' => 100,
'mods' => [],
'passed' => true,
'rank' => 'S',
'ruleset_id' => 1,
'statistics' => ['Great' => 10, 'SmallTickHit' => 1],
'total_score' => 1000,
'total_score_without_mods' => -1000,
'user_id' => 1,
]);
}
}