148 lines
6.5 KiB
JavaScript
148 lines
6.5 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
|
|
const upload = require('../services/storage');
|
|
|
|
const Campaign = require('../models/Campaign');
|
|
const Actor = require('../models/Actor');
|
|
const Folder = require('../models/Folder');
|
|
const Map = require('../models/Map');
|
|
|
|
async function userOwnsCampaign(campaignId, userId) {
|
|
const campaign = await Campaign.findOne({ _id: campaignId, createdBy: userId }).lean();
|
|
return Boolean(campaign);
|
|
}
|
|
|
|
// Hit points and hit dice travel as a bar the sheet fills (`current` of `max`, see models/Actor.js).
|
|
// A partial update keeps whatever half of the pair it did not send, and `current` never passes the
|
|
// maximum it ends up with.
|
|
function barPool(existing, value) {
|
|
const max = Math.max(0, Number(value?.max ?? existing?.max) || 0);
|
|
const current = Math.max(0, Math.min(Number(value?.current ?? existing?.current) || 0, max));
|
|
return { current, max };
|
|
}
|
|
|
|
// Folders the campaign's actors are grouped into, in the shape the sidebar groups by
|
|
// (the notes, items and maps each keep a tree of their own, see models/Folder.js).
|
|
async function actorFolders(campaignId) {
|
|
return Folder.find({ campaign: campaignId, target: 'actor' })
|
|
.select('_id name parentFolder date color')
|
|
.sort({ date: -1 })
|
|
.lean();
|
|
}
|
|
|
|
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 actors = await Actor.find({ campaign }).sort({ name: 1 }).lean();
|
|
res.json({ status: 'ok', actors, folders: await actorFolders(campaign) });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
router.post('/create', async (req, res) => {
|
|
try {
|
|
const { name, campaign, type, species, className, subclass, level, background, challengeRating, description } = 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 actor = await Actor.create({
|
|
name: name.trim(), campaign, type, species, className, subclass,
|
|
level: level === '' || level === undefined ? undefined : Number(level),
|
|
background, challengeRating, description
|
|
});
|
|
res.json({ status: 'ok', actor });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
router.post('/update', async (req, res) => {
|
|
try {
|
|
const { id, name, species, className, subclass, level, abilityScores, armorClass, speed, hitPoints, hitDice, proficiencies, inventory, traits, spells, image, folder } = req.body;
|
|
if (!id) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
|
|
|
const actor = await Actor.findById(id);
|
|
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' });
|
|
|
|
// `null` moves the actor to the root of the actor list, an id files it in one of
|
|
// this campaign's actor folders; anything else is refused.
|
|
if (folder !== undefined) {
|
|
if (!(await Folder.belongsToSection(folder || null, actor.campaign, 'actor'))) {
|
|
return res.json({ status: 'error', msg: 'errors.notfound' });
|
|
}
|
|
actor.folder = folder || null;
|
|
}
|
|
|
|
if (name !== undefined) actor.name = String(name).trim();
|
|
if (species !== undefined) actor.species = species;
|
|
if (className !== undefined) actor.className = className;
|
|
if (subclass !== undefined) actor.subclass = subclass;
|
|
if (level !== undefined) actor.level = Number(level);
|
|
if (abilityScores !== undefined) actor.abilityScores = abilityScores;
|
|
if (armorClass !== undefined) actor.armorClass = Number(armorClass);
|
|
if (speed !== undefined) actor.speed = Number(speed);
|
|
if (hitPoints !== undefined) actor.hitPoints = barPool(actor.hitPoints, hitPoints);
|
|
if (hitDice !== undefined) actor.hitDice = barPool(actor.hitDice, hitDice);
|
|
if (proficiencies !== undefined) actor.proficiencies = proficiencies;
|
|
if (inventory !== undefined) actor.inventory = inventory;
|
|
if (traits !== undefined) actor.traits = traits;
|
|
if (spells !== undefined) actor.spells = spells;
|
|
if (image !== undefined) actor.image = String(image);
|
|
|
|
await actor.save();
|
|
res.json({ status: 'ok', actor });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
// Upload actor image post
|
|
router.post("/upload-image", upload.single("image"), async (req, res) => {
|
|
try {
|
|
const actorId = req.query.actor || req.body.actor;
|
|
if (!actorId || !req.file) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
|
|
|
const actor = await Actor.findById(actorId);
|
|
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' });
|
|
|
|
actor.image = req.file.filename;
|
|
await actor.save();
|
|
res.json({ status: "ok", msg: "uploaded", actor });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: "error", msg: "internal" });
|
|
}
|
|
});
|
|
|
|
router.post('/delete', async (req, res) => {
|
|
try {
|
|
const { id } = req.body;
|
|
if (!id) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
|
const actor = await Actor.findById(id);
|
|
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);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|