]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/SyncSpeedGaming.php
177ee1947675b67485ffa8ca1148439a94572ce0
[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, $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, $sgSchedule) {
73                 $ext_ids = [];
74                 foreach ($sgSchedule as $sgEntry) {
75                         $ext_ids[] = 'sg:'.$sgEntry['id'];
76                 }
77                 $event->episodes()->whereNotIn('ext_id', $ext_ids)->delete();
78         }
79
80         private function syncSchedule(Event $event, $sgEntry) {
81                 $ext_id = 'sg:'.$sgEntry['id'];
82                 $episode = Episode::firstWhere('ext_id', '=', $ext_id);
83                 if (!$episode) {
84                         $episode = new Episode();
85                         $episode->ext_id = $ext_id;
86                 }
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;
92                 }
93                 $episode->estimate = $sgEntry['length'] * 60;
94                 $episode->confirmed = $sgEntry['approved'];
95                 $episode->comment = $sgEntry['match1']['note'];
96                 $episode->save();
97                 $this->purgePlayers($episode, $sgEntry);
98                 foreach ($sgEntry['match1']['players'] as $sgPlayer) {
99                         try {
100                                 $this->syncPlayer($episode, $sgPlayer);
101                         } catch (Exception $e) {
102                                 $this->error('error syncing player '.$sgPlayer['id'].': '.$e->getMessage());
103                         }
104                 }
105         }
106
107         private function purgePlayers(Episode $episode, $sgEntry) {
108                 $ext_ids = [];
109                 foreach ($sgEntry['match1']['players'] as $sgPlayer) {
110                         $ext_ids[] = 'sg:'.$sgPlayer['id'];
111                 }
112                 $episode->players()->whereNotIn('ext_id', $ext_ids)->delete();
113         }
114
115         private function syncPlayer(Episode $episode, $sgPlayer) {
116                 $ext_id = 'sg:'.$sgPlayer['id'];
117                 $player = $episode->players()->firstWhere('ext_id', '=', $ext_id);
118                 if (!$player) {
119                         $player = new EpisodePlayer();
120                         $player->ext_id = $ext_id;
121                         $player->episode()->associate($episode);
122                 }
123                 $user = $this->getUserBySGPlayer($sgPlayer);
124                 if ($user) {
125                         $player->user()->associate($user);
126                 } else {
127                         $player->user()->disassociate();
128                 }
129                 if (!empty($sgPlayer['displayName'])) {
130                         $player->name_override = $sgPlayer['displayName'];
131                 }
132                 if (!empty($sgPlayer['streamingFrom'])) {
133                         $player->stream_override = $sgPlayer['streamingFrom'];
134                 }
135                 $player->save();
136         }
137
138         private function getUserBySGPlayer($player) {
139                 if (!empty($player['discordId'])) {
140                         $user = User::find($player['discordId']);
141                         if ($user) return $user;
142                 }
143                 if (!empty($player['discordTag'])) {
144                         $tag = explode('#', $player['discordTag']);
145                         $user = User::firstWhere([
146                                 ['username', 'LIKE', $tag[0]],
147                                 ['username', 'LIKE', $tag[1]],
148                         ]);
149                         if ($user) return $user;
150                 }
151                 return null;
152         }
153
154 }