fix: migrate store routes to separate file
authorxangelo <me@xangelo.ca>
Mon, 18 Dec 2023 06:01:43 +0000 (01:01 -0500)
committerxangelo <me@xangelo.ca>
Mon, 18 Dec 2023 06:01:47 +0000 (01:01 -0500)
src/server/api.ts
src/server/routes/index.ts
src/server/routes/stores.ts [new file with mode: 0644]

index e3095ed5621b4313f5c7b259e0247642c490a20d..75134f5cf9e98c2685490c13fc198d68460da35a 100644 (file)
@@ -10,16 +10,15 @@ import http from 'http';
 import { Server, Socket } from 'socket.io';
 import * as CONSTANT from '../shared/constants';
 import { logger } from './lib/logger';
-import { loadPlayer, createPlayer, updatePlayer, movePlayer } from './player';
-import { random, sample, each } from 'lodash';
+import { loadPlayer, createPlayer, updatePlayer } from './player';
+import { random, each } from 'lodash';
 import {broadcastMessage} from '../shared/message';
-import {maxHp, Player} from '../shared/player';
+import { Player } from '../shared/player';
 import {createFight, getMonsterList, getMonsterLocation, getRandomMonster, loadMonster, loadMonsterFromFight} from './monster';
-import {addInventoryItem, getInventory } from './inventory';
-import { getItemFromPlayer, getItemFromShop, getPlayersItems, getShopItems, givePlayerItem, updateItemCount } from './items';
+import { addInventoryItem } from './inventory';
 import {FightTrigger, Monster} from '../shared/monsters';
-import {getShopEquipment, listShopItems } from './shopEquipment';
-import { clearTravelPlan, completeTravel, getAllPaths, getAllServices, getCityDetails, getService, getTravelPlan, stepForward, travel, getDungeon } from './map';
+import {getShopEquipment } from './shopEquipment';
+import { getAllPaths, getAllServices, getCityDetails, getService, getTravelPlan, getDungeon } from './map';
 import { signup, login, authEndpoint } from './auth';
 import {db} from './lib/db';
 import { getPlayerSkills} from './skills';
@@ -35,18 +34,14 @@ import * as Routers from './routes';
 import * as Alert from './views/alert';
 import { ExplorePane } from './views/components/explore-pane';
 import { renderPlayerBar } from './views/player-bar'
-import { renderEquipmentDetails, renderStore } from './views/stores';
 import { renderMap } from './views/map';
 import { renderSkills } from './views/skills';
-import { renderInventoryPage } from './views/inventory';
 import { renderMonsterSelector, renderOnlyMonsterSelector } from './views/monster-selector';
 import { renderFight, renderFightPreRound, renderRoundDetails } from './views/fight';
 import { renderTravel, travelButton } from './views/travel';
 import { renderChatMessage } from './views/chat';
 
 // TEMP!
-import { Item, PlayerItem, ShopItem } from 'shared/items';
-import { HealthPotionSmall } from '../shared/items/health_potion';
 import { completeDungeonFight, getActiveDungeon, getRoomVists, loadRoom } from './dungeon';
 import { renderDungeon, renderDungeonRoom } from './views/dungeons/room';
 import { flushBuffer, addEvent } from './events';
@@ -251,108 +246,6 @@ app.put('/location/:location_id/equipment/:item_id', authEndpoint, async (req: R
   res.send(renderPlayerBar(req.player) + Alert.SuccessAlert(`You purchased ${item.name}`));
 });
 
