From: xangelo Date: Mon, 20 Jan 2025 16:57:51 +0000 (-0500) Subject: fix: add confirmation to repair X-Git-Tag: v0.4.5~3 X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=a0c52131abc8a3ab97326b118ab0668a35e10c8d;p=risinglegends.git fix: add confirmation to repair --- diff --git a/src/server/routes/locations/repair.ts b/src/server/routes/locations/repair.ts index 64f8b3f..48e4c7f 100644 --- a/src/server/routes/locations/repair.ts +++ b/src/server/routes/locations/repair.ts @@ -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); +}); diff --git a/src/server/views/repair.ts b/src/server/views/repair.ts index 52f001a..b05016b 100644 --- a/src/server/views/repair.ts +++ b/src/server/views/repair.ts @@ -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 `` + return Button({ + id: `repair-${i.item_id}`, + class: ['repair-button'], + 'hx-get': `/city/services/${location.id}/repair/${i.item_id}/check`, + }, 'Repair'); }, player); });