1import { apiIO, playOut } from './bot.mjs'; 2import { maxHp } from '../web/js/stats.js'; 3import { applyEvent } from '../web/js/playback.js'; 4 5export function replayFight(before, after) { 6 const hp = new Map(); 7 const cap = new Map(); 8 for (const u of before.party.units) { 9 hp.set(u.id, Math.min(u.stats.hp, maxHp(u))); 10 cap.set(u.id, maxHp(u)); 11 } 12 for (const e of after.encounter?.enemies || []) { 13 hp.set(e.id, e.stats.hp); 14 cap.set(e.id, e.stats.maxHp); 15 } 16 const out = []; 17 const heroes = new Set(before.party.units.map((u) => u.id)); 18 for (const ev of after.log || []) { 19 const same = heroes.has(ev.actor) === heroes.has(ev.target); 20 if (!(hp.get(ev.actor) > 0) && ev.ability !== 'thorns') out.push(`a fallen unit acts: ${ev.text}`); 21 if (!(hp.get(ev.target) > 0)) out.push(`a fallen unit is targeted: ${ev.text}`); 22 const friendly = ev.kind === 'heal' || ev.kind === 'buff'; 23 if (ev.ability !== 'thorns' && (friendly || ev.kind === 'debuff' || ev.kind === 'damage' || ev.kind === 'aoe') && friendly !== same) out.push(`${ev.kind} crosses sides the wrong way: ${ev.text}`); 24 if (ev.dodged && ev.amount !== 0) out.push(`a dodged hit lands: ${ev.text}`); 25 applyEvent(hp, ev, cap.get(ev.target)); 26 } 27 const was = Object.fromEntries(before.party.units.map((u) => [u.id, u.level])); 28 for (const u of after.party.units) if (was[u.id] === u.level && hp.get(u.id) !== u.stats.hp) out.push(`${u.name} replays to ${hp.get(u.id)} hp, server ${u.stats.hp}`); 29 if (after.rewards) for (const e of after.encounter.enemies) if (hp.get(e.id) !== 0) out.push(`${e.name} replays to ${hp.get(e.id)} hp after a won fight`); 30 return out; 31} 32 33export async function checkFights(api, seeds) { 34 const io = apiIO(api); 35 const fight = io.fight; 36 const report = { fights: 0, kinds: {}, abilities: {}, problems: [] }; 37 io.fight = async (s) => { 38 const after = await fight(s); 39 if (!after.encounter) return after; 40 report.fights++; 41 for (const ev of after.log) { 42 report.kinds[ev.kind] = (report.kinds[ev.kind] || 0) + 1; 43 report.abilities[ev.ability] = (report.abilities[ev.ability] || 0) + 1; 44 } 45 for (const p of replayFight(s, after)) report.problems.push(`seed ${after.seed} fight ${after.summary.battles}: ${p}`); 46 return after; 47 }; 48 for (const seed of seeds) await playOut(io, await api('/api/new', { seed }), {}); 49 return report; 50} 51 52if (import.meta.url === `file://${process.argv[1]}`) { 53 const [base, ...seeds] = process.argv.slice(2); 54 const api = async (path, body) => (await fetch(base + path, body === undefined ? { headers: { 'X-Player': 'f'.repeat(32) } } : { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Player': 'f'.repeat(32) }, body: JSON.stringify(body) })).json(); 55 const r = await checkFights(api, seeds.length ? seeds.map(Number) : [1, 2, 3, 4, 5]); 56 console.log(`${r.fights} fights, kinds ${JSON.stringify(r.kinds)}, abilities ${JSON.stringify(r.abilities)}`); 57 for (const p of r.problems.slice(0, 10)) console.log(`FAIL ${p}`); 58 process.exit(r.problems.length ? 1 : 0); 59}