aorecomp

Anarchy Online classic in the browser

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


tools/client/test_sledgehammer.mjs (8692B)


      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 = {};
     12 async function bagClick(game, id) {
     13   await game.wait('native equipment action finishes', state =>
     14     !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6));
     15   const cell = game.local().inventory.find(item => item.id === id).slot - 48;
     16   assert(cell >= 0 && cell < 9);
     17   await game.click(681 + cell % 3 * 54, 45 + Math.floor(cell / 3) * 54, 'right');
     18 }
     19 try {
     20   for (const index of [0, 1]) {
     21     const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     22     await context.addCookies([{name: 'ao_client', value: 'sledgehammer-' + index, url}]);
     23     const page = await context.newPage(), game = await GameTest.attach(page);
     24     games.push(game);
     25     await page.goto(url);
     26     await game.ready();
     27   }
     28   const [owner, observer] = games;
     29   const state = async () => (await owner.page.request.get(url + '/test-state')).json();
     30   evidence.initial = await state();
     31   if (evidence.initial.inventory.Items.length === 4) {
     32     assert.equal(owner.local().stats[4], 1);
     33     assert.equal(owner.local().stats[107], 40);
     34     assert.equal(owner.local().stats[142], 23);
     35     assert.equal(owner.local().stats[144], 7);
     36     await owner.key('KeyI');
     37     await owner.page.keyboard.press('Control+1');
     38     for (const id of [1, 2, 3]) {
     39       const sequence = observer.sequence;
     40       await bagClick(owner, id);
     41       await owner.wait('selected Sledgehammer worn', snapshot => owner.local(snapshot).inventory.some(item => item.id === id && item.slot === 6));
     42       await observer.wait('observer receives selected Sledgehammer', () => observer.events.some(event =>
     43         event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '3b1d2268' &&
     44         event.actor[0] === 51018 && event.actor[1] === id));
     45       const event = observer.events.find(event => event.sequence > sequence && event.direction === 'framereceived' &&
     46         event.opcode === '3b1d2268' && event.actor[0] === 51018 && event.actor[1] === id);
     47       const packet = Buffer.from(event.hex, 'hex'), stats = new Map();
     48       for (let offset = 59; offset < packet.readUInt16BE(6) - 4; offset += 8)
     49         stats.set(packet.readUInt32BE(offset), packet.readUInt32BE(offset + 4));
     50       const [quality, definition, minimum, maximum, critical, value, attack] = {
     51         1: [2, 129026, 1, 44, 20, 1165, 309], 2: [10, 129026, 1, 79, 24, 1653, 305],
     52         3: [11, 129027, 1, 84, 24, 1768, 305],
     53       }[id];
     54       assert.equal(stats.get(23), definition);
     55       const base = {
     56         129026: {74: 1159, 284: 20, 285: 40, 286: 1, 294: 310},
     57         129027: {74: 3360, 284: 28, 285: 123, 286: 1, 294: 300},
     58       }[definition];
     59       for (const [stat, expected] of Object.entries({74: value, 284: critical, 285: maximum, 286: minimum, 294: attack,
     60         701: quality, 702: 129026, 703: 129027}))
     61         assert.equal(stats.get(Number(stat)) ?? base[stat], expected, 'selected native Sledgehammer stat ' + stat);
     62       evidence['worn' + id] = {native: owner.local().inventory, server: await state()};
     63       await owner.save(output + '/worn-' + id);
     64       if (id !== 3) {
     65         await owner.click(867, 206, 'right');
     66         await owner.wait('same Sledgehammer removed', snapshot => owner.local(snapshot).inventory.some(item => item.id === id && item.slot >= 48));
     67       }
     68     }
     69     const rejected = await state(), rejectionSequence = owner.sequence;
     70     await bagClick(owner, 4);
     71     await owner.save(output + '/rejected-12');
     72     assert(!owner.events.some(event => event.sequence > rejectionSequence && event.direction === 'framesent' && event.opcode === '47537a24'));
     73     assert.deepEqual((await state()).inventory, rejected.inventory);
     74 
     75     // An ordinary Q attack uses the selected QL11 damage and consumes no ammunition.
     76     const deadline = Date.now() + 10000;
     77     while (owner.state.selected[0] !== 50000 || owner.state.selected[1] !== rejected.target) {
     78       assert(Date.now() < deadline, 'Native Tab did not select the controlled Leet');
     79       const selected = owner.state.selected;
     80       await owner.key('Tab');
     81       await owner.wait('native target cycle advances', snapshot => snapshot.selected[0] !== selected[0] || snapshot.selected[1] !== selected[1]);
     82     }
     83     const attack = owner.sequence;
     84     await owner.key('KeyQ');
     85     await owner.wait('ordinary selected-quality hit', () => owner.events.some(event => event.sequence > attack &&
     86       event.direction === 'framereceived' && event.opcode === '46002f16' && event.actor[1] === owner.local().id));
     87     const result = owner.events.find(event => event.sequence > attack && event.direction === 'framereceived' &&
     88       event.opcode === '46002f16' && event.actor[1] === owner.local().id);
     89     assert.equal(Buffer.from(result.hex, 'hex').readUInt32BE(33), 0xffffffff);
     90     await owner.key('KeyQ');
     91     await Promise.all(games.map(game => game.wait('native selected-quality impact', snapshot =>
     92       snapshot.actors.some(actor => actor.id === rejected.target && actor.stats[27] === 99))));
     93     evidence.hit = await state();
     94     assert.equal(evidence.hit.health, 99);
     95     assert.equal(evidence.hit.fighting, 0);
     96     assert.equal(evidence.hit.inventory.Items[2].Ammo, 0);
     97     await owner.save(output + '/hit');
     98 
     99     // Cancel, then accept the QL2 instance's sale at the original training machine.
    100     await owner.page.keyboard.press('Control+1');
    101     await owner.wait('weapon panel closed', snapshot => !snapshot.regions.some(([left, top, right, bottom]) =>
    102       left === 832 && top === 0 && right === 1024 && bottom === 360));
    103     if (!owner.state.camera.firstPerson) await owner.key('F8');
    104     const offered = await state();
    105     for (let attempt = 0; attempt < 2; ++attempt) {
    106       await owner.click(...await owner.pick(51035, 3221226422), 'right');
    107       await owner.wait('training machine open', snapshot => snapshot.regions.some(([left, top, right, bottom]) =>
    108         left === 832 && top === 0 && right === 1024 && bottom === 494));
    109       const cell = owner.local().inventory.find(item => item.id === 1).slot - 48;
    110       await owner.click(681 + cell % 3 * 54, 45 + Math.floor(cell / 3) * 54);
    111       const sequence = owner.sequence;
    112       await owner.click(864, 416);
    113       await owner.wait('sale offered', () => owner.events.some(event => event.sequence > sequence &&
    114         event.direction === 'framesent' && event.opcode === '36284f6e'));
    115       assert.deepEqual((await state()).inventory, offered.inventory);
    116       await owner.save(output + '/sale-offer-' + attempt);
    117       if (attempt === 0) {
    118         await owner.click(945, 475);
    119         await owner.wait('cancelled Sledgehammer restored', snapshot => owner.local(snapshot).inventory.some(item => item.id === 1 && item.slot === 48));
    120         assert.deepEqual((await state()).inventory, offered.inventory);
    121       }
    122     }
    123     await owner.click(874, 475);
    124     await owner.wait('exact selected-quality sale', snapshot => owner.local(snapshot).stats[61] === 58);
    125     evidence.sale = await state();
    126     await owner.page.reload();
    127     await owner.ready();
    128   }
    129   assert.deepEqual(owner.local().inventory.map(item => [item.id, item.slot, item.quality, ...item.templates, item.count]).sort((a, b) => a[0] - b[0]), [
    130     [2, 49, 10, 129026, 129027, 1], [3, 6, 11, 129026, 129027, 1], [4, 51, 12, 129026, 129027, 1],
    131   ]);
    132   assert.equal(owner.local().stats[61], 58);
    133   assert.deepEqual(observer.local().inventory.map(item => [item.id, item.slot, item.quality, ...item.templates, item.count]),
    134     []);
    135   evidence.final = {native: owner.local().inventory, server: await state()};
    136   assert.equal(evidence.final.server.inventory.Items[1].Ammo, 0);
    137   assert(evidence.final.server.ownerHealth > 0);
    138   assert(games.every(game => game.errors.length === 0));
    139   console.log('PASS: native Sledgehammer qualities, wear/removal, strict refusal, ordinary melee, exact sale and page/cold ownership');
    140 } finally {
    141   try {
    142     await fs.mkdir(output, {recursive: true});
    143     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2));
    144     await Promise.all(games.map((game, index) => game.save(output + '/final-' + index)));
    145   } finally { await browser.close(); }
    146 }