3 namespace App\Console\Commands;
6 use App\Models\AosSeed;
7 use Illuminate\Console\Command;
8 use Illuminate\Support\Facades\Storage;
9 use Symfony\Component\Process\Process;
11 class GenerateAosSeed extends Command
14 * The name and signature of the console command.
18 protected $signature = 'aos:generate {id}';
21 * The console command description.
25 protected $description = 'Generate AoS seed';
28 * Execute the console command.
32 public function handle()
34 $seed = AosSeed::findOrFail($this->argument('id'));
35 $seed->status = 'generating';
36 $seed->error_detail = null;
41 $temp_dir = sys_get_temp_dir();
43 $params = array_merge(['seed' => $seed->seed], $seed->settings);
44 $settings = http_build_query($params, '', '&');
46 $romFile = $temp_dir.'/'.$seed->hash.'.gba';
47 $spoilerFile = $temp_dir.'/'.$seed->hash.'.txt';
49 $stage = 'randomizing';
50 $proc = new Process([config('aos.cli'), config('aos.base_rom'), $romFile, $settings]);
53 $stage = 'calculating patch';
54 $encoder = new Encoder(file_get_contents(config('aos.base_rom')));
55 $patch = $encoder->createPatch(file_get_contents($romFile));
56 Storage::disk('aos-seeds')->put($seed->hash.'.bps', $patch);
59 $stage = 'saving spoiler';
60 Storage::disk('aos-spoilers')->put($seed->hash.'.txt', $proc->getOutput());
63 $seed->status = 'generated';
65 } catch (\Throwable $e) {
66 $seed->status = 'error';
67 $seed->error_detail = [
69 'type' => get_class($e),
70 'message' => $e->getMessage(),