-// used to purchase items from a particular shop
-app.put('/location/:location_id/items/:item_id', authEndpoint, async (req: Request, res: Response) => {
-  const item: (ShopItem & Item) = await getItemFromShop(parseInt(req.params.item_id), parseInt(req.params.location_id));
-
-  if(!item) {
-    logger.log(`Invalid item [${req.params.item_id}]`);
-    return res.sendStatus(400);
-  }
-
-  if(req.player.gold < item.price_per_unit) {
-    res.send(Alert.ErrorAlert(`Sorry, you need at least ${item.price_per_unit.toLocaleString()}G to purchase this.`));
-    return;
-  }
-
-  req.player.gold -= item.price_per_unit;
-
-  await updatePlayer(req.player);
-  await givePlayerItem(req.player.id, item.id, 1);
-
-  res.send(renderPlayerBar(req.player) + Alert.SuccessAlert(`You purchased a ${item.name}`));
-});
-
-// used to display equipment modals in a store, validates that 
-// the equipment is actually in this store before displaying 
-// the modal
-app.get('/location/:location_id/equipment/:item_id/overview', authEndpoint, async (req: Request, res: Response) => {
-  const equipment = await getShopEquipment(parseInt(req.params.item_id), parseInt(req.params.location_id));
-
-  if(!equipment) {
-    logger.log(`Invalid equipment [${req.params.item_id}]`);
-    return res.sendStatus(400);
-  }
-
-  let html = `
-<dialog>
-  <div class="item-modal-overview">
-    <div class="icon">
-      <img src="${equipment.icon ? `/assets/img/icons/equipment/${equipment.icon}` : 'https://via.placeholder.com/64x64'}" title="${equipment.name}" alt="${equipment.name}"> 
-    </div>
-    <div>
-      ${renderEquipmentDetails(equipment, req.player)}
-    </div>
-  </div>
-  <div class="actions">
-    <button hx-put="/location/${equipment.location_id}/equipment/${equipment.id}" formmethod="dialog" value="cancel" class="green">Buy</button>
-    <button class="close-modal" formmethod="dialog" value="cancel">Cancel</button>
-  </div>
-</dialog>
-`;
-
-  res.send(html);
-});
-
-// used to display item modals in a store, validates that 
-// the item is actually in this store before displaying 
-// the modal
-app.get('/location/:location_id/items/:item_id/overview', authEndpoint, async (req: Request, res: Response) => {
-  const item: (ShopItem & Item) = await getItemFromShop(parseInt(req.params.item_id), parseInt(req.params.location_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="/location/${item.location_id}/items/${item.id}" formmethod="dialog" value="cancel" class="red">Buy</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));
-
-  if(!location || location.city_id !== req.player.city_id) {
-    logger.log(`Invalid location: [${req.params.location_id}]`);
-    res.sendStatus(400);
-  }
-  const [shopEquipment, shopItems] = await Promise.all([
-    listShopItems({location_id: location.id}),
-    getShopItems(location.id),
-  ]);
-
-  const html = await renderStore(shopEquipment, shopItems, req.player, location);
-
-  res.send(html);
-});
-
 app.get('/city/explore/city:explore/:location_id', authEndpoint, async (req: Request, res: Response) => {
   const location = await getService(parseInt(req.params.location_id));
   if(!location || location.city_id !== req.player.city_id) {
index efa07674089f722c96f39479fc9e739affc02e01..b08846f1719370991a9b5217644933a1b1008601 100644 (file)
@@ -2,3 +2,4 @@ export { chatRouter } from './chat';
 export { inventoryRouter } from './inventory';
 export { profileRouter } from './profile';
 export { travelRouter } from './travel';
+export { storeRouter } from './stores';
diff --git a/src/server/routes/stores.ts b/src/server/routes/stores.ts
new file mode 100644 (file)
index 0000000..b126fa8
--- /dev/null
@@ -0,0 +1,114 @@
+import { Request, Response, Router } from 'express';
+import { logger } from '../lib/logger';
+import { authEndpoint } from '../auth';
+import { getShopEquipment, listShopItems } from '../shopEquipment';
+import { updatePlayer } from '../player';
+import { getService } from '../map';
+import { getItemFromShop, getShopItems, givePlayerItem } from '../items';
+import { Item, ShopItem } from '../../shared/items';
+import { renderPlayerBar } from '../views/player-bar';
+import * as Alert from '../views/alert';
+import { renderEquipmentDetails, renderStore } from '../views/stores';
+
+export const storeRouter = Router();
+// used to purchase items from a particular shop
+storeRouter.put('/location/:location_id/items/:item_id', authEndpoint, async (req: Request, res: Response) => {
+  const item: (ShopItem & Item) = await getItemFromShop(parseInt(req.params.item_id), parseInt(req.params.location_id));
+
+  if(!item) {
+    logger.log(`Invalid item [${req.params.item_id}]`);
+    return res.sendStatus(400);
+  }
+
+  if(req.player.gold < item.price_per_unit) {
+    res.send(Alert.ErrorAlert(`Sorry, you need at least ${item.price_per_unit.toLocaleString()}G to purchase this.`));
+    return;
+  }
+
+  req.player.gold -= item.price_per_unit;
+
+  await updatePlayer(req.player);
+  await givePlayerItem(req.player.id, item.id, 1);
+
+  res.send(renderPlayerBar(req.player) + Alert.SuccessAlert(`You purchased a ${item.name}`));
+});
+
+// used to display equipment modals in a store, validates that 
+// the equipment is actually in this store before displaying 
+// the modal
+storeRouter.get('/location/:location_id/equipment/:item_id/overview', authEndpoint, async (req: Request, res: Response) => {
+  const equipment = await getShopEquipment(parseInt(req.params.item_id), parseInt(req.params.location_id));
+
+  if(!equipment) {
+    logger.log(`Invalid equipment [${req.params.item_id}]`);
+    return res.sendStatus(400);
+  }
+
+  let html = `
+<dialog>
+  <div class="item-modal-overview">
+    <div class="icon">
+      <img src="${equipment.icon ? `/assets/img/icons/equipment/${equipment.icon}` : 'https://via.placeholder.com/64x64'}" title="${equipment.name}" alt="${equipment.name}"> 
+    </div>
+    <div>
+      ${renderEquipmentDetails(equipment, req.player)}
+    </div>
+  </div>
+  <div class="actions">
+    <button hx-put="/location/${equipment.location_id}/equipment/${equipment.id}" formmethod="dialog" value="cancel" class="green">Buy</button>
+    <button class="close-modal" formmethod="dialog" value="cancel">Cancel</button>
+  </div>
+</dialog>
+`;
+
+  res.send(html);
+});
+
+// used to display item modals in a store, validates that 
+// the item is actually in this store before displaying 
+// the modal
+storeRouter.get('/location/:location_id/items/:item_id/overview', authEndpoint, async (req: Request, res: Response) => {
+  const item: (ShopItem & Item) = await getItemFromShop(parseInt(req.params.item_id), parseInt(req.params.location_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="/location/${item.location_id}/items/${item.id}" formmethod="dialog" value="cancel" class="red">Buy</button>
+    <button class="close-modal" formmethod="dialog" value="cancel">Cancel</button>
+  </div>
+</dialog>
+`;
+
+  res.send(html);
+});
+
+storeRouter.get('/city/stores/city:stores/:location_id', authEndpoint, async (req: Request, res: Response) => {
+  const location = await getService(parseInt(req.params.location_id));
+
+  if(!location || location.city_id !== req.player.city_id) {
+    logger.log(`Invalid location: [${req.params.location_id}]`);
+    res.sendStatus(400);
+  }
+  const [shopEquipment, shopItems] = await Promise.all([
+    listShopItems({location_id: location.id}),
+    getShopItems(location.id),
+  ]);
+
+  const html = await renderStore(shopEquipment, shopItems, req.player, location);
+
+  res.send(html);
+});