chore(release): 0.2.1
[risinglegends.git] / src / events / travel / server.ts
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";
6
7 const MONSTER_ENCOUNTER_CHANCE = 30;
8
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', {
14         type: 'error',
15         text: 'Sorry, you need some HP to start your travel'
16       });
17       return;
18     }
19
20     const destinationCity = parseInt(data.args);
21
22
23     if(!destinationCity || isNaN(destinationCity)) {
24       console.log(`Invalid destination city [${destinationCity}]`);
25       return;
26     }
27
28     try {
29       const city = await getCityDetails(destinationCity);
30
31       if(!city) {
32         console.log(`Invalid destination city [${destinationCity}]`);
33         return;
34       }
35
36       console.log(`${api.player.username} attempting travel to ${city.name}`);
37       const travelPlan = await travel(api.player, city.id);
38
39       api.socket.emit('city:travel', {
40         things: [],
41         closestTown: api.player.city_id,
42         walkingText: ''
43       });
44     }
45     catch(e) {
46       console.log(e);
47     }
48   }
49 }
50
51 const walkingText: string[] = [
52   'You take a step forward',
53   'You keep moving'
54 ];
55
56 export const nextStep: SocketEvent = {
57   eventName: 'travel:step',
58   handler: async (api, data: { args: string }) => {
59     const travelPlan = await getTravelPlan(api.player.id);
60
61     if(!travelPlan) {
62       return;
63     }
64
65     travelPlan.current_position++;
66     if(travelPlan.current_position >= travelPlan.total_distance) {
67       const travel = await completeTravel(api.player.id);
68
69       api.player.city_id = travel.destination_id;
70       await movePlayer(travel.destination_id, api.player.id);
71
72       const [city, locations, paths] = await Promise.all([
73         getCityDetails(travel.destination_id),
74         getAllServices(travel.destination_id),
75         getAllPaths(travel.destination_id)
76       ]);
77
78       api.socket.emit('city:display', {
79         city,
80         locations,
81         paths
82       });
83     }
84     else {
85       // update existing plan..
86       // decide if they will run into anything
87       const travelPlan = await stepForward(api.player.id);
88
89       const closest: number = (travelPlan.current_position / travelPlan.total_distance) > 0.5 ? travelPlan.destination_id : travelPlan.source_id;
90
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]);
96         things.push(monster);
97       }
98
99       // send back NOTHING!
100       api.socket.emit('city:travel', {
101         things,
102         closestTown: closest,
103         walkingText: sample(walkingText)
104       });
105     }
106   }
107 };