1 import {movePlayer, updatePlayer} from "../../server/player";
2 import {getCityDetails, getAllServices, getAllPaths, travel, getTravelPlan, stepForward, completeTravel} from "../../server/map";
3 import {SocketEvent} from "../../server/socket-event.server";
4 import { sample, random } from 'lodash';
5 import { createFight, getRandomMonster, loadMonster } from "../../server/monster";
7 const MONSTER_ENCOUNTER_CHANCE = 30;
9 export const explore: SocketEvent = {
10 eventName: 'city:travel',
11 handler: async (api, data: { args: string }) => {
12 if(api.player.hp <= 0) {
13 api.socket.emit('alert', {
15 text: 'Sorry, you need some HP to start your travel'
20 const destinationCity = parseInt(data.args);
23 if(!destinationCity || isNaN(destinationCity)) {
24 console.log(`Invalid destination city [${destinationCity}]`);
29 const city = await getCityDetails(destinationCity);
32 console.log(`Invalid destination city [${destinationCity}]`);
36 console.log(`${api.player.username} attempting travel to ${city.name}`);
37 const travelPlan = await travel(api.player, city.id);
39 api.socket.emit('city:travel', {
41 closestTown: api.player.city_id,
51 const walkingText: string[] = [
52 'You take a step forward',
56 export const nextStep: SocketEvent = {
57 eventName: 'travel:step',
58 handler: async (api, data: { args: string }) => {
59 const travelPlan = await getTravelPlan(api.player.id);
65 travelPlan.current_position++;
66 if(travelPlan.current_position >= travelPlan.total_distance) {
67 const travel = await completeTravel(api.player.id);
69 api.player.city_id = travel.destination_id;
70 await movePlayer(travel.destination_id, api.player.id);
72 const [city, locations, paths] = await Promise.all([
73 getCityDetails(travel.destination_id),
74 getAllServices(travel.destination_id),
75 getAllPaths(travel.destination_id)
78 api.socket.emit('city:display', {
85 // update existing plan..
86 // decide if they will run into anything
87 const travelPlan = await stepForward(api.player.id);
89 const closest: number = (travelPlan.current_position / travelPlan.total_distance) > 0.5 ? travelPlan.destination_id : travelPlan.source_id;
91 // @TODO send back monsters
92 const chanceToSeeMonster = random(0, 100);
93 const things: any[] = [];
94 if(chanceToSeeMonster < MONSTER_ENCOUNTER_CHANCE) {
95 const monster = await getRandomMonster([closest]);
100 api.socket.emit('city:travel', {
102 closestTown: closest,
103 walkingText: sample(walkingText)