fix: migrate stat increase to htmx
authorxangelo <me@xangelo.ca>
Wed, 9 Aug 2023 14:55:02 +0000 (10:55 -0400)
committerxangelo <me@xangelo.ca>
Wed, 9 Aug 2023 14:55:02 +0000 (10:55 -0400)
src/server/api.ts
src/server/views/profile.ts

index 815c11bb9e2ce6f5b5eb5fabb5d0cb7277d01f90..b99640132ac5a89d3482a33b14c75edf994a8953 100644 (file)
@@ -419,7 +419,28 @@ app.post('/chat', authEndpoint, async (req: AuthRequest, res: Response) => {
 app.get('/player', authEndpoint, async (req: AuthRequest, res: Response) => {
   const inventory = await getEquippedItems(req.player.id);
 
-  res.send(renderPlayerBar(req.player, inventory) + (await renderProfilePage(req.player)));
+  res.send(renderPlayerBar(req.player, inventory) + renderProfilePage(req.player));
+});
+
+app.post('/player/stat/:stat', authEndpoint, async (req: AuthRequest, res: Response) => {
+  const stat = req.params.stat;
+  if(!['strength', 'constitution', 'dexterity', 'intelligence'].includes(stat)) {
+    res.send(Alert.ErrorAlert(`Sorry, that's not a valid stat to increase`));
+    return;
+  }
+
+  if(req.player.stat_points <= 0) {
+    res.send(Alert.ErrorAlert(`Sorry, you don't have enough stat points`));
+    return;
+  }
+
+  req.player.stat_points -= 1;
+  req.player[stat]++;
+
+  updatePlayer(req.player);
+
+  const equippedItems = await getEquippedItems(req.player.id);
+  res.send(renderPlayerBar(req.player, equippedItems) + renderProfilePage(req.player));
 });
 
 app.get('/player/skills', authEndpoint, async (req: AuthRequest, res: Response) => {
index 0d8468b0bd3e5c6864fa9c0b2163fea7632e7769..93a9bf739801b94abed608803d12184ba9290730 100644 (file)
@@ -1,9 +1,9 @@
 import { Player, StatDef, StatDisplay } from "../../shared/player";
 
 function statPointIncreaser(stat: StatDisplay) {
-  return `<button class="increase-stat emit-event" data-event="spend-stat-point" data-args="${stat.id}">+</button>`;
+  return `<button class="increase-stat" hx-post="/player/stat/${stat.id}" hx-target="#profile">+</button>`;
 }
-export async function renderProfilePage(player: Player): Promise<string> {
+export function renderProfilePage(player: Player): string {
   let statBreakdown = '';
 
   StatDef.forEach(stat => {