|
2 | 2 |
|
3 | 3 | namespace App\Console\Commands; |
4 | 4 |
|
| 5 | +use App\Models\Event; |
5 | 6 | use App\Models\Fursuit\Fursuit; |
| 7 | +use App\Services\FursuitCatchCode; |
6 | 8 | use Illuminate\Console\Command; |
| 9 | +use Illuminate\Support\Facades\DB; |
7 | 10 |
|
8 | 11 | class FursuitCreateCatchCodeCommand extends Command |
9 | 12 | { |
10 | | - protected $signature = 'fursuit:create-catch-code'; |
| 13 | + protected $signature = 'fursuit:create-catch-code {--purge-all : Purge all existing catch codes before generating new ones}'; |
11 | 14 |
|
12 | | - protected $description = 'Command description'; |
| 15 | + protected $description = 'Generate catch codes for fursuits participating in catch-em-all game'; |
13 | 16 |
|
14 | 17 | // Code which should be run once to generate all missing codes |
15 | 18 | // Further Fursuit Creation/Updates should be caught by an observer |
16 | 19 | // Will fill catch_em_all_code of fursuits who choose to participate |
17 | 20 | public function handle(): void |
18 | 21 | { |
19 | | - $purgeAllCodes = false; // disabled as you might want keep codes (especially if badges are printed) |
20 | | - $purgeCodesOfNonParticipants = false; // disabled as you might want to keep codes if users reenter the system at later time |
21 | | - |
22 | | - // to purge all codes and build new |
23 | | - if ($purgeAllCodes) { |
24 | | - Fursuit::query()->update(['catch_code' => null]); |
| 22 | + $activeEvent = Event::getActiveEvent(); |
| 23 | + |
| 24 | + if (!$activeEvent) { |
| 25 | + $this->error('No active event found.'); |
| 26 | + return; |
25 | 27 | } |
26 | 28 |
|
27 | | - // removing of codes of these who unregistered from the system |
28 | | - if ($purgeCodesOfNonParticipants) { |
29 | | - Fursuit::where('catch_em_all', 0)->update(['catch_code' => null]); |
30 | | - } |
| 29 | + $this->info("Working with active event: {$activeEvent->name}"); |
31 | 30 |
|
32 | | - $counter = 0; |
33 | | - // Iterate and save manually to trigger Fursuit::saving() |
34 | | - foreach (Fursuit::all() as $fursuit) { |
35 | | - if (! $fursuit->catch_em_all || $fursuit->catch_code != null) { |
36 | | - continue; |
| 31 | + DB::transaction(function () use ($activeEvent) { |
| 32 | + // to purge all codes and build new |
| 33 | + if ($this->option('purge-all')) { |
| 34 | + $activeEvent->fursuits()->update(['catch_code' => null]); |
| 35 | + $this->info('All existing catch codes for the current event have been purged.'); |
37 | 36 | } |
38 | 37 |
|
39 | | - $counter++; |
40 | | - $fursuit->save(); |
41 | | - } |
| 38 | + // Get fursuits that need catch codes |
| 39 | + $fursuitsThatNeedCodes = $activeEvent->fursuits() |
| 40 | + ->where('catch_em_all', true) |
| 41 | + ->whereNull('catch_code') |
| 42 | + ->get(); |
| 43 | + |
| 44 | + if ($fursuitsThatNeedCodes->isEmpty()) { |
| 45 | + $this->info('No fursuits need catch codes generated.'); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // Generate catch codes with progress bar |
| 50 | + $progressBar = $this->output->createProgressBar($fursuitsThatNeedCodes->count()); |
| 51 | + $progressBar->setFormat('Generating catch codes: %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%'); |
| 52 | + $progressBar->start(); |
| 53 | + |
| 54 | + $counter = 0; |
| 55 | + foreach ($fursuitsThatNeedCodes as $fursuit) { |
| 56 | + $fursuit->catch_code = $this->generateCatchCode(); |
| 57 | + $fursuit->save(); |
| 58 | + $counter++; |
| 59 | + $progressBar->advance(); |
| 60 | + } |
| 61 | + |
| 62 | + $progressBar->finish(); |
| 63 | + $this->newLine(); |
| 64 | + |
| 65 | + $this->info($counter.' Fursuit Catch Codes created!'); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + private function generateCatchCode(): string |
| 70 | + { |
| 71 | + // Random uppercase 5 letter string that does not already exist, loop until it does not exist |
| 72 | + do { |
| 73 | + // NO 0 or O for readability |
| 74 | + $catch_code = (new FursuitCatchCode(Fursuit::class, 'catch_code'))->generate(); |
| 75 | + } while (Fursuit::where('catch_code', $catch_code)->exists()); |
42 | 76 |
|
43 | | - $this->info($counter.' Fursuit Catch Codes created!'); |
| 77 | + return $catch_code; |
44 | 78 | } |
45 | 79 | } |
0 commit comments