]> git.localhorst.tv Git - alttp.git/blobdiff - app/Console/Commands/SyncSpeedGaming.php
some discord discriminator stuff
[alttp.git] / app / Console / Commands / SyncSpeedGaming.php
index eaaa4fa48e7ccb5989d0c4ca29218ed3c80857c5..d7f9a63653d9a9b25f7f610861ecdddcc82dfe5c 100644 (file)
@@ -61,7 +61,7 @@ class SyncSpeedGaming extends Command {
        private function syncEvent(Event $event) {
                $sgHandle = substr($event->external_schedule, 3);
                $from = now()->sub(1, 'day');
-               $to = now()->add(6, 'day');
+               $to = now()->add(14, 'day');
                $sgSchedule = HTTP::get('https://speedgaming.org/api/schedule/', [
                        'event' => $sgHandle,
                        'from' => $from->toIso8601String(),
@@ -87,6 +87,8 @@ class SyncSpeedGaming extends Command {
                        ->where('start', '<=', $to)
                        ->whereNotIn('ext_id', $ext_ids);
                foreach ($to_purge->get() as $episode) {
+                       $episode->channels()->detach();
+                       $episode->crew()->delete();
                        $episode->players()->delete();
                }
                $to_purge->delete();
@@ -102,7 +104,17 @@ class SyncSpeedGaming extends Command {
                $episode->event()->associate($event);
                $episode->title = $sgEntry['match1']['title'];
                $start = Carbon::createFromFormat('Y-m-d\TH:i:sP', $sgEntry['when']);
-               if ($start->ne($episode->start)) {
+               if ($event->fix_timezone && !$episode->timezone_fix_override) {
+                       $sg_zone = new \DateTimeZone('America/Detroit');
+                       $event_zone = new \DateTimeZone($event->fix_timezone);
+                       // if speedgaming is in DST, it fucks up the timestamp
+                       if (Carbon::createFromTimestamp($start->timestamp, $sg_zone)->dst && !Carbon::createFromTimestamp($start->timestamp, $event_zone)->dst) {
+                               $start->add(1, 'hour');
+                       } else if (!Carbon::createFromTimestamp($start->timestamp, $sg_zone)->dst && Carbon::createFromTimestamp($start->timestamp, $event_zone)->dst) {
+                               $start->sub(1, 'hour');
+                       }
+               }
+               if (!$episode->start || $start->ne($episode->start)) {
                        $episode->start = $start;
                }
                $episode->estimate = $sgEntry['length'] * 60;
@@ -113,7 +125,7 @@ class SyncSpeedGaming extends Command {
                $this->purgeChannels($episode, $sgEntry);
                $channelIds = [];
                foreach ($sgEntry['channels'] as $sgChannel) {
-                       if ($sgChannel['initials'] == 'NONE') continue;
+                       if ($sgChannel['initials'] == 'NONE' || $sgChannel['name'] == 'Undecided, Not SG') continue;
                        try {
                                $channel = $this->syncChannel($episode, $sgChannel);
                                $channelIds[] = $channel->id;
@@ -176,10 +188,13 @@ class SyncSpeedGaming extends Command {
                foreach ($sgEntry['channels'] as $sgChannel) {
                        $ext_ids[] = 'sg:'.$sgChannel['id'];
                }
-               $episode->channels()
-                 ->where('ext_id', 'LIKE', 'sg:%')
-                 ->whereNotIn('ext_id', $ext_ids)
-                 ->detach();
+               $channels = $episode->channels()
+                       ->where('ext_id', 'LIKE', 'sg:%')
+                       ->whereNotIn('ext_id', $ext_ids)
+                       ->get();
+               if (!$channels->isEmpty()) {
+                       $episode->channels()->detach($channels->pluck('id'));
+               }
        }
 
        private function syncChannel(Episode $episode, $sgChannel) {
@@ -278,6 +293,9 @@ class SyncSpeedGaming extends Command {
                if ($channel) {
                        return $channel;
                }
+               return $episode->channels()
+                 ->where('ext_id', 'LIKE', 'sg:%')
+                 ->first();
        }
 
        private function getUserBySGPlayer($player) {
@@ -299,10 +317,14 @@ class SyncSpeedGaming extends Command {
                }
                if (!empty($player['discordTag'])) {
                        $tag = explode('#', $player['discordTag']);
-                       $user = User::firstWhere([
-                               ['username', 'LIKE', $tag[0]],
-                               ['discriminator', '=', $tag[1]],
-                       ]);
+                       $user = count($tag) < 2 || $tag[1] == '0'
+                               ? User::firstWhere([
+                                       ['username', 'LIKE', $tag[0]],
+                               ])
+                               : User::firstWhere([
+                                       ['username', 'LIKE', $tag[0]],
+                                       ['discriminator', '=', $tag[1]],
+                               ]);
                        if ($user) return $user;
                }
                return null;