aorecomp

Anarchy Online classic in the browser

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


tools/client/test_kerans.mjs (10502B)


      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, button = 'right') {
     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, button);
     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: 'kerans-' + 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 === 6) {
     32     assert.equal(owner.local().stats[4], 1);
     33     assert.equal(owner.local().stats[114], 400);
     34     assert.equal(owner.local().stats[148], 283);
     35     await owner.key('KeyI');
     36     await owner.page.keyboard.press('Control+1');
     37     for (const id of [1, 2, 3, 4]) {
     38       const sequence = observer.sequence;
     39       await bagClick(owner, id);
     40       await owner.wait('selected Kerans worn', snapshot => owner.local(snapshot).inventory.some(item => item.id === id && item.slot === 6));
     41       await observer.wait('observer receives selected Kerans', () => observer.events.some(event =>
     42         event.sequence > sequence && event.direction === 'framereceived' && event.opcode === '3b1d2268' &&
     43         event.actor[0] === 51018 && event.actor[1] === id));
     44       const event = observer.events.find(event => event.sequence > sequence && event.direction === 'framereceived' &&
     45         event.opcode === '3b1d2268' && event.actor[0] === 51018 && event.actor[1] === id);
     46       const packet = Buffer.from(event.hex, 'hex'), stats = new Map();
     47       for (let offset = 59; offset < packet.readUInt16BE(6) - 4; offset += 8)
     48         stats.set(packet.readUInt32BE(offset), packet.readUInt32BE(offset + 4));
     49       const [quality, definition, maximum, critical, value, low, high, sense] = {
     50         1: [2, 160476, 27, 25, 2888, 160476, 160477, 2],
     51         2: [50, 160477, 92, 91, 37732, 160476, 160477, 2],
     52         3: [98, 160477, 157, 156, 139476, 160477, 160477, 2],
     53         4: [100, 160478, 159, 158, 142334, 160478, 160479, 4],
     54       }[id];
     55       assert.equal(stats.get(23), definition);
     56       const base = {
     57         160476: {74: 2874, 284: 24, 285: 26, 286: 1, 294: 150},
     58         160477: {74: 139476, 284: 156, 285: 157, 286: 1, 294: 150},
     59         160478: {74: 142292, 284: 157, 285: 158, 286: 1, 294: 150},
     60       }[definition];
     61       for (const [stat, expected] of Object.entries({74: value, 284: critical, 285: maximum,
     62         286: 1, 294: 150, 701: quality, 702: low, 703: high}))
     63         assert.equal(stats.get(Number(stat)) ?? base[stat], expected, 'selected Kerans stat ' + stat);
     64       const worn = await state();
     65       assert.equal(worn.sense, 300 + sense);
     66       assert.equal(worn.rating, 475);
     67       assert.equal(worn.maxNano, 1050);
     68       await owner.wait('native ability-derived foundations', snapshot =>
     69         owner.local(snapshot).stats[22] === 475 && owner.local(snapshot).stats[221] === 1050);
     70       if (id >= 3) {
     71         await owner.key('KeyU');
     72         await owner.save(output + '/sense-' + sense);
     73         await owner.key('KeyU');
     74       }
     75       evidence['worn' + id] = {native: owner.local().inventory, server: await state()};
     76       await owner.save(output + '/worn-' + id);
     77       if (id !== 4) {
     78         await owner.click(867, 206, 'right');
     79         await owner.wait('same Kerans removed', snapshot => owner.local(snapshot).inventory.some(item => item.id === id && item.slot >= 48));
     80         assert.equal((await state()).sense, 300);
     81         assert.equal((await state()).maxNano, 1047);
     82       }
     83     }
     84     const rejected = await state(), rejectionSequence = owner.sequence;
     85     await bagClick(owner, 5);
     86     await owner.save(output + '/rejected-101');
     87     assert(!owner.events.some(event => event.sequence > rejectionSequence && event.direction === 'framesent' && event.opcode === '47537a24'));
     88     assert.deepEqual((await state()).inventory, rejected.inventory);
     89 
     90     // Native ammunition-on-weapon input loads exactly one hundred twenty bullets.
     91     await bagClick(owner, 6, 'left');
     92     await owner.click(867, 206, 'right');
     93     await owner.wait('bullet stack debited', snapshot => owner.local(snapshot).inventory.some(item => item.id === 6 && item.count === 80));
     94     assert.equal((await state()).inventory.Items[3].Ammo, 120);
     95     await owner.wait('native reload completes', snapshot => !owner.local(snapshot).actions.includes(5));
     96 
     97     // An ordinary Q attack consumes one round from the selected QL100 instance.
     98     const deadline = Date.now() + 10000;
     99     while (owner.state.selected[0] !== 50000 || owner.state.selected[1] !== rejected.target) {
    100       assert(Date.now() < deadline, 'Native Tab did not select the controlled Leet');
    101       const selected = owner.state.selected;
    102       await owner.key('Tab');
    103       await owner.wait('native target cycle advances', snapshot => snapshot.selected[0] !== selected[0] || snapshot.selected[1] !== selected[1]);
    104     }
    105     const attack = owner.sequence;
    106     await owner.key('KeyQ');
    107     await owner.wait('ordinary selected-quality hit', () => owner.events.some(event => event.sequence > attack &&
    108       event.direction === 'framereceived' && event.opcode === '46002f16' && event.actor[1] === owner.local().id));
    109     const result = owner.events.find(event => event.sequence > attack && event.direction === 'framereceived' &&
    110       event.opcode === '46002f16' && event.actor[1] === owner.local().id);
    111     assert.equal(Buffer.from(result.hex, 'hex').readUInt32BE(33), 119);
    112     await owner.key('KeyQ');
    113     await Promise.all(games.map(game => game.wait('native selected-quality impact', snapshot =>
    114       snapshot.actors.some(actor => actor.id === rejected.target && actor.stats[27] === 98))));
    115     evidence.hit = await state();
    116     assert.equal(evidence.hit.health, 98);
    117     assert.equal(evidence.hit.fighting, 0);
    118     assert.equal(evidence.hit.inventory.Items[3].Ammo, 119);
    119     await owner.save(output + '/hit');
    120 
    121     // Cancel, then accept the QL2 instance's sale at the original training machine.
    122     await owner.page.keyboard.press('Control+1');
    123     await owner.wait('weapon panel closed', snapshot => !snapshot.regions.some(([left, top, right, bottom]) =>
    124       left === 832 && top === 0 && right === 1024 && bottom === 360));
    125     if (!owner.state.camera.firstPerson) await owner.key('F8');
    126     const offered = await state();
    127     for (let attempt = 0; attempt < 2; ++attempt) {
    128       await owner.click(...await owner.pick(51035, 3221226422), 'right');
    129       await owner.wait('training machine open', snapshot => snapshot.regions.some(([left, top, right, bottom]) =>
    130         left === 832 && top === 0 && right === 1024 && bottom === 494));
    131       const cell = owner.local().inventory.find(item => item.id === 1).slot - 48;
    132       await owner.click(681 + cell % 3 * 54, 45 + Math.floor(cell / 3) * 54);
    133       const sequence = owner.sequence;
    134       await owner.click(864, 416);
    135       await owner.wait('sale offered', () => owner.events.some(event => event.sequence > sequence &&
    136         event.direction === 'framesent' && event.opcode === '36284f6e'));
    137       assert.deepEqual((await state()).inventory, offered.inventory);
    138       await owner.save(output + '/sale-offer-' + attempt);
    139       if (attempt === 0) {
    140         await owner.click(945, 475);
    141         await owner.wait('cancelled Kerans restored', snapshot => owner.local(snapshot).inventory.some(item => item.id === 1 && item.slot === 48));
    142         assert.deepEqual((await state()).inventory, offered.inventory);
    143       }
    144     }
    145     await owner.click(874, 475);
    146     await owner.wait('exact selected-quality sale', snapshot => owner.local(snapshot).stats[61] === 145);
    147     evidence.sale = await state();
    148     await owner.page.reload();
    149     await owner.ready();
    150   }
    151   assert.deepEqual(owner.local().inventory.map(item => [item.id, item.slot, item.quality, ...item.templates, item.count]).sort((a, b) => a[0] - b[0]), [
    152     [2, 49, 50, 160476, 160477, 1], [3, 50, 98, 160477, 160477, 1],
    153     [4, 6, 100, 160478, 160479, 1], [5, 52, 101, 160478, 160479, 1], [6, 53, 1, 21605, 21605, 80],
    154   ]);
    155   assert.equal(owner.local().stats[61], 145);
    156   assert.deepEqual(observer.local().inventory.map(item => [item.id, item.slot, item.quality, ...item.templates, item.count]),
    157     []);
    158   evidence.final = {native: owner.local().inventory, server: await state()};
    159   assert.equal(evidence.final.server.inventory.Items[2].Ammo, 119);
    160   assert(evidence.final.server.ownerHealth > 0);
    161   assert.equal(evidence.final.server.sense, 304);
    162   assert.equal(owner.local().stats[22], 475);
    163   assert.equal(owner.local().stats[221], 1050);
    164   assert.equal(observer.local().stats[221], 1047);
    165   await owner.key('KeyU');
    166   await owner.save(output + '/retained-sense');
    167   await owner.key('KeyU');
    168   if (!owner.state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 104 && !cell.dummy))) {
    169     await owner.key('KeyI');
    170     await owner.wait('retained inventory opens', snapshot => snapshot.grids.some(grid =>
    171       grid.cells.some(cell => cell.identity[0] === 104 && !cell.dummy)));
    172   }
    173   assert(games.every(game => game.errors.length === 0));
    174   console.log('PASS: native Kerans qualities, wear/removal, strict refusal, bullet reload/shot, exact sale and page/cold ownership');
    175 } finally {
    176   try {
    177     await fs.mkdir(output, {recursive: true});
    178     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2));
    179     await Promise.all(games.map((game, index) => game.save(output + '/final-' + index)));
    180   } finally { await browser.close(); }
    181 }