step 5.3: done

This commit is contained in:
bepis
2026-10-20 22:55:51 +11:00
parent 0e053aaee0
commit bf3cb35e68
6 changed files with 248 additions and 22 deletions
+20 -2
View File
@@ -1,9 +1,9 @@
// Inventory routes: list, equip, unequip.
// Inventory routes: list, equip, unequip, use consumables.
const zod = require('zod');
const { AppError } = require('../utils/errors');
const { requireAuth } = require('../middleware/session');
const { getCharacterIdForUser } = require('../services/characterService');
const { listInventory, equipItem, unequipItem } = require('../services/inventoryService');
const { listInventory, equipItem, unequipItem, useConsumable } = require('../services/inventoryService');
// Validation schemas
const slotSchema = zod.enum(['weapon', 'armor', 'trinket']);
@@ -56,10 +56,28 @@ async function handleUnequip(req, res) {
res.json({ ok: true, data: result });
}
/**
* POST /api/inventory/use — use a stackable consumable (heal/energy).
*/
async function handleUse(req, res) {
const parsed = zod.strictObject({
itemId: zod.number().int().positive(),
}).safeParse(req.body);
if (!parsed.success) {
throw new AppError('VALIDATION_ERROR', 'Request validation failed.', 400);
}
const { itemId } = parsed.data;
const characterId = getCharacterIdForUser(req.session.userId);
const result = useConsumable(characterId, itemId);
res.json({ ok: true, data: result });
}
module.exports = {
routes: {
getInventory: wrap(handleListInventory),
postEquip: wrap(handleEquip),
postUnequip: wrap(handleUnequip),
postUse: wrap(handleUse),
},
};