Skip to content

Commit 1be0af4

Browse files
committed
add regen catch codes command
1 parent 5460733 commit 1be0af4

1 file changed

Lines changed: 55 additions & 21 deletions

File tree

app/Console/Commands/FursuitCreateCatchCodeCommand.php

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,78 @@
22

33
namespace App\Console\Commands;
44

5+
use App\Models\Event;
56
use App\Models\Fursuit\Fursuit;
7+
use App\Services\FursuitCatchCode;
68
use Illuminate\Console\Command;
9+
use Illuminate\Support\Facades\DB;
710

811
class FursuitCreateCatchCodeCommand extends Command
912
{
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}';
1114

12-
protected $description = 'Command description';
15+
protected $description = 'Generate catch codes for fursuits participating in catch-em-all game';
1316

1417
// Code which should be run once to generate all missing codes
1518
// Further Fursuit Creation/Updates should be caught by an observer
1619
// Will fill catch_em_all_code of fursuits who choose to participate
1720
public function handle(): void
1821
{
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;
2527
}
2628

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}");
3130

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.');
3736
}
3837

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());
4276

43-
$this->info($counter.' Fursuit Catch Codes created!');
77+
return $catch_code;
4478
}
4579
}

0 commit comments

Comments
 (0)