fix: make route imports dynamic
authorxangelo <me@xangelo.ca>
Sat, 21 Oct 2023 03:26:42 +0000 (23:26 -0400)
committerxangelo <me@xangelo.ca>
Sat, 21 Oct 2023 03:26:42 +0000 (23:26 -0400)
src/server/api.ts
src/server/routes/index.ts [new file with mode: 0644]

index 033eb764a83def5e4709b8877a51d4baf2816085..1551e2954fb2e69a348f5fb2838152f57d1af02b 100644 (file)
@@ -11,27 +11,26 @@ 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 } from 'lodash';
+import { random, sample, each } from 'lodash';
 import {broadcastMessage} from '../shared/message';
 import {maxHp, maxVigor, Player} from '../shared/player';
-import {createFight, getMonsterList, getMonsterLocation, getRandomMonster, loadMonster, loadMonsterFromFight, loadMonsterWithFaction} from './monster';
-import {addInventoryItem, getEquippedItems, getInventory, getInventoryItem} from './inventory';
+import {createFight, getMonsterList, getMonsterLocation, getRandomMonster, loadMonster, loadMonsterFromFight} from './monster';
+import {addInventoryItem, getEquippedItems, getInventory } from './inventory';
 import { getItemFromPlayer, getItemFromShop, getPlayersItems, getShopItems, givePlayerItem, updateItemCount } from './items';
 import {FightTrigger, Monster} from '../shared/monsters';
 import {getShopEquipment, listShopItems } from './shopEquipment';
-import {EquipmentSlot} from '../shared/inventory';
 import { clearTravelPlan, completeTravel, getAllPaths, getAllServices, getCityDetails, getService, getTravelPlan, stepForward, travel, getDungeon } from './map';
 import { signup, login, authEndpoint } from './auth';
 import {db} from './lib/db';
 import { getPlayerSkills} from './skills';
 
-import { fightRound, blockPlayerInFight } from './fight';
+import { fightRound } from './fight';
 
 import { router as healerRouter } from './locations/healer';
 import { router as professionRouter } from './locations/recruiter';
 import { router as repairRouter } from './locations/repair';
 import { router as dungeonRouter } from './locations/dungeon';
-import { chatRouter } from './routes/chat';
+import * as Routers from './routes';
 
 import * as Alert from './views/alert';
 import { ExplorePane } from './views/components/explore-pane';
@@ -48,9 +47,8 @@ import { renderChatMessage } from './views/chat';
 
 // TEMP!
 import { Item, PlayerItem, ShopItem } from 'shared/items';
-import { equip, unequip } from './equipment';
 import { HealthPotionSmall } from '../shared/items/health_potion';
-import { completeDungeonFight, getActiveDungeon, getRoomVists, loadRoom, blockPlayerInDungeon } from './dungeon';
+import { completeDungeonFight, getActiveDungeon, getRoomVists, loadRoom } from './dungeon';
 import { renderDungeon, renderDungeonRoom } from './views/dungeons/room';
 import { flushBuffer, addEvent } from './events';
 
@@ -164,7 +162,9 @@ app.use(healerRouter);
 app.use(professionRouter);
 app.use(repairRouter);
 app.use(dungeonRouter);
-app.use(chatRouter);
+each(Routers, router => {
+  app.use(router);
+});
 
 app.get('/player', authEndpoint, async (req: Request, res: Response) => {
   const equipment = await getEquippedItems(req.player.id);
@@ -200,81 +200,6 @@ app.get('/player/skills', authEndpoint, async (req: Request, res: Response) => {
   res.send(renderSkills(skills));
 });
 
-app.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));
-});
-
-app.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 = cache.get(`socket:${req.player.id}`).toString();
-    io.to(socketId).emit('updatePlayer', req.player);
-    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));
-});
-
-app.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));
-});
-
 app.get('/player/explore', authEndpoint, async (req: Request, res: Response) => {
   const fight = await loadMonsterFromFight(req.player.id);
   const travelPlan = await getTravelPlan(req.player.id);
diff --git a/src/server/routes/index.ts b/src/server/routes/index.ts
new file mode 100644 (file)
index 0000000..4151f66
--- /dev/null
@@ -0,0 +1,2 @@
+export { chatRouter } from './chat';
+export { inventoryRouter } from './inventory';