From: xangelo Date: Mon, 18 Dec 2023 06:01:43 +0000 (-0500) Subject: fix: migrate store routes to separate file X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=f92668846eb9e5fe6b067ea46cede468479117cf;p=risinglegends.git fix: migrate store routes to separate file --- diff --git a/src/server/api.ts b/src/server/api.ts index e3095ed..75134f5 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -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 = ` - -
-
- ${equipment.name} -
-
- ${renderEquipmentDetails(equipment, req.player)} -
-
-
- - -
-
-`; - - 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 = ` - -
-
- ${item.name} -
-
-

${item.name}

-

${item.description}

-
-
-
- - -
-
-`; - - 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) { diff --git a/src/server/routes/index.ts b/src/server/routes/index.ts index efa0767..b08846f 100644 --- a/src/server/routes/index.ts +++ b/src/server/routes/index.ts @@ -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 index 0000000..b126fa8 --- /dev/null +++ b/src/server/routes/stores.ts @@ -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 = ` + +
+
+ ${equipment.name} +
+
+ ${renderEquipmentDetails(equipment, req.player)} +
+
+
+ + +
+
+`; + + 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 = ` + +
+
+ ${item.name} +
+
+

${item.name}

+

${item.description}

+
+
+
+ + +
+
+`; + + 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); +});