Fix issue #100: Add walls to maps
Some checks failed
Test / test (push) Failing after 2s

This commit is contained in:
Gitea OpenCode Agent
2026-10-16 00:59:56 +00:00
parent c9ee16f2f2
commit 3c3df58d8d
13 changed files with 1546 additions and 1 deletions

View File

@@ -27,6 +27,19 @@ const MapSchema = new Schema({
x: { type: Number, default: 0 },
y: { type: Number, default: 0 }
}],
// Segments that block the map like walls. The endpoints are image pixels from the top left corner
// of the image, like a pin — and the very space Foundry draws a scene's walls in, so a paired
// scene and map trade them as they stand. `move`/`sight` say what passes through the segment, and
// `door` is 0 (not a door), 1 (open) or 2 (closed), following Foundry's own wall values.
walls: [{
x1: { type: Number, default: 0 },
y1: { type: Number, default: 0 },
x2: { type: Number, default: 0 },
y2: { type: Number, default: 0 },
move: { type: Boolean, default: true },
sight: { type: Boolean, default: true },
door: { type: Number, min: 0, max: 2, default: 0 }
}],
campaign: {
type: Schema.Types.ObjectId,
ref: 'Campaign',
@@ -39,7 +52,16 @@ const MapSchema = new Schema({
ref: 'Folder',
default: null
},
// The Foundry scene whose walls this map mirrors once the linked world has paired them (see
// routes/foundry-link.js): the whole wall set of one is the whole wall set of the other.
foundrySceneId: { type: String },
date: { type: Date, default: Date.now }
});
// A scene walls one map of a campaign at most, like a Foundry actor one actor row.
MapSchema.index(
{ campaign: 1, foundrySceneId: 1 },
{ unique: true, partialFilterExpression: { foundrySceneId: { $type: 'string' } } }
);
module.exports = mongoose.model('Map', MapSchema);

View File

@@ -7,6 +7,8 @@ const Campaign = require('../models/Campaign');
const Actor = require('../models/Actor');
const Item = require('../models/Item');
const Folder = require('../models/Folder');
// `MapModel`, not `Map`: this file uses the built-in `Map` for the item folder maps.
const MapModel = require('../models/Map');
const { foundryActorToCharacter } = require('../services/actor-foundry');
const { foundryItemToItemData } = require('../services/item-foundry');
const { joinFoundryAssetUrl } = require('../services/foundry-icons');
@@ -477,6 +479,103 @@ router.post('/sync/map-item', async (req, res) => {
}
});
// --- Scene walls -----------------------------------------------------------
// A campaign map can mirror the walls of a Foundry scene: the whole set travels whoever edits
// it, so the map's editor and the scene's wall tool see the same segments. A map is paired with
// a scene by name while the module reconciles its campaign — the way an unmapped item maps to
// the world document it shares its name with — and `POST /sync/map-scene` records that pairing.
// The endpoints are image pixels of the scene's background there too, so a segment crosses
// unchanged; `door` follows Foundry's own wall values (1 open, 2 closed), and locked doors
// arrive closed.
// Same shape the campaign's own `/map/walls` saves by: finite endpoints or nothing, whole pixels
// that never go off the top left corner, no segment without length.
function toSyncedWall(source) {
const corners = [source?.x1, source?.y1, source?.x2, source?.y2];
if (!corners.every((value) => Number.isFinite(Number(value)))) return null;
const toPixel = (value) => Math.max(0, Math.round(Number(value)));
const wall = {
x1: toPixel(source.x1),
y1: toPixel(source.y1),
x2: toPixel(source.x2),
y2: toPixel(source.y2),
move: source.move !== false,
sight: source.sight !== false,
door: [1, 2].includes(Math.round(Number(source.door))) ? Math.round(Number(source.door)) : 0
};
return wall.x1 === wall.x2 && wall.y1 === wall.y2 ? null : wall;
}
// The maps the module pairs scenes with and reconciles walls against: what a wall set needs to
// land on a scene, and nothing the module has no use for.
router.get('/sync/maps', async (req, res) => {
try {
const campaign = await syncCampaign(req, res);
if (!campaign) return;
const maps = await MapModel.find({ campaign: campaign._id })
.select('_id name image foundrySceneId walls')
.sort({ name: 1 })
.lean();
res.json({ status: 'ok', maps });
} catch (err) {
console.error(err);
res.json({ status: 'error', msg: 'errors.internal' });
}
});
router.post('/sync/map-scene', async (req, res) => {
try {
const campaign = await syncCampaign(req, res);
if (!campaign) return;
const { mapId, foundrySceneId, worldId } = req.body;
if (!requestMatchesWorld(campaign, worldId) || !mapId || !foundrySceneId) {
return res.json({ status: 'error', msg: 'errors.missing-data' });
}
const map = await MapModel.findOne({ _id: mapId, campaign: campaign._id });
if (!map) return res.json({ status: 'error', msg: 'errors.notfound' });
const existing = await MapModel.findOne({
campaign: campaign._id,
foundrySceneId: String(foundrySceneId).trim(),
_id: { $ne: map._id }
});
if (existing) return res.json({ status: 'error', msg: 'foundry.errors.scene-already-synced' });
map.foundrySceneId = String(foundrySceneId).trim().slice(0, 120);
await map.save();
res.json({ status: 'ok', map });
} catch (err) {
console.error(err);
res.json({ status: 'error', msg: 'errors.internal' });
}
});
// A scene's whole wall set, as the module draws it: the paired map takes it over, like the
// world's item folders take over the campaign's tree. A scene the campaign has no map paired
// with answers `notfound` — most scenes are none of the campaign's business.
router.post('/sync/map-walls', async (req, res) => {
try {
const campaign = await syncCampaign(req, res);
if (!campaign) return;
const { foundrySceneId, walls, worldId } = req.body;
if (!requestMatchesWorld(campaign, worldId) || !foundrySceneId || !Array.isArray(walls)) {
return res.json({ status: 'error', msg: 'errors.missing-data' });
}
const map = await MapModel.findOne({ campaign: campaign._id, foundrySceneId: String(foundrySceneId).trim() });
if (!map) return res.json({ status: 'error', msg: 'errors.notfound' });
map.walls = walls.map(toSyncedWall).filter(Boolean);
await map.save();
res.json({ status: 'ok', map: { _id: map.id } });
} catch (err) {
console.error(err);
res.json({ status: 'error', msg: 'errors.internal' });
}
});
module.exports = router;
// The writers every Foundry document goes through: the sync pushes above, and the
// force import a campaign asks for from its sidebar (routes/foundry.js). One set of

