3 namespace App\Console\Commands;
5 use App\Models\Episode;
8 use Illuminate\Console\Command;
9 use Illuminate\Database\Eloquent\Builder;
10 use Illuminate\Support\Facades\Http;
13 class SyncLadder extends Command {
16 * The name and signature of the console command.
20 protected $signature = 'sync:ladder';
23 * The console command description.
27 protected $description = 'Synchronize Ladder schedule';
31 * Execute the console command.
35 public function handle() {
36 $events = Event::where('external_schedule', 'LIKE', 'ladder:%')
37 ->where(function (Builder $query) {
38 $query->whereNull('end');
39 $query->orWhere('end', '>', now());
43 foreach ($events as $event) {
45 $this->line('syncing '.$event->name);
46 $this->syncEvent($event);
47 } catch (\Exception $e) {
48 $this->error('error syncing event '.$event->name.': '.$e->getMessage());
53 private function syncEvent(Event $event) {
54 $ladderHandle = substr($event->external_schedule, 7);
55 $ladderSchedule = Http::get('https://alttprladder.com/api/v1/PublicApi/GetSchedule', [
56 'season_id' => $ladderHandle,
58 foreach ($ladderSchedule as $ladderEntry) {
60 $this->syncSchedule($event, $ladderEntry);
61 } catch (Exception $e) {
62 $this->error('error syncing episode '.$ladderEntry['race_id'].': '.$e->getMessage());
67 private function syncSchedule(Event $event, $ladderEntry) {
68 $ext_id = 'ladder:'.$ladderEntry['race_id'].':'.$ladderEntry['RaceName'];
69 $episode = Episode::firstWhere('ext_id', '=', $ext_id);
71 $episode = new Episode();
72 $episode->ext_id = $ext_id;
74 $episode->event()->associate($event);
75 $episode->title = $ladderEntry['Mode'];
76 $episode->start = Carbon::createFromFormat('m/d/y h a', $ladderEntry['StartTime'], 'America/Detroit')->setTimezone('UTC');
77 $episode->estimate = 2 * 60 * 60;
78 $episode->confirmed = true;