[schema] PlanetGarden: The gardener's look, from fixed lists, kept on the device

This commit is contained in:
2026-11-17 18:57:34 +01:00
parent f0767bfed7
commit 9eef8a3bf7
6 changed files with 137 additions and 4 deletions
+30 -3
View File
@@ -12,7 +12,7 @@
import Dexie, { type DexieOptions, type Table } from 'dexie';
import { newCare, newOpened, type NamedPlace } from '@rosea/engine';
import { isIdentity, type Identity } from '@rosea/identity';
import { DATABASE_NAME, type CareEvent, type SentenceOpened } from '@rosea/schema';
import { DATABASE_NAME, GARDENER_FIELDS, GARDENER_OPTIONS, type CareEvent, type Gardener, type SentenceOpened } from '@rosea/schema';
export { DATABASE_NAME };
@@ -34,6 +34,17 @@ interface KeyRow {
}
const SUNSET_PLACE = 'sunsetPlace';
const GARDENER = 'gardener';
/** The shape check for a stored gardener: exactly the six fields, each from its list. */
export function asGardener(value: unknown): Gardener | null {
if (typeof value !== 'object' || value === null) return null;
const v = value as Record<string, unknown>;
const keys = GARDENER_FIELDS;
if (Object.keys(v).length !== keys.length) return null;
for (const k of keys) if (!(GARDENER_OPTIONS[k] as readonly unknown[]).includes(v[k])) return null;
return Object.fromEntries(keys.map((k) => [k, v[k]])) as unknown as Gardener;
}
const SELF = 'self';
const PAST_KEYS = 'pastKeys';
@@ -55,6 +66,9 @@ export interface RoseFile {
export interface Storage {
/** The saved sunset place, or null if the player has not answered yet. */
loadSunsetPlace(): Promise<SunsetPlace | null>;
/** The player's gardener, or null before they chose one (planet-garden.md G6). */
loadGardener(): Promise<Gardener | null>;
saveGardener(look: Gardener): Promise<void>;
saveSunsetPlace(place: SunsetPlace): Promise<void>;
/** Appends one act of care. The log is never edited or shortened. */
appendCare(event: CareEvent): Promise<void>;
@@ -194,7 +208,7 @@ class RoseaDatabase extends Dexie {
}
/** What another tab changed (two-tabs.md W2): only which part, never the data. */
export type Change = 'log' | 'place';
export type Change = 'log' | 'place' | 'gardener';
/** The channel every tab of the garden announces its writes on (two-tabs.md W1). */
export const CHANGES_CHANNEL = `${DATABASE_NAME}-changes`;
@@ -213,7 +227,7 @@ export function openStorage(options?: DexieOptions, settings: { readonly channel
if (channel) {
channel.onmessage = (message: MessageEvent<unknown>) => {
const what = message.data;
if (what !== 'log' && what !== 'place') return;
if (what !== 'log' && what !== 'place' && what !== 'gardener') return;
for (const listener of listeners) {
try {
listener(what);
@@ -250,6 +264,19 @@ export function openStorage(options?: DexieOptions, settings: { readonly channel
await db.profile.put({ key: SUNSET_PLACE, value: plain });
announce('place');
},
async loadGardener() {
const row = await db.profile.get(GARDENER);
if (row === undefined) return null;
const look = asGardener(row.value);
if (look === null) console.warn('storage: the saved gardener has an unknown shape; asking again', row.value);
return look;
},
async saveGardener(look) {
const plain = asGardener(look);
if (plain === null) throw new TypeError('storage: refusing to save a gardener of an unknown shape');
await db.profile.put({ key: GARDENER, value: plain });
announce('gardener');
},
async appendCare(event) {
const plain = asCareEvent(event);
if (plain === null) throw new TypeError('storage: refusing to append a care event of an unknown shape');