aorecomp

Anarchy Online classic in the browser

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


tools/client/test_outdoor_navigation.mjs (5470B)


      1 import assert from 'node:assert/strict';
      2 import fs from 'node:fs/promises';
      3 import {createRequire} from 'node:module';
      4 import {GameTest} from './game_test.mjs';
      5 
      6 const {chromium} = createRequire(import.meta.url)('playwright');
      7 const [url, output] = process.argv.slice(2);
      8 const browser = await chromium.launch({executablePath: '/usr/bin/chromium', headless: false,
      9   args: ['--enable-gpu', '--use-gl=angle', '--use-angle=gl', '--no-sandbox', '--disable-dev-shm-usage',
     10     '--disable-background-timer-throttling', '--disable-renderer-backgrounding']});
     11 const games = [], evidence = {samples: []};
     12 let enemy;
     13 const actor = game => game.state.actors.find(actor => actor.type === 50000 && actor.id === enemy);
     14 const state = async () => (await games[0].page.request.get(url + '/test-state')).json();
     15 // Fresh native snapshots arrive between these two authoritative pose reads.
     16 const matches = sample => {
     17   const delta = sample.server.position.map((value, axis) => value - sample.before.position[axis]);
     18   const length = delta.reduce((sum, value) => sum + value * value, 0);
     19   return sample.clients.every(client => {
     20     const along = length ? Math.max(0, Math.min(1, delta.reduce((sum, value, axis) =>
     21       sum + value * (client.position[axis] - sample.before.position[axis]), 0) / length)) : 0;
     22     return Math.hypot(...client.position.map((value, axis) =>
     23       value - sample.before.position[axis] - along * delta[axis])) < .4;
     24   });
     25 };
     26 const settled = sample => sample.clients.every(client =>
     27   Math.hypot(...client.position.map((value, axis) => value - sample.server.position[axis])) < .15);
     28 async function observe(label, complete) {
     29   const deadline = Date.now() + 20000;
     30   for (;;) {
     31     const before = await state();
     32     await Promise.all(games.map(game => game.snapshot()));
     33     const sample = {before, server: await state(), clients: games.map(game => ({position: actor(game).position,
     34       target: actor(game).combatTarget, health: actor(game).stats[27]}))};
     35     evidence.samples.push(sample);
     36     if (complete(sample)) return sample;
     37     assert(Date.now() < deadline, label + ': ' + JSON.stringify(sample.server));
     38   }
     39 }
     40 try {
     41   for (let index = 0; index < 2; ++index) {
     42     const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     43     await context.addCookies([{name: 'ao_client', value: 'detour-' + index, url}]);
     44     const page = await context.newPage(), game = await GameTest.attach(page);
     45     games.push(game);
     46     await page.goto(url);
     47     await game.ready();
     48   }
     49   const [owner, observer] = games;
     50   evidence.initial = await state();
     51   enemy = evidence.initial.enemy;
     52   await Promise.all(games.map(game => game.wait('original wall actors', () => !!actor(game))));
     53   if (!observer.state.camera.firstPerson) await observer.key('F8');
     54   assert.equal(evidence.initial.visible, false);
     55   assert((await owner.page.request.post(url + '/test-pursue')).ok());
     56   evidence.detour = await observe('original outdoor wall detour did not progress', sample =>
     57     sample.server.position[2] > 65 && sample.server.moving && matches(sample));
     58   assert.equal(evidence.detour.server.ownerHealth, evidence.initial.ownerHealth);
     59   await Promise.all(games.map((game, index) => game.save(output + '/detour-' + index)));
     60   evidence.arrived = await observe('pursuing Leet did not reach usable attack range', sample =>
     61     !sample.server.moving && sample.server.visible && sample.server.ownerHealth < evidence.initial.ownerHealth && settled(sample));
     62   assert.equal(evidence.arrived.server.fighting, 1);
     63 
     64   // Native Tab and Q verify an ordinary attack after the physical detour.
     65   await owner.key('Tab');
     66   await owner.wait('native Leet selection', snapshot => snapshot.selected[0] === 50000 && snapshot.selected[1] === enemy);
     67   await owner.key('KeyQ');
     68   evidence.hit = await observe('ordinary native attack after arrival did not hit', sample => sample.server.health < evidence.initial.health);
     69   await owner.key('KeyQ');
     70   await owner.wait('ordinary attack stops', () => owner.local().combatTarget === 0);
     71   await Promise.all(games.map((game, index) => game.save(output + '/combat-' + index)));
     72   await observer.page.reload();
     73   await observer.ready();
     74   await observer.wait('settled actor replay', () => !!actor(observer));
     75   evidence.replayed = await observe('observer replay changed the settled NPC pose', sample => !sample.server.moving && settled(sample));
     76   assert((await owner.page.request.post(url + '/test-return')).ok());
     77   evidence.returning = await observe('return did not walk around the same wall', sample =>
     78     sample.server.position[2] > 65 && sample.server.returning && sample.server.moving && matches(sample));
     79   evidence.returned = await observe('NPC did not reach its controlled home', sample =>
     80     !sample.server.returning && !sample.server.moving && settled(sample));
     81   assert.equal(evidence.returned.server.fighting, 0);
     82   assert(Math.hypot(...evidence.returned.server.position.map((value, axis) => value - evidence.initial.home[axis])) < 1.01);
     83   assert(games.every(game => game.errors.length === 0));
     84   console.log('PASS: both native replicas follow the original outdoor wall detour, ordinary combat, observer replay and return home');
     85 } finally {
     86   try {
     87     await fs.mkdir(output, {recursive: true});
     88     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2));
     89     await Promise.all(games.map((game, index) => game.save(output + '/final-' + index)));
     90   } finally { await browser.close(); }
     91 }