Something went wrong. Try again.
a tiny pet with a GPT-2 brain, living in your browser
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import type { PetState } from "../types.ts";import { dateKey, HOUR } from "./utils.ts";
export function fresh(): PetState { return { v: 1, created: Date.now(), owner: "", pet: null, egg: null, stats: { tummy: 70, fun: 70, energy: 80, clean: 90 }, sick: false, poop: 0, asleep: false, lightsOff: false, memories: [], shelf: [], chat: [], diary: [], today: { date: dateKey(Date.now()), log: [] }, history: {}, offer: null, wokeAt: 0, lastTick: Date.now(), lastActive: Date.now(), grimeSince: 0, nextPoop: Date.now() + 3 * HOUR, settings: { chatter: true }, counts: { fed: 0, pats: 0, games: 0, baths: 0 }, };}// Save versions. MIGRATIONS[n] turns a version n+1 save into version n+2;// bump VERSION and add one whenever a stored shape changes. A save that cannot// be read (corrupt, from a newer version, or a failed migration) is never// overwritten: it becomes a "bad egg" that the owner can download or replace.export const VERSION = 1;export const MIGRATIONS: ((s: PetState) => PetState)[] = [];export function parseSave(raw: string): PetState { let s: PetState = JSON.parse(raw); if (!s || typeof s !== "object" || !Number.isInteger(s.v) || s.v < 1) throw new Error("it isn't a tokenling save"); if (s.v > VERSION) { throw Object.assign( new Error( `it was saved by a newer version of tokenling (save version ${s.v}, this page reads ${VERSION})`, ), { newer: true }, ); } while (s.v < VERSION) { const from = s.v, step = MIGRATIONS[from - 1]; if (!step) throw new Error(`there is no way to update a version ${from} save`); s = step(s); if (s?.v !== from + 1) throw new Error(`updating it from version ${from} failed`); } if (!Array.isArray(s.chat) || !Array.isArray(s.diary) || !s.stats || typeof s.stats !== "object") throw new Error("parts of it are missing"); return { ...fresh(), ...s };}