feat: Loksyu & TinJi standalone AppV2 apps with chat buttons and dice automation

- CDELoksyuApp: standalone HandlebarsApplicationMixin(ApplicationV2) app
  - 5-element Wu Xing grid with yin/yang inputs per element
  - Per-element reset buttons + global reset-all
  - Auto-refresh via updateActor hook

- CDETinjiApp: standalone AppV2 for the collective Tin Ji dice pool
  - Large neon counter with +/- buttons and direct input
  - Spend button sends a chat message with remaining count

- singletons.js: shared utilities
  - getSingletonActor: find or auto-create singleton actor
  - updateLoksyuFromRoll: compute lokAspect from Wu Xing cycle, update yin/yang
  - updateTinjiFromRoll: add tinji face count to value

- rolling.js: auto-update both singletons after every dice roll
  (weapon path + main roll path)

- system.js: renderChatLog hook injects Loksyu/TinJi footer buttons
  in the chat sidebar

- loksyu.js / tinji.js: actor sheets redirect to standalone apps
  when opened via the sidebar

- CSS: .cde-loksyu-standalone, .cde-tinji-standalone, .cde-chat-app-buttons,
  .cde-tinji-spend-msg styles added

- i18n: new keys in fr-cde.json and en-cde.json for all new UI strings
  (LoksyuNotFound, TinjiNotFound, Reset, ResetAll, SpendTinji, etc.)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-03-30 09:51:39 +02:00
parent 6fda4b9246
commit 0689fae792
41 changed files with 1558 additions and 13 deletions

View File

@@ -105,4 +105,6 @@ export const TEMPLATE_PARTIALS = [
"systems/fvtt-chroniques-de-l-etrange/templates/actor/parts/cde-npc-spells.html",
"systems/fvtt-chroniques-de-l-etrange/templates/actor/parts/cde-npc-kungfus.html",
"systems/fvtt-chroniques-de-l-etrange/templates/actor/parts/cde-npc-items.html",
"systems/fvtt-chroniques-de-l-etrange/templates/apps/cde-loksyu-app.html",
"systems/fvtt-chroniques-de-l-etrange/templates/apps/cde-tinji-app.html",
]

View File

