chore(release): 0.1.1
[risinglegends.git] / src / events / travel / server.ts
1 import {movePlayer, updatePlayer} from "../../server/player";
2 import {getCityDetails, getAllServices, getAllPaths} from "../../server/map";
3 import {SocketEvent} from "../../server/socket-event.server";
4
5 export const travel: SocketEvent = {
6   eventName: 'city:travel',
7   handler: async (api, data: { args: string }) => {
8     const destinationCity = parseInt(data.args);
9
10     console.log(`${api.player.username} attempting travel to ${destinationCity}`);
11
12     if(!destinationCity || isNaN(destinationCity)) {
13       console.log('Bad input!');
14       return;
15     }
16
17     try {
18       const city = await getCityDetails(destinationCity);
19
20       if(!city) {
21         console.log('Bad city input');
22         // do nothing.. not a real place
23         return;
24       }
25
26       api.player.city_id = city.id;
27
28       await movePlayer(city.id, api.player.id);
29
30       const [locations, paths] = await Promise.all([
31         getAllServices(city.id),
32         getAllPaths(city.id)
33       ]);
34
35       api.socket.emit('city:travel', {
36         city,
37         locations,
38         paths
39       });
40     }
41     catch(e) {
42       console.log(e);
43     }
44   }
45 }