aboutsummaryrefslogtreecommitdiff
path: root/persistence/worlds.ts
blob: 0938a841cdc393802f4cd5fdc2e0c0b942cda00b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { userDataDb } from '@hollowdark/persistence/client'
import type { World } from '@hollowdark/engine/entities/world'

/**
 * The single world a player has on this device, or `null` if they have
 * never begun one. Design invariant: one continuous world per player.
 * Should the table ever hold more than one row, the earliest-created
 * row wins — newer rows would only arise from explicit import.
 */
export async function getCurrentWorld(): Promise<World | null> {
  const rows = await userDataDb().worlds.toArray()
  if (rows.length === 0) return null
  return rows.reduce((earliest, candidate) =>
    candidate.createdAt < earliest.createdAt ? candidate : earliest
  )
}

/** Persist a world record. Replaces any existing row with the same id. */
export async function saveWorld(world: World): Promise<void> {
  await userDataDb().worlds.put(world)
}