3 namespace App\Console\Commands;
5 use App\Models\Episode;
6 use App\Models\EpisodePlayer;
10 use Illuminate\Console\Command;
11 use Illuminate\Database\Eloquent\Builder;
12 use Illuminate\Support\Facades\Http;
14 class SyncSpeedGaming extends Command {
17 * The name and signature of the console command.
21 protected $signature = 'sync:speedgaming';
24 * The console command description.
28 protected $description = 'Synchronize SpeedGaming schedule';
31 * Execute the console command.
35 public function handle() {
36 $events = Event::where('external_schedule', 'LIKE', 'sg:%')
37 ->where(function (Builder $query) {
38 $query->whereNull('end');
39 $query->orWhere('end', '<', now());
42 foreach ($events as $event) {
44 $this->line('syncing '.$event->name);
45 $this->syncEvent($event);
46 } catch (\Exception $e) {
47 $this->error('error syncing event '.$event->name.': '.$e->getMessage());
53 private function syncEvent(Event $event) {
54 $sgHandle = substr($event->external_schedule, 3);
55 $from = now()->sub(1, 'day');
56 $to = now()->add(6, 'day');
57 $sgSchedule = HTTP::get('https://speedgaming.org/api/schedule/', [
59 'from' => $from->toIso8601String(),
60 'to' => $to->toIso8601String(),
62 $this->purgeSchedule($event, $sgSchedule);
63 foreach ($sgSchedule as $sgEntry) {
65 $this->syncSchedule($event, $sgEntry);
66 } catch (Exception $e) {
67 $this->error('error syncing episode '.$sgEntry['id'].': '.$e->getMessage());
72 private function purgeSchedule(Event $event, $sgSchedule) {
74 foreach ($sgSchedule as $sgEntry) {
75 $ext_ids[] = 'sg:'.$sgEntry['id'];
77 $event->episodes()->whereNotIn('ext_id', $ext_ids)->delete();
80 private function syncSchedule(Event $event, $sgEntry) {
81 $ext_id = 'sg:'.$sgEntry['id'];
82 $episode = Episode::firstWhere('ext_id', '=', $ext_id);
84 $episode = new Episode();
85 $episode->ext_id = $ext_id;
87 $episode->event()->associate($event);
88 $episode->title = $sgEntry['match1']['title'];
89 $start = Carbon::parse($sgEntry['when']);
90 if ($start->ne($episode->start)) {
91 $episode->start = $start;
93 $episode->estimate = $sgEntry['length'] * 60;
94 $episode->confirmed = $sgEntry['approved'];
95 $episode->comment = $sgEntry['match1']['note'];
97 $this->purgePlayers($episode, $sgEntry);
98 foreach ($sgEntry['match1']['players'] as $sgPlayer) {
100 $this->syncPlayer($episode, $sgPlayer);
101 } catch (Exception $e) {
102 $this->error('error syncing player '.$sgPlayer['id'].': '.$e->getMessage());
107 private function purgePlayers(Episode $episode, $sgEntry) {
109 foreach ($sgEntry['match1']['players'] as $sgPlayer) {
110 $ext_ids[] = 'sg:'.$sgPlayer['id'];
112 $episode->players()->whereNotIn('ext_id', $ext_ids)->delete();
115 private function syncPlayer(Episode $episode, $sgPlayer) {
116 $ext_id = 'sg:'.$sgPlayer['id'];
117 $player = $episode->players()->firstWhere('ext_id', '=', $ext_id);
119 $player = new EpisodePlayer();
120 $player->ext_id = $ext_id;
121 $player->episode()->associate($episode);
123 $user = $this->getUserBySGPlayer($sgPlayer);
125 $player->user()->associate($user);
127 $player->user()->disassociate();
129 if (!empty($sgPlayer['displayName'])) {
130 $player->name_override = $sgPlayer['displayName'];
132 if (!empty($sgPlayer['streamingFrom'])) {
133 $player->stream_override = $sgPlayer['streamingFrom'];
138 private function getUserBySGPlayer($player) {
139 if (!empty($player['discordId'])) {
140 $user = User::find($player['discordId']);
141 if ($user) return $user;
143 if (!empty($player['discordTag'])) {
144 $tag = explode('#', $player['discordTag']);
145 $user = User::firstWhere([
146 ['username', 'LIKE', $tag[0]],
147 ['username', 'LIKE', $tag[1]],
149 if ($user) return $user;