import type { Disturbance, Generated, Generation, PetState } from "../types.ts"; import { act } from "./care.ts"; import { DEBUG } from "./config.ts"; import { petTurn, wantTurn } from "./conversation.ts"; import { writeDiary } from "./diary.ts"; import { changed, editState, petEngine } from "./engine.ts"; import { brain, emit, store, toast, ui, upgrade, view } from "./store.ts"; import { canRunLocally } from "./hosting.ts"; export function debugTurn(opts: Generation, result: Generated) { if (!DEBUG) return; ui.debugLog.push({ ...result, at: new Date(), mode: opts.mode }); if (ui.debugLog.length > 20) ui.debugLog.shift(); emit(); } export function setAge(ms: number) { if (!canRunLocally() || !store.state.pet) return; petEngine().setAge(ms, Date.now()); changed(); } export const copyText = (text: string) => navigator.clipboard.writeText(text).then( () => toast("Copied."), () => toast("Couldn't copy."), ); // Debug controls run the same engine paths as the pet; tags applied here // behave as if the brain had written them. const debug = { setWakeRoll: (roll: (() => number) | null) => petEngine().setWakeRoll(roll), disturb: (kind: Disturbance) => { const woke = petEngine().disturb(kind, Date.now()); changed(); return woke; }, get disturbances() { return petEngine().disturbances; }, resetDisturbances: () => petEngine().resetDisturbances(), applyTags: (text: string) => { if (!canRunLocally()) return; petEngine().applyLine(text, Date.now()); changed(); }, simulate: (from: number, to: number, quiet: boolean) => { const events = petEngine().simulate(from, to, quiet); changed(); return events; }, /** Edits the save in place, e.g. `edit((s) => { s.stats.tummy = 15 })`. */ edit: (fn: (state: PetState) => void) => editState(fn), }; export function testControls() { return { state: () => store.state, brain, view, act, petTurn, writeDiary, wantTurn, renderAll: emit, upgrade, ...(DEBUG ? { debug } : {}), }; } export type TestControls = ReturnType;