-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into mp-isnew
- Loading branch information
Showing
39 changed files
with
777 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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 App\Libraries\User; | ||
|
||
use App\Exceptions\InvariantException; | ||
use App\Models\Beatmap; | ||
use App\Models\Country; | ||
use App\Models\User; | ||
use App\Models\UserAccountHistory; | ||
|
||
class CountryChange | ||
{ | ||
public static function handle(User $user, string $newCountry, string $reason): void | ||
{ | ||
// Assert valid country acronym | ||
$country = Country::find($newCountry); | ||
if ($country === null) { | ||
throw new InvariantException('invalid country specified'); | ||
} | ||
|
||
if ($user->country_acronym === $newCountry) { | ||
return; | ||
} | ||
|
||
$user->getConnection()->transaction(function () use ($newCountry, $reason, $user) { | ||
$oldCountry = $user->country_acronym; | ||
$user->update(['country_acronym' => $newCountry]); | ||
foreach (Beatmap::MODES as $ruleset => $_rulesetId) { | ||
$user->statistics($ruleset, true)->update(['country_acronym' => $newCountry]); | ||
$user->scoresBest($ruleset, true)->update(['country_acronym' => $newCountry]); | ||
} | ||
UserAccountHistory::addNote($user, "Changing country from {$oldCountry} to {$newCountry} ({$reason})"); | ||
}); | ||
|
||
\Artisan::queue('es:index-scores:queue', [ | ||
'--all' => true, | ||
'--no-interaction' => true, | ||
'--user' => $user->getKey(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?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 App\Libraries\User; | ||
|
||
use App\Models\Tournament; | ||
use App\Models\TournamentRegistration; | ||
use App\Models\User; | ||
use App\Models\UserCountryHistory; | ||
use Carbon\CarbonImmutable; | ||
|
||
class CountryChangeTarget | ||
{ | ||
const MIN_DAYS_MONTH = 15; | ||
|
||
public static function currentMonth(): CarbonImmutable | ||
{ | ||
$now = CarbonImmutable::now(); | ||
$subMonths = $now->day > static::MIN_DAYS_MONTH ? 0 : 1; | ||
|
||
return $now->startOfMonth()->subMonths($subMonths); | ||
} | ||
|
||
public static function get(User $user): ?string | ||
{ | ||
$minMonths = static::minMonths(); | ||
$now = CarbonImmutable::now(); | ||
$until = static::currentMonth(); | ||
$since = $until->subMonths($minMonths - 1); | ||
|
||
if (static::isUserInTournament($user)) { | ||
return null; | ||
} | ||
|
||
$history = $user | ||
->userCountryHistory() | ||
->whereBetween('year_month', [ | ||
UserCountryHistory::formatDate($since), | ||
UserCountryHistory::formatDate($until), | ||
])->whereHas('country') | ||
->get(); | ||
|
||
// First group countries by year_month | ||
$byMonth = []; | ||
foreach ($history as $entry) { | ||
$byMonth[$entry->year_month] ??= []; | ||
$byMonth[$entry->year_month][] = $entry->country_acronym; | ||
} | ||
|
||
// For each year_month, summarise each countries | ||
$byCountry = []; | ||
foreach ($byMonth as $countries) { | ||
$mixed = count($countries) > 1; | ||
foreach ($countries as $country) { | ||
$byCountry[$country] ??= [ | ||
'total' => 0, | ||
'mixed' => 0, | ||
]; | ||
$byCountry[$country]['total']++; | ||
if ($mixed) { | ||
$byCountry[$country]['mixed']++; | ||
} | ||
} | ||
} | ||
|
||
// Finally find the first country which fulfills the requirement | ||
foreach ($byCountry as $country => $data) { | ||
if ($data['total'] === $minMonths && $data['mixed'] <= static::maxMixedMonths()) { | ||
if ($user->country_acronym === $country) { | ||
return null; | ||
} else { | ||
return $country; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static function maxMixedMonths(): int | ||
{ | ||
return config('osu.user.country_change.max_mixed_months'); | ||
} | ||
|
||
public static function minMonths(): int | ||
{ | ||
return config('osu.user.country_change.min_months'); | ||
} | ||
|
||
private static function isUserInTournament(User $user): bool | ||
{ | ||
return TournamentRegistration | ||
::where('user_id', $user->getKey()) | ||
->whereIn( | ||
'tournament_id', | ||
Tournament | ||
::where('end_date', '>', CarbonImmutable::now()) | ||
->orWhereNull('end_date') | ||
->select('tournament_id'), | ||
)->exists(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.