From: xangelo Date: Mon, 18 Dec 2023 05:50:21 +0000 (-0500) Subject: fix: migrate missing inventory routes to separate file X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=e3b94ff3ccae828520ef9ba9f5443400e08063c0;p=risinglegends.git fix: migrate missing inventory routes to separate file --- diff --git a/src/server/api.ts b/src/server/api.ts index 69ddea1..e3095ed 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -336,78 +336,6 @@ app.get('/location/:location_id/items/:item_id/overview', authEndpoint, async (r res.send(html); }); -app.put('/item/:item_id', authEndpoint, async (req: Request, res: Response) => { - const item: PlayerItem = await getItemFromPlayer(req.player.id, parseInt(req.params.item_id)); - - if(!item) { - console.log(`Can't find item [${req.params.item_id}]`); - return; - } - - if(item.amount < 1) { - res.send(Alert.ErrorAlert(`You dont have enough ${item.name}`)); - return; - } - - item.amount -= 1; - - switch(item.effect_name) { - case 'heal_small': - const hpGain = HealthPotionSmall.effect(req.player); - - req.player.hp += hpGain; - - if(req.player.hp > maxHp(req.player.constitution, req.player.level)) { - req.player.hp = maxHp(req.player.constitution, req.player.level); - } - break; - } - - await updateItemCount(req.player.id, item.item_id, -1); - await updatePlayer(req.player); - - const inventory = await getInventory(req.player.id); - const items = await getPlayersItems(req.player.id); - - res.send( - [ - renderPlayerBar(req.player), - renderInventoryPage(inventory, items, 'ITEMS'), - Alert.SuccessAlert(`You used the ${item.name}`) - ].join("") - ); - -}); - -app.get('/modal/items/:item_id', authEndpoint, async (req: Request, res: Response) => { - const item: PlayerItem = await getItemFromPlayer(req.player.id, parseInt(req.params.item_id)); - - if(!item) { - logger.log(`Invalid item [${req.params.item_id}]`); - return res.sendStatus(400); - } - - let html = ` - -
-
- ${item.name} -
-
-

${item.name}

-

${item.description}

-
-
-
- - -
-
-`; - - res.send(html); -}); - app.get('/city/stores/city:stores/:location_id', authEndpoint, async (req: Request, res: Response) => { const location = await getService(parseInt(req.params.location_id)); diff --git a/src/server/routes/inventory.ts b/src/server/routes/inventory.ts index 4153b99..cf88bdf 100644 --- a/src/server/routes/inventory.ts +++ b/src/server/routes/inventory.ts @@ -3,12 +3,17 @@ import { logger } from '../lib/logger'; import { authEndpoint } from '../auth'; import { blockPlayerInFight } from '../fight'; import { blockPlayerInDungeon } from '../dungeon'; +import { updatePlayer } from '../player'; import { getEquippedItems, getInventory, getInventoryItem } from '../inventory'; -import { getPlayersItems } from '../items'; +import { getPlayersItems, getItemFromPlayer, updateItemCount } from '../items'; import { equip, unequip } from '../equipment'; import { renderInventoryPage } from '../views/inventory'; import { renderPlayerBar } from '../views/player-bar'; +import * as Alert from '../views/alert'; import { EquipmentSlot } from '../../shared/inventory'; +import { PlayerItem } from '../../shared/items'; +import { maxHp } from '../../shared/player'; +import { HealthPotionSmall } from '../../shared/items/health_potion'; export const inventoryRouter = Router(); @@ -88,3 +93,76 @@ inventoryRouter.post('/player/equip/:item_id/:slot', authEndpoint, blockPlayerIn res.send(renderInventoryPage(inventory, items, inventoryItem.type) + renderPlayerBar(req.player)); }); +inventoryRouter.put('/item/:item_id', authEndpoint, async (req: Request, res: Response) => { + const item: PlayerItem = await getItemFromPlayer(req.player.id, parseInt(req.params.item_id)); + + if(!item) { + console.log(`Can't find item [${req.params.item_id}]`); + return; + } + + if(item.amount < 1) { + res.send(Alert.ErrorAlert(`You dont have enough ${item.name}`)); + return; + } + + item.amount -= 1; + + switch(item.effect_name) { + case 'heal_small': + const hpGain = HealthPotionSmall.effect(req.player); + + req.player.hp += hpGain; + + if(req.player.hp > maxHp(req.player.constitution, req.player.level)) { + req.player.hp = maxHp(req.player.constitution, req.player.level); + } + break; + } + + await updateItemCount(req.player.id, item.item_id, -1); + await updatePlayer(req.player); + + const inventory = await getInventory(req.player.id); + const items = await getPlayersItems(req.player.id); + + res.send( + [ + renderPlayerBar(req.player), + renderInventoryPage(inventory, items, 'ITEMS'), + Alert.SuccessAlert(`You used the ${item.name}`) + ].join("") + ); + +}); + +inventoryRouter.get('/modal/items/:item_id', authEndpoint, async (req: Request, res: Response) => { + const item: PlayerItem = await getItemFromPlayer(req.player.id, parseInt(req.params.item_id)); + + if(!item) { + logger.log(`Invalid item [${req.params.item_id}]`); + return res.sendStatus(400); + } + + let html = ` + +
+
+ ${item.name} +
+
+

${item.name}

+

${item.description}

+
+
+
+ + +
+
+`; + + res.send(html); +}); + +