1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Console\Commands;
7
8use App\Models\BeatmapsetEvent;
9use App\Models\BeatmapsetNomination;
10use Illuminate\Console\Command;
11use Illuminate\Database\QueryException;
12use Log;
13
14class BeatmapsetNominationSyncCommand extends Command
15{
16 protected $signature = 'beatmapset:nomination-sync';
17 protected $description = 'Migrates nomination-related BeatmapsetEvents to BeatmapsetNomination.';
18
19 public function handle()
20 {
21 $progress = $this->output->createProgressBar();
22 $max = BeatmapsetEvent::max('id');
23
24 BeatmapsetEvent::where('id', '<=', $max)->whereIn('type', [BeatmapsetEvent::NOMINATE, BeatmapsetEvent::NOMINATION_RESET, BeatmapsetEvent::DISQUALIFY])
25 ->with('beatmapset')
26 ->chunkById(1000, function ($chunk) use ($progress) {
27 /** @var BeatmapsetEvent $event */
28 foreach ($chunk as $event) {
29 switch ($event->type) {
30 case BeatmapsetEvent::NOMINATE:
31 try {
32 Log::debug('nominate', ['beatmapset_id' => $event->beatmapset_id, 'user_id' => $event->user_id]);
33 BeatmapsetNomination::create([
34 'beatmapset_id' => $event->beatmapset_id,
35 'created_at' => $event->created_at,
36 'event_id' => $event->getKey(),
37 'modes' => $event->comment['modes'] ?? null,
38 'user_id' => $event->user_id,
39 ]);
40 } catch (QueryException $e) {
41 if (!is_sql_unique_exception($e)) {
42 throw $e;
43 }
44
45 Log::debug('nominate already exists', ['beatmapset_id' => $event->beatmapset_id, 'user_id' => $event->user_id]);
46 }
47 break;
48 case BeatmapsetEvent::DISQUALIFY:
49 case BeatmapsetEvent::NOMINATION_RESET:
50 Log::debug('nomination reset', ['beatmapset_id' => $event->beatmapset_id, 'user_id' => $event->user_id]);
51 BeatmapsetNomination::current()
52 ->where('beatmapset_id', $event->beatmapset_id)
53 ->where('event_id', '<', $event->getKey())
54 ->update([
55 'reset' => true,
56 'reset_at' => $event->created_at,
57 'reset_user_id' => $event->user_id,
58 ]);
59 break;
60 }
61
62 $progress->advance();
63 }
64 });
65
66 $progress->finish();
67 $this->line('');
68 }
69}