--- /dev/null
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\Episode;
+use App\Models\EpisodePlayer;
+use App\Models\Event;
+use App\Models\User;
+use Carbon\Carbon;
+use Illuminate\Console\Command;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Support\Facades\Http;
+
+class SyncPilotSpoiler extends Command {
+
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'sync:pilot-spoiler';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Synchronize Pilot Spoiler Tournament schedule';
+
+ /**
+ * Execute the console command.
+ *
+ * @return int
+ */
+ public function handle(): int {
+ $events = Event::query()
+ ->where('external_schedule', '=', 'pilot-spoiler')
+ ->where(function (Builder $query) {
+ $query->whereNull('end');
+ $query->orWhere('end', '>', now());
+ })
+ ->get();
+
+ if (empty($events)) {
+ return Command::SUCCESS;
+ }
+
+ $html = Http::get('https://docs.google.com/spreadsheets/d/1xuZggJsTT-USjgZjpI1qW07XIs1uXGToGj7AHY5Xh0A/edit?gid=1433653457')->body();
+ $dom = new \DOMDocument();
+ $dom->loadHTML($html);
+ $rows = $dom->getElementsByTagName('tr');
+ $schedule = [];
+ foreach ($rows as $row) {
+ $cells = $row->getElementsByTagName('td');
+ $entry = [];
+ foreach ($cells as $cell) {
+ $entry[] = $cell->textContent;
+ }
+ if (count($entry) == 11) {
+ $schedule[] = $entry;
+ }
+ }
+
+ foreach ($events as $event) {
+ try {
+ $this->line('syncing '.$event->name);
+ $this->syncEvent($event, $schedule);
+ } catch (\Exception $e) {
+ $this->error('error syncing event '.$event->name.': '.$e->getMessage());
+ }
+ }
+
+ return Command::SUCCESS;
+ }
+
+ private function syncEvent(Event $event, $schedule): void {
+ for ($i = 1; $i < count($schedule); ++$i) {
+ $this->syncSchedule($event, $schedule[$i]);
+ }
+ }
+
+ private function syncSchedule(Event $event, $row): void {
+ if (empty($row[0])) {
+ return;
+ }
+ $ext_id = 'pilot-spoiler:'.$event->id.':'.$row[0];
+ $episode = Episode::query()->firstWhere('ext_id', '=', $ext_id);
+ if (!$episode) {
+ $episode = new Episode();
+ $episode->ext_id = $ext_id;
+ }
+ $episode->event()->associate($event);
+ $episode->title = $row[1].' vs. '.$row[2];
+ $start = Carbon::createFromFormat('m/d/Y h:i:s A', $row[3].' '.$row[4], 'America/Detroit')->setTimezone('UTC');
+ if (!$episode->start || $start->ne($episode->start)) {
+ $episode->start = $start;
+ }
+ $episode->estimate = 60 * 60;
+ $episode->confirmed = true;
+ $episode->save();
+
+ $players_a = $this->getTeam($row[1]);
+ $players_b = $this->getTeam($row[2]);
+
+ $this->syncPlayer($episode, $players_a[0]);
+ $this->syncPlayer($episode, $players_b[0]);
+ $this->syncPlayer($episode, $players_a[1]);
+ $this->syncPlayer($episode, $players_b[1]);
+ }
+
+ private function syncPlayer(Episode $episode, string $uid): EpisodePlayer|null {
+ $ext_id = 'pilot-spoiler:'.$uid;
+ $player = $episode->players()->firstWhere('ext_id', '=', $ext_id);
+ if (!$player) {
+ $player = new EpisodePlayer();
+ $player->ext_id = $ext_id;
+ $player->episode()->associate($episode);
+ }
+ $user = $this->getUser($uid);
+ if ($user) {
+ $player->user()->associate($user);
+ } else {
+ $player->user()->disassociate();
+ }
+ $player->save();
+ return $player;
+ }
+
+ private function getTeam($team_name) {
+ if (!isset($this->teams[$team_name])) {
+ throw new Exception('missing team '.$team_name);
+ }
+ return $this->teams[$team_name];
+ }
+
+ private function getUser(string $uid): User|null {
+ $user = User::query()->firstWhere('id', '=', $uid);
+ if ($user) {
+ return $user;
+ }
+ return null;
+ }
+
+ private $teams = [
+ 'Hylian Fluff Brigade' => ['250359697352294403', '201148317306978305'],
+ 'BA (Bonkers Anonymous)' => ['599373366847209482', '556232109786660885'],
+ 'Lost Flight Plan' => ['138876266328752128', '494281016052023307'],
+ '"Don\'t Forget Arrows" 2026 Reunion Tour' => ['221090754087354368', '523596161639251999'],
+ 'The spoiler logarithms' => ['285792304491528192', '615949908226080781'],
+ 'Trading Card Game' => ['226954634894376960', '456652691771359234'],
+ 'ratSTEERing the Apple under the Midnight Sun' => ['138692388767006720', '84253020518379520'],
+ 'German Fever Dream' => ['95574504360648704', '531429222938443777'],
+ 'Suecica-Norvegica' => ['205967929920061441', '148114577475895296'],
+ 'Spoil Deez' => ['1256338007607345183', '721148393723920535'],
+ 'Service Bunny' => ['259540285380362242', '254933538892283905'],
+ 'ShyFeesh UwU' => ['1319432974340722732', '269249177257639936'],
+ 'Fluff for Brains' => ['171825625278054409', '116481821147004928'],
+ 'Anscylla and Charybdeez' => ['238940719925035008', '255676979460702210'],
+ 'Winners Don\'t Do Spiral' => ['747174011380957224', '158012662872014858'],
+ 'The Longshots' => ['358327737703989260', '755277999749726319'],
+ 'Gaslight, Gatekeep, Go Mode' => ['300998041413091329', '101834678083813376'],
+ '12 foot 8' => ['222928132590534656', '254616492992233472'],
+ 'Die Namenlosen' => ['986161067975639113', '944262998170693674'],
+ ];
+
+}