fix: migrate travel to separate route file
authorxangelo <me@xangelo.ca>
Mon, 18 Dec 2023 05:11:14 +0000 (00:11 -0500)
committerxangelo <me@xangelo.ca>
Mon, 18 Dec 2023 05:11:14 +0000 (00:11 -0500)
src/server/api.ts
src/server/routes/index.ts
src/server/routes/travel.ts [new file with mode: 0644]

index 237980845e5b15e018fd0da51c57209724813d57..69ddea12337b3d40519f32ade648e548b163f17e 100644 (file)
@@ -437,19 +437,6 @@ app.get('/city/explore/city:explore/:location_id', authEndpoint, async (req: Req
   res.send(renderOnlyMonsterSelector(monsters, 0, location));
 });
 
-app.post('/travel', authEndpoint, async (req: Request, res: Response) => {
-  const destination_id = parseInt(req.body.destination_id);
-
-  if(!destination_id || isNaN(destination_id)) {
-    logger.log(`Invalid destination_id [${req.body.destination_id}]`);
-    return res.sendStatus(400);
-  }
-
-  const travelPlan = travel(req.player, req.body.destination_id);
-
-  res.json(travelPlan);
-});
-
 app.post('/fight/turn', authEndpoint, async (req: Request, res: Response) => {
   const fightBlockKey = `fightturn:${req.player.id}`;
 
@@ -562,124 +549,6 @@ app.post('/fight', fightRateLimiter, authEndpoint, async (req: Request, res: Res
   res.send(renderFightPreRound(fight, true, location, location.city_id));
 });
 
-app.post('/travel/step', authEndpoint, async (req: Request, res: Response) => {
-  const stepTimerKey = `step:${req.player.id}`;
-
-  const travelPlan = await getTravelPlan(req.player.id);
-  if(!travelPlan) {
-    res.send(Alert.ErrorAlert('You don\'t have a travel plan'));
-    return;
-  }
-
-  if(cache[stepTimerKey]) {
-    if(cache[stepTimerKey] > Date.now()) {
-      res.send(Alert.ErrorAlert('Hmm.. travelling too quickly'));
-      return;
-    }
-  }
-
-  travelPlan.current_position++;
-
-  if(travelPlan.current_position >= travelPlan.total_distance) {
-    const travel = await completeTravel(req.player.id);
-
-    req.player.city_id = travel.destination_id;
-    await movePlayer(travel.destination_id, req.player.id);
-
-    const [city, locations, paths] = await Promise.all([
-      getCityDetails(travel.destination_id),
-      getAllServices(travel.destination_id, req.player.level),
-      getAllPaths(travel.destination_id)
-    ]);
-
-    delete cache[stepTimerKey];
-    res.send(await renderMap({city, locations, paths}, req.player.city_id));
-  }
-  else {
-    const walkingText: string[] = [
-      'You take a step forward',
-      'You keep moving'
-    ];
-    // update existing plan..
-    // decide if they will run into anything
-    const travelPlan = await stepForward(req.player.id);
-
-    const closest: number = (travelPlan.current_position / travelPlan.total_distance) > 0.5 ? travelPlan.destination_id : travelPlan.source_id;
-
-    const chanceToSeeMonster = random(0, 100);
-    const things: any[] = [];
-    if(chanceToSeeMonster <= 30) {
-      const monster = await getRandomMonster([closest]);
-      things.push(monster);
-    }
-
-    // STEP_DELAY
-    const nextAction = Date.now() + CONSTANT.STEP_DELAY;
-
-    cache[stepTimerKey] = nextAction;
-
-    res.send(renderTravel({
-      things,
-      nextAction,
-      closestTown: closest,
-      walkingText: sample(walkingText),
-      travelPlan
-    }));
-
-  }
-});
-
-app.post('/travel/return-to-source', authEndpoint, async (req: Request, res: Response) => {
-  // puts the player back in their starting town
-  // doesn't matter if they don't have one
-  // redirect them!
-  await clearTravelPlan(req.player.id);
-
-  const fight = await loadMonsterFromFight(req.player.id);
-  if(fight) {
-    // go to the fight screen
-    const location = await getMonsterLocation(fight.ref_id);
-
-    res.send(renderPlayerBar(req.player) + renderFightPreRound(fight, true, location, req.player.city_id));
-  }
-  else {
-    const [city, locations, paths] = await Promise.all([
-      getCityDetails(req.player.city_id),
-      getAllServices(req.player.city_id, req.player.level),
-      getAllPaths(req.player.city_id)
-    ]);
-
-    res.send(renderPlayerBar(req.player) + await renderMap({city, locations, paths}, req.player.city_id));
-
-  }
-
-});
-
-app.post('/travel/:destination_id', authEndpoint, async (req: Request, res: Response) => {
-if(req.player.hp <= 0) {
-    logger.log(`Player didn\'t have enough hp`);
-    res.send(Alert.ErrorAlert('Sorry, you need some HP to start travelling.'));
-    return;
-  }
-
-  const destination = await getCityDetails(parseInt(req.params.destination_id));
-
-  if(!destination) {
-    res.send(Alert.ErrorAlert(`Thats not a valid desination`));
-    return;
-  }
-
-  const travelPlan = await travel(req.player, destination.id);
-
-  res.send(renderTravel({
-    things: [],
-    nextAction: 0,
-    walkingText: '',
-    closestTown: req.player.city_id,
-    travelPlan
-  }));
-});
-
 app.get('/settings', authEndpoint, async (req: Request, res: Response) => {
   let warning = '';
   let html = '';
index 8202630112834c4f3d94bb55b9230bf23ecaa187..efa07674089f722c96f39479fc9e739affc02e01 100644 (file)
@@ -1,3 +1,4 @@
 export { chatRouter } from './chat';
 export { inventoryRouter } from './inventory';
 export { profileRouter } from './profile';
+export { travelRouter } from './travel';
diff --git a/src/server/routes/travel.ts b/src/server/routes/travel.ts
new file mode 100644 (file)
index 0000000..5e84c24
--- /dev/null
@@ -0,0 +1,137 @@
+import { Request, Response, Router } from 'express';
+import { random, sample } from 'lodash';
+import { logger } from '../lib/logger';
+import { authEndpoint } from '../auth';
+import { movePlayer } from '../player';
+import { getRandomMonster, loadMonsterFromFight, getMonsterLocation } from '../monster';
+import { clearTravelPlan, completeTravel, getAllPaths, getAllServices, getCityDetails, getService, getTravelPlan, stepForward, travel, getDungeon } from '../map';
+import * as CONSTANT from '../../shared/constants';
+import * as Alert from '../views/alert';
+import { renderTravel, travelButton } from '../views/travel';
+import { renderMap } from '../views/map';
+import { renderPlayerBar } from '../views/player-bar'
+import { renderFightPreRound } from '../views/fight';
+
+export const travelRouter = Router();
+
+// take a step along your travel plan
+travelRouter.post('/travel/step', authEndpoint, async (req: Request, res: Response) => {
+  const stepTimerKey = `step:${req.player.id}`;
+
+  const travelPlan = await getTravelPlan(req.player.id);
+  if(!travelPlan) {
+    res.send(Alert.ErrorAlert('You don\'t have a travel plan'));
+    return;
+  }
+
+  if(req.rl.cache[stepTimerKey]) {
+    if(req.rl.cache[stepTimerKey] > Date.now()) {
+      res.send(Alert.ErrorAlert('Hmm.. travelling too quickly'));
+      return;
+    }
+  }
+
+  travelPlan.current_position++;
+
+  if(travelPlan.current_position >= travelPlan.total_distance) {
+    const travel = await completeTravel(req.player.id);
+
+    req.player.city_id = travel.destination_id;
+    await movePlayer(travel.destination_id, req.player.id);
+
+    const [city, locations, paths] = await Promise.all([
+      getCityDetails(travel.destination_id),
+      getAllServices(travel.destination_id, req.player.level),
+      getAllPaths(travel.destination_id)
+    ]);
+
+    delete req.rl.cache[stepTimerKey];
+    res.send(await renderMap({city, locations, paths}, req.player.city_id));
+  }
+  else {
+    const walkingText: string[] = [
+      'You take a step forward',
+      'You keep moving'
+    ];
+    // update existing plan..
+    // decide if they will run into anything
+    const travelPlan = await stepForward(req.player.id);
+
+    const closest: number = (travelPlan.current_position / travelPlan.total_distance) > 0.5 ? travelPlan.destination_id : travelPlan.source_id;
+
+    const chanceToSeeMonster = random(0, 100);
+    const things: any[] = [];
+    if(chanceToSeeMonster <= 30) {
+      const monster = await getRandomMonster([closest]);
+      things.push(monster);
+    }
+
+    // STEP_DELAY
+    const nextAction = Date.now() + CONSTANT.STEP_DELAY;
+
+    req.rl.cache[stepTimerKey] = nextAction;
+
+    res.send(renderTravel({
+      things,
+      nextAction,
+      closestTown: closest,
+      walkingText: sample(walkingText),
+      travelPlan
+    }));
+
+  }
+});
+
+travelRouter.post('/travel/return-to-source', authEndpoint, async (req: Request, res: Response) => {
+  // puts the player back in their starting town
+  // doesn't matter if they don't have one
+  // redirect them!
+  await clearTravelPlan(req.player.id);
+
+  const fight = await loadMonsterFromFight(req.player.id);
+  if(fight) {
+    // go to the fight screen
+    const location = await getMonsterLocation(fight.ref_id);
+
+    res.send(renderPlayerBar(req.player) + renderFightPreRound(fight, true, location, req.player.city_id));
+  }
+  else {
+    const [city, locations, paths] = await Promise.all([
+      getCityDetails(req.player.city_id),
+      getAllServices(req.player.city_id, req.player.level),
+      getAllPaths(req.player.city_id)
+    ]);
+
+    res.send(renderPlayerBar(req.player) + await renderMap({city, locations, paths}, req.player.city_id));
+
+  }
+
+});
+
+// start a new travel plan between current and source destination
+travelRouter.post('/travel/:destination_id', authEndpoint, async (req: Request, res: Response) => {
+  if(req.player.hp <= 0) {
+    logger.log(`Player didn\'t have enough hp`);
+    res.send(Alert.ErrorAlert('Sorry, you need some HP to start travelling.'));
+    return;
+  }
+
+  const destination = await getCityDetails(parseInt(req.params.destination_id));
+
+  if(!destination) {
+    res.send(Alert.ErrorAlert(`Thats not a valid desination`));
+    return;
+  }
+
+  const travelPlan = await travel(req.player, destination.id);
+
+  res.send(renderTravel({
+    things: [],
+    nextAction: 0,
+    walkingText: '',
+    closestTown: req.player.city_id,
+    travelPlan
+  }));
+});
+
+