chore(release): 0.0.2
[risinglegends.git] / src / events / healer / server.ts
1 import {SocketEvent} from "../../server/socket-event.server";
2 import { getCityDetails, getService } from "../../server/map";
3 import { City, Location } from "../../shared/map";
4 import {maxHp, Player} from '../../shared/player';
5 import { updatePlayer } from "../../server/player";
6 import { sample } from 'lodash';
7
8 type TextSegment = 'intro' | 'insufficient_money' | 'heal_successful';
9
10 type HealText = Record<TextSegment, string[]>;
11
12 const healCost = 10;
13
14 const defaultTexts: HealText = {
15   intro: [
16     `Welcome traveller, I am {{NAME}}, Healer of {{CITY_NAME}}`,
17     "Please come in traveller, I am {{NAME}}, Healer of {{CITY_NAME}}",
18   ],
19   insufficient_money: [
20     "Sorry friend, you don't have enough money..",
21     "Sorry, that won't be enough..",
22     "Healing is hard work.. I'm afraid that won't cover it.."
23   ],
24   heal_successful: [
25     "I hope you feel better now",
26     "Good luck on your travels!",
27     "Glad to be of service..."
28   ]
29 }
30
31 // overrides for specific areas
32 const playerTexts: Record<number, HealText> = {
33   [8]: {
34     intro: [
35       'Welcome to Midfield traveller, I am Casim - healer in these parts',
36       'I am Casim the Healer here... how are you enjoying your stay at Midfield?'
37     ],
38     insufficient_money: [
39       'Sorry friend, you don\'t have enough money',
40       'Look.. I\'m sorry.. that won\'t be enough...'
41     ],
42     heal_successful: [
43       'Glad to help!'
44     ]
45   },
46   [16]: {
47     intro: [
48       'Ah, welcome to Wildegard, one of the few safehavens in the Akari Woods. I am Adovras, healer in these parts.',
49       'Welcome traveller, I am Adovras - healer in these parts'
50     ],
51     insufficient_money: [
52       `Sorry friend, you don't have enough money...`
53     ],
54     heal_successful: [
55       "Hope this small healing will be helpful on your journeys"
56     ]
57
58   },
59   [11]: {
60     intro: [
61       'Ah, welcome traveler - I am Uthar, healer of Davelfell',
62       'Hello, I am Uthar, healer of Davelfell',
63       'Sorry I\'m a bit busy today, I am Uthar, healer of Davelfell'
64     ],
65     insufficient_money: [
66       "Bah, don't bother me if you don't have the money",
67       "Look, I'm very busy - come back when you have the money"
68     ],
69     heal_successful: [
70       "*Fizz* *POOF* YOU'RE HEALED!"
71     ]
72   }
73 }
74
75 function getText(type: TextSegment, location: Location, city: City): string {
76   let selected = sample(defaultTexts[type]);
77
78   if(playerTexts[location.id]) {
79     if(playerTexts[location.id][type].length) {
80       selected = sample(playerTexts[location.id][type]);
81     }
82   }
83
84   return selected.replace("{{NAME}}", location.name).replace("{{CITY_NAME}}", city.name);
85
86 }
87
88 export const healer: SocketEvent = {
89   eventName: 'city:services:healer',
90   handler: async (api, data: { args: number }) => {
91     const text: string[] = [];
92     const healerId = data.args;
93     const service = await getService(healerId);
94
95     if(service.city_id !== api.player.city_id) {
96       api.socket.emit('alert', {
97         type: 'error',
98         text: `You don't seem to be in the right city...`
99       });
100       return;
101     }
102
103     const city = await getCityDetails(service.city_id);
104
105     text.push(`<p><b>${service.name}</b></p>`);
106     text.push(`<p>"${getText('intro', service, city)}"</p>`);
107
108
109     if(api.player.hp === maxHp(api.player.constitution, api.player.level)) {
110       text.push(`<p>You're already at full health?</p>`);
111     }
112     else {
113       if(api.player.gold <= (healCost * 2)) {
114         text.push(`<p>You don't seem to have too much money... I guess I can do it for free this time...</p>`);
115         text.push(`<p><button type="button" class="city-emit-event" data-event="city:services:healer:heal" data-args="${service.id}">Heal for free!</button></p>`);
116       }
117       else {
118         text.push(`<p><button type="button" class="city-emit-event" data-event="city:services:healer:heal" data-args="${service.id}">Heal for ${healCost}g!</button></p>`);
119       }
120
121     }
122
123     api.socket.emit('city:service:healer', {
124       text: text.join("\n")
125     });
126   }
127 }
128
129 export const heal: SocketEvent = {
130   eventName: 'city:services:healer:heal',
131   handler: async (api, data: { args: number }) => {
132     const text: string[] = [];
133     const healerId = data.args;
134     const service = await getService(healerId);
135
136     if(service.city_id !== api.player.city_id) {
137       api.socket.emit('alert', {
138         type: 'error',
139         text: `You don't seem to be in the right city...`
140       });
141       return;
142     }
143
144     const city = await getCityDetails(service.city_id);
145
146     text.push(`<p><b>${service.name}</b></p>`);
147
148     const cost = api.player.gold <= (healCost * 2) ? 0 : healCost;
149
150     if(api.player.gold < cost) {
151       text.push(`<p>${getText('insufficient_money', service, city)}</p>`)
152       api.socket.emit('city:service:healer', {
153         text: text.join("\n")
154       });
155       return;
156     }
157
158     api.player.hp = maxHp(api.player.constitution, api.player.level);
159     api.player.gold -= cost;
160
161     await updatePlayer(api.player);
162     api.socket.emit('updatePlayer', api.player);
163
164
165     text.push(`<p>${getText('heal_successful', service, city)}</p>`);
166     api.socket.emit('city:service:healer', {
167       text: text.join("\n")
168     });
169   }
170 }