X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FConsole%2FCommands%2FSyncSpeedGaming.php;h=13eb3a30e8b1724857ee4492e692962f21d9796f;hb=35dca1d496a5298daad2fc5f926a9fe400f68dce;hp=656a079cf87ad2c843b9131c0c6a42bc6e104776;hpb=11e76fc2410dc26cd9c19cac4117d36ce3212523;p=alttp.git diff --git a/app/Console/Commands/SyncSpeedGaming.php b/app/Console/Commands/SyncSpeedGaming.php index 656a079..13eb3a3 100644 --- a/app/Console/Commands/SyncSpeedGaming.php +++ b/app/Console/Commands/SyncSpeedGaming.php @@ -2,9 +2,12 @@ namespace App\Console\Commands; +use App\Models\Channel; +use App\Models\DiscordBotCommand; 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 +36,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 +53,7 @@ class SyncSpeedGaming extends Command { $this->error('error syncing event '.$event->name.': '.$e->getMessage()); } } + return 0; } @@ -93,7 +100,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; } @@ -101,6 +108,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 +134,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,6 +180,15 @@ class SyncSpeedGaming extends Command { $user = $this->getUserBySGPlayer($sgPlayer); if ($user) { $player->user()->associate($user); + if (empty($user->stream_link)) { + if (!empty($sgPlayer['publicStream'])) { + $user->stream_link = 'https://twitch.tv/'.strtolower($sgPlayer['publicStream']); + $user->save(); + } else if (!empty($sgPlayer['streamingFrom'])) { + $user->stream_link = 'https://twitch.tv/'.strtolower($sgPlayer['streamingFrom']); + $user->save(); + } + } } else { $player->user()->disassociate(); } @@ -137,7 +196,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(); } @@ -146,6 +205,7 @@ class SyncSpeedGaming extends Command { if (!empty($player['discordId'])) { $user = User::find($player['discordId']); if ($user) return $user; + DiscordBotCommand::syncUser($player['discordId']); } if (!empty($player['discordTag'])) { $tag = explode('#', $player['discordTag']); @@ -158,4 +218,6 @@ class SyncSpeedGaming extends Command { return null; } + private $org; + }