]> git.localhorst.tv Git - alttp.git/commitdiff
add step ladder importer
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 19 May 2025 16:46:46 +0000 (18:46 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 19 May 2025 16:48:14 +0000 (18:48 +0200)
app/Console/Commands/SyncStepLadder.php [new file with mode: 0644]
app/Console/Kernel.php
app/Models/StepLadderMode.php [new file with mode: 0644]
database/migrations/2025_05_19_162331_create_step_ladder_modes_table.php [new file with mode: 0644]

diff --git a/app/Console/Commands/SyncStepLadder.php b/app/Console/Commands/SyncStepLadder.php
new file mode 100644 (file)
index 0000000..1b9f48e
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\Episode;
+use App\Models\Event;
+use App\Models\StepLadderMode;
+use Carbon\Carbon;
+use Illuminate\Console\Command;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Support\Facades\Http;
+
+
+class SyncLadder extends Command {
+
+       /**
+        * The name and signature of the console command.
+        *
+        * @var string
+        */
+       protected $signature = 'sync:stepladder';
+
+       /**
+        * The console command description.
+        *
+        * @var string
+        */
+       protected $description = 'Synchronize Step Ladder schedule';
+
+
+       /**
+        * Execute the console command.
+        *
+        * @return int
+        */
+       public function handle() {
+               $events = Event::where('external_schedule', 'LIKE', 'stepladder')
+                       ->where(function (Builder $query) {
+                               $query->whereNull('end');
+                               $query->orWhere('end', '>', now());
+                       })
+                       ->get();
+
+               foreach ($events as $event) {
+                       try {
+                               $this->line('syncing '.$event->name);
+                               $this->syncEvent($event);
+                       } catch (\Exception $e) {
+                               $this->error('error syncing event '.$event->name.': '.$e->getMessage());
+                       }
+               }
+       }
+
+       private function syncEvent(Event $event) {
+               $ladderSchedule = Http::get('https://alttpr.racing/api/v1/upcoming')->json();
+               foreach ($ladderSchedule as $ladderEntry) {
+                       try {
+                               $this->syncSchedule($event, $ladderEntry);
+                       } catch (\Exception $e) {
+                               $this->error('error syncing episode '.$ladderEntry['id'].': '.$e->getMessage());
+                       }
+               }
+       }
+
+       private function syncSchedule(Event $event, $ladderEntry) {
+               $ext_id = 'stepladder:'.$ladderEntry['id'];
+               $episode = Episode::firstWhere('ext_id', '=', $ext_id);
+               if (!$episode) {
+                       $episode = new Episode();
+                       $episode->ext_id = $ext_id;
+               }
+               $mode = $this->getMode($ladderEntry);
+               $episode->event()->associate($event);
+               $episode->title = $mode->name;
+               $episode->start = Carbon::createFromTimestamp($ladderEntry['time']);
+               $episode->estimate = 2 * 60 * 60;
+               $episode->confirmed = true;
+               $episode->save();
+       }
+
+       private function getMode($ladderEntry) {
+               $ext_id = 'stepladder:'.$ladderEntry['mode'];
+               $mode = StepLadderMode::firstWhere('ext_id', '=', $ext_id);
+               if (!$mode) {
+                       $ladderMode = Http::get('https://alttpr.racing/api/v1/modes/'.$ladderEntry['mode'])->json();
+                       $mode = new StepLadderMode();
+                       $mode->ext_id = $ext_id;
+                       $mode->name = $ladderMode['name'];
+                       $mode->last_sync = Carbon::now();
+                       $mode->save();
+               }
+               return $mode;
+       }
+
+}
index 14c784032cf28fd76dd4c181a39510cd73056278..f762c4fa98afc4d81977faf5fa2a6f4231bc14b8 100644 (file)
@@ -16,7 +16,7 @@ class Kernel extends ConsoleKernel
     protected function schedule(Schedule $schedule)
     {
                $schedule->command('twitch:channel-info')->everyFiveMinutes();
-               $schedule->command('sync:ladder')->daily();
+               $schedule->command('sync:stepladder')->daily();
                $schedule->command('sync:speedgaming')->everyFiveMinutes();
                $schedule->command('sync:sra')->everyFifteenMinutes();
                $schedule->command('sync:zsr')->everyFifteenMinutes();
diff --git a/app/Models/StepLadderMode.php b/app/Models/StepLadderMode.php
new file mode 100644 (file)
index 0000000..8cfec9a
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class StepLadderMode extends Model {
+
+       use HasFactory;
+
+}
diff --git a/database/migrations/2025_05_19_162331_create_step_ladder_modes_table.php b/database/migrations/2025_05_19_162331_create_step_ladder_modes_table.php
new file mode 100644 (file)
index 0000000..9474c61
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+       /**
+        * Run the migrations.
+        */
+       public function up(): void
+       {
+               Schema::create('step_ladder_modes', function (Blueprint $table) {
+                       $table->id();
+                       $table->string('ext_id');
+                       $table->string('name');
+                       $table->timestamps();
+                       $table->timestamp('last_sync')->nullable()->default(null);
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        */
+       public function down(): void
+       {
+               Schema::dropIfExists('step_ladder_modes');
+       }
+};