tools/client/test_team_loot_order.mjs (5525B)
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 games = []; 12 const evidence = {collections: []}; 13 const action = event => event.opcode === '5e477770' ? Buffer.from(event.hex, 'hex').readUInt32BE(29) : 0; 14 try { 15 for (let id = 1; id <= 3; ++id) { 16 const context = await browser.newContext({viewport: {width: 1024, height: 768}}); 17 await context.addCookies([{name: 'ao_client', value: 'team-loot-' + id, url}]); 18 const page = await context.newPage(); 19 const game = await GameTest.attach(page); 20 games.push(game); 21 await page.goto(url); 22 await game.ready(); 23 if (id <= 2) await game.wait('team leader delivered', () => game.events.some(event => 24 event.direction === 'framereceived' && action(event) === 35)); 25 } 26 const server = async () => (await games[0].page.request.get(url + '/test-state')).json(); 27 const command = async (game, text) => { 28 await game.key('Enter'); 29 await game.page.keyboard.type(text); 30 await game.key('Enter'); 31 }; 32 const setMode = async (text, mode) => { 33 const sequences = games.map(game => game.sequence); 34 await command(games[0], '/team loot ' + text); 35 for (let index = 0; index < 2; ++index) await games[index].wait('native loot mode changed', () => games[index].events.some(event => 36 event.sequence > sequences[index] && event.direction === 'framereceived' && action(event) === 127 && 37 Buffer.from(event.hex, 'hex').readUInt32BE(37) === mode)); 38 assert.equal((await server()).mode, mode); 39 const sequence = games[1].sequence; 40 await command(games[1], '/team loot'); 41 await games[1].wait('native current loot order', () => games[1].events.some(event => event.sequence > sequence && 42 event.direction === 'framereceived' && action(event) === 145 && Buffer.from(event.hex, 'hex').readUInt32BE(45) === mode)); 43 await games[1].save(output + '/mode-' + mode); 44 }; 45 const use = async (game, id, accepted) => { 46 const sequence = game.sequence; 47 await game.click(...await game.pick(51050, id), 'right'); 48 await game.wait('actual corpse Use result', () => game.events.some(event => event.sequence > sequence && 49 event.direction === 'framereceived' && event.opcode === '52526858' && 50 Buffer.from(event.hex, 'hex').readUInt32BE(29) === (accepted ? 1 : 2))); 51 if (accepted) await game.wait('corpse loot window', state => state.regions.some(region => 52 region[0] === 0 && region[1] === 0 && region[2] === 178 && region[3] === 128)); 53 }; 54 await setMode('all', 3); 55 for (const [index, owner] of [2, 1, 2, 1, 2].entries()) { 56 if (index === 1) await setMode('leader', 4); 57 if (index === 2) await setMode('alpha', 5); 58 const id = 1000000 + index; 59 assert((await games[0].page.request.post(url + '/test-kill/' + id)).ok()); 60 for (const game of games) await game.wait('new typed corpse', state => state.actors.some(actor => actor.type === 51050 && actor.id === id)); 61 const before = await server(); 62 assert.equal(before.corpses[id].looter, index === 0 ? 0 : owner); 63 if (index === 0) { 64 await use(games[2], id, false); 65 assert.deepEqual(await server(), before); 66 await use(games[0], id, true); 67 await games[0].click(170, 8); 68 } else { 69 const denied = games[owner === 1 ? 1 : 0]; 70 await use(denied, id, false); 71 assert.deepEqual(await server(), before); 72 await denied.save(output + '/refused-' + index); 73 } 74 const game = games[owner - 1]; 75 await use(game, id, true); 76 const count = game.local().inventory.length; 77 await game.click(30, 45); 78 await game.wait('native assigned item collected', state => game.local(state).inventory.length === count + 1); 79 const after = await server(); 80 const item = before.corpses[id].items[0]; 81 assert(after.players[owner].Items.some(owned => owned.ID === item.ID && owned.Template === item.Template)); 82 assert.deepEqual(after.corpses[id].items, []); 83 assert.equal(after.corpses[id].credits, 0); 84 evidence.collections.push({id, owner, item, mode: before.corpses[id].mode}); 85 await game.save(output + '/collected-' + index); 86 await game.click(170, 8); 87 } 88 evidence.after = await server(); 89 for (let index = 0; index < 2; ++index) { 90 const game = games[index]; 91 await game.page.reload(); 92 await game.ready(); 93 const expected = evidence.after.players[index + 1].Items.map(item => [item.ID, item.Slot - 16, item.Count]); 94 assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.count]).sort((a, b) => a[0] - b[0]), expected); 95 } 96 assert(games.every(game => game.errors.length === 0)); 97 console.log('PASS: real All/Leader/Alpha commands, native refusal/collection, repeating allocation and exact page/cold ownership'); 98 } finally { 99 try { 100 await fs.mkdir(output, {recursive: true}); 101 await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); 102 for (let index = 0; index < games.length; ++index) await games[index].save(output + '/final-' + (index + 1)); 103 } finally { await browser.close(); } 104 }