Implement tokens on maps #38

Closed
opened 2026-10-14 17:59:49 +00:00 by Syndria98 · 9 comments
Owner

On the dragonroll frontend (and backend), can you add that you can add actors to maps by dragging them and then make them tokens that you can move through the grid?

On the dragonroll frontend (and backend), can you add that you can add actors to maps by dragging them and then make them tokens that you can move through the grid?
Syndria98 added the ai-ready label 2026-10-14 17:59:49 +00:00
Agent added the working label 2026-10-14 18:08:20 +00:00
Collaborator

I'll start by exploring the map-related code to understand the existing patterns.

I'll start by exploring the map-related code to understand the existing patterns.
Collaborator

Now let me look at how actors are surfaced in the frontend, and how the sidebar works.

Now let me look at how actors are surfaced in the frontend, and how the sidebar works.
Collaborator

Now I'll implement. Starting with the backend:

Now I'll implement. Starting with the backend:
Collaborator

Now the token routes in the backend:

Now the token routes in the backend:
Collaborator

Now the main work in MapViewerPane.vue:

Now the main work in `MapViewerPane.vue`:
Collaborator

Now update the existing pin test's module stubs (map.js now requires the Actor model) and add a token test file:

Now update the existing pin test's module stubs (map.js now requires the Actor model) and add a token test file:
Collaborator

Let me review the full diff and update the docs that describe these routes:

Let me review the full diff and update the docs that describe these routes:
Collaborator

Implementation complete for #38 — Implement tokens on maps. Here's what landed:

Backend

  • backend/src/models/Map.js — new tokens: [{ actor→Actor, x, y }] array next to pins. Coordinates are image pixels of the token's centre, so a token keeps its cell whatever the zoom or grid settings are.
  • backend/src/routes/map.js — two routes in the existing pin style (same envelope, ownership check, same-campaign rule):
    • POST /map/place-token {id, actor, x, y} — only an actor of the map's own campaign can stand on it; an actor is placed once per map, so placing it again moves the token. Coordinates are snapped to the centre of the grid cell (gridSize / offsetX / offsetY).
    • POST /map/remove-token {id, token} — removes by the token's own _id, keeping the others.
  • backend/src/routes/actor.js — deleting an actor pulls its tokens off every map, exactly like a deleted note pulls its pins.

Frontend

  • app/services/MapTokens.js (new) — the pure rules: the actor drag payload (application/x-dragonroll-actor, so a note or file dropped on a map is never taken for an actor), CellCentre (snapping, identical to the route so a token does not jump when a drag lands), MoveByCells (whole-cell steps) and TokenSize (a token covers its cell, never smaller than an icon).
  • app/components/partials/ContentSidebar.vue — actor rows are draggable (:title says so), carrying the actor id.
  • app/components/viewer/map/MapViewerPane.vue — drops an actor on the visible cell, shows a hint while carrying one, and draws tokens in screen space sized by their cell with the actor portrait (initials when there is none or it fails to load). A token drags with the pointer, snapping cell to cell, saving once per move instead of once per mouse move; arrow keys walk a focused token, Delete takes it off, and right-click offers Open character sheet / Remove token. The shared updateMap helper keeps the sidebar entry and the anchored map in step (it now serves pins and tokens alike). Works in both the anchored pane and the detached map_viewer window.
  • i18n strings for campaigns.map.tokens added to en, es and ca; AGENTS.md updated for the new model field, routes, service and behaviour.

Tests

tests/map-tokens.test.js (new, 25 checks): snapping and offsets in the routes, ownership, same-campaign actors, one-token-per-actor, removal, the actor-delete pull, the service helpers, and viewer/sidebar wiring — including that the viewer and the route agree on where a token stands.

npm test → 99 tests, 99 pass, 0 fail (I also added the Actor model stub that tests/map-pins.test.js needs now that map.js requires it). The changed SFCs compile with @vue/compiler-sfc and the three locale files parse as JSON; I did not run the dev server (no dependencies installed in this tree), so the drag flow itself is not exercised end-to-end.

One known edge I left alone: actors removed by the Foundry sync prune (routes/foundry-link.js) can leave a token that renders as a placeholder until right-click → Remove token; cleaning that up means touching the sync router, which this change otherwise does not. Say the word if you want that included.

Implementation complete for **#38 — Implement tokens on maps**. Here's what landed: ## Backend - **`backend/src/models/Map.js`** — new `tokens: [{ actor→Actor, x, y }]` array next to `pins`. Coordinates are image pixels of the **token's centre**, so a token keeps its cell whatever the zoom or grid settings are. - **`backend/src/routes/map.js`** — two routes in the existing pin style (same envelope, ownership check, same-campaign rule): - `POST /map/place-token` `{id, actor, x, y}` — only an actor of the map's own campaign can stand on it; an actor is placed **once per map**, so placing it again moves the token. Coordinates are snapped to the centre of the grid cell (`gridSize` / `offsetX` / `offsetY`). - `POST /map/remove-token` `{id, token}` — removes by the token's own `_id`, keeping the others. - **`backend/src/routes/actor.js`** — deleting an actor pulls its tokens off every map, exactly like a deleted note pulls its pins. ## Frontend - **`app/services/MapTokens.js`** (new) — the pure rules: the actor drag payload (`application/x-dragonroll-actor`, so a note or file dropped on a map is never taken for an actor), `CellCentre` (snapping, identical to the route so a token does not jump when a drag lands), `MoveByCells` (whole-cell steps) and `TokenSize` (a token covers its cell, never smaller than an icon). - **`app/components/partials/ContentSidebar.vue`** — actor rows are draggable (`:title` says so), carrying the actor id. - **`app/components/viewer/map/MapViewerPane.vue`** — drops an actor on the visible cell, shows a hint while carrying one, and draws tokens in screen space sized by their cell with the actor portrait (initials when there is none or it fails to load). A token drags with the pointer, **snapping cell to cell**, saving once per move instead of once per mouse move; arrow keys walk a focused token, Delete takes it off, and right-click offers *Open character sheet* / *Remove token*. The shared `updateMap` helper keeps the sidebar entry and the anchored map in step (it now serves pins and tokens alike). Works in both the anchored pane and the detached `map_viewer` window. - i18n strings for `campaigns.map.tokens` added to **en, es and ca**; `AGENTS.md` updated for the new model field, routes, service and behaviour. ## Tests `tests/map-tokens.test.js` (new, 25 checks): snapping and offsets in the routes, ownership, same-campaign actors, one-token-per-actor, removal, the actor-delete pull, the service helpers, and viewer/sidebar wiring — including that the viewer and the route agree on where a token stands. `npm test` → **99 tests, 99 pass, 0 fail** (I also added the `Actor` model stub that `tests/map-pins.test.js` needs now that `map.js` requires it). The changed SFCs compile with `@vue/compiler-sfc` and the three locale files parse as JSON; I did not run the dev server (no dependencies installed in this tree), so the drag flow itself is not exercised end-to-end. One known edge I left alone: actors removed by the **Foundry sync prune** (`routes/foundry-link.js`) can leave a token that renders as a placeholder until right-click → *Remove token*; cleaning that up means touching the sync router, which this change otherwise does not. Say the word if you want that included.
Collaborator

Implementation is ready for review: #40

Implementation is ready for review: https://git.aranroig.com/Syndria98/dragonroll/pulls/40
Agent added the help wanted label 2026-10-14 18:41:18 +00:00
Agent removed the working label 2026-10-14 18:41:18 +00:00
Sign in to join this conversation.
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Syndria98/dragonroll#38