From 8f42a668e8bc79952a4605566fa4c1c47fee3661 Mon Sep 17 00:00:00 2001 From: xangelo Date: Mon, 21 Aug 2023 13:24:23 -0400 Subject: [PATCH] fix: migrate recruiter to htmx --- src/server/api.ts | 4 +- src/server/locations/healer/index.ts | 8 +- src/server/locations/recruiter.ts | 117 +++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 src/server/locations/recruiter.ts diff --git a/src/server/api.ts b/src/server/api.ts index 8a7eec8..baed723 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -27,6 +27,7 @@ import { getPlayerSkills, getPlayerSkillsAsObject, updatePlayerSkills } from './ import {SkillID, Skills} from '../shared/skills'; import { router as healerRouter } from './locations/healer'; +import { router as professionRouter } from './locations/recruiter'; import * as Alert from './views/alert'; import { renderPlayerBar } from './views/player-bar' @@ -374,6 +375,7 @@ async function fightRound(player: Player, monster: MonsterWithFaction, data: {a }; app.use(healerRouter); +app.use(professionRouter); app.get('/chat/history', authEndpoint, async (req: AuthRequest, res: Response) => { @@ -671,7 +673,7 @@ app.get('/location/:location_id/equipment/:item_id/overview', authEndpoint, asyn
- +
diff --git a/src/server/locations/healer/index.ts b/src/server/locations/healer/index.ts index 3504d2a..3e658fe 100644 --- a/src/server/locations/healer/index.ts +++ b/src/server/locations/healer/index.ts @@ -92,7 +92,7 @@ function getText(type: TextSegment, location: Location, city: City): string { } -router.get('/city/services/city:services:healer/:location_id', authEndpoint, async (req: AuthRequest, res: Response) => { +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); @@ -111,10 +111,10 @@ router.get('/city/services/city:services:healer/:location_id', authEndpoint, asy 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(`

`); + text.push(`

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

`); + text.push(`

`); } } @@ -134,7 +134,7 @@ ${text.join("\n")} -router.post('/city/services/city:services:healer:heal/:location_id', authEndpoint, async (req: AuthRequest, res: Response) => { +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); diff --git a/src/server/locations/recruiter.ts b/src/server/locations/recruiter.ts new file mode 100644 index 0000000..10623d8 --- /dev/null +++ b/src/server/locations/recruiter.ts @@ -0,0 +1,117 @@ +import { Response, Router } from "express"; +import { getService } from "../map"; +import { authEndpoint, AuthRequest } from '../auth'; +import { logger } from "../lib/logger"; +import * as Alert from "../views/alert"; +import { changeProfession } from "../player"; +import { renderPlayerBar } from "../views/player-bar"; +import { getEquippedItems } from "../inventory"; + +function p(str: string) { + return `

${str}

`; +} + +export const router = Router(); + +const MIN_LEVEL = 25; + +router.get('/city/services/profession_recruitor/:location_id', authEndpoint, async(req: AuthRequest, res: Response) => { + const service = await getService(parseInt(req.params.location_id)); + + if(!service || service.city_id !== req.player.city_id) { + logger.log(`Invalid location: [${req.params.location_id}]`); + res.sendStatus(400); + } + + let html: string[] = []; + if(req.player.profession === 'Wanderer') { + html.push(`

Our duty is to help Wanderers such as yourself become more than they are. By helping you achieve new levels in service of the King, we can ensure that the Kingdom of Khatis continues to grow!

`); + html.push(`

You have 3 choices laid before you.

`); + html.push(`

You could become a great and mighty Warrior! Wielding powerful swords and maces.

`); + html.push(`

You could become a powerful Mage! Casting spells to rain fire upon our enemies.

`); + html.push(`

You could become a lithe Rogue! Attacking our enemies swiftly when they least expect!

`); + + if(req.player.level < MIN_LEVEL) { + html.push(p(`Unfortunately you have to be at least level ${MIN_LEVEL} to take part in our training...`)); + } + else { + html.push(p(`Be Careful! Once you change your profession, you'll never be a Wanderer again...`)); + html.push(` +
+
+ + + +
+
+ `); + } + } + else { + let town = 'UNSET'; + let place = 'UNSETPLACE'; + switch(req.player.profession) { + case 'Warrior': + town = 'Stether'; + place = 'Highbreaker Inn' + break; + case 'Mage': + town = 'Davelfell'; + place = 'Mages Tower'; + break; + case 'Rogue': + town = 'Ferbalt Gap'; + place = 'Keepers Tavern'; + break; + } + + html.push(p(`Welcome ${req.player.profession}!`)); + html.push(`

Unfortunately I won't be of much help to you now that you are no longer a wanderer...

`); + html.push(`

However, you should visit the ${place} in ${town} that can probably provide some guidance!

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

${service.name}

+
${html.join("\n")}
+
+ `); +}); + +router.post('/city/services/profession_change/:location_id', authEndpoint, async(req: AuthRequest, res: Response) => { + const service = await getService(parseInt(req.params.location_id)); + + if(!service || service.city_id !== req.player.city_id) { + logger.log(`Invalid location: [${req.params.location_id}]`); + res.sendStatus(400); + } + + let update: {level: number, exp: number}; + + switch(req.body.profession.toLowerCase()) { + case 'warrior': + update = await changeProfession(req.player.id, 'Warrior'); + req.player.profession = 'Warrior'; + break; + case 'mage': + update = await changeProfession(req.player.id, 'Mage'); + req.player.profession = 'Mage'; + break; + case 'rogue': + update = await changeProfession(req.player.id, 'Rogue'); + req.player.profession = 'Rogue'; + break; + default: + res.send(Alert.ErrorAlert(`Invalid profession`)); + break; + } + + if(update) { + const equipped = await getEquippedItems(req.player.id); + req.player.level = update.level; + req.player.exp = update.exp; + res.send(renderPlayerBar(req.player, equipped) + `
Congrats! You are now a ${req.player.profession}
`); + } + +}); -- 2.25.1