]> git.localhorst.tv Git - alttp.git/commitdiff
alttprfr sync
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 21 Jul 2025 20:37:04 +0000 (22:37 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 21 Jul 2025 20:37:50 +0000 (22:37 +0200)
app/Console/Commands/SyncAlttprFr.php [new file with mode: 0644]
routes/console.php

diff --git a/app/Console/Commands/SyncAlttprFr.php b/app/Console/Commands/SyncAlttprFr.php
new file mode 100644 (file)
index 0000000..ceeeec3
--- /dev/null
@@ -0,0 +1,142 @@
+<?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 SyncAlttprFr extends Command {
+
+       /**
+        * The name and signature of the console command.
+        *
+        * @var string
+        */
+       protected $signature = 'sync:alttprfr';
+
+       /**
+        * The console command description.
+        *
+        * @var string
+        */
+       protected $description = 'Synchronize Francophone schedule';
+
+       /**
+        * Execute the console command.
+        *
+        * @return int
+        */
+       public function handle(): int {
+               $events = Event::query()
+                       ->where('external_schedule', '=', 'alttprfr')
+                       ->where(function (Builder $query) {
+                               $query->whereNull('end');
+                               $query->orWhere('end', '>', now());
+                       })
+                       ->get();
+
+               if (empty($events)) {
+                       return Command::SUCCESS;
+               }
+
+               $html = Http::get('https://alttprfr.com/tournament/schedule/')->body();
+               $dom = \Dom\HTMLDocument::createFromString($html);
+               $rows = $dom->getElementsByTagName('tr');
+               $schedule = [];
+               foreach ($rows as $row) {
+                       $cells = $row->getElementsByTagName('td');
+                       if ($cells->length != 3) {
+                               continue;
+                       }
+                       $entry = [];
+                       $entry[] = $cells->item(0)->textContent;
+                       $entry[] = $cells->item(1)->getElementsByTagName('span')->item(0)->getAttribute('data-date');
+                       $entry[] = $cells->item(2)->textContent;
+                       $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) {
+               for ($i = 0; $i < count($schedule); ++$i) {
+                       $this->syncSchedule($event, $schedule[$i]);
+               }
+       }
+
+       private function syncSchedule(Event $event, $row) {
+               $ext_id = 'alttprfr:'.$event->id.':'.$row[1];
+               $episode = Episode::query()->firstWhere('ext_id', '=', $ext_id);
+               if (preg_match('/^(.*) vs (.*) - (.*)$/', $row[0], $matches) === false) {
+                       return;
+               }
+               if (!$episode) {
+                       $episode = new Episode();
+                       $episode->ext_id = $ext_id;
+               }
+               $episode->event()->associate($event);
+               $episode->title = $row[2] == '-' ? $matches[3] : $row[2];
+               $start = Carbon::parse($row[1]);
+               if (!$episode->start || $start->ne($episode->start)) {
+                       $episode->start = $start;
+               }
+               $episode->estimate = 120 * 60;
+               $episode->confirmed = true;
+               $episode->save();
+
+               $this->syncPlayer($episode, $matches[1]);
+               $this->syncPlayer($episode, $matches[2]);
+       }
+
+       private function syncPlayer(Episode $episode, $name) {
+               $lower_name = strtolower($name);
+               $ext_id = 'alttprfr:'.$lower_name;
+               $player = $episode->players()->firstWhere('ext_id', '=', $ext_id);
+               if (!$player) {
+                       $player = new EpisodePlayer();
+                       $player->ext_id = $ext_id;
+                       $player->episode()->associate($episode);
+               }
+               $player->name_override = $name;
+               $user = $this->getUser($lower_name);
+               if ($user) {
+                       $player->user()->associate($user);
+               } else {
+                       $player->user()->disassociate();
+               }
+               $player->save();
+               return $player;
+       }
+
+       private function getUser($name) {
+               $user = User::query()->firstWhere('discord_nickname', 'LIKE', $name);
+               if ($user) {
+                       return $user;
+               }
+               $user = User::query()->firstWhere('username', 'LIKE', $name);
+               if ($user) {
+                       return $user;
+               }
+               $user = User::query()->firstWhere('stream_link', 'LIKE', '%/'.$name);
+               if ($user) {
+                       return $user;
+               }
+               return null;
+       }
+
+}
index e603eddcd1700d4c682b2a29f72829695d67ea0c..41ca0c60caff91e3f97fe5f9768052c9e1b28808 100644 (file)
@@ -26,6 +26,7 @@ Schedule::everyFiveMinutes()
                Schedule::command('sync:enemizer');
                Schedule::command('sync:hth');
                Schedule::command('sync:nmg-league');
+               Schedule::command('sync:alttprfr');
        });
 
 Schedule::everyFifteenMinutes()