3 namespace App\Console\Commands;
5 use App\Models\Channel;
6 use App\Models\Episode;
7 use App\Models\EpisodePlayer;
9 use App\Models\Organization;
12 use Illuminate\Console\Command;
13 use Illuminate\Database\Eloquent\Builder;
14 use Illuminate\Support\Facades\Http;
15 use Illuminate\Support\Str;
17 class SyncZSR extends Command {
20 * The name and signature of the console command.
24 protected $signature = 'sync:zsr';
27 * The console command description.
31 protected $description = 'Synchronize Zelda Speedruns schedule';
34 * Execute the console command.
38 public function handle() {
39 $this->org = Organization::where('name', '=', 'zsr')->firstOrFail();
41 $events = Event::where('external_schedule', 'LIKE', 'zsr:%')
42 ->where(function (Builder $query) {
43 $query->whereNull('end');
44 $query->orWhere('end', '<', now());
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', [
52 'key' => config('google.api_key'),
53 'timeMax' => $to->toIso8601String(),
54 'timeMin' => $from->toIso8601String(),
57 foreach ($events as $event) {
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());
66 return Command::SUCCESS;
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);
78 private function syncSchedule(Event $event, $entry) {
79 $ext_id = 'zsr:'.$entry['id'];
80 $episode = Episode::firstWhere('ext_id', '=', $ext_id);
82 $episode = new Episode();
83 $episode->ext_id = $ext_id;
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);
89 $episode->title = $entry['summary'];
91 if (preg_match('/Restream: https?:\/\/(www\.)?twitch\.tv\/(\w+)/u', $entry['description'], $matches)) {
92 $channel = $this->syncChannel($episode, $matches[2]);
94 $episode->channels()->syncWithoutDetaching([$channel->id]);
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]);
102 $start = Carbon::parse($entry['start']['dateTime'])->setTimezone('UTC');
103 if (!$episode->start || $start->ne($episode->start)) {
104 $episode->start = $start;
106 $end = Carbon::parse($entry['end']['dateTime'])->setTimezone('UTC');
107 $episode->estimate = $start->diffInSeconds($end);
108 $episode->confirmed = true;
112 private function syncChannel(Episode $episode, $zsrChannel) {
113 $ext_id = 'zsr:'.$zsrChannel;
114 $channel = $this->org->channels()->firstWhere('ext_id', '=', $ext_id);
116 $channel = new Channel();
117 $channel->ext_id = $ext_id;
118 $channel->organization()->associate($this->org);
119 $channel->languages = ['en'];
120 $channel->short_name = '';
122 $channel->title = $zsrChannel;
123 $channel->stream_link = 'https://twitch.tv/'.strtolower($zsrChannel);
128 private function syncPlayer(Episode $episode, $zsrPlayer) {
129 $ext_id = 'zsr:'.$zsrPlayer;
130 $player = $episode->players()->firstWhere('ext_id', '=', $ext_id);
132 $player = new EpisodePlayer();
133 $player->ext_id = $ext_id;
134 $player->episode()->associate($episode);
136 $user = $this->getUserByZSRPlayer($zsrPlayer);
138 $player->user()->associate($user);
140 $player->user()->disassociate();
142 $player->name_override = $zsrPlayer;
147 private function getUserByZSRPlayer($player) {
148 $user = User::firstWhere('discord_nickname', '=', $player);
152 $user = User::firstWhere('username', '=', strtolower($player));