]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/SyncLadder.php
add myustery circus link
[alttp.git] / app / Console / Commands / SyncLadder.php
1 <?php
2
3 namespace App\Console\Commands;
4
5 use App\Models\Episode;
6 use App\Models\Event;
7 use Carbon\Carbon;
8 use Illuminate\Console\Command;
9 use Illuminate\Database\Eloquent\Builder;
10 use Illuminate\Support\Facades\Http;
11
12
13 class SyncLadder extends Command {
14
15         /**
16          * The name and signature of the console command.
17          *
18          * @var string
19          */
20         protected $signature = 'sync:ladder';
21
22         /**
23          * The console command description.
24          *
25          * @var string
26          */
27         protected $description = 'Synchronize Ladder schedule';
28
29
30         /**
31          * Execute the console command.
32          *
33          * @return int
34          */
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());
40                         })
41                         ->get();
42
43                 foreach ($events as $event) {
44                         try {
45                                 $this->line('syncing '.$event->name);
46                                 $this->syncEvent($event);
47                         } catch (\Exception $e) {
48                                 $this->error('error syncing event '.$event->name.': '.$e->getMessage());
49                         }
50                 }
51         }
52
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,
57                 ])->json();
58                 foreach ($ladderSchedule as $ladderEntry) {
59                         try {
60                                 $this->syncSchedule($event, $ladderEntry);
61                         } catch (Exception $e) {
62                                 $this->error('error syncing episode '.$ladderEntry['race_id'].': '.$e->getMessage());
63                         }
64                 }
65         }
66
67         private function syncSchedule(Event $event, $ladderEntry) {
68                 $ext_id = 'ladder:'.$ladderEntry['race_id'].':'.$ladderEntry['RaceName'];
69                 $episode = Episode::firstWhere('ext_id', '=', $ext_id);
70                 if (!$episode) {
71                         $episode = new Episode();
72                         $episode->ext_id = $ext_id;
73                 }
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;
79                 $episode->save();
80         }
81
82 }