diablo2-web/scripts/browser/checks/map-save-load.js

40 lines
1.8 KiB
JavaScript

(async () => {
const sleep = (ms) => new Promise(r => setTimeout(r, ms))
const key = (type, code) => window.dispatchEvent(new KeyboardEvent(type, { code, bubbles: true }))
const tap = async (code, ms) => { key('keydown', code); await sleep(ms); key('keyup', code); await sleep(60) }
for (let i = 0; i < 40 && (window.__d2webMap === undefined || window.__d2webMap.tick < 5); i++) await sleep(250)
const state = window.__d2webMap
if (state === undefined) return { error: 'no scene state' }
const snap = () => ({
tick: state.tick, x: Math.round(state.x), y: Math.round(state.y), level: state.playerLevel,
xp: state.playerXp, kills: state.kills, gold: state.gold, bag: state.bagItems,
ground: state.groundItems, monsters: state.monstersNear ? state.monstersNear.length : null,
quests: state.quests, hp: state.playerHp, mana: state.playerMana,
})
// Fight for a few seconds: walk and swing repeatedly.
key('keydown', 'KeyD')
for (let i = 0; i < 12; i++) await tap('Space', 220)
key('keyup', 'KeyD')
key('keydown', 'KeyS')
for (let i = 0; i < 12; i++) await tap('Space', 220)
key('keyup', 'KeyS')
await sleep(600)
const before = snap()
key('keydown', 'KeyK'); await sleep(200); key('keyup', 'KeyK')
await sleep(400)
const savedAt = snap()
key('keydown', 'KeyD'); await sleep(2000); key('keyup', 'KeyD')
await sleep(600)
const drifted = snap()
key('keydown', 'KeyL'); await sleep(200); key('keyup', 'KeyL')
await sleep(800)
const after = snap()
const gl = document.querySelector('#view').getContext('webgl2')
return {
before, savedAt, drifted, after,
saves: state.saves, loads: state.loads, saveError: state.saveError,
tickRate: state.tickRate, glError: gl ? gl.getError() : null, error: state.error ?? null,
source: state.source ?? null, itemTablesSource: state.itemTablesSource ?? null,
}
})()