From: xangelo Date: Tue, 5 Sep 2023 14:58:59 +0000 (-0400) Subject: chore: move healer handler X-Git-Tag: v0.3.3~7 X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=e6eeb759bd23814a529fbcd98c7b6ad7b21fb91f;p=risinglegends.git chore: move healer handler --- diff --git a/src/server/locations/healer.ts b/src/server/locations/healer.ts new file mode 100644 index 0000000..2d12db0 --- /dev/null +++ b/src/server/locations/healer.ts @@ -0,0 +1,173 @@ +import { Response, Router } from "express"; +import { maxHp, maxVigor } from "../../shared/player"; +import { authEndpoint, AuthRequest } from '../auth'; +import { logger } from "../lib/logger"; +import { updatePlayer } from "../player"; +import { getCityDetails, getService } from '../map'; +import { sample } from 'lodash'; +import { City, Location } from "../../shared/map"; +import { renderPlayerBar } from "../views/player-bar"; +import { BackToTown } from "../views/components/button"; + +export const router = Router(); + +type TextSegment = 'intro' | 'insufficient_money' | 'heal_successful'; + +type HealText = Record; + +const healCost = 10; + +const defaultTexts: HealText = { + intro: [ + `Welcome traveller, I am {{NAME}}, Healer of {{CITY_NAME}}`, + "Please come in traveller, I am {{NAME}}, Healer of {{CITY_NAME}}", + ], + insufficient_money: [ + "Sorry friend, you don't have enough money..", + "Sorry, that won't be enough..", + "Healing is hard work.. I'm afraid that won't cover it.." + ], + heal_successful: [ + "I hope you feel better now", + "Good luck on your travels!", + "Glad to be of service..." + ] +}; + +// overrides for specific areas +const playerTexts: Record = { + [8]: { + intro: [ + 'Welcome to Midfield traveller, I am Casim - healer in these parts', + 'I am Casim the Healer here... how are you enjoying your stay at Midfield?' + ], + insufficient_money: [ + 'Sorry friend, you don\'t have enough money', + 'Look.. I\'m sorry.. that won\'t be enough...' + ], + heal_successful: [ + 'Glad to help!' + ] + }, + [16]: { + intro: [ + 'Ah, welcome to Wildegard, one of the few safehavens in the Akari Woods. I am Adovras, healer in these parts.', + 'Welcome traveller, I am Adovras - healer in these parts' + ], + insufficient_money: [ + `Sorry friend, you don't have enough money...` + ], + heal_successful: [ + "Hope this small healing will be helpful on your journeys" + ] + + }, + [11]: { + intro: [ + 'Ah, welcome traveler - I am Uthar, healer of Davelfell', + 'Hello, I am Uthar, healer of Davelfell', + 'Sorry I\'m a bit busy today, I am Uthar, healer of Davelfell' + ], + insufficient_money: [ + "Bah, don't bother me if you don't have the money", + "Look, I'm very busy - come back when you have the money" + ], + heal_successful: [ + "*Fizz* *POOF* YOU'RE HEALED!" + ] + } +} + +function getText(type: TextSegment, location: Location, city: City): string { + let selected = sample(defaultTexts[type]); + + if(playerTexts[location.id]) { + if(playerTexts[location.id][type].length) { + selected = sample(playerTexts[location.id][type]); + } + } + + return selected.replace("{{NAME}}", location.name).replace("{{CITY_NAME}}", city.name); + +} + +router.get('/city/services/healer/:location_id', authEndpoint, async (req: AuthRequest, res: Response) => { + const service = await getService(parseInt(req.params.location_id)); + const city = await getCityDetails(service.city_id); + + if(!service || service.city_id !== req.player.city_id) { + logger.log(`Invalid location: [${req.params.location_id}]`); + res.sendStatus(400); + } + + const text: string[] = []; + + text.push(`

"${getText('intro', service, city)}"

`); + + if(req.player.hp === maxHp(req.player.constitution, req.player.level) && req.player.vigor === maxVigor(req.player.constitution, req.player.level)) { + text.push(`

You're already in peak condition!

`); + } + else { + if(req.player.gold <= (healCost * 2)) { + text.push(`

You don't seem to have too much money... I guess I can do it for free this time...

`); + text.push(`

`); + } + else { + text.push(`

`); + } + + } + + res.send(` +
${service.city_name}
+
+

${service.name}

+
+${text.join("\n")} +${BackToTown()} +
+
+ `); + + //res.send(`
${text.join("\n")}
`); +}); + + + +router.post('/city/services/healer/heal/:location_id', authEndpoint, async (req: AuthRequest, res: Response) => { + const service = await getService(parseInt(req.params.location_id)); + const city = await getCityDetails(service.city_id); + + if(!service || service.city_id !== req.player.city_id) { + logger.log(`Invalid location: [${req.params.location_id}]`); + res.sendStatus(400); + } + + const text: string[] = []; + const cost = req.player.gold <= (healCost * 2) ? 0 : healCost; + + if(req.player.gold < cost) { + text.push(`

${getText('insufficient_money', service, city)}

`) + } + else { + req.player.hp = maxHp(req.player.constitution, req.player.level); + req.player.vigor = maxVigor(req.player.constitution, req.player.level); + req.player.gold -= cost; + + await updatePlayer(req.player); + + text.push(`

${getText('heal_successful', service, city)}

`); + } + + res.send(` +
${service.city_name}
+
+

