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';
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';
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) {
--- /dev/null
+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);
+});