]> git.localhorst.tv Git - alttp.git/commitdiff
full stepladder schedule sync command
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 30 Jun 2025 17:05:49 +0000 (19:05 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 30 Jun 2025 17:05:49 +0000 (19:05 +0200)
app/Console/Commands/SyncStepLadderFull.php [new file with mode: 0644]

diff --git a/app/Console/Commands/SyncStepLadderFull.php b/app/Console/Commands/SyncStepLadderFull.php
new file mode 100644 (file)
index 0000000..a81033f
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\Episode;
+use App\Models\Event;
+use App\Models\StepLadderMode;
+use Carbon\Carbon;
+use Illuminate\Console\Command;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Support\Facades\Http;
+
+
+class SyncStepLadderFull extends Command {
+
+       /**
+        * The name and signature of the console command.
+        *
+        * @var string
+        */
+       protected $signature = 'sync:stepladderfull';
+
+       /**
+        * The console command description.
+        *
+        * @var string
+        */
+       protected $description = 'Synchronize complete Step Ladder schedule';
+
+
+       /**
+        * Execute the console command.
+        *
+        * @return int
+        */
+       public function handle() {
+               $events = Event::where('external_schedule', 'LIKE', 'stepladder')
+                       ->where(function (Builder $query) {
+                               $query->whereNull('end');
+                               $query->orWhere('end', '>', now());
+                       })
+                       ->get();
+
+               foreach ($events as $event) {
+                       try {
+                               $this->line('syncing '.$event->name);
+                               $this->syncEvent($event);
+                       } catch (\Exception $e) {
+                               $this->error('error syncing event '.$event->name.': '.$e->getMessage());
+                       }
+               }
+       }
+
+       private function syncEvent(Event $event) {
+               $ladderSchedule = Http::get('https://alttpr.racing/api/v1/schedule')->json();
+               foreach ($ladderSchedule as $ladderEntry) {
+                       try {
+                               $this->syncSchedule($event, $ladderEntry);
+                       } catch (\Exception $e) {
+                               $this->error('error syncing episode '.$ladderEntry['id'].': '.$e->getMessage());
+                       }
+               }
+       }
+
+       private function syncSchedule(Event $event, $ladderEntry) {
+               $ext_id = 'stepladder:'.$ladderEntry['id'];
+               $episode = Episode::firstWhere('ext_id', '=', $ext_id);
+               if (!$episode) {
+                       $episode = new Episode();
+                       $episode->ext_id = $ext_id;
+               }
+               $mode = $this->getMode($ladderEntry);
+               $episode->event()->associate($event);
+               $episode->title = $mode->name;
+               $episode->start = Carbon::createFromFormat('Y-m-d H:i:s', $ladderEntry['time'], 'America/Detroit')->setTimezone('UTC');
+               $episode->estimate = 2 * 60 * 60;
+               $episode->confirmed = true;
+               $episode->save();
+       }
+
+       private function getMode($ladderEntry) {
+               $ext_id = 'stepladder:'.$ladderEntry['mode'];
+               $mode = StepLadderMode::firstWhere('ext_id', '=', $ext_id);
+               if (!$mode) {
+                       $ladderMode = Http::get('https://alttpr.racing/api/v1/modes/'.$ladderEntry['mode'])->json();
+                       $mode = new StepLadderMode();
+                       $mode->ext_id = $ext_id;
+                       $mode->name = $ladderMode['name'];
+                       $mode->last_sync = Carbon::now();
+                       $mode->save();
+               }
+               return $mode;
+       }
+
+}