From 98135a55c26ba5d485a0aa08aa4a3d1d42374cd9 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 26 Feb 2024 14:32:44 +0100 Subject: [PATCH] sync ladder schedule --- app/Console/Commands/SyncLadder.php | 82 +++++++++++++++++++++++++++++ app/Console/Kernel.php | 1 + 2 files changed, 83 insertions(+) create mode 100644 app/Console/Commands/SyncLadder.php diff --git a/app/Console/Commands/SyncLadder.php b/app/Console/Commands/SyncLadder.php new file mode 100644 index 0000000..e6f800f --- /dev/null +++ b/app/Console/Commands/SyncLadder.php @@ -0,0 +1,82 @@ +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) { + $ladderHandle = substr($event->external_schedule, 7); + $ladderSchedule = Http::get('https://alttprladder.com/api/v1/PublicApi/GetSchedule', [ + 'season_id' => $ladderHandle, + ])->json(); + foreach ($ladderSchedule as $ladderEntry) { + try { + $this->syncSchedule($event, $ladderEntry); + } catch (Exception $e) { + $this->error('error syncing episode '.$ladderEntry['race_id'].': '.$e->getMessage()); + } + } + } + + private function syncSchedule(Event $event, $ladderEntry) { + $ext_id = 'ladder:'.$ladderEntry['race_id'].':'.$ladderEntry['RaceName']; + $episode = Episode::firstWhere('ext_id', '=', $ext_id); + if (!$episode) { + $episode = new Episode(); + $episode->ext_id = $ext_id; + } + $episode->event()->associate($event); + $episode->title = $ladderEntry['Mode']; + $episode->start = Carbon::createFromFormat('m/d/y h a', $ladderEntry['StartTime'], 'America/Detroit')->setTimezone('UTC'); + $episode->estimate = 2 * 60 * 60; + $episode->confirmed = true; + $episode->save(); + } + +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 4f8719f..ea3807d 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,6 +15,7 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { + $schedule->command('sync:ladder')->daily(); $schedule->command('sync:speedgaming')->everyFiveMinutes(); $schedule->command('sync:sra')->everyFifteenMinutes(); $schedule->command('sync:avatars')->everyFiveMinutes(); -- 2.39.2