fix: migrate missing inventory routes to separate file
authorxangelo <me@xangelo.ca>
Mon, 18 Dec 2023 05:50:21 +0000 (00:50 -0500)
committerxangelo <me@xangelo.ca>
Mon, 18 Dec 2023 05:50:21 +0000 (00:50 -0500)
src/server/api.ts
src/server/routes/inventory.ts

index 69ddea12337b3d40519f32ade648e548b163f17e..e3095ed5621b4313f5c7b259e0247642c490a20d 100644 (file)
@@ -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 = `
-<dialog>
-  <div class="item-modal-overview">
-    <div class="icon">
-      <img src="/assets/img/icons/items/${item.icon_name}" title="${item.name}" alt="${item.name}"> 
-    </div>
-    <div>
-      <h4>${item.name}</h4>
-      <p>${item.description}</p>
-    </div>
-  </div>
-  <div class="actions">
-    <button hx-put="/item/${item.item_id}" formmethod="dialog" value="cancel" hx-target="#inventory" class="red">Use</button>
-    <button class="close-modal" formmethod="dialog" value="cancel">Cancel</button>
-  </div>
-</dialog>
-`;
-
-  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));
 
index 4153b994b26bd4de773a60a6fd678a7d9c1e559a..cf88bdf35ed49e636e48cf3d4f3447e1a7b92fa4 100644 (file)
@@ -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 = `
+<dialog>
+  <div class="item-modal-overview">
+    <div class="icon">
+      <img src="/assets/img/icons/items/${item.icon_name}" title="${item.name}" alt="${item.name}"> 
+    </div>
+    <div>
+      <h4>${item.name}</h4>
+      <p>${item.description}</p>
+    </div>
+  </div>
+  <div class="actions">
+    <button hx-put="/item/${item.item_id}" formmethod="dialog" value="cancel" hx-target="#inventory" class="red">Use</button>
+    <button class="close-modal" formmethod="dialog" value="cancel">Cancel</button>
+  </div>
+</dialog>
+`;
+
+  res.send(html);
+});
+
+