fix: migrate recruiter to htmx
authorxangelo <me@xangelo.ca>
Mon, 21 Aug 2023 17:24:23 +0000 (13:24 -0400)
committerxangelo <me@xangelo.ca>
Mon, 21 Aug 2023 17:24:23 +0000 (13:24 -0400)
src/server/api.ts
src/server/locations/healer/index.ts
src/server/locations/recruiter.ts [new file with mode: 0644]

index 8a7eec80dc29f03b1363e194597a55b4839a6abe..baed723e2c7e50348d50aa18d36077929b718dfe 100644 (file)
@@ -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
     </div>
   </div>
   <div class="actions">
-    <button hx-put="/location/${equipment.location_id}/equipment/${equipment.id}" formmethod="dialog" value="cancel">Buy</button>
+    <button hx-put="/location/${equipment.location_id}/equipment/${equipment.id}" formmethod="dialog" value="cancel" class="green">Buy</button>
     <button class="close-modal" formmethod="dialog" value="cancel">Cancel</button>
   </div>
 </dialog>
index 3504d2aab194e81869050f202ca2a1f2f26389c2..3e658fe5c11020442b82f2fe6c084e5d5d01f7d9 100644 (file)
@@ -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(`<p>You don't seem to have too much money... I guess I can do it for free this time...</p>`);
-      text.push(`<p><button type="button" hx-post="/city/services/city:services:healer:heal/${service.id}" hx-target="#explore">Heal for Free!</button></p>`);
+      text.push(`<p><button type="button" hx-post="/city/services/healer/heal/${service.id}" hx-target="#explore">Heal for Free!</button></p>`);
     }
     else {
-      text.push(`<p><button type="button" hx-post="/city/services/city:services:healer:heal/${service.id}" hx-target="#explore">Heal for ${healCost}g!</button></p>`);
+      text.push(`<p><button type="button" hx-post="/city/services/healer/heal/${service.id}" hx-target="#explore">Heal for ${healCost}g!</button></p>`);
     }
 
   }
@@ -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 (file)
index 0000000..10623d8
--- /dev/null
@@ -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 `<p>${str}</p>`;
+}
+
+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(`<p>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!</p>`);
+    html.push(`<p>You have 3 choices laid before you.</p>`);
+    html.push(`<p>You could become a great and mighty <b>Warrior</b>! Wielding powerful swords and maces.</p>`);
+    html.push(`<p>You could become a powerful <b>Mage</b>! Casting spells to rain fire upon our enemies.</p>`);
+    html.push(`<p>You could become a lithe <b>Rogue</b>! Attacking our enemies swiftly when they least expect!</p>`);
+
+    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(`<b>Be Careful!</b> Once you change your profession, you'll never be a Wanderer again...`));
+      html.push(`
+          <div>
+          <form hx-post="/city/services/profession_change/${service.id}">
+          <button type="submit" value="warrior" name="profession">Become a Warrior</button>
+          <button type="submit" value="mage" name="profession">Become a Mage</button>
+          <button type="submit" value="rogue" name="profession">Become a Rogue</button>
+          </form>
+          </div>
+        `);
+    }
+  }
+  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 <b>${req.player.profession}</b>!`));
+    html.push(`<p>Unfortunately I won't be of much help to you now that you are no longer a wanderer...</p>`);
+    html.push(`<p>However, you should visit the ${place} in ${town} that can probably provide some guidance!</p>`);
+  }
+
+  res.send(`
+    <div class="city-title-wrapper"><div class="city-title">${service.city_name}</div></div>
+    <div class="city-details">
+      <h3 class="location-name"><span>${service.name}</span></h3>
+      <div class="service-in-town" id="recruiter-target">${html.join("\n")}</div>
+    </div>
+  `);
+});
+
+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) + `<div id="recruiter-target" class="service-in-town" hx-swap-oob="true">Congrats! You are now a ${req.player.profession}</div>`);
+  }
+
+});