-
Notifications
You must be signed in to change notification settings - Fork 391
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 #11377 from notbakaneko/feature/beatmaps-multiple-…
…mappers Support multiple mappers on beatmap difficulties
- Loading branch information
Showing
59 changed files
with
993 additions
and
445 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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. | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Beatmap; | ||
use Illuminate\Console\Command; | ||
|
||
class BeatmapsMigrateOwners extends Command | ||
{ | ||
protected $signature = 'beatmaps:migrate-owners'; | ||
|
||
protected $description = 'Migrates beatmap owners to new table.'; | ||
|
||
public function handle() | ||
{ | ||
$progress = $this->output->createProgressBar(); | ||
|
||
Beatmap::chunkById(1000, function ($beatmaps) use ($progress) { | ||
foreach ($beatmaps as $beatmap) { | ||
$beatmap->beatmapOwners()->firstOrCreate(['user_id' => $beatmap->user_id]); | ||
$progress->advance(); | ||
} | ||
}); | ||
|
||
$progress->finish(); | ||
$this->line(''); | ||
} | ||
} |
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,80 @@ | ||
<?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\Beatmapset; | ||
|
||
use App\Exceptions\InvariantException; | ||
use App\Jobs\Notifications\BeatmapOwnerChange; | ||
use App\Models\Beatmap; | ||
use App\Models\BeatmapOwner; | ||
use App\Models\BeatmapsetEvent; | ||
use App\Models\User; | ||
use Ds\Set; | ||
|
||
class ChangeBeatmapOwners | ||
{ | ||
private Set $userIds; | ||
|
||
public function __construct(private Beatmap $beatmap, array $newUserIds, private User $source) | ||
{ | ||
priv_check_user($source, 'BeatmapUpdateOwner', $beatmap->beatmapset)->ensureCan(); | ||
|
||
$this->userIds = new Set($newUserIds); | ||
|
||
if ($this->userIds->count() > $GLOBALS['cfg']['osu']['beatmaps']['owners_max']) { | ||
throw new InvariantException(osu_trans('beatmaps.change_owner.too_many')); | ||
} | ||
|
||
if ($this->userIds->isEmpty()) { | ||
throw new InvariantException('user_ids must be specified'); | ||
} | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
$currentOwners = new Set($this->beatmap->getOwners()->pluck('user_id')); | ||
if ($currentOwners->xor($this->userIds)->isEmpty()) { | ||
return; | ||
} | ||
|
||
$newUserIds = $this->userIds->diff($currentOwners); | ||
|
||
if (User::whereIn('user_id', $newUserIds->toArray())->default()->count() !== $newUserIds->count()) { | ||
throw new InvariantException('invalid user_id'); | ||
} | ||
|
||
$this->beatmap->getConnection()->transaction(function () { | ||
$params = array_map( | ||
fn ($userId) => ['beatmap_id' => $this->beatmap->getKey(), 'user_id' => $userId], | ||
$this->userIds->toArray() | ||
); | ||
|
||
$this->beatmap->fill(['user_id' => $this->userIds->first()])->saveOrExplode(); | ||
$this->beatmap->beatmapOwners()->delete(); | ||
BeatmapOwner::insert($params); | ||
|
||
$this->beatmap->refresh(); | ||
|
||
$newUsers = $this->beatmap->getOwners()->select('id', 'username')->all(); | ||
$beatmapset = $this->beatmap->beatmapset; | ||
$firstMapper = $newUsers[0]; | ||
|
||
BeatmapsetEvent::log(BeatmapsetEvent::BEATMAP_OWNER_CHANGE, $this->source, $beatmapset, [ | ||
'beatmap_id' => $this->beatmap->getKey(), | ||
'beatmap_version' => $this->beatmap->version, | ||
// TODO: mainly for compatibility during dev when switching branches, can be removed after deployed. | ||
'new_user_id' => $firstMapper['id'], | ||
'new_user_username' => $firstMapper['username'], | ||
'new_users' => $newUsers, | ||
])->saveOrExplode(); | ||
|
||
$beatmapset->update(['eligible_main_rulesets' => null]); | ||
}); | ||
|
||
(new BeatmapOwnerChange($this->beatmap, $this->source))->dispatch(); | ||
} | ||
} |
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,31 @@ | ||
<?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. | ||
|
||
namespace App\Models; | ||
|
||
/** | ||
* @property Beatmap $beatmap | ||
* @property int $beatmap_id | ||
* @property User $user | ||
* @property int $user_id | ||
*/ | ||
class BeatmapOwner extends Model | ||
{ | ||
public $incrementing = false; | ||
public $timestamps = false; | ||
|
||
protected $primaryKey = ':composite'; | ||
protected $primaryKeys = ['beatmap_id', 'user_id']; | ||
|
||
public function beatmap() | ||
{ | ||
return $this->belongsTo(Beatmap::class, 'beatmap_id'); | ||
} | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class, 'user_id'); | ||
} | ||
} |
Oops, something went wrong.