From 752325d3f7ae45636e52a5bce71a41abe09948d1 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 13 May 2022 00:07:25 +0200 Subject: [PATCH] configurable surge URL --- .env.example | 1 + .gitignore | 1 + app/Jobs/GenerateAosSeed.php | 26 +++++++++++++++++++++----- config/aos.php | 1 + 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index c1b602f..3811845 100644 --- a/.env.example +++ b/.env.example @@ -63,4 +63,5 @@ DISCORD_BOT_ENABLE_COMMANDS= AOS_BASE_ROM= AOS_HOSTNAME=aos.localhorst.tv +AOS_SURGE_URL=https://aosrando.surge.sh AOS_URL=https://aos.localhorst.tv diff --git a/.gitignore b/.gitignore index 42465fe..3165581 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ /public/js/vendor.js.map /public/mix-manifest.json /public/storage +/public/surge /reports /storage/*.key /vendor diff --git a/app/Jobs/GenerateAosSeed.php b/app/Jobs/GenerateAosSeed.php index 7169c36..a95c372 100644 --- a/app/Jobs/GenerateAosSeed.php +++ b/app/Jobs/GenerateAosSeed.php @@ -34,37 +34,52 @@ class GenerateAosSeed implements ShouldQueue */ public function handle() { + $stage = 'initial'; try { $temp_dir = sys_get_temp_dir(); $seed = $this->seed; $params = array_merge(['seed' => $seed->seed], $seed->settings); - $url = 'https://aosrando.surge.sh/?'.http_build_query($params, '', '&'); + $url = config('aos.surge_url').'/?'.http_build_query($params, '', '&'); $fac = new BrowserFactory('chromium'); $browser = $fac->createBrowser(); + $stage = 'loading page'; $page = $browser->createPage(); $page->setDownloadPath($temp_dir); $page->navigate($url)->waitForNavigation(); + $stage = 'selecting rom'; $fileInput = $page->dom()->querySelector('input[type=file]'); $fileInput->sendFile(config('aos.base_rom')); $page->waitUntilContainsElement('select'); + $stage = 'clicking randomize'; $page->mouse()->find('button', 2)->click(); $page->waitUntilContainsElement('a[download]'); + $stage = 'clicking download'; $page->dom()->querySelector('a[download]')->setAttributeValue('download', $seed->hash.'.gba'); $page->mouse()->find('a[download]')->click(); - sleep(2); - + $stage = 'waiting for rom'; + $romFile = $temp_dir.'/'.$seed->hash.'.gba'; + for ($i = 0; $i < 20; ++$i) { + clearstatcache(); + if (is_file($romFile) && filesize($romFile) >= 8388608) { + break; + } + usleep(100_000); + } + + $stage = 'calculating patch'; $encoder = new Encoder(file_get_contents(config('aos.base_rom'))); - $patch = $encoder->createPatch(file_get_contents($temp_dir.'/'.$seed->hash.'.gba')); + $patch = $encoder->createPatch(file_get_contents($romFile)); Storage::disk('aos-seeds')->put($seed->hash.'.bps', $patch); - unlink($temp_dir.'/'.$seed->hash.'.gba'); + unlink($romFile); + $stage = 'done'; $seed->status = 'generated'; $seed->save(); @@ -72,6 +87,7 @@ class GenerateAosSeed implements ShouldQueue } catch (\Throwable $e) { $seed->status = 'error'; $seed->error_detail = [ + 'stage' => $stage, 'type' => get_class($e), 'message' => $e->getMessage(), ]; diff --git a/config/aos.php b/config/aos.php index 7f98a90..b6eaf2a 100644 --- a/config/aos.php +++ b/config/aos.php @@ -3,5 +3,6 @@ return [ 'base_rom' => env('AOS_BASE_ROM', ''), 'hostname' => env('AOS_HOSTNAME', 'aos.localhorst.tv'), + 'surge_url' => env('AOS_SURGE_URL', 'https://aosrando.surge.sh'), 'url' => env('AOS_URL', 'https://aos.localhorst.tv'), ]; -- 2.39.2