]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/SyncSpeedGaming.php
properly purge episodes
[alttp.git] / app / Console / Commands / SyncSpeedGaming.php
1 <?php
2
3 namespace App\Console\Commands;
4
5 use App\Models\Episode;
6 use App\Models\EpisodePlayer;
7 use App\Models\Event;
8 use App\Models\User;
9 use Carbon\Carbon;
10 use Illuminate\Console\Command;
11 use Illuminate\Database\Eloquent\Builder;
12 use Illuminate\Support\Facades\Http;
13
14 class SyncSpeedGaming extends Command {
15
16         /**
17          * The name and signature of the console command.
18          *
19          * @var string
20          */
21         protected $signature = 'sync:speedgaming';
22
23         /**
24          * The console command description.
25          *
26          * @var string
27          */
28         protected $description = 'Synchronize SpeedGaming schedule';
29
30         /**
31          * Execute the console command.
32          *
33          * @return int
34          */
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());
40                         })
41                         ->get();
42                 foreach ($events as $event) {
43                         try {
44                                 $this->line('syncing '.$event->name);
45                                 $this->syncEvent($event);
46                         } catch (\Exception $e) {
47                                 $this->error('error syncing event '.$event->name.': '.$e->getMessage());
48                         }
49                 }
50                 return 0;
51         }
52
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/', [
58                         'event' => $sgHandle,
59                         'from' => $from->toIso8601String(),
60                         'to' => $to->toIso8601String(),
61                 ])->json();
62                 $this->purgeSchedule($event, $from, $to, $sgSchedule);
63                 foreach ($sgSchedule as $sgEntry) {
64                         try {
65                                 $this->syncSchedule($event, $sgEntry);
66                         } catch (Exception $e) {
67                                 $this->error('error syncing episode '.$sgEntry['id'].': '.$e->getMessage());
68                         }
69                 }
70         }
71
72         private function purgeSchedule(Event $event, $from, $to, $sgSchedule) {
73                 $ext_ids = [];
74                 foreach ($sgSchedule as $sgEntry) {
75                         $ext_ids[] = 'sg:'.$sgEntry['id'];
76                 }
77                 $to_purge = $event->episodes()
78                         ->where('start', '>=', $from)
79                         ->where('start', '<=', $to)
80                         ->whereNotIn('ext_id', $ext_ids);
81                 foreach ($to_purge->get() as $episode) {
82                         $episode->players()->delete();
83                 }
84                 $to_purge->delete();
85         }
86
87         private function syncSchedule(Event $event, $sgEntry) {
88                 $ext_id = 'sg:'.$sgEntry['id'];
89                 $episode = Episode::firstWhere('ext_id', '=', $ext_id);
90                 if (!$episode) {
91                         $episode = new Episode();
92                         $episode->ext_id = $ext_id;
93                 }
94                 $episode->event()->associate($event);
95                 $episode->title = $sgEntry['match1']['title'];
96                 $start = Carbon::parse($sgEntry['when']);
97                 if ($start->ne($episode->start)) {
98                         $episode->start = $start;
99                 }
100                 $episode->estimate = $sgEntry['length'] * 60;
101                 $episode->confirmed = $sgEntry['approved'];
102                 $episode->comment = $sgEntry['match1']['note'];
103                 $episode->save();
104                 $this->purgePlayers($episode, $sgEntry);
105                 foreach ($sgEntry['match1']['players'] as $sgPlayer) {
106                         try {
107                                 $this->syncPlayer($episode, $sgPlayer);
108                         } catch (Exception $e) {
109                                 $this->error('error syncing player '.$sgPlayer['id'].': '.$e->getMessage());
110                         }
111                 }
112         }
113
114         private function purgePlayers(Episode $episode, $sgEntry) {
115                 $ext_ids = [];
116                 foreach ($sgEntry['match1']['players'] as $sgPlayer) {
117                         $ext_ids[] = 'sg:'.$sgPlayer['id'];
118                 }
119                 $episode->players()->whereNotIn('ext_id', $ext_ids)->delete();
120         }
121
122         private function syncPlayer(Episode $episode, $sgPlayer) {
123                 $ext_id = 'sg:'.$sgPlayer['id'];
124                 $player = $episode->players()->firstWhere('ext_id', '=', $ext_id);
125                 if (!$player) {
126                         $player = new EpisodePlayer();
127                         $player->ext_id = $ext_id;
128                         $player->episode()->associate($episode);
129                 }
130                 $user = $this->getUserBySGPlayer($sgPlayer);
131                 if ($user) {
132                         $player->user()->associate($user);
133                 } else {
134                         $player->user()->disassociate();
135                 }
136                 if (!empty($sgPlayer['displayName'])) {
137                         $player->name_override = $sgPlayer['displayName'];
138                 }
139                 if (!empty($sgPlayer['streamingFrom'])) {
140                         $player->stream_override = $sgPlayer['streamingFrom'];
141                 }
142                 $player->save();
143         }
144
145         private function getUserBySGPlayer($player) {
146                 if (!empty($player['discordId'])) {
147                         $user = User::find($player['discordId']);
148                         if ($user) return $user;
149                 }
150                 if (!empty($player['discordTag'])) {
151                         $tag = explode('#', $player['discordTag']);
152                         $user = User::firstWhere([
153                                 ['username', 'LIKE', $tag[0]],
154                                 ['username', 'LIKE', $tag[1]],
155                         ]);
156                         if ($user) return $user;
157                 }
158                 return null;
159         }
160
161 }