--- /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 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;
+ }
+
+}