From: xangelo Date: Mon, 18 Dec 2023 04:38:19 +0000 (-0500) Subject: fix: migrate profile to separate route file X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=752656961cec3aee37277d5be2399b5a34a56b7e;p=risinglegends.git fix: migrate profile to separate route file --- diff --git a/src/server/api.ts b/src/server/api.ts index 1551e29..2379808 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -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); diff --git a/src/server/routes/index.ts b/src/server/routes/index.ts index 4151f66..8202630 100644 --- a/src/server/routes/index.ts +++ b/src/server/routes/index.ts @@ -1,2 +1,3 @@ export { chatRouter } from './chat'; export { inventoryRouter } from './inventory'; +export { profileRouter } from './profile'; diff --git a/src/server/routes/inventory.ts b/src/server/routes/inventory.ts index 6fc38fc..4153b99 100644 --- a/src/server/routes/inventory.ts +++ b/src/server/routes/inventory.ts @@ -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 index 0000000..da86864 --- /dev/null +++ b/src/server/routes/profile.ts @@ -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)); +}); + +