]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/SyncZSR.php
add up to 4 player support for 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                 $start = Carbon::parse($entry['start']['dateTime'])->setTimezone('UTC');
92                 if (!$episode->start || $start->ne($episode->start)) {
93                         $episode->start = $start;
94                 }
95                 $end = Carbon::parse($entry['end']['dateTime'])->setTimezone('UTC');
96                 $episode->estimate = $start->diffInSeconds($end);
97                 $episode->confirmed = true;
98                 if (preg_match('/^(.*) - (.*?) vs\.? (.*?) vs\.? (.*?) vs\.? (.*?)$/u', $episode->title, $matches)) {
99                         $episode->title = $matches[1];
100                         $episode->save();
101                         $this->syncPlayer($episode, $matches[2]);
102                         $this->syncPlayer($episode, $matches[3]);
103                         $this->syncPlayer($episode, $matches[4]);
104                         $this->syncPlayer($episode, $matches[5]);
105                 } else if (preg_match('/^(.*) - (.*?) vs\.? (.*?) vs\.? (.*?)$/u', $episode->title, $matches)) {
106                         $episode->title = $matches[1];
107                         $episode->save();
108                         $this->syncPlayer($episode, $matches[2]);
109                         $this->syncPlayer($episode, $matches[3]);
110                         $this->syncPlayer($episode, $matches[4]);
111                 } else if (preg_match('/^(.*) - (.*?) vs\.? (.*?)$/u', $episode->title, $matches)) {
112                         $episode->title = $matches[1];
113                         $episode->save();
114                         $this->syncPlayer($episode, $matches[2]);
115                         $this->syncPlayer($episode, $matches[3]);
116                 } else {
117                         $episode->save();
118                 }
119                 if (preg_match('/Restream: https?:\/\/(www\.)?twitch\.tv\/(\w+)/u', $entry['description'], $matches)) {
120                         $channel = $this->syncChannel($episode, $matches[2]);
121                         if ($channel) {
122                                 $episode->channels()->syncWithoutDetaching([$channel->id]);
123                         }
124                 }
125         }
126
127         private function syncChannel(Episode $episode, $zsrChannel) {
128                 $ext_id = 'zsr:'.$zsrChannel;
129                 $channel = $this->org->channels()->firstWhere('ext_id', '=', $ext_id);
130                 if (!$channel) {
131                         $channel = new Channel();
132                         $channel->ext_id = $ext_id;
133                         $channel->organization()->associate($this->org);
134                         $channel->languages = ['en'];
135                         $channel->short_name = '';
136                 }
137                 $channel->title = $zsrChannel;
138                 $channel->stream_link = 'https://twitch.tv/'.strtolower($zsrChannel);
139                 $channel->save();
140                 return $channel;
141         }
142
143         private function syncPlayer(Episode $episode, $zsrPlayer) {
144                 $ext_id = 'zsr:'.$zsrPlayer;
145                 $player = $episode->players()->firstWhere('ext_id', '=', $ext_id);
146                 if (!$player) {
147                         $player = new EpisodePlayer();
148                         $player->ext_id = $ext_id;
149                         $player->episode()->associate($episode);
150                 }
151                 $user = $this->getUserByZSRPlayer($zsrPlayer);
152                 if ($user) {
153                         $player->user()->associate($user);
154                 } else {
155                         $player->user()->disassociate();
156                 }
157                 $player->name_override = $zsrPlayer;
158                 $player->save();
159                 return $player;
160         }
161
162         private function getUserByZSRPlayer($player) {
163                 $user = User::firstWhere('discord_nickname', '=', $player);
164                 if ($user) {
165                         return $user;
166                 }
167                 $user = User::firstWhere('username', '=', strtolower($player));
168                 if ($user) {
169                         return $user;
170                 }
171                 return null;
172         }
173
174         private $org;
175
176 }