]> git.localhorst.tv Git - alttp.git/blobdiff - app/Console/Commands/SyncSpeedGaming.php
sync episode channels
[alttp.git] / app / Console / Commands / SyncSpeedGaming.php
index 8c74c68124428eb190237dae117f957680ceb0fe..4d2cb9cb27cb9a4a9195f21c93f3851238a6f9c2 100644 (file)
@@ -2,9 +2,11 @@
 
 namespace App\Console\Commands;
 
+use App\Models\Channel;
 use App\Models\Episode;
 use App\Models\EpisodePlayer;
 use App\Models\Event;
+use App\Models\Organization;
 use App\Models\User;
 use Carbon\Carbon;
 use Illuminate\Console\Command;
@@ -33,12 +35,15 @@ class SyncSpeedGaming extends Command {
         * @return int
         */
        public function handle() {
+               $this->org = Organization::where('name', '=', 'sg')->firstOrFail();
+
                $events = Event::where('external_schedule', 'LIKE', 'sg:%')
                        ->where(function (Builder $query) {
                                $query->whereNull('end');
                                $query->orWhere('end', '<', now());
                        })
                        ->get();
+
                foreach ($events as $event) {
                        try {
                                $this->line('syncing '.$event->name);
@@ -47,6 +52,7 @@ class SyncSpeedGaming extends Command {
                                $this->error('error syncing event '.$event->name.': '.$e->getMessage());
                        }
                }
+
                return 0;
        }
 
@@ -101,6 +107,22 @@ class SyncSpeedGaming extends Command {
                $episode->confirmed = $sgEntry['approved'];
                $episode->comment = $sgEntry['match1']['note'];
                $episode->save();
+
+               $this->purgeChannels($episode, $sgEntry);
+               $channelIds = [];
+               foreach ($sgEntry['channels'] as $sgChannel) {
+                       if ($sgChannel['initials'] == 'NONE') continue;
+                       try {
+                               $channel = $this->syncChannel($episode, $sgChannel);
+                               $channelIds[] = $channel->id;
+                       } catch (Exception $e) {
+                               $this->error('error syncing channel '.$sgChannel['id'].': '.$e->getMessage());
+                       }
+               }
+               if (!empty($channelIds)) {
+                       $episode->channels()->syncWithoutDetaching($channelIds);
+               }
+
                $this->purgePlayers($episode, $sgEntry);
                foreach ($sgEntry['match1']['players'] as $sgPlayer) {
                        try {
@@ -111,6 +133,33 @@ class SyncSpeedGaming extends Command {
                }
        }
 
+       private function purgeChannels(Episode $episode, $sgEntry) {
+               $ext_ids = [];
+               foreach ($sgEntry['channels'] as $sgChannel) {
+                       $ext_ids[] = 'sg:'.$sgChannel['id'];
+               }
+               $episode->channels()
+                 ->where('ext_id', 'LIKE', 'sg:%')
+                 ->whereNotIn('ext_id', $ext_ids)
+                 ->detach();
+       }
+
+       private function syncChannel(Episode $episode, $sgChannel) {
+               $ext_id = 'sg:'.$sgChannel['id'];
+               $channel = $this->org->channels()->firstWhere('ext_id', '=', $ext_id);
+               if (!$channel) {
+                       $channel = new Channel();
+                       $channel->ext_id = $ext_id;
+                       $channel->organization()->associate($this->org);
+               }
+               $channel->short_name = $sgChannel['initials'];
+               $channel->title = $sgChannel['name'];
+               $channel->stream_link = 'https://twitch.tv/'.strtolower($sgChannel['name']);
+               $channel->languages = [$sgChannel['language']];
+               $channel->save();
+               return $channel;
+       }
+
        private function purgePlayers(Episode $episode, $sgEntry) {
                $ext_ids = [];
                foreach ($sgEntry['match1']['players'] as $sgPlayer) {
@@ -137,7 +186,7 @@ class SyncSpeedGaming extends Command {
                        $player->name_override = $sgPlayer['displayName'];
                }
                if (!empty($sgPlayer['streamingFrom'])) {
-                       $player->stream_override = $sgPlayer['streamingFrom'];
+                       $player->stream_override = strtolower($sgPlayer['streamingFrom']);
                }
                $player->save();
        }
@@ -158,4 +207,6 @@ class SyncSpeedGaming extends Command {
                return null;
        }
 
+       private $org;
+
 }