@@ -11,6 +11,8 @@ import { registerHandlebarsHelpers } from "./ui/helpers.js"
import { preloadPartials } from "./ui/templates.js"
import { CDELoksyuSheet, CDECharacterSheet, CDENpcSheet, CDETinjiSheet } from "./ui/sheets/actors/index.js"
import { CDEItemSheet, CDEKungfuSheet, CDESpellSheet, CDESupernaturalSheet, CDEWeaponSheet, CDEArmorSheet, CDESanheiSheet, CDEIngredientSheet } from "./ui/sheets/items/index.js"
import { CDELoksyuApp } from "./ui/apps/loksyu-app.js"
import { CDETinjiApp } from "./ui/apps/tinji-app.js"
import { migrateIfNeeded, registerSettings } from "./migration.js"
Hooks.once("i18nInit", preLocalizeConfig)
@@ -122,6 +124,28 @@ Hooks.once("ready", async () => {
await migrateIfNeeded()
})
/** Add Loksyu + Tin Ji quick-access buttons to the chat footer */
Hooks.on("renderChatLog", (_app, html) => {
const el = html instanceof HTMLElement ? html : html[0]
const controls = el?.querySelector?.(".chat-controls")
if (!controls) return
const wrapper = document.createElement("div")
wrapper.classList.add("cde-chat-app-buttons")
wrapper.innerHTML = `
<button class="cde-chat-btn cde-chat-btn--loksyu" title="${game.i18n.localize("CDE.Loksyu")}">
<i class="fas fa-yin-yang"></i> ${game.i18n.localize("CDE.Loksyu")}
</button>
<button class="cde-chat-btn cde-chat-btn--tinji" title="${game.i18n.localize("CDE.TinJi2")}">
<i class="fas fa-star"></i> ${game.i18n.localize("CDE.TinJi2")}
</button>
`
controls.appendChild(wrapper)
wrapper.querySelector(".cde-chat-btn--loksyu")?.addEventListener("click", () => CDELoksyuApp.open())
wrapper.querySelector(".cde-chat-btn--tinji")?.addEventListener("click", () => CDETinjiApp.open())
})
function injectCompendiumLink(html) {
const header = html[0]?.querySelector?.("h4.divider")
if (!header) return

3
src/ui/apps/index.js Normal file
View File

@@ -0,0 +1,3 @@
export { CDELoksyuApp } from "./loksyu-app.js"
export { CDETinjiApp } from "./tinji-app.js"
export { getSingletonActor, updateLoksyuFromRoll, updateTinjiFromRoll } from "./singletons.js"

121
src/ui/apps/loksyu-app.js Normal file
View File

@@ -0,0 +1,121 @@
import { getSingletonActor } from "./singletons.js"
import { ACTOR_TYPES } from "../../config/constants.js"
const SYSTEM_ID = "fvtt-chroniques-de-l-etrange"
export class CDELoksyuApp extends foundry.applications.api.HandlebarsApplicationMixin(
foundry.applications.api.ApplicationV2
) {
static DEFAULT_OPTIONS = {
id: "cde-loksyu-app",
tag: "div",
window: {
title: "CDE.Loksyu",
icon: "fas fa-yin-yang",
resizable: false,
},
classes: ["cde-app", "cde-loksyu-standalone"],
position: { width: 540, height: "auto" },
actions: {
resetElement: CDELoksyuApp.#onResetElement,
resetAll: CDELoksyuApp.#onResetAll,
},
}
static PARTS = {
main: {
template: `systems/${SYSTEM_ID}/templates/apps/cde-loksyu-app.html`,
},
}
/** @type {Actor|null} */
#actor = null
/** @type {Function|null} bound hook handler */
#updateHook = null
/** Singleton accessor — open or bring to front */
static open() {
const existing = Object.values(foundry.applications.instances ?? {}).find(
(app) => app instanceof CDELoksyuApp
)
if (existing) { existing.bringToFront(); return existing }
const app = new CDELoksyuApp()
app.render(true)
return app
}
async _prepareContext() {
this.#actor = await getSingletonActor(ACTOR_TYPES.loksyu)
if (!this.#actor) return { hasActor: false }
const sys = this.#actor.system
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` },
{ key: "earth", nameKey: "CDE.Earth", qualKey: "CDE.EarthQualities", img: `systems/${SYSTEM_ID}/images/cde_terre.webp` },
{ key: "metal", nameKey: "CDE.Metal", qualKey: "CDE.MetalQualities", img: `systems/${SYSTEM_ID}/images/cde_metal.webp` },
{ key: "water", nameKey: "CDE.Water", qualKey: "CDE.WaterQualities", img: `systems/${SYSTEM_ID}/images/cde_eau.webp` },
]
return {
hasActor: true,
canEdit: this.#actor.isOwner,
elements: ELEMENTS.map((el) => ({
...el,
yang: sys[el.key]?.yang?.value ?? 0,
yin: sys[el.key]?.yin?.value ?? 0,
})),
}
}
_onRender(context, options) {
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()
})
}
_onClose(options) {
if (this.#updateHook !== null) {
Hooks.off("updateActor", this.#updateHook)
this.#updateHook = null
}
super._onClose(options)
}
#bindInputs() {
const inputs = this.element?.querySelectorAll("input[data-field]")
if (!inputs?.length) return
inputs.forEach((input) => {
input.addEventListener("change", async (ev) => {
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) })
})
})
}
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,
})
}
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)
}
}

99
src/ui/apps/singletons.js Normal file
View File

@@ -0,0 +1,99 @@
/**
* Singleton actor utilities for Loksyu and Tin Ji.
*
* Both are world-level shared trackers backed by a singleton Actor document
* of type "loksyu" / "tinji". GMs can create them via the Actors sidebar;
* the apps find the first one or offer to create it.
*/
import { ACTOR_TYPES } from "../../config/constants.js"
/** Wu Xing generating cycle — [successes, auspicious, noxious, loksyu, tinji] */
const WU_XING_CYCLE = {
wood: ["wood", "fire", "water", "earth", "metal"],
fire: ["fire", "earth", "wood", "metal", "water"],
earth: ["earth", "metal", "fire", "water", "wood"],
metal: ["metal", "water", "earth", "wood", "fire"],
water: ["water", "wood", "metal", "fire", "earth"],
}
/** Die face pairs [yin, yang] per aspect (0 = face "10") */
const ASPECT_FACES = {
metal: [3, 8],
water: [1, 6],
earth: [0, 5],
fire: [2, 7],
wood: [4, 9],
}
/**
* Find the first actor of the given type in the world, or create one if the
* current user is a GM and none exists.
*
* @param {"loksyu"|"tinji"} type
* @returns {Promise<Actor|null>}
*/
export async function getSingletonActor(type) {
const existing = game.actors.find((a) => a.type === type)
if (existing) return existing
if (!game.user.isGM) {
ui.notifications.warn(game.i18n.localize(type === ACTOR_TYPES.loksyu ? "CDE.LoksyuNotFound" : "CDE.TinjiNotFound"))
return null
}
// Auto-create the singleton when the GM opens the app for the first time.
const nameKey = type === ACTOR_TYPES.loksyu ? "CDE.UpperCaseLoksyu" : "CDE.UpperCaseTinJi"
const actor = await Actor.create({
name: game.i18n.localize(nameKey),
type,
img: type === ACTOR_TYPES.loksyu
? "systems/fvtt-chroniques-de-l-etrange/images/loksyu_long.webp"
: "systems/fvtt-chroniques-de-l-etrange/images/tinji.webp",
})
return actor ?? null
}
/**
* After a WuXing roll, add the loksyu faces (yin + yang) of the relevant
* aspect to the singleton Loksyu actor.
*
* @param {string} activeAspect - The aspect used for the roll (e.g. "fire")
* @param {Object} faces - Die face counts { 0: n, 1: n, …, 9: n }
*/
export async function updateLoksyuFromRoll(activeAspect, faces) {
const cycle = WU_XING_CYCLE[activeAspect]
if (!cycle) return
const lokAspect = cycle[3]
const [yinFace, yangFace] = ASPECT_FACES[lokAspect] ?? []
if (yinFace === undefined) return
const yinCount = faces[yinFace] ?? 0
const yangCount = faces[yangFace] ?? 0
if (yinCount === 0 && yangCount === 0) return
const actor = await getSingletonActor(ACTOR_TYPES.loksyu)
if (!actor) return
const current = actor.system[lokAspect] ?? { yin: { value: 0 }, yang: { value: 0 } }
await actor.update({
[`system.${lokAspect}.yin.value`]: (current.yin.value ?? 0) + yinCount,
[`system.${lokAspect}.yang.value`]: (current.yang.value ?? 0) + yangCount,
})
}
/**
* After a WuXing roll, add tinji faces to the singleton TinJi actor.
*
* @param {number} count - Number of tinji faces rolled
*/
export async function updateTinjiFromRoll(count) {
if (!count || count <= 0) return
const actor = await getSingletonActor(ACTOR_TYPES.tinji)
if (!actor) return
const current = actor.system.value ?? 0
await actor.update({ "system.value": current + count })
}

124
src/ui/apps/tinji-app.js Normal file
View File

@@ -0,0 +1,124 @@
import { getSingletonActor } from "./singletons.js"
import { ACTOR_TYPES } from "../../config/constants.js"
const SYSTEM_ID = "fvtt-chroniques-de-l-etrange"
export class CDETinjiApp extends foundry.applications.api.HandlebarsApplicationMixin(
foundry.applications.api.ApplicationV2
) {
static DEFAULT_OPTIONS = {
id: "cde-tinji-app",
tag: "div",
window: {
title: "CDE.TinJi2",
icon: "fas fa-star",
resizable: false,
},
classes: ["cde-app", "cde-tinji-standalone"],
position: { width: 320, height: "auto" },
actions: {
increment: CDETinjiApp.#onIncrement,
decrement: CDETinjiApp.#onDecrement,
reset: CDETinjiApp.#onReset,
spend: CDETinjiApp.#onSpend,
},
}
static PARTS = {
main: {
template: `systems/${SYSTEM_ID}/templates/apps/cde-tinji-app.html`,
},
}
/** @type {Actor|null} */
#actor = null
/** @type {Function|null} */
#updateHook = null
static open() {
const existing = Object.values(foundry.applications.instances ?? {}).find(
(app) => app instanceof CDETinjiApp
)
if (existing) { existing.bringToFront(); return existing }
const app = new CDETinjiApp()
app.render(true)
return app
}
async _prepareContext() {
this.#actor = await getSingletonActor(ACTOR_TYPES.tinji)
if (!this.#actor) return { hasActor: false, value: 0 }
return {
hasActor: true,
canEdit: this.#actor.isOwner,
value: this.#actor.system.value ?? 0,
}
}
_onRender(context, options) {
super._onRender(context, options)
this.#bindDirectInput()
this.#updateHook = Hooks.on("updateActor", (actor) => {
if (actor.id === this.#actor?.id) this.render()
})
}
_onClose(options) {
if (this.#updateHook !== null) {
Hooks.off("updateActor", this.#updateHook)
this.#updateHook = null
}
super._onClose(options)
}
#bindDirectInput() {
const input = this.element?.querySelector("input.cde-tinji-direct")
if (!input) return
input.addEventListener("change", async (ev) => {
const val = parseInt(ev.currentTarget.value, 10)
if (!isNaN(val) && this.#actor) {
await this.#actor.update({ "system.value": Math.max(0, val) })
}
})
}
static async #onIncrement() {
if (!this.#actor) return
const current = this.#actor.system.value ?? 0
await this.#actor.update({ "system.value": current + 1 })
}
static async #onDecrement() {
if (!this.#actor) return
const current = this.#actor.system.value ?? 0
if (current <= 0) return
await this.#actor.update({ "system.value": current - 1 })
}
static async #onReset() {
if (!this.#actor) return
await this.#actor.update({ "system.value": 0 })
}
/** Spend 1 Tin Ji die and announce it in chat */
static async #onSpend() {
if (!this.#actor) return
const current = this.#actor.system.value ?? 0
if (current <= 0) {
ui.notifications.warn(game.i18n.localize("CDE.TinjiEmpty"))
return
}
await this.#actor.update({ "system.value": current - 1 })
ChatMessage.create({
user: game.user.id,
content: `<div class="cde-tinji-spend-msg">
<i class="fas fa-star"></i>
<strong>${game.i18n.localize("CDE.TinJi2")}</strong>
${game.i18n.format("CDE.TinjiSpent", { name: game.user.name })}
<span class="cde-tinji-remain">(${current - 1} ${game.i18n.localize("CDE.TinjiRemaining")})</span>
</div>`,
})
}
}

View File

@@ -14,6 +14,7 @@
*/
import { MAGICS } from "../config/constants.js"
import { updateLoksyuFromRoll, updateTinjiFromRoll } from "./apps/singletons.js"
const RESULT_TEMPLATE = "systems/fvtt-chroniques-de-l-etrange/templates/form/cde-dice-result.html"
const SKILL_PROMPT_TEMPLATE = "systems/fvtt-chroniques-de-l-etrange/templates/form/cde-skill-dice-prompt.html"
@@ -445,6 +446,9 @@ export async function rollForActor(actor, rollKey) {
if (game.modules.get("dice-so-nice")?.active && wpMsg?.id) {
await game.dice3d.waitFor3DAnimationByMessageID(wpMsg.id)
}
// Auto-update Loksyu/TinJi singletons from weapon roll faces
if ((wpResults.loksyudice ?? 0) > 0) await updateLoksyuFromRoll(wpAspectName, wpFaces)
if ((wpResults.tinjidice ?? 0) > 0) await updateTinjiFromRoll(wpResults.tinjidice)
return
}
default:
@@ -625,4 +629,8 @@ export async function rollForActor(actor, rollKey) {
if (game.modules.get("dice-so-nice")?.active && msg?.id) {
await game.dice3d.waitFor3DAnimationByMessageID(msg.id)
}
// ---- Auto-update Loksyu / TinJi singletons ----
if ((results.loksyudice ?? 0) > 0) await updateLoksyuFromRoll(wuXingAspectName, faces)
if ((results.tinjidice ?? 0) > 0) await updateTinjiFromRoll(results.tinjidice)
}

View File

@@ -1,4 +1,5 @@
import { CDEBaseActorSheet } from "./base.js"
import { CDELoksyuApp } from "../../apps/loksyu-app.js"
export class CDELoksyuSheet extends CDEBaseActorSheet {
static DEFAULT_OPTIONS = {
@@ -10,4 +11,11 @@ export class CDELoksyuSheet extends CDEBaseActorSheet {
}
tabGroups = { primary: "loksyu" }
/** Redirect any direct actor-sheet open to the standalone app instead */
async _onFirstRender(context, options) {
// Close this actor sheet immediately and open the standalone app
await this.close({ animate: false })
CDELoksyuApp.open()
}
}

View File

@@ -1,4 +1,5 @@
import { CDEBaseActorSheet } from "./base.js"
import { CDETinjiApp } from "../../apps/tinji-app.js"
export class CDETinjiSheet extends CDEBaseActorSheet {
static DEFAULT_OPTIONS = {
@@ -10,4 +11,10 @@ export class CDETinjiSheet extends CDEBaseActorSheet {
}
tabGroups = { primary: "tinji" }
/** Redirect any direct actor-sheet open to the standalone app instead */
async _onFirstRender(context, options) {
await this.close({ animate: false })
CDETinjiApp.open()
}
}