]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/GenerateAosSeed.php
wait for seed generation before posting message
[alttp.git] / app / Console / Commands / GenerateAosSeed.php
1 <?php
2
3 namespace App\Console\Commands;
4
5 use App\Beat\Encoder;
6 use App\Models\AosSeed;
7 use HeadlessChromium\BrowserFactory;
8 use Illuminate\Console\Command;
9 use Illuminate\Support\Facades\Storage;
10
11 class GenerateAosSeed extends Command
12 {
13         /**
14          * The name and signature of the console command.
15          *
16          * @var string
17          */
18         protected $signature = 'aos:generate {id}';
19
20         /**
21          * The console command description.
22          *
23          * @var string
24          */
25         protected $description = 'Generate AoS seed';
26
27         /**
28          * Execute the console command.
29          *
30          * @return int
31          */
32         public function handle()
33         {
34                 $seed = AosSeed::findOrFail($this->argument('id'));
35                 $seed->status = 'generating';
36                 $seed->error_detail = null;
37                 $seed->save();
38
39                 $stage = 'initial';
40                 try {
41                         $temp_dir = sys_get_temp_dir();
42
43                         $params = array_merge(['seed' => $seed->seed], $seed->settings);
44                         $url = config('aos.surge_url').'/?'.http_build_query($params, '', '&');
45
46                         $fac = new BrowserFactory('chromium');
47                         $browser = $fac->createBrowser();
48
49                         $stage = 'loading page';
50                         $page = $browser->createPage();
51                         $page->setDownloadPath($temp_dir);
52                         $page->navigate($url)->waitForNavigation();
53
54                         $stage = 'selecting rom';
55                         $fileInput = $page->dom()->querySelector('input[type=file]');
56                         $fileInput->sendFile(config('aos.base_rom'));
57                         $page->waitUntilContainsElement('select');
58
59                         $stage = 'clicking randomize';
60                         $page->mouse()->find('button', 2)->click();
61                         $page->waitUntilContainsElement('a[download]');
62
63                         $stage = 'clicking download';
64                         $page->dom()->querySelector('a[download]')->setAttributeValue('download', $seed->hash.'.gba');
65                         $page->mouse()->find('a[download]')->click();
66
67                         $stage = 'waiting for rom';
68                         $romFile = $temp_dir.'/'.$seed->hash.'.gba';
69                         for ($i = 0; $i < 100; ++$i) {
70                                 clearstatcache();
71                                 if (is_file($romFile) && filesize($romFile) >= 8388608) {
72                                         break;
73                                 }
74                                 usleep(100_000);
75                         }
76
77                         $stage = 'calculating patch';
78                         $encoder = new Encoder(file_get_contents(config('aos.base_rom')));
79                         $patch = $encoder->createPatch(file_get_contents($romFile));
80                         Storage::disk('aos-seeds')->put($seed->hash.'.bps', $patch);
81                         unlink($romFile);
82
83                         $stage = 'done';
84                         $seed->status = 'generated';
85                         $seed->save();
86
87                         $browser->close();
88                 } catch (\Throwable $e) {
89                         $seed->status = 'error';
90                         $seed->error_detail = [
91                                 'stage' => $stage,
92                                 'type' => get_class($e),
93                                 'message' => $e->getMessage(),
94                         ];
95                         $seed->save();
96                         return 1;
97                 }
98
99                 return 0;
100         }
101 }