aorecomp

Anarchy Online classic in the browser

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


tools/client/test_pet_attack.mjs (4839B)


      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', args: [
      9   '--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 const actor = (game, id) => game.state.actors.find(actor => actor.type === 50000 && actor.id === id);
     13 try {
     14   for (let index = 0; index < 2; index++) {
     15     const context = await browser.newContext({viewport: {width: 1024, height: 768}});
     16     await context.addCookies([{name: 'ao_client', value: 'pet-cast-' + index, url}]);
     17     const page = await context.newPage(), game = await GameTest.attach(page);
     18     games.push(game);
     19     await page.goto(url);
     20     await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204);
     21     await page.waitForFunction(() => touchRegion(500, 400)?.world === true);
     22   }
     23   const [owner] = games;
     24   const server = async () => (await owner.page.request.get(url + '/test-state')).json();
     25   const command = async text => {
     26     await owner.key('Enter');
     27     await owner.page.keyboard.type('/pet ' + text);
     28     await owner.key('Enter');
     29   };
     30   await Promise.all(games.map(game => game.wait('pets and target visible', () => actor(game, 1000001) && actor(game, 1000002) && actor(game, 1000000))));
     31   evidence.before = await server();
     32   if (!owner.state.camera.firstPerson) await owner.key('F8');
     33   await owner.hold('KeyA', 'face the enemy along the open street', state => owner.local(state).rotation[1] < -0.69);
     34   await owner.click(...await owner.pick(50000, 1000000));
     35   await owner.wait('enemy selected through native picking', state => state.selected[0] === 50000 && state.selected[1] === 1000000);
     36   await command('attack');
     37   await Promise.all(games.map(game => game.wait('pet attacking selected enemy', () => actor(game, 1000001).combatTarget === 1000000)));
     38   await command('behind');
     39   await Promise.all(games.map(game => game.wait('behind cancels attack', () => actor(game, 1000001).combatTarget === 0)));
     40   evidence.retreated = await server();
     41   assert.equal(evidence.retreated.follow, 1);
     42   assert.equal(evidence.retreated.targetHealth, 32);
     43   await command('wait');
     44   await owner.waitFor('wait clears owner follow', async () => (await server()).follow === 0);
     45 
     46   await command('attack');
     47   await owner.waitFor('pet lands first attack and enemy retaliates', async () => {
     48     const state = await server();
     49     return state.targetHealth === 16 && state.petHealth === 9;
     50   });
     51   evidence.retaliation = await server();
     52   assert.equal(evidence.retaliation.targetFighting, 1000001);
     53   assert.equal(evidence.retaliation.ownerHealth, evidence.before.ownerHealth);
     54   await Promise.all(games.map((game, index) => game.save(output + '/combat-' + index)));
     55   await owner.waitFor('pet kill credits owner', async () => (await server()).experience === 100);
     56   evidence.kill = await server();
     57   assert.equal(evidence.kill.targetHealth, 0);
     58   assert.equal(evidence.kill.looter, 1);
     59   assert.equal(evidence.kill.credits, 5);
     60   assert.equal(evidence.kill.petHealth, 9);
     61   assert.equal(evidence.kill.ownerNano, 25);
     62   await Promise.all(games.map(game => game.wait('native combat ends', () => actor(game, 1000001).combatTarget === 0)));
     63 
     64   await Promise.all(games.map(async game => { await game.page.reload(); await game.ready(); assert.equal((await game.page.request.post(url + '/test-defer-recovery')).status(), 204); }));
     65   await Promise.all(games.map(game => game.wait('owner membership recovers', () => actor(game, 1000001) && actor(game, 1).stats[458] === 1)));
     66   evidence.reconnected = await server();
     67   assert.equal(evidence.reconnected.experience, 100);
     68   await command('terminate');
     69   await Promise.all(games.map(game => game.wait('only owner pet removed', () => !actor(game, 1000001) && actor(game, 1000002) && actor(game, 1).stats[458] === 0)));
     70   evidence.final = await server();
     71   assert.equal(evidence.final.ownerPets, 0);
     72   assert.equal(evidence.final.peerPets, 1);
     73   for (const game of games) {
     74     assert.deepEqual(game.errors, []);
     75     assert.equal(game.dropped, 0);
     76   }
     77   console.log('PASS: native pet selection/attack, behind/wait, pursuit, retaliation, owner reward, reconnect and termination');
     78 } finally {
     79   try {
     80     await fs.mkdir(output, {recursive: true});
     81     await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n');
     82     await Promise.all(games.map((game, index) => game.save(output + '/final-' + index)));
     83   } finally { await browser.close(); }
     84 }