aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/sweep_browser.html (3675B)


      1 <!doctype html>
      2 <html lang="en">
      3 <meta charset="utf-8">
      4 <title>AO WASM playfield sweep</title>
      5 <style>
      6 body { background: #111; color: #ddd; font: 16px sans-serif; }
      7 iframe { width: 1040px; height: 790px; border: 0; }
      8 pre { white-space: pre-wrap; }
      9 </style>
     10 <button id="start">Start WASM sweep</button>
     11 <pre id="status">Ready</pre>
     12 <iframe id="game" title="AO WASM client"></iframe>
     13 <pre id="result"></pre>
     14 <pre id="log"></pre>
     15 <script>
     16 const status = document.querySelector('#status');
     17 const frame = document.querySelector('#game');
     18 let instance = 0, frames = 0, frameTime = 0, input = null, error = '', log = '';
     19 let inputRequested = false, captureRequested = false, screenshot = null;
     20 window.addEventListener('message', event => {
     21   if (event.origin !== location.origin || event.source !== frame.contentWindow) return;
     22   const message = event.data;
     23   if (message.instance !== instance) return;
     24   if (message.kind === 'frame') { frames = message.frames; frameTime = message.time; }
     25   if (message.kind === 'log') {
     26     log += message.text + '\n';
     27     document.querySelector('#log').textContent = log.slice(-8000);
     28   }
     29   if (message.kind === 'error') error = message.text;
     30   if (message.kind === 'input') input = message.events;
     31   if (message.kind === 'capture') screenshot = message.png;
     32 });
     33 async function request(path, body) {
     34   const response = await fetch('http://127.0.0.1:8083' + path, {
     35     method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(body)
     36   });
     37   if (!response.ok) throw new Error(await response.text());
     38   return response.json();
     39 }
     40 document.querySelector('#start').onclick = async event => {
     41   event.target.disabled = true;
     42   if (!crossOriginIsolated || !HTMLCanvasElement.prototype.transferControlToOffscreen) {
     43     status.textContent = 'This browser needs shared memory and OffscreenCanvas support';
     44     return;
     45   }
     46   window.sweepAudio = new AudioContext();
     47   await window.sweepAudio.resume();
     48   try {
     49     while (true) {
     50       const command = await request('/tick', {instance, frames, frame_time: frameTime,
     51         input, error, user_agent: navigator.userAgent});
     52       status.textContent = JSON.stringify(command);
     53       if (command.action === 'done') break;
     54       if (command.action === 'unload' && instance !== 0) {
     55         frame.src = 'about:blank';
     56         instance = 0;
     57       }
     58       if (command.action === 'launch') {
     59         instance = command.instance;
     60         frames = 0; frameTime = 0; input = null; error = ''; log = '';
     61         document.querySelector('#log').textContent = '';
     62         inputRequested = captureRequested = false; screenshot = null;
     63         frame.src = 'sweep-client.html?instance=' + instance;
     64       }
     65       if (command.action === 'input' && !inputRequested) {
     66         inputRequested = true;
     67         frame.contentWindow.sweepInput();
     68       }
     69       if (command.action === 'capture' && !captureRequested) {
     70         captureRequested = true;
     71         frame.contentWindow.Module._sweep_capture();
     72       }
     73       if (command.action === 'finish' || (command.action === 'capture' && screenshot)) {
     74         const result = await request('/finish', {instance, log, screenshot, input});
     75         document.querySelector('#result').textContent = JSON.stringify(result, null, 2);
     76         input = null; inputRequested = captureRequested = false; screenshot = null; log = '';
     77         document.querySelector('#log').textContent = '';
     78       }
     79       await new Promise(resolve => setTimeout(resolve, 250));
     80     }
     81     frame.src = 'about:blank';
     82     await window.sweepAudio.close();
     83   } catch (failure) {
     84     status.textContent = 'Sweep controller failed: ' + failure.stack;
     85     throw failure;
     86   }
     87 };
     88 </script>
     89 </html>