]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/SyncZSR.php
more spam
[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\.? (.*?)$/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                 } else {
104                         $episode->save();
105                 }
106                 if (preg_match('/Restream: https?:\/\/(www\.)?twitch\.tv\/(\w+)/u', $entry['description'], $matches)) {
107                         $channel = $this->syncChannel($episode, $matches[2]);
108                         if ($channel) {
109                                 $episode->channels()->syncWithoutDetaching([$channel->id]);
110                         }
111                 }
112         }
113
114         private function syncChannel(Episode $episode, $zsrChannel) {
115                 $ext_id = 'zsr:'.$zsrChannel;
116                 $channel = $this->org->channels()->firstWhere('ext_id', '=', $ext_id);
117                 if (!$channel) {
118                         $channel = new Channel();
119                         $channel->ext_id = $ext_id;
120                         $channel->organization()->associate($this->org);
121                         $channel->languages = ['en'];
122                         $channel->short_name = '';
123                 }
124                 $channel->title = $zsrChannel;
125                 $channel->stream_link = 'https://twitch.tv/'.strtolower($zsrChannel);
126                 $channel->save();
127                 return $channel;
128         }
129
130         private function syncPlayer(Episode $episode, $zsrPlayer) {
131                 $ext_id = 'zsr:'.$zsrPlayer;
132                 $player = $episode->players()->firstWhere('ext_id', '=', $ext_id);
133                 if (!$player) {
134                         $player = new EpisodePlayer();
135                         $player->ext_id = $ext_id;
136                         $player->episode()->associate($episode);
137                 }
138                 $user = $this->getUserByZSRPlayer($zsrPlayer);
139                 if ($user) {
140                         $player->user()->associate($user);
141                 } else {
142                         $player->user()->disassociate();
143                 }
144                 $player->name_override = $zsrPlayer;
145                 $player->save();
146                 return $player;
147         }
148
149         private function getUserByZSRPlayer($player) {
150                 $user = User::firstWhere('discord_nickname', '=', $player);
151                 if ($user) {
152                         return $user;
153                 }
154                 $user = User::firstWhere('username', '=', strtolower($player));
155                 if ($user) {
156                         return $user;
157                 }
158                 return null;
159         }
160
161         private $org;
162
163 }