]> git.localhorst.tv Git - alttp.git/commitdiff
add aos generate api endpoint
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 28 May 2022 12:11:33 +0000 (14:11 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 28 May 2022 12:11:33 +0000 (14:11 +0200)
app/Http/Controllers/AosSeedController.php
app/Http/Middleware/VerifyCsrfToken.php
routes/api.php

index 7c7e5f9fdaf04012e1f52def459b52bfabc8b88e..1e2aa73a91b637a07d223cacfa4fb3b729249d2e 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace App\Http\Controllers;
 
+use App\DiscordAppCommands\AosrPresetCommand;
 use App\Models\AosSeed;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Artisan;
@@ -22,6 +23,25 @@ class AosSeedController extends Controller
                return $seed->toJson();
        }
 
+       public function generate(Request $request) {
+               $validatedData = $request->validate([
+                       'preset' => 'string|required',
+                       'race' => 'boolean',
+               ]);
+               $presetName = $validatedData['preset'];
+               $race = $validatedData['race'] ?? false;
+
+               if (!isset(AosrPresetCommand::$presets[$presetName])) {
+                       abort(404);
+               }
+               $preset = AosrPresetCommand::$presets[$presetName];
+               $seed = AosSeed::generateSurge($presetName, $preset['settings'], $race);
+               Artisan::call('aos:generate '.intval($seed->id));
+
+               $seed = $this->censor($seed->fresh());
+               return $seed->toJson();
+       }
+
        public function retry($hash) {
                $seed = AosSeed::where('hash', '=', $hash)->firstOrFail();
 
@@ -31,7 +51,18 @@ class AosSeedController extends Controller
                        Artisan::call('aos:generate '.intval($seed->id));
                }
 
+               $seed = $this->censor($seed->fresh());
                return $seed->toJson();
        }
 
+       private function censor(AosSeed $seed) {
+               if ($seed->race) {
+                       $seed->makeHidden('seed');
+               }
+               if ($seed->mystery) {
+                       $seed->makeHidden('settings');
+               }
+               return $seed;
+       }
+
 }
index 9e86521722b083582f0f100e7b4d3a63bcc1bdfc..c06c06b92ac5bf9482c3b9df91722791d1950836 100644 (file)
@@ -12,6 +12,6 @@ class VerifyCsrfToken extends Middleware
      * @var array<int, string>
      */
     protected $except = [
-        //
+               'api/aos-generate',
     ];
 }
index c5f18ee34ba4dd912e9f95dbb30f013eed5291dc..ad624c3e60f2d964e76bbb3a3b6c6ef038a8478a 100644 (file)
@@ -21,6 +21,7 @@ Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
 Route::get('alttp-seed/{hash}', 'App\Http\Controllers\AlttpSeedController@byHash');
 Route::post('alttp-seed/{hash}/retry', 'App\Http\Controllers\AlttpSeedController@retry');
 
+Route::post('aos-generate', 'App\Http\Controllers\AosSeedController@generate');
 Route::get('aos-seed/{hash}', 'App\Http\Controllers\AosSeedController@byHash');
 Route::post('aos-seed/{hash}/retry', 'App\Http\Controllers\AosSeedController@retry');