]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/GenerateAlttpSeed.php
sync ladder schedule
[alttp.git] / app / Console / Commands / GenerateAlttpSeed.php
1 <?php
2
3 namespace App\Console\Commands;
4
5 use App\Beat\Encoder;
6 use App\Models\AlttpSeed;
7 use Illuminate\Console\Command;
8 use Illuminate\Support\Facades\Http;
9 use Illuminate\Support\Facades\Storage;
10 use Symfony\Component\Process\Process;
11
12 class GenerateAlttpSeed extends Command
13 {
14         /**
15          * The name and signature of the console command.
16          *
17          * @var string
18          */
19         protected $signature = 'alttp:generate {id}';
20
21         /**
22          * The console command description.
23          *
24          * @var string
25          */
26         protected $description = 'Generate ALttP seed';
27
28         /**
29          * Execute the console command.
30          *
31          * @return int
32          */
33         public function handle()
34         {
35                 $seed = AlttpSeed::findOrFail($this->argument('id'));
36                 $seed->status = 'generating';
37                 $seed->error_detail = null;
38                 $seed->save();
39
40                 $temp_dir = sys_get_temp_dir();
41                 $rom_file = $temp_dir.'/DR_'.$seed->hash.'.sfc';
42                 $settings_file = $temp_dir.'/'.$seed->hash.'.json';
43                 $spoiler_file = $temp_dir.'/DR_'.$seed->hash.'_Spoiler.txt';
44
45                 $stage = 'initial';
46                 try {
47
48                         $stage = 'settings';
49                         $settings = array_merge([
50                                 'calc_playthrough' => true,
51                                 'create_rom' => true,
52                                 'create_spoiler' => true,
53                                 'count' => 1,
54                                 'enemizercli' => base_path().'/vendor/z3/enemizer_linux/EnemizerCLI.Core',
55                                 'multi' => 1,
56                                 'names' => '',
57                                 'outputpath' => $temp_dir,
58                                 'outputname' => $seed->hash,
59                                 'race' => $seed->race,
60                                 'rom' => config('alttp.base_rom'),
61                                 'seed' => $seed->seed,
62                         ], $seed->settings);
63                         file_put_contents($settings_file, json_encode($settings));
64
65                         $stage = 'generate';
66                         $proc = new Process([config('alttp.doors_cli'), '--settingsfile', $settings_file]);
67                         $proc->setWorkingDirectory(dirname(config('alttp.doors_cli')));
68                         $proc->mustRun();
69
70                         $stage = 'calculating patch';
71                         $encoder = new Encoder(file_get_contents(config('alttp.base_rom')));
72                         $patch = $encoder->createPatch(file_get_contents($rom_file));
73                         $spoiler = file_get_contents($spoiler_file);
74                         Storage::disk('alttp-seeds')->put($seed->hash.'.bps', $patch);
75                         Storage::disk('alttp-spoilers')->put($seed->hash.'.txt', $spoiler);
76
77                         $stage = 'done';
78                         $seed->status = 'generated';
79                         $seed->save();
80                 } catch (\Throwable $e) {
81                         $this->error($e->getMessage());
82                         $seed->status = 'error';
83                         $seed->error_detail = [
84                                 'stage' => $stage,
85                                 'type' => get_class($e),
86                                 'message' => $e->getMessage(),
87                         ];
88                         $seed->save();
89                         return 1;
90                 }
91
92                 if (file_exists($rom_file)) unlink($rom_file);
93                 if (file_exists($settings_file)) unlink($settings_file);
94                 if (file_exists($spoiler_file)) unlink($spoiler_file);
95
96                 return 0;
97         }
98 }