223 lines
9.2 KiB
JavaScript
223 lines
9.2 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
|
|
const upload = require('../services/storage');
|
|
|
|
const Campaign = require('../models/Campaign');
|
|
const Map = require('../models/Map');
|
|
const Note = require('../models/Note');
|
|
const Actor = require('../models/Actor');
|
|
|
|
async function userOwnsCampaign(campaignId, userId) {
|
|
const campaign = await Campaign.findOne({ _id: campaignId, createdBy: userId }).lean();
|
|
return Boolean(campaign);
|
|
}
|
|
|
|
// Grid values arrive as strings when the request is a multipart form upload.
|
|
function toNumber(value, fallback) {
|
|
if (value === undefined || value === null || value === '') return fallback;
|
|
const number = Number(value);
|
|
return Number.isFinite(number) ? number : fallback;
|
|
}
|
|
|
|
router.get('/list', async (req, res) => {
|
|
try {
|
|
const { campaign } = req.query;
|
|
if (!campaign) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
|
if (!(await userOwnsCampaign(campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
|
|
|
|
const maps = await Map.find({ campaign }).sort({ name: 1 }).lean();
|
|
res.json({ status: 'ok', maps });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
router.post('/create', upload.single('image'), async (req, res) => {
|
|
try {
|
|
const { name, campaign, gridSize, offsetX, offsetY, scale } = req.body;
|
|
if (!name?.trim() || !campaign) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
|
if (!(await userOwnsCampaign(campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
|
|
|
|
const map = await Map.create({
|
|
name: name.trim(), campaign,
|
|
image: req.file ? req.file.filename : '',
|
|
gridSize: toNumber(gridSize, 50),
|
|
offsetX: toNumber(offsetX, 0),
|
|
offsetY: toNumber(offsetY, 0),
|
|
scale: toNumber(scale, 5)
|
|
});
|
|
res.json({ status: 'ok', map });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
router.post('/update', upload.single('image'), async (req, res) => {
|
|
try {
|
|
const { id, name, gridSize, offsetX, offsetY, scale } = req.body;
|
|
if (!id) 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' });
|
|
|
|
if (name !== undefined) map.name = String(name).trim();
|
|
if (gridSize !== undefined) map.gridSize = toNumber(gridSize, map.gridSize);
|
|
if (offsetX !== undefined) map.offsetX = toNumber(offsetX, map.offsetX);
|
|
if (offsetY !== undefined) map.offsetY = toNumber(offsetY, map.offsetY);
|
|
if (scale !== undefined) map.scale = toNumber(scale, map.scale);
|
|
if (req.file) map.image = req.file.filename;
|
|
|
|
await map.save();
|
|
res.json({ status: 'ok', map });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
router.post('/delete', async (req, res) => {
|
|
try {
|
|
const { id } = req.body;
|
|
if (!id) 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' });
|
|
await map.deleteOne();
|
|
res.json({ status: 'ok' });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
// A pin is a note placed at a spot of the map, in image pixels, and so is the centre of a token.
|
|
// Coordinates arrive as strings with multipart requests and are never negative: the viewer clamps
|
|
// them to the image already.
|
|
function toCoordinate(value) {
|
|
const number = Number(value);
|
|
if (!Number.isFinite(number)) return 0;
|
|
return Math.max(0, Math.round(number));
|
|
}
|
|
|
|
router.post('/pin', async (req, res) => {
|
|
try {
|
|
const { id, note, x, y } = req.body;
|
|
if (!id || !note) 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' });
|
|
|
|
// Only a note of the same campaign can be pinned onto one of its maps.
|
|
const noteDoc = await Note.findOne({ _id: note, campaign: map.campaign }).select('_id').lean();
|
|
if (!noteDoc) return res.json({ status: 'error', msg: 'errors.notfound' });
|
|
|
|
// A note is pinned once per map: pinning it again moves the marker instead of adding a twin.
|
|
const pin = map.pins.find((current) => String(current.note) === String(note));
|
|
if (pin) {
|
|
pin.x = toCoordinate(x);
|
|
pin.y = toCoordinate(y);
|
|
} else {
|
|
map.pins.push({ note, x: toCoordinate(x), y: toCoordinate(y) });
|
|
}
|
|
|
|
await map.save();
|
|
res.json({ status: 'ok', map });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
router.post('/unpin', async (req, res) => {
|
|
try {
|
|
const { id, pin } = req.body;
|
|
if (!id || !pin) 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' });
|
|
|
|
// `_id` stays with the pins that remain: the viewer removes them by that id.
|
|
map.pins = map.pins
|
|
.filter((current) => String(current._id) !== String(pin))
|
|
.map((current) => ({ _id: current._id, note: current.note, x: current.x, y: current.y }));
|
|
await map.save();
|
|
res.json({ status: 'ok', map });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
// A token is an actor standing on the map, at the middle of a grid cell. Anywhere it is dropped in a
|
|
// cell lands it in that cell, whatever the grid is drawn from: same rounding as the viewer does while
|
|
// it drags, so the token does not jump when the move is saved.
|
|
function toCellCoordinate(value, size, offset) {
|
|
const grid = Math.max(1, Number(size) || 50);
|
|
const origin = Number(offset) || 0;
|
|
const cell = Math.floor((toCoordinate(value) - origin) / grid);
|
|
return Math.max(0, Math.round(origin + (cell + 0.5) * grid));
|
|
}
|
|
|
|
router.post('/place-token', async (req, res) => {
|
|
try {
|
|
const { id, actor, x, y } = req.body;
|
|
if (!id || !actor) 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' });
|
|
|
|
// Only an actor of the same campaign can stand on one of its maps.
|
|
const actorDoc = await Actor.findOne({ _id: actor, campaign: map.campaign }).select('_id').lean();
|
|
if (!actorDoc) return res.json({ status: 'error', msg: 'errors.notfound' });
|
|
|
|
// An actor stands on a map once: placing it again moves its token instead of adding a twin.
|
|
const token = map.tokens.find((current) => String(current.actor) === String(actor));
|
|
const spot = {
|
|
x: toCellCoordinate(x, map.gridSize, map.offsetX),
|
|
y: toCellCoordinate(y, map.gridSize, map.offsetY)
|
|
};
|
|
if (token) {
|
|
token.x = spot.x;
|
|
token.y = spot.y;
|
|
} else {
|
|
map.tokens.push({ actor, ...spot });
|
|
}
|
|
|
|
await map.save();
|
|
res.json({ status: 'ok', map });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
router.post('/remove-token', async (req, res) => {
|
|
try {
|
|
const { id, token } = req.body;
|
|
if (!id || !token) 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' });
|
|
|
|
// `_id` stays with the tokens that remain, the same way the pins are removed.
|
|
map.tokens = map.tokens
|
|
.filter((current) => String(current._id) !== String(token))
|
|
.map((current) => ({ _id: current._id, actor: current.actor, x: current.x, y: current.y }));
|
|
await map.save();
|
|
res.json({ status: 'ok', map });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|