From: Daniel Karbach Date: Mon, 26 Feb 2024 13:32:44 +0000 (+0100) Subject: sync ladder schedule X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;ds=sidebyside;h=98135a55c26ba5d485a0aa08aa4a3d1d42374cd9;hp=e1ecc76c1c6d527502d6576ee19be06df2a15bb7;p=alttp.git sync ladder schedule --- 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();