import { SYSTEM } from "../config/system.mjs" export default class HamalronResistanceRecovery { static SYMBOLE_LABELS = { coupe: "Coupe", epee: "Épée", denier: "Denier", baton: "Bâton", } static async prompt(actor) { const resistances = actor.system.resistances const recovery = actor.system.resistanceRecovery || {} const today = new Date().toISOString().split("T")[0] const todayNum = Number(new Date().toISOString().split("T")[0].replace(/-/g, "")) const symInfos = Object.entries(SYSTEM.TAROT_SYMBOLES).map(([key, val]) => { const r = resistances[key] const lastDate = recovery[key] const lastNum = lastDate ? Number(lastDate.replace(/-/g, "")) : 0 const canRecover = key === "epee" ? true : (lastNum < todayNum) const isDenierCoupe = key === "denier" || key === "coupe" return { id: key, label: val.label, value: r?.value ?? 0, max: r?.max ?? 0, canRecover, isDenierCoupe, lastRecovery: lastDate || "-", } }) const content = HamalronResistanceRecovery.#renderContent(actor, symInfos) const result = await foundry.applications.api.DialogV2.wait({ window: { title: game.i18n.localize("HAMALRON.Recovery.Title") }, classes: ["fvtt-hamalron"], position: { width: 480 }, content, buttons: [ { action: "close", label: game.i18n.localize("Cancel"), default: true, }, ], actions: { recoverTest: async (event, button, dialog) => { const btn = event.currentTarget const symbole = btn.dataset.symbole if (!symbole) return const rollItem = { name: HamalronResistanceRecovery.SYMBOLE_LABELS[symbole] || symbole, value: 0 } await HamalronResistanceRecovery.#doRecoveryTest(actor, symbole, rollItem) HamalronResistanceRecovery.prompt(actor) }, adjustResistance: async (event, button, dialog) => { const btn = event.currentTarget const symbole = btn.dataset.symbole const delta = Number(btn.dataset.delta) || 0 if (!symbole) return const resistance = actor.system.resistances[symbole] if (!resistance) return const newValue = Math.max(0, Math.min(resistance.max, (resistance.value || 0) + delta)) await actor.update({ [`system.resistances.${symbole}.value`]: newValue }) HamalronResistanceRecovery.prompt(actor) }, }, rejectClose: false, }) } static #renderContent(actor, symInfos) { const today = new Date().toISOString().split("T")[0] let html = `

${game.i18n.localize("HAMALRON.Recovery.Subtitle")}

` html += `

${game.i18n.format("HAMALRON.Recovery.Today", { date: today })}

` html += `
` for (const info of symInfos) { const pct = info.max > 0 ? Math.round((info.value / info.max) * 100) : 0 html += `
${info.label} ${info.value}/${info.max}
` if (info.isDenierCoupe) { if (info.canRecover) { html += `` } else { html += ` ${game.i18n.localize("HAMALRON.Recovery.Done")}` } } else if (info.id === "epee") { html += `` html += `` } else { // baton html += `` } html += `` html += `
${game.i18n.localize("HAMALRON.Recovery.Last")}: ${info.lastRecovery}
` } html += `
` return html } static async #doRecoveryTest(actor, symbole, rollItem) { const niveauData = SYSTEM.NIVEAU_COMPETENCES.inexperimente const roll = await HamalronTirageTarot.prompt({ rollType: "stat", rollItem, actorId: actor.id, actorName: actor.name, actorImage: actor.img, hasTarget: false, difficulty: "7", }) if (!roll) return const today = new Date().toISOString().split("T")[0] const recoveryData = foundry.utils.duplicate(actor.system.resistanceRecovery || {}) recoveryData[symbole] = today await actor.update({ [`system.resistanceRecovery`]: recoveryData }) if (roll.total >= 7) { const resistance = actor.system.resistances[symbole] if (resistance && resistance.value < resistance.max) { const newValue = Math.min(resistance.max, (resistance.value || 0) + 1) await actor.update({ [`system.resistances.${symbole}.value`]: newValue }) } ui.notifications.info(game.i18n.format("HAMALRON.Recovery.Success", { label: HamalronResistanceRecovery.SYMBOLE_LABELS[symbole] })) } else { ui.notifications.info(game.i18n.format("HAMALRON.Recovery.Failure", { label: HamalronResistanceRecovery.SYMBOLE_LABELS[symbole] })) } } }