]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/SyncZSR.php
tentative implementation of ZSR sync
[alttp.git] / app / Console / Commands / SyncZSR.php
1 <?php
2
3 namespace App\Console\Commands;
4
5 use App\Models\Channel;
6 use App\Models\Episode;
7 use App\Models\EpisodePlayer;
8 use App\Models\Event;
9 use App\Models\Organization;
10 use App\Models\User;
11 use Carbon\Carbon;
12 use Illuminate\Console\Command;
13 use Illuminate\Database\Eloquent\Builder;
14 use Illuminate\Support\Facades\Http;
15 use Illuminate\Support\Str;
16
17 class SyncZSR extends Command {
18
19         /**
20          * The name and signature of the console command.
21          *
22          * @var string
23          */
24         protected $signature = 'sync:zsr';
25
26         /**
27          * The console command description.
28          *
29          * @var string
30          */
31         protected $description = 'Synchronize Zelda Speedruns schedule';
32
33         /**
34          * Execute the console command.
35          *
36          * @return int
37          */
38         public function handle() {
39                 $this->org = Organization::where('name', '=', 'zsr')->firstOrFail();
40
41                 $events = Event::where('external_schedule', 'LIKE', 'zsr:%')
42                         ->where(function (Builder $query) {
43                                 $query->whereNull('end');
44                                 $query->orWhere('end', '<', now());
45                         })
46                         ->get();
47
48                 $from = now()->sub(1, 'day');
49                 $to = now()->add(14, 'day');
50                 $schedule = Http::get('https://www.googleapis.com/calendar/v3/calendars/zsrstaff%40gmail.com/events', [
51                         'alt' => 'json',
52                         'key' => config('google.api_key'),
53                         'timeMax' => $to->toIso8601String(),
54                         'timeMin' => $from->toIso8601String(),
55                 ])->json();
56
57                 foreach ($events as $event) {
58                         try {
59                                 $this->line('syncing '.$event->name);
60                                 $this->syncEvent($event, $schedule['items']);
61                         } catch (\Exception $e) {
62                                 $this->error('error syncing event '.$event->name.': '.$e->getMessage());
63                         }
64                 }
65
66                 return Command::SUCCESS;
67         }
68
69         private function syncEvent(Event $event, $schedule) {
70                 $tag = substr($event->external_schedule, 4);
71                 foreach ($schedule as $entry) {
72                         if (!Str::startsWith($entry['summary'], $tag)) continue;
73                         $this->syncSchedule($event, $entry);
74                 }
75
76         }
77
78         private function syncSchedule(Event $event, $entry) {
79                 $ext_id = 'zsr:'.$entry['id'];
80                 $episode = Episode::firstWhere('ext_id', '=', $ext_id);
81                 if (!$episode) {
82                         $episode = new Episode();
83                         $episode->ext_id = $ext_id;
84                 }
85                 $episode->event()->associate($event);
86                 if (strlen($entry['summary']) > (strlen($event->external_schedule) - 4)) {
87                         $episode->title = substr($entry['summary'], strlen($event->external_schedule) - 4);
88                 } else {
89                         $episode->title = $entry['summary'];
90                 }
91                 if (preg_match('/Restream: https?:\/\/(www\.)?twitch\.tv\/(\w+)/u', $entry['description'], $matches)) {
92                         $channel = $this->syncChannel($episode, $matches[2]);
93                         if ($channel) {
94                                 $episode->channels()->syncWithoutDetaching([$channel->id]);
95                         }
96                 }
97                 if (preg_match('/^(.*) - (.*?) vs (.*?)$/u', $episode->title, $matches)) {
98                         $episode->title = $matches[1];
99                         $this->syncPlayer($episode, $matches[2]);
100                         $this->syncPlayer($episode, $matches[3]);
101                 }
102                 $start = Carbon::parse($entry['start']['dateTime'])->setTimezone('UTC');
103                 if (!$episode->start || $start->ne($episode->start)) {
104                         $episode->start = $start;
105                 }
106                 $end = Carbon::parse($entry['end']['dateTime'])->setTimezone('UTC');
107                 $episode->estimate = $start->diffInSeconds($end);
108                 $episode->confirmed = true;
109                 $episode->save();
110         }
111
112         private function syncChannel(Episode $episode, $zsrChannel) {
113                 $ext_id = 'zsr:'.$zsrChannel;
114                 $channel = $this->org->channels()->firstWhere('ext_id', '=', $ext_id);
115                 if (!$channel) {
116                         $channel = new Channel();
117                         $channel->ext_id = $ext_id;
118                         $channel->organization()->associate($this->org);
119                         $channel->languages = ['en'];
120                         $channel->short_name = '';
121                 }
122                 $channel->title = $zsrChannel;
123                 $channel->stream_link = 'https://twitch.tv/'.strtolower($zsrChannel);
124                 $channel->save();
125                 return $channel;
126         }
127
128         private function syncPlayer(Episode $episode, $zsrPlayer) {
129                 $ext_id = 'zsr:'.$zsrPlayer;
130                 $player = $episode->players()->firstWhere('ext_id', '=', $ext_id);
131                 if (!$player) {
132                         $player = new EpisodePlayer();
133                         $player->ext_id = $ext_id;
134                         $player->episode()->associate($episode);
135                 }
136                 $user = $this->getUserByZSRPlayer($zsrPlayer);
137                 if ($user) {
138                         $player->user()->associate($user);
139                 } else {
140                         $player->user()->disassociate();
141                 }
142                 $player->name_override = $zsrPlayer;
143                 $player->save();
144                 return $player;
145         }
146
147         private function getUserByZSRPlayer($player) {
148                 $user = User::firstWhere('discord_nickname', '=', $player);
149                 if ($user) {
150                         return $user;
151                 }
152                 $user = User::firstWhere('username', '=', strtolower($player));
153                 if ($user) {
154                         return $user;
155                 }
156                 return null;
157         }
158
159         private $org;
160
161 }