1async function call(method, url, body) { 2 const res = await fetch(url, { 3 method, 4 headers: { 'Content-Type': 'application/json' }, 5 body: body ? JSON.stringify(body) : undefined, 6 }); 7 const data = await res.json(); 8 if (!res.ok) throw new Error(data.error || res.statusText); 9 return data.campaign ? normalize(data) : data; 10} 11 12function normalize(s) { 13 const arr = (o, k) => (o[k] ||= []); 14 arr(s, 'log'); 15 arr(s.party, 'inventory'); 16 arr(s.party, 'keys'); 17 arr(s.campaign, 'sideQuests'); 18 arr(s.campaign, 'factions'); 19 arr(s.campaign, 'systems'); 20 ['pois', 'spawns', 'loot'].forEach((k) => arr(s.map, k)); 21 s.party.units.forEach((u) => (u.gear ||= {})); 22 s.encounter?.enemies.forEach((e) => arr(e, 'abilities')); 23 if (s.camp) arr(s.camp, 'stock'); 24 if (s.rewards) arr(s.rewards, 'items'); 25 return s; 26} 27 28export const getState = () => call('GET', '/api/state'); 29export const newGame = (seed) => call('POST', '/api/new', seed ? { seed } : {}); 30export const move = (x, y, steps = 0) => call('POST', '/api/move', { x, y, steps }); 31export const path = (x, y) => call('GET', `/api/path?x=${x}&y=${y}`); 32export const interact = (id) => call('POST', '/api/interact', { id }); 33export const equip = (unit, item, unequip = false) => call('POST', '/api/equip', { unit, item, unequip }); 34export const rest = () => call('POST', '/api/rest', {}); 35export const buy = (item) => call('POST', '/api/buy', { item }); 36export const sell = (item) => call('POST', '/api/sell', { item }); 37export const drink = (unit) => call('POST', '/api/drink', { unit }); 38export const run = (id) => call('GET', `/api/run/${id}`); 39export const feedback = (text) => call('POST', '/api/feedback', { text });