Make maps sync #99

Closed
opened 2026-10-15 22:50:52 +00:00 by Syndria98 · 7 comments
Owner

Make that dragonroll maps sync with foundry maps. At the moment make that it just syncs the background image and the grid size and positioning

Make that dragonroll maps sync with foundry maps. At the moment make that it just syncs the background image and the grid size and positioning
Syndria98 added the ai-ready label 2026-10-15 22:50:52 +00:00
Agent added the working label 2026-10-15 23:19:43 +00:00
Collaborator

I'll start by exploring how maps and the Foundry sync currently work.

I'll start by exploring how maps and the Foundry sync currently work.
Collaborator

Now I have a clear picture. Let me implement the backend first.

Now I have a clear picture. Let me implement the backend first.
Collaborator

Now the Foundry module side:

Now the Foundry module side:
Collaborator

Now let me write the tests:

Now let me write the tests:
Collaborator

Now the documentation:

Now the documentation:
Collaborator

Done. Here's what I implemented for #99.

Maps now follow the world's scenes — background picture and grid only, as the issue asks, one-way (Foundry → campaign).

Backend

  • backend/src/models/Map.js — new foundrySceneId, unique per campaign (partial index, same shape as foundryItemId / foundryActorId).
  • backend/src/services/map-foundry.js (new) — foundrySceneToMapData: the scene's background picture (background.src since V13, img before), gridSize from grid / grid.size (Foundry's 50 px default when unset), offsetX/offsetY from the world's flags.core.shiftX/shiftY. Returns null for a scene with no picture, so those never become an empty map. The campaign's scale, folder, pins and tokens are never touched.
  • backend/src/routes/foundry-link.js — saveFoundryScene (modelled on saveFoundryItem: identity by foundrySceneId, adoption via a dragonroll-foundry.mapId flag or a hand-made unmapped map of the same name, fullSync preserves existing rows while live edits overwrite) plus two routes on the sync token: POST /api/foundry/sync/scenes (upserts, prunes only when fullSync) and POST /api/foundry/sync/scene-delete. The Map model is required as MapModel there because that file builds plain Maps for the folder tree.

Foundry module (dragonroll-foundry/scripts/api.js) — scenes are sent as plain document data with the picture rewritten to an absolute URL of that world (like a portrait). The whole scene list goes when the world links and when it loads (the only push that prunes); createScene / updateScene / deleteScene are debounced into one push 400 ms later, deletions reported separately, failures retried. Autosync covers maps: off = nothing crosses, and edits queued while off go over when it is turned back on.

Frontend — no app change needed: the map viewer already treats an image stored as a full URL, so a synced scene renders as it is.

Docs — new Maps section and route rows in dragonroll-foundry/README.md, a row and paragraph in the /docs/foundry "What syncs", a Maps from a linked Foundry world section in /docs/maps, and the AGENTS.md model/rule lines.

Tests — new tests/map-foundry-sync.test.js: 22 tests over the route (arrival, grid shift, V13 shapes, relative picture read against the campaign's world URL, scale left alone, no-picture scenes, one map per scene, live overwrite vs full-snapshot preserving campaign edits, hand-made adoption by name and by flag, prune only on fullSync, scene delete, refusals) and the module (game.scenes in the vm sandbox: full list on load, live scene edit, delete, autosync off/back on). Three existing files that load foundry-link.js needed stubs for the new model and service. npm test → 371 passed, 0 failed (349 before).

Two behaviours worth flagging, both documented: a map's scale (what a cell stands for) stays the campaign's, since the world's gridDistance comes with its own units; and because nothing goes back to the world, a map deleted in the campaign returns with its scene on the next world load.

Done. Here's what I implemented for #99. **Maps now follow the world's scenes** — background picture and grid only, as the issue asks, one-way (Foundry → campaign). **Backend** - `backend/src/models/Map.js` — new `foundrySceneId`, unique per campaign (partial index, same shape as `foundryItemId` / `foundryActorId`). - `backend/src/services/map-foundry.js` (new) — `foundrySceneToMapData`: the scene's background picture (`background.src` since V13, `img` before), `gridSize` from `grid` / `grid.size` (Foundry's 50 px default when unset), `offsetX/offsetY` from the world's `flags.core.shiftX/shiftY`. Returns `null` for a scene with no picture, so those never become an empty map. The campaign's `scale`, folder, pins and tokens are never touched. - `backend/src/routes/foundry-link.js` — `saveFoundryScene` (modelled on `saveFoundryItem`: identity by `foundrySceneId`, adoption via a `dragonroll-foundry.mapId` flag or a hand-made unmapped map of the same name, `fullSync` preserves existing rows while live edits overwrite) plus two routes on the sync token: `POST /api/foundry/sync/scenes` (upserts, prunes only when `fullSync`) and `POST /api/foundry/sync/scene-delete`. The Map model is required as `MapModel` there because that file builds plain `Map`s for the folder tree. **Foundry module** (`dragonroll-foundry/scripts/api.js`) — scenes are sent as plain document data with the picture rewritten to an absolute URL of that world (like a portrait). The whole scene list goes when the world links and when it loads (the only push that prunes); `createScene` / `updateScene` / `deleteScene` are debounced into one push 400 ms later, deletions reported separately, failures retried. Autosync covers maps: off = nothing crosses, and edits queued while off go over when it is turned back on. **Frontend** — no app change needed: the map viewer already treats an image stored as a full URL, so a synced scene renders as it is. **Docs** — new *Maps* section and route rows in `dragonroll-foundry/README.md`, a row and paragraph in the `/docs/foundry` "What syncs", a *Maps from a linked Foundry world* section in `/docs/maps`, and the AGENTS.md model/rule lines. **Tests** — new `tests/map-foundry-sync.test.js`: 22 tests over the route (arrival, grid shift, V13 shapes, relative picture read against the campaign's world URL, scale left alone, no-picture scenes, one map per scene, live overwrite vs full-snapshot preserving campaign edits, hand-made adoption by name and by flag, prune only on `fullSync`, scene delete, refusals) and the module (`game.scenes` in the vm sandbox: full list on load, live scene edit, delete, autosync off/back on). Three existing files that load `foundry-link.js` needed stubs for the new model and service. `npm test` → **371 passed, 0 failed** (349 before). Two behaviours worth flagging, both documented: a map's `scale` (what a cell stands for) stays the campaign's, since the world's `gridDistance` comes with its own units; and because nothing goes back to the world, a map deleted in the campaign returns with its scene on the next world load.
Collaborator

Implementation is ready for review: #104

Implementation is ready for review: https://git.aranroig.com/Syndria98/dragonroll/pulls/104
Agent added help wanted and removed working labels 2026-10-15 23:59:55 +00:00
Agent referenced this issue from a commit 2026-10-15 23:59:56 +00:00
Sign in to join this conversation.
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Syndria98/dragonroll#99