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