fix: auto-enter dungeon if you are in it
authorxangelo <me@xangelo.ca>
Thu, 14 Sep 2023 16:11:49 +0000 (12:11 -0400)
committerxangelo <me@xangelo.ca>
Thu, 14 Sep 2023 16:11:49 +0000 (12:11 -0400)
src/server/api.ts
src/server/dungeon.ts
src/server/views/components/explore-pane.ts [new file with mode: 0644]
src/server/views/dungeons/room.ts

index da7f014ffbd90a94799d4aecf5f0ef3f7f3cb0fd..b1ff042b03771c8b19803ae3ac42afcbdd2110b5 100644 (file)
@@ -21,7 +21,7 @@ import { getItemFromPlayer, getItemFromShop, getPlayersItems, getShopItems, give
 import {FightTrigger, Monster, MonsterForFight} from '../shared/monsters';
 import {getShopEquipment, listShopItems } from './shopEquipment';
 import {EquipmentSlot} from '../shared/inventory';
-import { clearTravelPlan, completeTravel, getAllPaths, getAllServices, getCityDetails, getService, getTravelPlan, stepForward, travel } from './map';
+import { clearTravelPlan, completeTravel, getAllPaths, getAllServices, getCityDetails, getService, getTravelPlan, stepForward, travel, getDungeon } from './map';
 import { signup, login, authEndpoint } from './auth';
 import {db} from './lib/db';
 import { getPlayerSkills} from './skills';
@@ -35,6 +35,7 @@ import { router as repairRouter } from './locations/repair';
 import { router as dungeonRouter } from './locations/dungeon';
 
 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';
@@ -50,7 +51,7 @@ import { renderChatMessage } from './views/chat';
 import { Item, PlayerItem, ShopItem } from 'shared/items';
 import { equip, unequip } from './equipment';
 import { HealthPotionSmall } from '../shared/items/health_potion';
-import { completeDungeonFight, getRoomVists, loadRoom, updatePlayerDungeonState } from './dungeon';
+import { completeDungeonFight, getActiveDungeon, getRoomVists, loadRoom, blockPlayerInDungeon } from './dungeon';
 import { renderDungeon, renderDungeonRoom } from './views/dungeons/room';
 
 dotenv();
@@ -237,7 +238,7 @@ app.get('/player/inventory', authEndpoint, async (req: Request, res: Response) =
   res.send(renderInventoryPage(inventory, items));
 });
 
