aorecomp

Anarchy Online classic in the browser

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


tools/client/test_weapon_hit_admission.mjs (4390B)


      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',
      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 evidence = {attempts: []};
     12 let game;
     13 try {
     14   const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     15   await context.addCookies([{name: 'ao_client', value: 'weapon-hit-admission', url}]);
     16   const page = await context.newPage();
     17   game = await GameTest.attach(page);
     18   await page.goto(url);
     19   await game.ready();
     20   const server = async () => (await page.request.get(url + '/test-state')).json();
     21   evidence.initial = await server();
     22   await game.key('KeyI');
     23   await page.keyboard.press('Control+1');
     24 
     25   // Exercise real pickup/place twice, including reuse of the refused source.
     26   for (let attempt = 0; attempt < 2; ++attempt) {
     27     await game.wait('native equipment action finishes', state =>
     28       !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6));
     29     const sequence = game.sequence;
     30     await game.click(681, 45);
     31     await game.wait('rod picked from carried inventory', state => state.dragged[0] === 104 && state.dragged[1] === 48);
     32     await game.click(867, 206);
     33     await game.wait('weapon move receives a native reply', () => game.events.some(event =>
     34       event.sequence > sequence && event.direction === 'framesent' && event.opcode === '47537a24') &&
     35       (game.events.filter(event => event.sequence > sequence && event.direction === 'framereceived' &&
     36         event.opcode === '47537a24').length === 2 || game.events.some(event =>
     37         event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '3b1d2268')));
     38     evidence.attempts.push(await server());
     39     await game.save(output + '/refused-' + attempt);
     40     assert.deepEqual(evidence.attempts[attempt], evidence.initial);
     41     const moves = game.events.filter(event => event.sequence > sequence &&
     42       event.direction === 'framereceived' && event.opcode === '47537a24').map(event => {
     43       const bytes = Buffer.from(event.hex, 'hex');
     44       return [bytes.readUInt32BE(29), bytes.readUInt32BE(33), bytes.readUInt32BE(45)];
     45     });
     46     assert.deepEqual(moves, [[104, 48, 49], [104, 49, 48]]);
     47     await game.wait('refused rod source remains usable', state => state.grids.some(grid => grid.cells.some(cell =>
     48       cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy)));
     49     assert.deepEqual(game.state.dragged, [0, 0]);
     50     assert.equal(game.local().inventory.find(item => item.id === 1).slot, 48);
     51   }
     52 
     53   // A supported weapon still completes wear, removal and a second wear.
     54   for (const remove of [true, false]) {
     55     await game.click(735, 45, 'right');
     56     await game.wait('supported bat wears', state => game.local(state).inventory.some(item => item.id === 2 && item.slot === 6));
     57     if (remove) {
     58       await game.wait('bat equip action finishes', state =>
     59         !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6));
     60       await game.click(867, 206, 'right');
     61       await game.wait('supported bat removes', state => game.local(state).inventory.some(item => item.id === 2 && item.slot === 49));
     62     }
     63   }
     64   evidence.after = await server();
     65   await game.save(output + '/supported');
     66   await page.reload();
     67   await game.ready();
     68   assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.quality]).sort((a, b) => a[0] - b[0]),
     69     [[1, 48, 53], [2, 6, 1]]);
     70   assert.deepEqual(await server(), evidence.after);
     71   assert.equal(evidence.after.nanoMaximum, evidence.initial.nanoMaximum);
     72   assert.deepEqual(game.errors, []);
     73   console.log('PASS: two automatic-hit weapon refusals retain a usable source; supported bat wear/removal, page replay and cold ownership agree');
     74 } finally {
     75   try {
     76     await fs.mkdir(output, {recursive: true});
     77     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     78     if (game) await game.save(output + '/final');
     79   } finally { await browser.close(); }
     80 }