]> git.localhorst.tv Git - alttp.git/commitdiff
endpoint for twitch leaderboard
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 12 Mar 2024 13:50:56 +0000 (14:50 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 12 Mar 2024 13:50:56 +0000 (14:50 +0100)
app/Console/Commands/TwitchChannelInfo.php [new file with mode: 0644]
app/Console/Kernel.php
app/Http/Controllers/ChannelController.php
app/Models/TwitchToken.php
database/migrations/2024_03_12_133614_channel_twitch_sync.php [new file with mode: 0644]
routes/api.php

diff --git a/app/Console/Commands/TwitchChannelInfo.php b/app/Console/Commands/TwitchChannelInfo.php
new file mode 100644 (file)
index 0000000..09dc718
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\Channel;
+use App\Models\TwitchToken;
+use Illuminate\Console\Command;
+use Illuminate\Http\Client\RequestException;
+use Illuminate\Support\Facades\Http;
+
+class TwitchChannelInfo extends Command {
+
+       /**
+        * The name and signature of the console command.
+        *
+        * @var string
+        */
+       protected $signature = 'twitch:channel-info';
+
+       /**
+        * The console command description.
+        *
+        * @var string
+        */
+       protected $description = 'Refresh twitch channel info';
+
+       /**
+        * Execute the console command.
+        *
+        * @return int
+        */
+       public function handle()
+       {
+               $this->token = TwitchToken::firstWhere('nick', 'localhorsttv');
+               if (!$this->token) {
+                       $this->line('please acquire a token for localhorsttv first');
+                       return 1;
+               }
+               $channels = Channel::where('twitch_chat', '!=', '')->where('twitch_id', '=', '')->get();
+               foreach ($channels as $channel) {
+                       try {
+                               $this->updateChannel($channel);
+                       } catch (RequestException $e) {
+                               if ($e->response->status() == 401) {
+                                       $this->token->refresh();
+                                       $this->updateChannel($channel);
+                               }
+                       }
+               }
+               return Command::SUCCESS;
+       }
+
+       private function updateChannel(Channel $channel) {
+               $this->line($channel->twitch_chat);
+               $login = substr($channel->twitch_chat, 1);
+               $rsp = $this->token->request()
+                       ->get('/users', [
+                               'login' => $login,
+                       ])
+                       ->throw();
+               foreach ($rsp['data'] as $user) {
+                       if ($user['login'] != $login) continue;
+                       $channel->twitch_id = $user['id'];
+                       $channel->save();
+               }
+       }
+
+       private $token;
+
+}
index ea3807d3e84c6b553cdab5b08afac864817f97fc..bbb747d3d614fbdc9e52828f021fd87247466ce5 100644 (file)
@@ -15,6 +15,7 @@ class Kernel extends ConsoleKernel
      */
     protected function schedule(Schedule $schedule)
     {
+               $schedule->command('twitch:channel-info')->everyFiveMinutes();
                $schedule->command('sync:ladder')->daily();
                $schedule->command('sync:speedgaming')->everyFiveMinutes();
                $schedule->command('sync:sra')->everyFifteenMinutes();
index 78648af8dea8ece74e65e2987ea06a37e7479a58..f7d57c037137cb330d1fec49036952e3e701e664 100644 (file)
@@ -216,6 +216,12 @@ class ChannelController extends Controller {
                ];
        }
 
+       public function getGuessingGameLeaderboard(Channel $channel, $type) {
+               return [
+                       'all' => $channel->getGuessingLeaderboard(),
+               ];
+       }
+
        public function getGuessingGameMonitor($key) {
                $channel = Channel::where('access_key', '=', $key)->firstOrFail();
 
index 9b15977b41e881afae2cf5c367c09ed553156cee..a4c561eaffa40ce75cc49f5272cf13c8a1fb5b87 100644 (file)
@@ -35,6 +35,14 @@ class TwitchToken extends Model
                $this->save();
        }
 
+       public function request() {
+               return Http::baseUrl('https://api.twitch.tv/helix')
+                       ->withToken($this->access)
+                       ->withHeaders([
+                               'Client-Id' => config('twitch.client_id'),
+                       ]);
+       }
+
        protected $casts = [
                'scope' => 'array',
        ];
diff --git a/database/migrations/2024_03_12_133614_channel_twitch_sync.php b/database/migrations/2024_03_12_133614_channel_twitch_sync.php
new file mode 100644 (file)
index 0000000..5f4422c
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+       /**
+        * Run the migrations.
+        *
+        * @return void
+        */
+       public function up()
+       {
+               Schema::table('channels', function (Blueprint $table) {
+                       $table->string('twitch_id')->default('');
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        *
+        * @return void
+        */
+       public function down()
+       {
+               Schema::table('channels', function (Blueprint $table) {
+                       $table->dropColumn('twitch_id');
+               });
+       }
+};
index 0c897af2f23f463e0cfdaac57bb8b698ee2cba03..363a1127a5106519c373f748f5136bb926ccb72f 100644 (file)
@@ -37,6 +37,7 @@ Route::post('channels/{channel}/guessing-game/{name}', 'App\Http\Controllers\Cha
 Route::put('channels/{channel}/guessing-game/{name}', 'App\Http\Controllers\ChannelController@saveGuessingGame');
 
 Route::get('guessing-game-monitor/{key}', 'App\Http\Controllers\ChannelController@getGuessingGameMonitor');
+Route::get('guessing-game-leaderboard/twitch/{channel:twitch_id}/{type}', 'App\Http\Controllers\ChannelController@getGuessingGameLeaderboard');
 
 Route::get('content', 'App\Http\Controllers\TechniqueController@search');
 Route::get('content/{tech:name}', 'App\Http\Controllers\TechniqueController@single');