Skip to content

Commit

Permalink
Merge pull request #10484 from nanaya/country-change-variant
Browse files Browse the repository at this point in the history
Update country in ruleset variant statistics table when changing user country
  • Loading branch information
notbakaneko authored Aug 22, 2023
2 parents c472c87 + fa196db commit 5e2e4f5
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 9 deletions.
12 changes: 9 additions & 3 deletions app/Libraries/User/CountryChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ public static function handle(User $user, string $newCountry, string $reason): v

$user->getConnection()->transaction(function () use ($newCountry, $reason, $user) {
$oldCountry = $user->country_acronym;
$user->update(['country_acronym' => $newCountry]);

$newAttrs = ['country_acronym' => $newCountry];
$user->update($newAttrs);
foreach (Beatmap::MODES as $ruleset => $_rulesetId) {
$user->statistics($ruleset, true)->update(['country_acronym' => $newCountry]);
$user->scoresBest($ruleset, true)->update(['country_acronym' => $newCountry]);
$user->statistics($ruleset, true)->update($newAttrs);
$user->scoresBest($ruleset, true)->update($newAttrs);
foreach (Beatmap::VARIANTS[$ruleset] ?? [] as $variant) {
$user->statistics($ruleset, true, $variant)->update($newAttrs);
}
}

UserAccountHistory::addNote($user, "Changing country from {$oldCountry} to {$newCountry} ({$reason})");
});

Expand Down
26 changes: 23 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@
* @property-read Collection<Score\Taiko> $scoresTaiko
* @property-read UserStatistics\Fruits|null $statisticsFruits
* @property-read UserStatistics\Mania|null $statisticsMania
* @property-read UserStatistics\Mania4k|null $statisticsMania4k
* @property-read UserStatistics\Mania7k|null $statisticsMania7k
* @property-read UserStatistics\Osu|null $statisticsOsu
* @property-read UserStatistics\Taiko|null $statisticsTaiko
* @property-read Collection<Store\Address> $storeAddresses
Expand Down Expand Up @@ -918,6 +920,8 @@ public function getAttribute($key)
'scoresTaiko',
'statisticsFruits',
'statisticsMania',
'statisticsMania4k',
'statisticsMania7k',
'statisticsOsu',
'statisticsTaiko',
'storeAddresses',
Expand Down Expand Up @@ -1318,18 +1322,34 @@ public function statisticsMania()
return $this->hasOne(UserStatistics\Mania::class);
}

public function statisticsMania4k()
{
return $this->hasOne(UserStatistics\Mania4k::class);
}

public function statisticsMania7k()
{
return $this->hasOne(UserStatistics\Mania4k::class);
}

public function statisticsTaiko()
{
return $this->hasOne(UserStatistics\Taiko::class);
}

public function statistics(string $mode, bool $returnQuery = false)
public function statistics(string $ruleset, bool $returnQuery = false, ?string $variant = null)
{
if (!Beatmap::isModeValid($mode)) {
if (!Beatmap::isModeValid($ruleset)) {
return;
}

$relation = 'statistics'.studly_case($mode);
if (!Beatmap::isVariantValid($ruleset, $variant)) {
return;
}

$variantSuffix = $variant === null ? '' : "_{$variant}";

$relation = 'statistics'.studly_case("{$ruleset}{$variantSuffix}");

return $returnQuery ? $this->$relation() : $this->$relation;
}
Expand Down
21 changes: 18 additions & 3 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use App\Libraries\Fulfillments\ApplySupporterTag;
use App\Libraries\User\CountryChangeTarget;
use App\Models\Beatmap;
use App\Models\Country;
use App\Models\User;
use App\Models\UserAccountHistory;
Expand Down Expand Up @@ -138,10 +139,24 @@ public function withNote()
return $this->has(UserAccountHistory::factory(), 'accountHistories');
}

public function withPlays(?int $count = null, ?string $mode = 'osu')
public function withPlays(?int $count = null, ?string $ruleset = 'osu'): static
{
return $this->has(UserStatisticsModel::getClass($mode)::factory()->state([
$state = [
'playcount' => $count ?? config('osu.user.min_plays_for_posting'),
]), 'statistics'.studly_case($mode));
];

$ret = $this->has(
UserStatisticsModel::getClass($ruleset)::factory()->state($state),
'statistics'.studly_case($ruleset),
);

foreach (Beatmap::VARIANTS[$ruleset] ?? [] as $variant) {
$ret = $ret->has(
UserStatisticsModel::getClass($ruleset, $variant)::factory()->state($state),
'statistics'.studly_case("{$ruleset}_{$variant}"),
);
}

return $ret;
}
}
15 changes: 15 additions & 0 deletions database/factories/UserStatistics/Mania4kFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace Database\Factories\UserStatistics;

use App\Models\UserStatistics\Mania4k;

class Mania4kFactory extends ModelFactory
{
protected $model = Mania4k::class;
}
15 changes: 15 additions & 0 deletions database/factories/UserStatistics/Mania7kFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace Database\Factories\UserStatistics;

use App\Models\UserStatistics\Mania7k;

class Mania7kFactory extends ModelFactory
{
protected $model = Mania7k::class;
}
12 changes: 12 additions & 0 deletions tests/Libraries/User/CountryChangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

class CountryChangeTest extends TestCase
{
/**
* @group EsSoloScores
*/
public function testDo(): void
{
$user = User::factory();
Expand All @@ -41,10 +44,19 @@ public function testDo(): void
foreach (Beatmap::MODES as $ruleset => $_rulesetId) {
$this->assertSame($user->statistics($ruleset)->country_acronym, $targetCountry);

foreach (Beatmap::VARIANTS[$ruleset] ?? [] as $variant) {
$this->assertSame(
$user->statistics($ruleset, false, $variant)->country_acronym,
$targetCountry,
);
}

foreach ($user->scoresBest($ruleset) as $score) {
$this->assertSame($score->country_acronym, $targetCountry);
}
}

// TODO: add test for solo score country change (in es index)
}

public function testDoInvalidCountry(): void
Expand Down

0 comments on commit 5e2e4f5

Please sign in to comment.