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