X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FConsole%2FCommands%2FSyncSpeedGaming.php;h=4d2cb9cb27cb9a4a9195f21c93f3851238a6f9c2;hb=15132749249f6418fd5695547b5c323a0ad10939;hp=177ee1947675b67485ffa8ca1148439a94572ce0;hpb=071885a30f24b980699b337d9cdb65952f8c6c42;p=alttp.git diff --git a/app/Console/Commands/SyncSpeedGaming.php b/app/Console/Commands/SyncSpeedGaming.php index 177ee19..4d2cb9c 100644 --- a/app/Console/Commands/SyncSpeedGaming.php +++ b/app/Console/Commands/SyncSpeedGaming.php @@ -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; } @@ -59,7 +65,7 @@ class SyncSpeedGaming extends Command { 'from' => $from->toIso8601String(), 'to' => $to->toIso8601String(), ])->json(); - $this->purgeSchedule($event, $sgSchedule); + $this->purgeSchedule($event, $from, $to, $sgSchedule); foreach ($sgSchedule as $sgEntry) { try { $this->syncSchedule($event, $sgEntry); @@ -69,12 +75,19 @@ class SyncSpeedGaming extends Command { } } - private function purgeSchedule(Event $event, $sgSchedule) { + private function purgeSchedule(Event $event, $from, $to, $sgSchedule) { $ext_ids = []; foreach ($sgSchedule as $sgEntry) { $ext_ids[] = 'sg:'.$sgEntry['id']; } - $event->episodes()->whereNotIn('ext_id', $ext_ids)->delete(); + $to_purge = $event->episodes() + ->where('start', '>=', $from) + ->where('start', '<=', $to) + ->whereNotIn('ext_id', $ext_ids); + foreach ($to_purge->get() as $episode) { + $episode->players()->delete(); + } + $to_purge->delete(); } private function syncSchedule(Event $event, $sgEntry) { @@ -86,7 +99,7 @@ class SyncSpeedGaming extends Command { } $episode->event()->associate($event); $episode->title = $sgEntry['match1']['title']; - $start = Carbon::parse($sgEntry['when']); + $start = Carbon::createFromFormat('Y-m-d\TH:i:sP', $sgEntry['when']); if ($start->ne($episode->start)) { $episode->start = $start; } @@ -94,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 { @@ -104,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) { @@ -130,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(); } @@ -151,4 +207,6 @@ class SyncSpeedGaming extends Command { return null; } + private $org; + }