Repository navigation
Expand file tree
/
Copy pathpersistence.js
More file actions
329 lines (316 loc) · 14.9 KB
/
Copy pathpersistence.js
File metadata and controls
329 lines (316 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* persistence.js — serialization primitives for save/load.
* Pure data transforms: capture a floor, split live state into module + session, derive cell grids,
* migrate old save versions forward, validate/merge session data, hydrate floor images from IndexedDB.
* No rendering, no UI, no db writes of records — the save/delete/import ACTIONS that combine these with
* db writes, the library panel, and re-render orchestration stay in app.js until the orchestrator layer.
*/
import {
deleteMapRecord, getImage, listMapRecords, putImage, saveModuleRecord, saveSessionRecord,
} from "./db.js";
import {
APP_NAME, DEFAULT_GM_FOG_OPACITY, INITIAL_FLOOR_ID, LEGACY_APP_NAME, SAVE_FILE_VERSION, state, uuid,
} from "./state.js";
function makeMapId(name) {
return name.toLocaleLowerCase().replace(/\s+/g, " ").trim();
}
function validateSessionData(data) {
if (!data || ![APP_NAME, LEGACY_APP_NAME].includes(data.app) || !data.state) {
throw new Error("invalid or unsupported battlemap save");
}
if (!data.state.grid || !data.state.fog) {
throw new Error("the save file is missing required map data");
}
return migrateState(data.state, data.version || 1);
}
function migrateState(snapshot, version) {
// v1 -> v2: fog/room points were stored in scaled-world coords (native x map.scale).
if (version < 2) {
const scale = snapshot.map?.scale || 1;
if (scale !== 1 && snapshot.fog) {
(snapshot.fog.rooms || []).forEach((room) => {
room.points = (room.points || []).map((p) => ({ x: p.x / scale, y: p.y / scale }));
});
(snapshot.fog.strokes || []).forEach((stroke) => {
stroke.points = (stroke.points || []).map((p) => ({ x: p.x / scale, y: p.y / scale }));
stroke.size = stroke.size / scale;
});
}
snapshot._refit = true; // old view used a different model; refit on load
}
snapshot.tokens = snapshot.tokens || [];
if (snapshot.blackout === undefined) snapshot.blackout = false;
// v2 -> v3: a single map becomes the first (and only) floor.
if (!Array.isArray(snapshot.floors) || !snapshot.floors.length) {
snapshot.floors = [
{
id: INITIAL_FLOOR_ID,
imageId: snapshot.imageId || "",
imageData: snapshot.imageData || "",
imageName: snapshot.imageName || "",
imageWidth: snapshot.imageWidth || 0,
imageHeight: snapshot.imageHeight || 0,
mapScale: snapshot.map?.scale || 1,
rooms: snapshot.fog?.rooms || [],
strokes: snapshot.fog?.strokes || [],
tokens: snapshot.tokens || [],
stairs: [],
view: { ...(snapshot.view || { scale: 1, cx: 0, cy: 0 }) },
},
];
snapshot.currentFloorId = INITIAL_FLOOR_ID;
}
return snapshot;
}
// Fill each floor's imageData back in from the image store (records carry only imageId).
// getImg is injectable so the map-navigation path can route image fetches through content.js's
// resolver (memory -> IndexedDB -> fallon); it defaults to the local store for every other caller.
async function hydrateFloorImages(snapshot, getImg = getImage) {
const floors = Array.isArray(snapshot.floors) ? snapshot.floors : [];
for (const floor of floors) {
if (!floor.imageData && floor.imageId) {
floor.imageData = await getImg(floor.imageId);
}
}
const current = floors.find((f) => f.id === (snapshot.currentFloorId || (floors[0] && floors[0].id)));
if (current) {
snapshot.imageId = current.imageId || snapshot.imageId;
snapshot.imageData = current.imageData || snapshot.imageData;
}
}
// Partition a full live-state snapshot into an authored module body + a session body. Floors
// split by id: the authored half (image ref + dimensions + scale + stairs) goes to the module,
// the play half (fog/tokens/dropped images/notes/view) goes to the session. Neither carries the
// image bytes — those live in the image store, keyed by imageId.
function splitState(stateObj) {
const floors = stateObj.floors || [];
const moduleFloors = floors.map((f) => ({
id: f.id,
imageId: f.imageId || "",
imageName: f.imageName || "",
imageWidth: f.imageWidth || 0,
imageHeight: f.imageHeight || 0,
mapScale: f.mapScale || 1,
stairs: JSON.parse(JSON.stringify(f.stairs || [])),
mapLinks: JSON.parse(JSON.stringify(f.mapLinks || [])),
obstacles: JSON.parse(JSON.stringify(f.obstacles || [])),
lights: JSON.parse(JSON.stringify(f.lights || [])),
}));
const sessionFloors = floors.map((f) => ({
id: f.id,
rooms: JSON.parse(JSON.stringify(f.rooms || [])),
strokes: JSON.parse(JSON.stringify(f.strokes || [])),
tokens: JSON.parse(JSON.stringify(f.tokens || [])),
images: JSON.parse(JSON.stringify(f.images || [])),
notes: JSON.parse(JSON.stringify(f.notes || [])),
view: { ...(f.view || { scale: 1, cx: 0, cy: 0, rotation: 0 }) },
}));
const primaryModuleFloor = moduleFloors.find((f) => f.imageWidth) || moduleFloors[0] || null;
const moduleGrid = JSON.parse(JSON.stringify(stateObj.grid || {}));
Object.assign(moduleGrid, deriveCellGrid(moduleGrid, stateObj.measure || {}, primaryModuleFloor));
const module = {
grid: moduleGrid,
measure: JSON.parse(JSON.stringify(stateObj.measure || {})),
stairColor: stateObj.stairColor || "#ffffff",
floors: moduleFloors,
// Containment-trail metadata (chunk 1). mapKind gates which subsystems run (chunk 3);
// parentId positions this map under another for the breadcrumb; source distinguishes a
// locally-authored map from one served by fallon (chunk 4). Defaults keep old saves valid.
mapKind: stateObj.mapKind || "battle",
parentId: stateObj.parentId ?? null,
source: stateObj.source || "local",
// Forward-empty — filled by later steps (room pins, VWAG linkage). Obstacle geometry and
// placed lights are per-floor (module.floors[].obstacles / .lights), authored on the map.
pins: [],
ambient: { timeOfDay: 12 },
vwag: {},
};
const session = {
blackout: Boolean(stateObj.blackout),
los: JSON.parse(JSON.stringify(stateObj.los || { enabled: false })),
splash: JSON.parse(JSON.stringify(stateObj.splash || { enabled: false, imageData: "", imageName: "" })),
initiative: JSON.parse(JSON.stringify(stateObj.initiative || {})),
playerView: JSON.parse(JSON.stringify(stateObj.playerView || {})),
currentFloorId: stateObj.currentFloorId,
activeFloorId: stateObj.activeFloorId || stateObj.currentFloorId,
openDoors: [], // session-tracked open door ids (forward; the toggle that uses it is step 5)
floorPosition: stateObj.floorPosition || 1,
floorCount: stateObj.floorCount || floors.length || 1,
fog: {
gmColor: stateObj.fog?.gmColor || "#080909",
gmOpacity: stateObj.fog?.gmOpacity ?? DEFAULT_GM_FOG_OPACITY,
toolSize: stateObj.fog?.toolSize ?? 70,
toolShape: stateObj.fog?.toolShape ?? "round",
stampShape: stateObj.fog?.stampShape ?? "rectangle",
},
floors: sessionFloors,
};
return { module, session };
}
// Recombine a module + session into a single snapshot loadSnapshot() can consume. Floors are
// re-merged by id; imageData is left blank for hydrateFloorImages() to fill from the store.
function mergeModuleSession(module, session) {
const sessionById = new Map((session.floors || []).map((f) => [f.id, f]));
const floors = (module.floors || []).map((mf) => {
const sf = sessionById.get(mf.id) || {};
return {
id: mf.id,
imageId: mf.imageId || "",
imageData: "",
imageName: mf.imageName || "",
imageWidth: mf.imageWidth || 0,
imageHeight: mf.imageHeight || 0,
mapScale: mf.mapScale || 1,
stairs: JSON.parse(JSON.stringify(mf.stairs || [])),
mapLinks: JSON.parse(JSON.stringify(mf.mapLinks || [])),
obstacles: JSON.parse(JSON.stringify(mf.obstacles || [])),
lights: JSON.parse(JSON.stringify(mf.lights || [])),
rooms: JSON.parse(JSON.stringify(sf.rooms || [])),
strokes: JSON.parse(JSON.stringify(sf.strokes || [])),
tokens: JSON.parse(JSON.stringify(sf.tokens || [])),
images: JSON.parse(JSON.stringify(sf.images || [])),
notes: JSON.parse(JSON.stringify(sf.notes || [])),
view: { ...(sf.view || { scale: 1, cx: 0, cy: 0, rotation: 0 }) },
};
});
return {
grid: JSON.parse(JSON.stringify(module.grid || {})),
measure: JSON.parse(JSON.stringify(module.measure || {})),
stairColor: module.stairColor || "#ffffff",
mapKind: module.mapKind || "battle",
parentId: module.parentId ?? null,
source: module.source || "local",
blackout: Boolean(session.blackout),
los: JSON.parse(JSON.stringify(session.los || { enabled: false })),
splash: JSON.parse(JSON.stringify(session.splash || { enabled: false, imageData: "", imageName: "" })),
initiative: JSON.parse(JSON.stringify(session.initiative || {})),
playerView: JSON.parse(JSON.stringify(session.playerView || {})),
fog: {
rooms: [],
strokes: [],
gmColor: session.fog?.gmColor || "#080909",
gmOpacity: session.fog?.gmOpacity ?? DEFAULT_GM_FOG_OPACITY,
toolSize: session.fog?.toolSize ?? 70,
toolShape: session.fog?.toolShape ?? "round",
stampShape: session.fog?.stampShape ?? "rectangle",
},
currentFloorId: session.currentFloorId || (floors[0] && floors[0].id),
activeFloorId: session.activeFloorId || session.currentFloorId || (floors[0] && floors[0].id),
floorPosition: session.floorPosition || 1,
floorCount: session.floorCount || floors.length,
floors,
};
}
// One-time, idempotent: drain the legacy single-record map store into module + session pairs.
// Runs on GM startup and after a legacy import. Per record: normalize its shape, de-embed any
// still-inline image bytes into the image store, split it, write the pair, delete the original.
// Best-effort per record so a single bad record never blocks startup.
async function migrateMapsToModulesAndSessions() {
let records;
try {
records = await listMapRecords();
} catch {
return;
}
if (!Array.isArray(records) || !records.length) return;
for (const record of records) {
try {
if (!record || !record.id || !record.state) continue;
const snapshot = validateSessionData(record); // app check + shape migration -> floors guaranteed
for (const floor of snapshot.floors || []) {
if (floor.imageData) {
if (!floor.imageId) floor.imageId = uuid();
await putImage(floor.imageId, floor.imageData);
}
}
const { module, session } = splitState(snapshot);
const now = record.savedAt || new Date().toISOString();
const name = record.name || record.id;
Object.assign(module, { id: record.id, app: APP_NAME, version: SAVE_FILE_VERSION, kind: "module", name, savedAt: now });
Object.assign(session, { id: record.id, moduleId: record.id, app: APP_NAME, version: SAVE_FILE_VERSION, kind: "session", name, savedAt: now });
await saveModuleRecord(module);
await saveSessionRecord(session);
await deleteMapRecord(record.id);
} catch {
// Leave this record in place for a later attempt; never block startup.
}
}
}
// Flush active-floor mutable fields back into the floor record.
function captureCurrentFloor() {
const floor = state.floors.find((f) => f.id === state.currentFloorId);
if (!floor) return;
floor.imageId = state.imageId;
floor.imageData = state.imageData;
floor.imageName = state.imageName;
floor.imageWidth = state.imageWidth;
floor.imageHeight = state.imageHeight;
floor.mapScale = state.map.scale;
floor.rooms = JSON.parse(JSON.stringify(state.fog.rooms));
floor.strokes = JSON.parse(JSON.stringify(state.fog.strokes));
floor.tokens = JSON.parse(JSON.stringify(state.tokens));
floor.stairs = JSON.parse(JSON.stringify(state.stairs));
floor.mapLinks = JSON.parse(JSON.stringify(state.mapLinks || []));
floor.obstacles = JSON.parse(JSON.stringify(state.obstacles));
floor.lights = JSON.parse(JSON.stringify(state.lights));
floor.aoes = JSON.parse(JSON.stringify(state.aoes || []));
floor.images = JSON.parse(JSON.stringify(state.images));
floor.notes = JSON.parse(JSON.stringify(state.notes));
floor.view = { ...state.view };
}
// Derive a module's cell-space declaration (DTT-style) from its grid/measure calibration and its
// primary floor image, for record processing (splitState + the backfill) where there's no live
// state. pxPerCell is native px/cell = world cell size / mapScale; cellsX/Y are the image divided
// by that. Degrades to zeros when there's no grid and no calibration yet.
function deriveCellGrid(gridObj, measureObj, floor) {
const cellWorld = gridObj.size > 0 ? gridObj.size : (measureObj && measureObj.cellSize > 0 ? measureObj.cellSize : 0);
const mapScale = (floor && floor.mapScale) || 1;
const pxPerCell = cellWorld > 0 ? cellWorld / mapScale : 0;
const w = (floor && floor.imageWidth) || 0;
const h = (floor && floor.imageHeight) || 0;
return {
pxPerCell,
cellsX: pxPerCell > 0 ? Math.round(w / pxPerCell) : 0,
cellsY: pxPerCell > 0 ? Math.round(h / pxPerCell) : 0,
type: gridObj.type || "square",
display: gridObj.display || "both",
};
}
// Freeze the current live state into a loadSnapshot-shaped object for the warm map cache (the
// inverse of loadSnapshot, mirroring mergeModuleSession's output). captureCurrentFloor() flushes
// the active floor's live edits into its record first; floors are returned BY REFERENCE so the
// hydrated image bytes survive in the cache and pop-back stays instant. The cached snapshot keeps
// pointing at this floors array even after the next map reassigns state.floors.
function snapshotFromLiveState() {
captureCurrentFloor();
return {
grid: JSON.parse(JSON.stringify(state.grid)),
measure: JSON.parse(JSON.stringify(state.measure || {})),
stairColor: state.stairColor || "#ffffff",
mapKind: state.mapKind || "battle",
parentId: state.parentId ?? null,
source: state.source || "local",
blackout: Boolean(state.blackout),
los: JSON.parse(JSON.stringify(state.los || { enabled: false })),
splash: JSON.parse(JSON.stringify(state.splash || { enabled: false, imageData: "", imageName: "" })),
initiative: JSON.parse(JSON.stringify(state.initiative || {})),
playerView: JSON.parse(JSON.stringify(state.playerView || {})),
fog: {
rooms: [],
strokes: [],
gmColor: state.fog?.gmColor || "#080909",
gmOpacity: state.fog?.gmOpacity ?? DEFAULT_GM_FOG_OPACITY,
toolSize: state.fog?.toolSize ?? 70,
toolShape: state.fog?.toolShape ?? "round",
stampShape: state.fog?.stampShape ?? "rectangle",
},
currentFloorId: state.currentFloorId,
activeFloorId: state.activeFloorId || state.currentFloorId,
floorPosition: state.floorPosition || 1,
floorCount: state.floorCount || (Array.isArray(state.floors) ? state.floors.length : 1),
floors: state.floors,
};
}
export {
validateSessionData, migrateState, hydrateFloorImages, mergeModuleSession, migrateMapsToModulesAndSessions, captureCurrentFloor, splitState, makeMapId,
deriveCellGrid, snapshotFromLiveState,
};