-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11768 from venix12/seasons-score-calculation
Add user season score calculation workflow
- Loading branch information
Showing
13 changed files
with
524 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?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\Console\Commands; | ||
|
||
use App\Models\Multiplayer\UserScoreAggregate; | ||
use App\Models\Season; | ||
use App\Models\User; | ||
use Illuminate\Console\Command; | ||
|
||
class UserSeasonScoresRecalculate extends Command | ||
{ | ||
protected $signature = 'user-season-scores:recalculate {--season-id=}'; | ||
protected $description = 'Recalculate user scores for all active seasons or a specified season.'; | ||
|
||
public function handle(): void | ||
{ | ||
$seasonId = $this->option('season-id'); | ||
|
||
if (present($seasonId)) { | ||
$this->recalculate(Season::findOrFail(get_int($seasonId))); | ||
} else { | ||
$activeSeasons = Season::active()->get(); | ||
|
||
foreach ($activeSeasons as $season) { | ||
$this->recalculate($season); | ||
} | ||
} | ||
} | ||
|
||
protected function recalculate(Season $season): void | ||
{ | ||
$scoreUserIds = UserScoreAggregate::whereIn('room_id', $season->rooms->pluck('id')) | ||
->distinct('user_id') | ||
->pluck('user_id'); | ||
|
||
$bar = $this->output->createProgressBar($scoreUserIds->count()); | ||
|
||
User::whereIn('user_id', $scoreUserIds) | ||
->chunkById(100, function ($userChunk) use ($bar, $season) { | ||
foreach ($userChunk as $user) { | ||
$seasonScore = $user->seasonScores() | ||
->where('season_id', $season->getKey()) | ||
->firstOrNew(); | ||
|
||
$seasonScore->season()->associate($season); | ||
$seasonScore->calculate(false); | ||
if ($seasonScore->total_score > 0) { | ||
$seasonScore->save(); | ||
} | ||
|
||
$bar->advance(); | ||
} | ||
}); | ||
|
||
$bar->finish(); | ||
$this->newLine(); | ||
} | ||
} |
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,69 @@ | ||
<?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\Models; | ||
|
||
use App\Exceptions\InvariantException; | ||
use App\Models\Multiplayer\UserScoreAggregate; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
/** | ||
* @property-read Season $season | ||
* @property int $season_id | ||
* @property float $total_score | ||
* @property int $user_id | ||
*/ | ||
class UserSeasonScoreAggregate extends Model | ||
{ | ||
public $incrementing = false; | ||
public $timestamps = false; | ||
|
||
protected $primaryKey = ':composite'; | ||
protected $primaryKeys = ['user_id', 'season_id']; | ||
|
||
public function calculate(bool $muteExceptions = true): void | ||
{ | ||
$seasonRooms = SeasonRoom::where('season_id', $this->season->getKey())->get(); | ||
$userScores = UserScoreAggregate::whereIn('room_id', $seasonRooms->pluck('room_id')) | ||
->where('user_id', $this->user_id) | ||
->get(); | ||
|
||
$factors = $this->season->score_factors ?? []; | ||
$roomGroupCount = $seasonRooms->groupBy('group_indicator')->count(); | ||
|
||
if ($roomGroupCount > count($factors)) { | ||
// don't interrupt Room::completePlay() and throw exception only for recalculation command | ||
if ($muteExceptions) { | ||
return; | ||
} else { | ||
throw new InvariantException(osu_trans('rankings.seasons.validation.not_enough_factors')); | ||
} | ||
} | ||
|
||
$roomsById = $seasonRooms->keyBy('room_id'); | ||
$scores = []; | ||
foreach ($userScores as $score) { | ||
$group = $roomsById[$score->room_id]->group_indicator; | ||
$scores[$group] = max($scores[$group] ?? 0, $score->total_score); | ||
} | ||
|
||
rsort($factors); | ||
rsort($scores); | ||
|
||
$total = 0; | ||
foreach ($scores as $index => $score) { | ||
$total += $score * $factors[$index]; | ||
} | ||
|
||
$this->total_score = $total; | ||
} | ||
|
||
public function season(): BelongsTo | ||
{ | ||
return $this->belongsTo(Season::class); | ||
} | ||
} |
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,25 @@ | ||
<?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; | ||
|
||
use App\Models\Multiplayer\Room; | ||
use App\Models\Season; | ||
use App\Models\SeasonRoom; | ||
|
||
class SeasonRoomFactory extends Factory | ||
{ | ||
protected $model = SeasonRoom::class; | ||
|
||
public function definition(): array | ||
{ | ||
return [ | ||
'room_id' => Room::factory(), | ||
'season_id' => Season::factory(), | ||
]; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
database/migrations/2025_01_22_134746_add_unique_index_to_room_id_on_season_rooms.php
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,33 @@ | ||
<?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); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('season_rooms', function (Blueprint $table) { | ||
$table->unique('room_id'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('season_rooms', function (Blueprint $table) { | ||
$table->dropUnique(['room_id']); | ||
}); | ||
} | ||
}; |
33 changes: 33 additions & 0 deletions
33
database/migrations/2025_01_22_172229_add_score_factors_on_seasons.php
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,33 @@ | ||
<?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); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('seasons', function (Blueprint $table) { | ||
$table->json('score_factors')->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('seasons', function (Blueprint $table) { | ||
$table->dropColumn('score_factors'); | ||
}); | ||
} | ||
}; |
33 changes: 33 additions & 0 deletions
33
database/migrations/2025_01_22_174650_add_group_indicator_on_season_rooms.php
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,33 @@ | ||
<?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); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('season_rooms', function (Blueprint $table) { | ||
$table->string('group_indicator')->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('season_rooms', function (Blueprint $table) { | ||
$table->dropColumn('group_indicator'); | ||
}); | ||
} | ||
}; |
Oops, something went wrong.