fix: migrate profile to separate route file
authorxangelo <me@xangelo.ca>
Mon, 18 Dec 2023 04:38:19 +0000 (23:38 -0500)
committerxangelo <me@xangelo.ca>
Mon, 18 Dec 2023 04:38:19 +0000 (23:38 -0500)
src/server/api.ts
src/server/routes/index.ts
src/server/routes/inventory.ts
src/server/routes/profile.ts [new file with mode: 0644]

index 1551e2954fb2e69a348f5fb2838152f57d1af02b..237980845e5b15e018fd0da51c57209724813d57 100644 (file)
@@ -13,9 +13,9 @@ import { logger } from './lib/logger';
 import { loadPlayer, createPlayer, updatePlayer, movePlayer } from './player';
 import { random, sample, each } from 'lodash';
 import {broadcastMessage} from '../shared/message';
-import {maxHp, maxVigor, Player} from '../shared/player';
+import {maxHp, Player} from '../shared/player';
 import {createFight, getMonsterList, getMonsterLocation, getRandomMonster, loadMonster, loadMonsterFromFight} from './monster';
-import {addInventoryItem, getEquippedItems, getInventory } from './inventory';
+import {addInventoryItem, getInventory } from './inventory';
 import { getItemFromPlayer, getItemFromShop, getPlayersItems, getShopItems, givePlayerItem, updateItemCount } from './items';
 import {FightTrigger, Monster} from '../shared/monsters';
 import {getShopEquipment, listShopItems } from './shopEquipment';
@@ -37,7 +37,6 @@ 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 { renderProfilePage } from './views/profile';
 import { renderSkills } from './views/skills';
 import { renderInventoryPage } from './views/inventory';
 import { renderMonsterSelector, renderOnlyMonsterSelector } from './views/monster-selector';
@@ -166,34 +165,6 @@ each(Routers, router => {
   app.use(router);
 });
 
-app.get('/player', authEndpoint, async (req: Request, res: Response) => {
-  const equipment = await getEquippedItems(req.player.id);
-  res.send(renderPlayerBar(req.player) + renderProfilePage(req.player, equipment));
-});
-
-app.post('/player/stat/:stat', authEndpoint, async (req: Request, res: Response) => {
-  const equipment = await getEquippedItems(req.player.id);
-  const stat = req.params.stat;
-  if(!['strength', 'constitution', 'dexterity', 'intelligence'].includes(stat)) {
-    res.send(Alert.ErrorAlert(`Sorry, that's not a valid stat to increase`));
-    return;
-  }
-
-  if(req.player.stat_points <= 0) {
-    res.send(Alert.ErrorAlert(`Sorry, you don't have enough stat points`));
-    return;
-  }
-
-  req.player.stat_points -= 1;
-  req.player[stat]++;
-
-  req.player.hp = maxHp(req.player.constitution, req.player.level);
-  req.player.vigor = maxVigor(req.player.constitution, req.player.level);
-  updatePlayer(req.player);
-
-  res.send(renderPlayerBar(req.player) + renderProfilePage(req.player, equipment));
-});
-
 app.get('/player/skills', authEndpoint, async (req: Request, res: Response) => {
   const skills = await getPlayerSkills(req.player.id);
 
index 4151f661559167f4606947d68a432b36d40634f0..8202630112834c4f3d94bb55b9230bf23ecaa187 100644 (file)
@@ -1,2 +1,3 @@
 export { chatRouter } from './chat';
 export { inventoryRouter } from './inventory';
+export { profileRouter } from './profile';
index 6fc38fc51ee5233540decd0ae7f69ada13f6fe5e..4153b994b26bd4de773a60a6fd678a7d9c1e559a 100644 (file)
@@ -3,8 +3,8 @@ 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 { getEquippedItems, getInventory, getInventoryItem } from '../inventory';
+import { getPlayersItems } from '../items';
 import { equip, unequip } from '../equipment';
 import { renderInventoryPage } from '../views/inventory';
 import { renderPlayerBar } from '../views/player-bar';
diff --git a/src/server/routes/profile.ts b/src/server/routes/profile.ts
new file mode 100644 (file)
index 0000000..da86864
--- /dev/null
@@ -0,0 +1,40 @@
+import { Request, Response, Router } from 'express';
+import { authEndpoint } from '../auth';
+import { getEquippedItems } from '../inventory';
+import { updatePlayer } from '../player';
+import {maxHp, maxVigor } from '../../shared/player';
+import * as Alert from '../views/alert';
+import { renderPlayerBar } from '../views/player-bar'
+import { renderProfilePage } from '../views/profile';
+
+export const profileRouter = Router();
+
+profileRouter.get('/player', authEndpoint, async (req: Request, res: Response) => {
+  const equipment = await getEquippedItems(req.player.id);
+  res.send(renderPlayerBar(req.player) + renderProfilePage(req.player, equipment));
+});
+
+profileRouter.post('/player/stat/:stat', authEndpoint, async (req: Request, res: Response) => {
+  const equipment = await getEquippedItems(req.player.id);
+  const stat = req.params.stat;
+  if(!['strength', 'constitution', 'dexterity', 'intelligence'].includes(stat)) {
+    res.send(Alert.ErrorAlert(`Sorry, that's not a valid stat to increase`));
+    return;
+  }
+
+  if(req.player.stat_points <= 0) {
+    res.send(Alert.ErrorAlert(`Sorry, you don't have enough stat points`));
+    return;
+  }
+
+  req.player.stat_points -= 1;
+  req.player[stat]++;
+
+  req.player.hp = maxHp(req.player.constitution, req.player.level);
+  req.player.vigor = maxVigor(req.player.constitution, req.player.level);
+  updatePlayer(req.player);
+
+  res.send(renderPlayerBar(req.player) + renderProfilePage(req.player, equipment));
+});
+
+