Skip to content

Commit

Permalink
Merge pull request ppy#10763 from nanaya/score-statistics-any
Browse files Browse the repository at this point in the history
Accept any attributes for score statistics
  • Loading branch information
notbakaneko authored Dec 1, 2023
2 parents 6b5322d + 8cfa604 commit 568fb0a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 81 deletions.
8 changes: 4 additions & 4 deletions app/Models/Solo/Score.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function makeLegacyEntry(): LegacyScore\Model
'enabled_mods' => app('mods')->idsToBitset(array_column($data->mods, 'acronym')),
'maxcombo' => $data->maxCombo,
'pass' => $data->passed,
'perfect' => $data->passed && $statistics->miss + $statistics->largeTickMiss === 0,
'perfect' => $data->passed && $statistics->miss + $statistics->large_tick_miss === 0,
'rank' => $data->rank,
'score' => $data->totalScore,
'scorechecksum' => "\0",
Expand All @@ -234,9 +234,9 @@ public function makeLegacyEntry(): LegacyScore\Model
break;
case 'fruits':
$score->count300 = $statistics->great;
$score->count100 = $statistics->largeTickHit;
$score->countkatu = $statistics->smallTickMiss;
$score->count50 = $statistics->smallTickHit;
$score->count100 = $statistics->large_tick_hit;
$score->countkatu = $statistics->small_tick_miss;
$score->count50 = $statistics->small_tick_hit;
break;
case 'mania':
$score->countgeki = $statistics->perfect;
Expand Down
96 changes: 20 additions & 76 deletions app/Models/Solo/ScoreDataStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,103 +7,47 @@

namespace App\Models\Solo;

use JsonSerializable;

class ScoreDataStatistics implements JsonSerializable
class ScoreDataStatistics implements \JsonSerializable
{
public int $good;
public int $great;
public int $ignoreHit;
public int $ignoreMiss;
public int $largeBonus;
public int $largeTickHit;
public int $largeTickMiss;
public int $legacyComboIncrease;
public int $meh;
public int $miss;
public int $ok;
public int $perfect;
public int $smallBonus;
public int $smallTickHit;
public int $smallTickMiss;
public array $attributes = [];

public function __construct($inputData)
{
$inputData = get_arr($inputData) ?? [];

foreach (static::fields() as $field => $map) {
$this->$field = get_int($inputData[$map['json']] ?? $inputData[$map['json_old']] ?? 0) ?? 0;
}
}

private static function fields(): array
{
static $map;
$n = 0;
foreach (get_arr($inputData) ?? [] as $key => $value) {
if ($n >= 32) {
break;
} else {
$n++;
}

if (!isset($map)) {
$map = [];
$fields = [
'good',
'great',
'ignoreHit',
'ignoreMiss',
'largeBonus',
'largeTickHit',
'largeTickMiss',
'legacyComboIncrease',
'meh',
'miss',
'ok',
'perfect',
'smallBonus',
'smallTickHit',
'smallTickMiss',
];
$intValue = get_int($value);

foreach ($fields as $field) {
$map[$field] = [
'json' => snake_case($field),
'json_old' => studly_case($field),
];
if ($intValue !== null && $intValue !== 0) {
$this->attributes[snake_case($key)] = $intValue;
}
}
}

return $map;
public function __get($key)
{
return $this->attributes[$key] ?? 0;
}

public function isEmpty(): bool
{
foreach (static::fields() as $field => $_map) {
if ($this->$field !== 0) {
return false;
}
}

return true;
return empty($this->attributes);
}

public function jsonSerialize(): array
{
$ret = [];

$fields = static::fields();
foreach ($fields as $field => $map) {
$value = $this->$field;

if ($value !== 0) {
$ret[$map['json']] = $value;
}
}

// This shouldn't be needed but it's to guarantee the return has
// at least one thing so php doesn't json encode it as array.
// Using stdClass is an alternative but it's a lot of hacks
// for what shouldn't be possible in the first place (short of
// completely bogus score data).
if (empty($ret)) {
$ret[$fields['miss']['json']] = $this->miss;
}

return $ret;
return $this->isEmpty()
? ['miss' => 0]
: $this->attributes;
}
}
2 changes: 1 addition & 1 deletion tests/Models/Solo/ScoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testStatisticsStoredInCorrectCasing()

$score = $score->fresh();
$this->assertSame(1, json_decode($score->getAttributes()['data'], true)['statistics']['small_tick_hit']);
$this->assertSame(1, $score->data->statistics->smallTickHit);
$this->assertSame(1, $score->data->statistics->small_tick_hit);
}

public function testLegacyPassScoreRetainsRank()
Expand Down

0 comments on commit 568fb0a

Please sign in to comment.