Rework des fiches creature/PJ et Tinji/Loksyu

This commit is contained in:
2026-03-30 16:36:38 +02:00
parent 0689fae792
commit cab77645b7
70 changed files with 1712 additions and 1075 deletions

View File

@@ -1,5 +1,4 @@
import { getSingletonActor } from "./singletons.js"
import { ACTOR_TYPES } from "../../config/constants.js"
import { getLoksyuData, setLoksyuData } from "./singletons.js"
const SYSTEM_ID = "fvtt-chroniques-de-l-etrange"
@@ -15,7 +14,7 @@ export class CDELoksyuApp extends foundry.applications.api.HandlebarsApplication
resizable: false,
},
classes: ["cde-app", "cde-loksyu-standalone"],
position: { width: 540, height: "auto" },
position: { width: 520, height: "auto" },
actions: {
resetElement: CDELoksyuApp.#onResetElement,
resetAll: CDELoksyuApp.#onResetAll,
@@ -28,14 +27,12 @@ export class CDELoksyuApp extends foundry.applications.api.HandlebarsApplication
},
}
/** @type {Actor|null} */
#actor = null
/** @type {Function|null} bound hook handler */
#updateHook = null
_updateHook = null
/** Singleton accessor — open or bring to front */
static open() {
const existing = Object.values(foundry.applications.instances ?? {}).find(
const existing = Array.from(foundry.applications.instances.values()).find(
(app) => app instanceof CDELoksyuApp
)
if (existing) { existing.bringToFront(); return existing }
@@ -45,10 +42,7 @@ export class CDELoksyuApp extends foundry.applications.api.HandlebarsApplication
}
async _prepareContext() {
this.#actor = await getSingletonActor(ACTOR_TYPES.loksyu)
if (!this.#actor) return { hasActor: false }
const sys = this.#actor.system
const sys = getLoksyuData()
const ELEMENTS = [
{ key: "wood", nameKey: "CDE.Wood", qualKey: "CDE.WoodQualities", img: `systems/${SYSTEM_ID}/images/cde_bois.webp` },
{ key: "fire", nameKey: "CDE.Fire", qualKey: "CDE.FireQualities", img: `systems/${SYSTEM_ID}/images/cde_feu.webp` },
@@ -58,12 +52,11 @@ export class CDELoksyuApp extends foundry.applications.api.HandlebarsApplication
]
return {
hasActor: true,
canEdit: this.#actor.isOwner,
canEdit: game.user.isGM,
elements: ELEMENTS.map((el) => ({
...el,
yang: sys[el.key]?.yang?.value ?? 0,
yin: sys[el.key]?.yin?.value ?? 0,
yang: sys[el.key]?.yang ?? 0,
yin: sys[el.key]?.yin ?? 0,
})),
}
}
@@ -72,16 +65,13 @@ export class CDELoksyuApp extends foundry.applications.api.HandlebarsApplication
super._onRender(context, options)
this.#bindInputs()
// Subscribe to actor updates to keep the app live
this.#updateHook = Hooks.on("updateActor", (actor) => {
if (actor.id === this.#actor?.id) this.render()
})
this._updateHook = Hooks.on("cde:loksyuUpdated", () => this.render())
}
_onClose(options) {
if (this.#updateHook !== null) {
Hooks.off("updateActor", this.#updateHook)
this.#updateHook = null
if (this._updateHook !== null) {
Hooks.off("cde:loksyuUpdated", this._updateHook)
this._updateHook = null
}
super._onClose(options)
}
@@ -94,28 +84,29 @@ export class CDELoksyuApp extends foundry.applications.api.HandlebarsApplication
const field = ev.currentTarget.dataset.field
const val = parseInt(ev.currentTarget.value, 10)
if (!field || isNaN(val)) return
await this.#actor?.update({ [field]: Math.max(0, val) })
// field is like "wood.yin" or "fire.yang"
const [aspect, dim] = field.split(".")
if (!aspect || !dim) return
const data = getLoksyuData()
if (!data[aspect]) data[aspect] = { yin: 0, yang: 0 }
data[aspect][dim] = Math.max(0, val)
await setLoksyuData(data)
})
})
}
static async #onResetElement(event, target) {
const key = target.dataset.element
if (!key || !this.#actor) return
await this.#actor.update({
[`system.${key}.yin.value`]: 0,
[`system.${key}.yang.value`]: 0,
})
if (!key) return
const data = getLoksyuData()
data[key] = { yin: 0, yang: 0 }
await setLoksyuData(data)
}
static async #onResetAll(_event, _target) {
if (!this.#actor) return
const KEYS = ["wood", "fire", "earth", "metal", "water"]
const update = {}
for (const k of KEYS) {
update[`system.${k}.yin.value`] = 0
update[`system.${k}.yang.value`] = 0
}
await this.#actor.update(update)
const data = getLoksyuData()
for (const k of KEYS) data[k] = { yin: 0, yang: 0 }
await setLoksyuData(data)
}
}