View File

@@ -238,4 +238,50 @@ router.post('/remove-token', async (req, res) => {
}
});
// A wall is a segment of the map that blocks it like one does. The editor saves the whole set at
// once — drawing, moving, re-typing and taking off segments are all the same request — and a paired
// Foundry scene takes that set whole too, so the set is the contract on both sides. Endpoints are
// image pixels like a pin's, and a wall keeps the `_id` it is sent with when one of the current
// walls has it, so the markers on screen do not jump through a save. A segment with no length is
// dropped: it is a click that was not meant to draw anything.
function toStoredWall(source, currentWalls) {
const corners = [source?.x1, source?.y1, source?.x2, source?.y2];
if (!corners.every((value) => Number.isFinite(Number(value)))) return null;
const wall = {
x1: toCoordinate(source.x1),
y1: toCoordinate(source.y1),
x2: toCoordinate(source.x2),
y2: toCoordinate(source.y2),
// What the editor draws is a wall, not an opening: both stay out unless it says otherwise.
move: source.move !== false,
sight: source.sight !== false,
door: [1, 2].includes(Math.round(Number(source.door))) ? Math.round(Number(source.door)) : 0
};
if (wall.x1 === wall.x2 && wall.y1 === wall.y2) return null;
const id = String(source?._id ?? '').trim();
if (id && currentWalls.some((current) => String(current?._id) === id)) wall._id = id;
return wall;
}
router.post('/walls', async (req, res) => {
try {
const { id, walls } = req.body;
if (!id || !Array.isArray(walls)) return res.json({ status: 'error', msg: 'errors.missing-data' });
const map = await Map.findById(id);
if (!map) return res.json({ status: 'error', msg: 'errors.notfound' });
if (!(await userOwnsCampaign(map.campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
const currentWalls = Array.from(map.walls ?? []);
map.walls = walls.map((source) => toStoredWall(source, currentWalls)).filter(Boolean);
await map.save();
res.json({ status: 'ok', map });
} catch (err) {
console.error(err);
res.json({ status: 'error', msg: 'errors.internal' });
}
});
module.exports = router;