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';
8 type TextSegment = 'intro' | 'insufficient_money' | 'heal_successful';
10 type HealText = Record<TextSegment, string[]>;
14 const defaultTexts: HealText = {
16 `Welcome traveller, I am {{NAME}}, Healer of {{CITY_NAME}}`,
17 "Please come in traveller, I am {{NAME}}, Healer of {{CITY_NAME}}",
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.."
25 "I hope you feel better now",
26 "Good luck on your travels!",
27 "Glad to be of service..."
31 // overrides for specific areas
32 const playerTexts: Record<number, HealText> = {
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?'
39 'Sorry friend, you don\'t have enough money',
40 'Look.. I\'m sorry.. that won\'t be enough...'
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'
52 `Sorry friend, you don't have enough money...`
55 "Hope this small healing will be helpful on your journeys"
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'
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"
70 "*Fizz* *POOF* YOU'RE HEALED!"
75 function getText(type: TextSegment, location: Location, city: City): string {
76 let selected = sample(defaultTexts[type]);
78 if(playerTexts[location.id]) {
79 if(playerTexts[location.id][type].length) {
80 selected = sample(playerTexts[location.id][type]);
84 return selected.replace("{{NAME}}", location.name).replace("{{CITY_NAME}}", city.name);
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);
95 if(service.city_id !== api.player.city_id) {
96 api.socket.emit('alert', {
98 text: `You don't seem to be in the right city...`
103 const city = await getCityDetails(service.city_id);
105 text.push(`<p><b>${service.name}</b></p>`);
106 text.push(`<p>"${getText('intro', service, city)}"</p>`);
109 if(api.player.hp === maxHp(api.player.constitution, api.player.level)) {
110 text.push(`<p>You're already at full health?</p>`);
113 if(api.player.level <= 3) {
114 text.push(`<p>Since you still seem to be new around here, this one's on the house!</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>`);
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>`);
123 api.socket.emit('city:service:healer', {
124 text: text.join("\n")
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);
136 if(service.city_id !== api.player.city_id) {
137 api.socket.emit('alert', {
139 text: `You don't seem to be in the right city...`
144 const city = await getCityDetails(service.city_id);
146 text.push(`<p><b>${service.name}</b></p>`);
148 const cost = api.player.level <= 3 ? 0 : healCost;
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")
158 api.player.hp = maxHp(api.player.constitution, api.player.level);
159 api.player.gold -= cost;
161 await updatePlayer(api.player);
162 api.socket.emit('updatePlayer', api.player);
165 text.push(`<p>${getText('heal_successful', service, city)}</p>`);
166 api.socket.emit('city:service:healer', {
167 text: text.join("\n")