fix: cant perform other actions in a fight
authorxangelo <me@xangelo.ca>
Wed, 16 Aug 2023 20:00:17 +0000 (16:00 -0400)
committerxangelo <me@xangelo.ca>
Wed, 16 Aug 2023 20:00:17 +0000 (16:00 -0400)
src/server/api.ts

index 35ba7e34c03d362b19b6b23b067725b6f3e1045d..96adb4e69df9fddc4c9fc7a3dff86eba712878fc 100644 (file)
@@ -16,7 +16,7 @@ import {clearFight, createFight, getMonsterList, getMonsterLocation, getRandomMo
 import {FightRound} from '../shared/fight';
 import {addInventoryItem, deleteInventoryItem, getEquippedItems, getInventory, getInventoryItem, updateAp} from './inventory';
 import { getItemFromPlayer, getItemFromShop, getPlayersItems, getShopItems, givePlayerItem, updateItemCount } from './items';
-import {FightTrigger, Monster, MonsterForFight, MonsterWithFaction} from '../shared/monsters';
+import {Fight, FightTrigger, Monster, MonsterForFight, MonsterWithFaction} from '../shared/monsters';
 import {getShopEquipment, listShopItems } from './shopEquipment';
 import {EquippedItemDetails} from '../shared/equipped';
 import {ArmourEquipmentSlot, EquipmentSlot} from '../shared/inventory';
@@ -451,13 +451,13 @@ app.post('/player/stat/:stat', authEndpoint, async (req: AuthRequest, res: Respo
   res.send(renderPlayerBar(req.player, equippedItems) + renderProfilePage(req.player));
 });
 
-app.get('/player/skills', authEndpoint, async (req: AuthRequest, res: Response) => {
+app.get('/player/skills', authEndpoint, blockPlayerInFight, async (req: AuthRequest, res: Response) => {
   const skills = await getPlayerSkills(req.player.id);
 
   res.send(renderSkills(skills));
 });
 
-app.get('/player/inventory', authEndpoint, async (req: AuthRequest, res: Response) => {
+app.get('/player/inventory', authEndpoint, blockPlayerInFight, async (req: AuthRequest, res: Response) => {
   const [inventory, items] = await Promise.all([
     getInventory(req.player.id),
     getPlayersItems(req.player.id)
@@ -466,7 +466,7 @@ app.get('/player/inventory', authEndpoint, async (req: AuthRequest, res: Respons
   res.send(renderInventoryPage(inventory, items));
 });
 
-app.post('/player/equip/:item_id/:slot', authEndpoint, async (req: AuthRequest, res: Response) => {
+app.post('/player/equip/:item_id/:slot', authEndpoint, blockPlayerInFight, async (req: AuthRequest, 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;
@@ -518,7 +518,7 @@ app.post('/player/equip/:item_id/:slot', authEndpoint, async (req: AuthRequest,
   res.send(renderInventoryPage(inventory, items, inventoryItem.type) + renderPlayerBar(req.player, inventory));
 });
 
-app.post('/player/unequip/:item_id', authEndpoint, async (req: AuthRequest, res: Response) => {
+app.post('/player/unequip/:item_id', authEndpoint, blockPlayerInFight, async (req: AuthRequest, res: Response) => {
   const [item, ] = await Promise.all([
     getInventoryItem(req.player.id, req.params.item_id),
     unequip(req.player.id, req.params.item_id)
@@ -532,13 +532,22 @@ app.post('/player/unequip/:item_id', authEndpoint, async (req: AuthRequest, res:
   res.send(renderInventoryPage(inventory, items, item.type) + renderPlayerBar(req.player, inventory));
 });
 
+async function blockPlayerInFight(req: AuthRequest, res: Response, next) {
+  const fight = await loadMonsterFromFight(req.player.id);
+  if(!fight) {
+    next();
+    return;
+  }
+
+  res.send(Alert.ErrorAlert(`You are currently in a fight with a ${fight.name}`));
+}
+
 app.get('/player/explore', authEndpoint, async (req: AuthRequest, res: Response) => {
   const fight = await loadMonsterFromFight(req.player.id);
   let closestTown = req.player.city_id;
 
   const equippedItems = await getEquippedItems(req.player.id);
   if(fight) {
-    // ok lets display the fight screen!
     const data: MonsterForFight = {
       id: fight.id,
       hp: fight.hp,