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;
* @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);
$this->error('error syncing event '.$event->name.': '.$e->getMessage());
}
}
+
return 0;
}
}
$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;
}
$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 {
}
}
+ 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) {
$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();
}
$player->name_override = $sgPlayer['displayName'];
}
if (!empty($sgPlayer['streamingFrom'])) {
- $player->stream_override = $sgPlayer['streamingFrom'];
+ $player->stream_override = strtolower($sgPlayer['streamingFrom']);
}
$player->save();
}
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']);
return null;
}
+ private $org;
+
}