--- /dev/null
+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));
+});
+