Fix issue #38: Implement tokens on maps
All checks were successful
Test / test (push) Successful in 2s
All checks were successful
Test / test (push) Successful in 2s
This commit is contained in:
@@ -19,6 +19,14 @@ const MapSchema = new Schema({
|
||||
x: { type: Number, default: 0 },
|
||||
y: { type: Number, default: 0 }
|
||||
}],
|
||||
// Actors dragged onto the map to stand on it as tokens. x/y are image pixels of the centre of the
|
||||
// token, like a pin, and the viewer keeps them snapped onto a grid cell so a token sits where the
|
||||
// square it stands in is.
|
||||
tokens: [{
|
||||
actor: { type: Schema.Types.ObjectId, ref: 'Actor', required: true },
|
||||
x: { type: Number, default: 0 },
|
||||
y: { type: Number, default: 0 }
|
||||
}],
|
||||
campaign: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'Campaign',
|
||||
|
||||
@@ -5,6 +5,7 @@ const upload = require('../services/storage');
|
||||
|
||||
const Campaign = require('../models/Campaign');
|
||||
const Actor = require('../models/Actor');
|
||||
const Map = require('../models/Map');
|
||||
|
||||
async function userOwnsCampaign(campaignId, userId) {
|
||||
const campaign = await Campaign.findOne({ _id: campaignId, createdBy: userId }).lean();
|
||||
@@ -101,6 +102,11 @@ router.post('/delete', async (req, res) => {
|
||||
if (!actor) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
if (!(await userOwnsCampaign(actor.campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
await actor.deleteOne();
|
||||
|
||||
// A token pointing at an actor that is gone would stand on the maps for nothing: drop those
|
||||
// along with the actor, like the pins of a deleted note.
|
||||
await Map.updateMany({ 'tokens.actor': id }, { $pull: { tokens: { actor: id } } });
|
||||
|
||||
res.json({ status: 'ok' });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
@@ -6,6 +6,7 @@ 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();
|
||||
@@ -93,8 +94,9 @@ router.post('/delete', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// A pin is a note placed at a spot of the map, in image pixels. Coordinates arrive as strings with
|
||||
// multipart requests and are never negative: the viewer clamps them to the image already.
|
||||
// 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;
|
||||
@@ -152,4 +154,69 @@ router.post('/unpin', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 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;
|
||||
|
||||
Reference in New Issue
Block a user