${service.name}

+
+${text.join("\n")} +${BackToTown()} +
+
+${renderPlayerBar(req.player)} +`); +}); diff --git a/src/server/locations/healer/index.ts b/src/server/locations/healer/index.ts deleted file mode 100644 index 83551b2..0000000 --- a/src/server/locations/healer/index.ts +++ /dev/null @@ -1,173 +0,0 @@ -import { Response, Router } from "express"; -import { maxHp, maxVigor } from "../../../shared/player"; -import { authEndpoint, AuthRequest } from '../../auth'; -import { logger } from "../../lib/logger"; -import { updatePlayer } from "../../player"; -import { getCityDetails, getService } from '../../map'; -import { sample } from 'lodash'; -import { City, Location } from "../../../shared/map"; -import { renderPlayerBar } from "../../views/player-bar"; -import { BackToTown } from "../../views/components/button"; - -export const router = Router(); - -type TextSegment = 'intro' | 'insufficient_money' | 'heal_successful'; - -type HealText = Record; - -const healCost = 10; - -const defaultTexts: HealText = { - intro: [ - `Welcome traveller, I am {{NAME}}, Healer of {{CITY_NAME}}`, - "Please come in traveller, I am {{NAME}}, Healer of {{CITY_NAME}}", - ], - insufficient_money: [ - "Sorry friend, you don't have enough money..", - "Sorry, that won't be enough..", - "Healing is hard work.. I'm afraid that won't cover it.." - ], - heal_successful: [ - "I hope you feel better now", - "Good luck on your travels!", - "Glad to be of service..." - ] -}; - -// overrides for specific areas -const playerTexts: Record = { - [8]: { - intro: [ - 'Welcome to Midfield traveller, I am Casim - healer in these parts', - 'I am Casim the Healer here... how are you enjoying your stay at Midfield?' - ], - insufficient_money: [ - 'Sorry friend, you don\'t have enough money', - 'Look.. I\'m sorry.. that won\'t be enough...' - ], - heal_successful: [ - 'Glad to help!' - ] - }, - [16]: { - intro: [ - 'Ah, welcome to Wildegard, one of the few safehavens in the Akari Woods. I am Adovras, healer in these parts.', - 'Welcome traveller, I am Adovras - healer in these parts' - ], - insufficient_money: [ - `Sorry friend, you don't have enough money...` - ], - heal_successful: [ - "Hope this small healing will be helpful on your journeys" - ] - - }, - [11]: { - intro: [ - 'Ah, welcome traveler - I am Uthar, healer of Davelfell', - 'Hello, I am Uthar, healer of Davelfell', - 'Sorry I\'m a bit busy today, I am Uthar, healer of Davelfell' - ], - insufficient_money: [ - "Bah, don't bother me if you don't have the money", - "Look, I'm very busy - come back when you have the money" - ], - heal_successful: [ - "*Fizz* *POOF* YOU'RE HEALED!" - ] - } -} - -function getText(type: TextSegment, location: Location, city: City): string { - let selected = sample(defaultTexts[type]); - - if(playerTexts[location.id]) { - if(playerTexts[location.id][type].length) { - selected = sample(playerTexts[location.id][type]); - } - } - - return selected.replace("{{NAME}}", location.name).replace("{{CITY_NAME}}", city.name); - -} - -router.get('/city/services/healer/:location_id', authEndpoint, async (req: AuthRequest, res: Response) => { - const service = await getService(parseInt(req.params.location_id)); - const city = await getCityDetails(service.city_id); - - if(!service || service.city_id !== req.player.city_id) { - logger.log(`Invalid location: [${req.params.location_id}]`); - res.sendStatus(400); - } - - const text: string[] = []; - - text.push(`

"${getText('intro', service, city)}"

`); - - if(req.player.hp === maxHp(req.player.constitution, req.player.level) && req.player.vigor === maxVigor(req.player.constitution, req.player.level)) { - text.push(`

You're already in peak condition!

`); - } - else { - if(req.player.gold <= (healCost * 2)) { - text.push(`

You don't seem to have too much money... I guess I can do it for free this time...

`); - text.push(`

`); - } - else { - text.push(`

`); - } - - } - - res.send(` -
${service.city_name}
-
-

${service.name}

-
-${text.join("\n")} -${BackToTown()} -
-
- `); - - //res.send(`
${text.join("\n")}
`); -}); - - - -router.post('/city/services/healer/heal/:location_id', authEndpoint, async (req: AuthRequest, res: Response) => { - const service = await getService(parseInt(req.params.location_id)); - const city = await getCityDetails(service.city_id); - - if(!service || service.city_id !== req.player.city_id) { - logger.log(`Invalid location: [${req.params.location_id}]`); - res.sendStatus(400); - } - - const text: string[] = []; - const cost = req.player.gold <= (healCost * 2) ? 0 : healCost; - - if(req.player.gold < cost) { - text.push(`

${getText('insufficient_money', service, city)}

`) - } - else { - req.player.hp = maxHp(req.player.constitution, req.player.level); - req.player.vigor = maxVigor(req.player.constitution, req.player.level); - req.player.gold -= cost; - - await updatePlayer(req.player); - - text.push(`

${getText('heal_successful', service, city)}

`); - } - - res.send(` -
${service.city_name}
-
-

${service.name}

-
-${text.join("\n")} -${BackToTown()} -
-
-${renderPlayerBar(req.player)} -`); -});