-app.post('/player/equip/:item_id/:slot', authEndpoint, blockPlayerInFight, async (req: Request, res: Response) => {
+app.post('/player/equip/:item_id/:slot', authEndpoint, blockPlayerInFight, blockPlayerInDungeon, async (req: Request, 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;
@@ -289,7 +290,7 @@ app.post('/player/equip/:item_id/:slot', authEndpoint, blockPlayerInFight, async
   res.send(renderInventoryPage(inventory, items, inventoryItem.type) + renderPlayerBar(req.player));
 });
 
-app.post('/player/unequip/:item_id', authEndpoint, blockPlayerInFight, async (req: Request, res: Response) => {
+app.post('/player/unequip/:item_id', authEndpoint, blockPlayerInFight, blockPlayerInDungeon, async (req: Request, res: Response) => {
   const [item, ] = await Promise.all([
     getInventoryItem(req.player.id, req.params.item_id),
     unequip(req.player.id, req.params.item_id)
@@ -316,40 +317,50 @@ app.get('/player/explore', authEndpoint, async (req: Request, res: Response) =>
     const location = await getMonsterLocation(fight.ref_id);
 
     res.send(renderPlayerBar(req.player) + renderFightPreRound(fight, true, location, closestTown));
+    return;
   }
-  else {
-    if(travelPlan) {
-      // traveling!
-      const chanceToSeeMonster = random(0, 100);
-      const things: any[] = [];
-      if(chanceToSeeMonster <= 30) {
-        const monster = await getRandomMonster([closestTown]);
-        things.push(monster);
-      }
 
-      // STEP_DELAY
-      const nextAction = cache[`step:${req.player.id}`] || 0;
+  const dungeon = await getActiveDungeon(req.player.id);
+  if(dungeon) {
+    const service = await getDungeon(dungeon.dungeon_id);
+    const room = await loadRoom(dungeon.current_room_id);
+    const visits = await getRoomVists(req.player.id, service.event_name);
 
-      res.send(renderPlayerBar(req.player) + renderTravel({
-        things,
-        nextAction,
-        closestTown: closestTown,
-        walkingText: '',
-        travelPlan
-      }));
-    }
-    else {
-      // display the city info!
-      const [city, locations, paths] = await Promise.all([
-        getCityDetails(req.player.city_id),
-        getAllServices(req.player.city_id),
-        getAllPaths(req.player.city_id)
-      ]);
-
-      res.send(renderPlayerBar(req.player) + await renderMap({city, locations, paths}, closestTown));
+    res.send(ExplorePane(service.city_id, renderDungeon(service.city_name, service.name, room, visits)));
+    return;
+  }
+
+  // are you in a dungeon?
+  if(travelPlan) {
+    // traveling!
+    const chanceToSeeMonster = random(0, 100);
+    const things: any[] = [];
+    if(chanceToSeeMonster <= 30) {
+      const monster = await getRandomMonster([closestTown]);
+      things.push(monster);
     }
 
+    // STEP_DELAY
+    const nextAction = cache[`step:${req.player.id}`] || 0;
+
+    res.send(renderPlayerBar(req.player) + renderTravel({
+      things,
+      nextAction,
+      closestTown: closestTown,
+      walkingText: '',
+      travelPlan
+    }));
+    return;
   }
+
+  // display the default explore view
+  const [city, locations, paths] = await Promise.all([
+    getCityDetails(req.player.city_id),
+    getAllServices(req.player.city_id),
+    getAllPaths(req.player.city_id)
+  ]);
+
+  res.send(renderPlayerBar(req.player) + await renderMap({city, locations, paths}, closestTown));
 });
 
 // used to purchase equipment from a particular shop
index b2d3830afd3afbb9c958422f405cb33305be14fc..a58168d9a29cf21d7f0f7c172ae35ed0099feea1 100644 (file)
@@ -1,6 +1,18 @@
 import { Fight } from "shared/monsters";
-import { Dungeon, DungeonRoom, DungeonThing, DungeonPlayer, DungeonState, DungeonStateSummaryVists, DungeonStateSummaryFights } from "../shared/dungeon";
+import { Dungeon, DungeonRoom, DungeonPlayer, DungeonState, DungeonStateSummaryVists, DungeonStateSummaryFights } from "../shared/dungeon";
 import { db } from './lib/db';
+import { Request, Response } from 'express';
+import { ErrorAlert } from "./views/alert";
+
+export async function blockPlayerInDungeon(req: Request, res: Response, next: any) {
+  const state = await getActiveDungeon(req.player.id);
+  if(!state) {
+    next();
+  }
+  else {
+    res.send(ErrorAlert('You are currently exploring a dungeon'));
+  }
+}
 
 export async function getActiveDungeon(player_id: string): Promise<DungeonPlayer> {
   return db.select('*').from<DungeonPlayer>('dungeon_players').where({
diff --git a/src/server/views/components/explore-pane.ts b/src/server/views/components/explore-pane.ts
new file mode 100644 (file)
index 0000000..581677b
--- /dev/null
@@ -0,0 +1,5 @@
+export function ExplorePane(townId: number, contents: string): string {
+  return `<section id="explore" class="tab active" style="background-image: url('/assets/img/map/${townId}.jpeg')" hx-swap-oob="true">
+  ${contents}
+</section>`;
+}
index 5f371e8b04596da9c38b218b7f4d244ea3c09ab5..cde75e544cea0e9668c8e566620c007d43eca2ff 100644 (file)
@@ -67,5 +67,4 @@ export function dungeonRewards(dungeon: Dungeon, rewards: DungeonRewards): strin
   </div>
 </dialog>
   `;
-
 }