fix: add confirmation to repair
authorxangelo <me@xangelo.ca>
Mon, 20 Jan 2025 16:57:51 +0000 (11:57 -0500)
committerxangelo <me@xangelo.ca>
Mon, 20 Jan 2025 16:57:51 +0000 (11:57 -0500)
src/server/routes/locations/repair.ts
src/server/views/repair.ts

index 64f8b3f1eff03a61b72e79040a95ba9840df4106..48e4c7f286f7f2e34ce134cf402972fb6869440a 100644 (file)
@@ -7,6 +7,7 @@ import { repairCost } from "../../../shared/inventory";
 import * as Alert from "../../views/alert";
 import { updatePlayer } from "../../player";
 import { renderPlayerBar } from "../../views/player-bar";
+import { Button } from '@server/views/components/button';
 
 export const repairRouter = Router();
 
@@ -67,3 +68,44 @@ repairRouter.post('/city/services/:location_id/repair/:item_id', authEndpoint, a
     + Alert.SuccessAlert(`You repaired your ${item.name} for ${cost}G`)
   );
 });
+
+// check if the player has enough gold to repair the item
+repairRouter.get('/city/services/:location_id/repair/:item_id/check', authEndpoint, async (req: Request, res: Response) => {
+  const service = await getService(parseInt(req.params.location_id));
+
+  if(!service || service.city_id !== req.player.city_id) {
+    req.logger.error(`Invalid location: [${req.params.location_id}]`);
+    return res.sendStatus(400);
+  }
+
+  const item = await getInventoryItem(req.player.id, req.params.item_id);
+
+  if(!item) {
+    req.logger.error(`Invalid item [${req.params.item_id}]`);
+    return res.sendStatus(400);
+  }
+
+  const cost = repairCost(item);
+  const repair_button = Button({
+    id: `repair-${item.item_id}`,
+    class: ['repair-button', 'green'],
+    'hx-swap-oob': 'true',
+    'hx-target': '#explore',
+    'hx-swap': 'innerHTML',
+    'hx-post': `/city/services/${service.id}/repair/${item.item_id}`,
+  }, 'Confirm');
+
+  const original_button = Button({
+    id: `repair-${item.item_id}`,
+    class: ['repair-button'],
+    'hx-swap-oob': 'true',
+    'hx-get': `/city/services/${service.id}/repair/${item.item_id}/check`,
+  }, 'Repair');
+
+  if(req.player.gold < cost) {
+    res.send(Alert.ErrorAlert(`Sorry, you need at least ${cost.toLocaleString()}G to repair your ${item.name}`) + original_button);
+    return;
+  }
+
+  return res.send(repair_button);
+});
index 52f001a7713959b8ad1be0026e0a3d4c362c49e4..b05016bd14f33649651e0c85b24d2d30096ed066 100644 (file)
@@ -6,7 +6,7 @@ import { EquippedItemDetails } from "@shared/equipped";
 import { ProgressBar } from "./components/progress-bar";
 import * as City from './components/city';
 import { BackToTown } from "./components/button";
-
+import { Button } from './components/button';
 
 type RenderStatOptions = {
   unsigned: boolean
@@ -100,7 +100,11 @@ export function renderRepairService(equipment: EquippedItemDetails[], player: Pl
     }
 
     listing[filter] += renderEquipmentToRepair(item, i => {
-      return `<button type="button" hx-post="/city/services/${location.id}/repair/${i.item_id}" hx-target="#explore">Repair</button>`
+      return Button({
+        id: `repair-${i.item_id}`,
+        class: ['repair-button'],
+        'hx-get': `/city/services/${location.id}/repair/${i.item_id}/check`,
+      }, 'Repair');
     }, player);
 
   });