]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/AosSeedController.php
first half of AoS generator
[alttp.git] / app / Http / Controllers / AosSeedController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\DiscordAppCommands\AosrPresetCommand;
6 use App\Models\AosSeed;
7 use Illuminate\Http\Request;
8 use Illuminate\Support\Facades\Artisan;
9
10 class AosSeedController extends Controller
11 {
12
13         public function byHash($hash) {
14                 $seed = AosSeed::where('hash', '=', $hash)->firstOrFail();
15
16                 if ($seed->race) {
17                         $seed->makeHidden('seed');
18                 }
19                 if ($seed->mystery) {
20                         $seed->makeHidden('settings');
21                 }
22
23                 return $seed->toJson();
24         }
25
26         public function generate(Request $request) {
27                 $validatedData = $request->validate([
28                         'preset' => 'string|required',
29                         'race' => 'boolean',
30                 ]);
31                 $presetName = $validatedData['preset'];
32                 $race = $validatedData['race'] ?? false;
33
34                 $preset = AosrPresetCommand::presetByName($presetName);
35                 if (!$preset) {
36                         abort(404);
37                 }
38                 $seed = AosSeed::generateSurge($preset['value'], $preset['settings'], $race);
39                 Artisan::call('aos:generate '.intval($seed->id));
40
41                 $seed = $this->censor($seed->fresh());
42                 return $seed->toJson();
43         }
44
45         public function presets() {
46                 return array_values(AosrPresetCommand::$presets);
47         }
48
49         public function retry($hash) {
50                 $seed = AosSeed::where('hash', '=', $hash)->firstOrFail();
51
52                 if ($seed->status == 'error') {
53                         $seed->status = 'pending';
54                         $seed->save();
55                         Artisan::call('aos:generate '.intval($seed->id));
56                 }
57
58                 $seed = $this->censor($seed->fresh());
59                 return $seed->toJson();
60         }
61
62         private function censor(AosSeed $seed) {
63                 if ($seed->race) {
64                         $seed->makeHidden('seed');
65                 }
66                 if ($seed->mystery) {
67                         $seed->makeHidden('settings');
68                 }
69                 return $seed;
70         }
71
72 }