From 324251fcc6c62cb63e52d689477c2d3093ebf1f5 Mon Sep 17 00:00:00 2001 From: xangelo Date: Fri, 20 Oct 2023 23:25:53 -0400 Subject: [PATCH] fix: migrate inventory to separate route file --- src/server/routes/inventory.ts | 90 ++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/server/routes/inventory.ts diff --git a/src/server/routes/inventory.ts b/src/server/routes/inventory.ts new file mode 100644 index 0000000..6fc38fc --- /dev/null +++ b/src/server/routes/inventory.ts @@ -0,0 +1,90 @@ +import { Request, Response, Router } from 'express'; +import { logger } from '../lib/logger'; +import { authEndpoint } from '../auth'; +import { blockPlayerInFight } from '../fight'; +import { blockPlayerInDungeon } from '../dungeon'; +import {addInventoryItem, getEquippedItems, getInventory, getInventoryItem} from '../inventory'; +import { getItemFromPlayer, getItemFromShop, getPlayersItems, getShopItems, givePlayerItem, updateItemCount } from '../items'; +import { equip, unequip } from '../equipment'; +import { renderInventoryPage } from '../views/inventory'; +import { renderPlayerBar } from '../views/player-bar'; +import { EquipmentSlot } from '../../shared/inventory'; + +export const inventoryRouter = Router(); + +inventoryRouter.get('/player/inventory', authEndpoint, async (req: Request, res: Response) => { + const [inventory, items] = await Promise.all([ + getInventory(req.player.id), + getPlayersItems(req.player.id) + ]); + + res.send(renderInventoryPage(inventory, items)); + +}); + +inventoryRouter.post('/player/unequip/:item_id', authEndpoint, blockPlayerInFight, blockPlayerInDungeon, async (req: Request, res: Response) => { + const [item, ] = await Promise.all([ + getInventoryItem(req.player.id, req.params.item_id), + unequip(req.player.id, req.params.item_id) + ]); + + const [inventory, items] = await Promise.all([ + getInventory(req.player.id), + getPlayersItems(req.player.id) + ]); + + res.send(renderInventoryPage(inventory, items, item.type) + renderPlayerBar(req.player)); +}); + +inventoryRouter.post('/player/equip/:item_id/:slot', authEndpoint, blockPlayerInFight, blockPlayerInDungeon, async (req: Request, res: Response) => { + const inventoryItem = await getInventoryItem(req.player.id, req.params.item_id); + const equippedItems = await getEquippedItems(req.player.id); + const requestedSlot = req.params.slot; + let desiredSlot: EquipmentSlot = inventoryItem.equipment_slot; + + try { + // handes the situation where you're trying to equip an item + // that can be equipped to any hand + if(inventoryItem.equipment_slot === 'ANY_HAND') { + if(requestedSlot === 'LEFT_HAND' || requestedSlot === 'RIGHT_HAND') { + // get the players current equipment in that slot! + if(equippedItems.some(v => { + return v.equipment_slot === requestedSlot || v.equipment_slot === 'TWO_HANDED'; + })) { + throw new Error(); + } + else { + desiredSlot = requestedSlot; + } + } + } + + if(requestedSlot === 'TWO_HANDED') { + if(equippedItems.some(v => { + return v.equipment_slot === 'LEFT_HAND' || v.equipment_slot === 'RIGHT_HAND'; + })) { + throw new Error(); + } + } + + + await equip(req.player.id, inventoryItem, desiredSlot); + const socketId = req.rl.cache.get(`socket:${req.player.id}`).toString(); + req.rl.io.to(socketId).emit('updatePlayer', req.player); + req.rl.io.to(socketId).emit('alert', { + type: 'success', + text: `You equipped your ${inventoryItem.name}` + }); + } + catch(e) { + logger.log(e); + } + + const [inventory, items] = await Promise.all([ + getInventory(req.player.id), + getPlayersItems(req.player.id) + ]); + + res.send(renderInventoryPage(inventory, items, inventoryItem.type) + renderPlayerBar(req.player)); +}); + -- 2.25.1