Finalize aappv2 data models migration

This commit is contained in:
2026-02-27 14:36:54 +01:00
parent 8735b7e4a4
commit c45837ea31
87 changed files with 10701 additions and 1225 deletions

View File

@@ -1,134 +1,133 @@
/* -------------------------------------------- */
import { MaleficesUtility } from "./malefices-utility.js";
const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api
/* -------------------------------------------- */
export class MaleficesCharacterSummary extends Application {
export class MaleficesCharacterSummary extends HandlebarsApplicationMixin(ApplicationV2) {
/* -------------------------------------------- */
static DEFAULT_OPTIONS = {
id: "malefices-character-summary",
classes: ["MaleficesDialog"],
window: { title: "Résumé des Personnages", resizable: true },
position: { width: 960, height: "auto" },
dragDrop: [{ dragSelector: null, dropSelector: ".character-summary-container" }],
}
static PARTS = {
form: { template: "systems/fvtt-malefices/templates/dialogs/character-summary.hbs" }
}
/* -------------------------------------------- */
static displayPCSummary() {
if (game.user.isGM) {
game.system.malefices.charSummary.render(true)
game.system.malefices.charSummary.render({ force: true })
} else {
ui.notifications.info("Commande /tirage réservée au MJ !")
ui.notifications.info("Commande /resume réservée au MJ !")
}
}
/* -------------------------------------------- */
updatePCSummary() {
if (this.rendered) {
this.render(true)
if (this.element?.isConnected) {
this.render({ force: true })
}
}
/* -------------------------------------------- */
static createSummaryPos() {
return { top: 200, left: 200 };
}
/* -------------------------------------------- */
static ready() {
if (!game.user.isGM) { // Uniquement si GM
if (!game.user.isGM) {
return
}
let charSummary = new MaleficesCharacterSummary()
game.system.malefices.charSummary = charSummary
game.system.malefices.charSummary = new MaleficesCharacterSummary()
}
/* -------------------------------------------- */
constructor() {
super();
//game.settings.set("world", "character-summary-data", {npcList: [], x:0, y:0})
this.settings = game.settings.get("world", "character-summary-data")
constructor(options = {}) {
super(options)
const saved = game.settings.get("world", "character-summary-data")
this.extraList = saved.extraList ?? saved.npcList ?? []
}
/* -------------------------------------------- */
static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
template: "systems/fvtt-malefices/templates/dialogs/character-summary.hbs",
popOut: true,
resizable: true,
dragDrop: [{ dragSelector: ".items-list .item", dropSelector: null }],
classes: ["bol", "dialog"], width: 920, height: 'fit-content'
async _prepareContext(_options) {
const pcs = game.actors.filter(ac => ac.type === "personnage" && ac.hasPlayerOwner)
const extras = []
const validList = []
for (const actorId of this.extraList) {
const actor = game.actors.get(actorId)
if (actor) { extras.push(actor); validList.push(actorId) }
}
if (validList.length !== this.extraList.length) {
this.extraList = validList
this._persist()
}
return { pcs, extras, config: game.system.malefices.config }
}
/* -------------------------------------------- */
_onRender(_context, _options) {
const el = this.element
el.querySelectorAll('.actor-open').forEach(a => {
a.addEventListener('click', event => {
const li = event.currentTarget.closest('.item')
const actor = game.actors.get(li.dataset.actorId)
actor?.sheet.render(true)
})
})
el.querySelectorAll('.summary-roll').forEach(a => {
a.addEventListener('click', event => {
const li = event.currentTarget.closest('.item')
const actor = game.actors.get(li.dataset.actorId)
const key = event.currentTarget.dataset.key
actor?.rollAttribut(key)
})
})
el.querySelectorAll('.actor-delete').forEach(a => {
a.addEventListener('click', event => {
const li = event.currentTarget.closest('.item')
this.extraList = this.extraList.filter(id => id !== li.dataset.actorId)
this._persist()
this.render({ force: true })
})
})
const dropZone = el.querySelector('.character-summary-container')
if (dropZone) {
dropZone.addEventListener('dragover', ev => ev.preventDefault())
dropZone.addEventListener('drop', ev => { ev.stopPropagation(); this._onDrop(ev) })
}
}
/* -------------------------------------------- */
getData() {
let formData = super.getData();
_canDragDrop(_selector) { return true }
formData.pcs = game.actors.filter(ac => ac.type == "personnage" && ac.hasPlayerOwner)
formData.npcs = []
let newList = []
let toUpdate = false
for (let actorId of this.settings.npcList) {
let actor = game.actors.get(actorId)
if (actor) {
formData.npcs.push(actor)
newList.push(actorId)
} else {
toUpdate = true
}
}
formData.config = game.system.malefices.config
if (toUpdate) {
this.settings.npcList = newList
//console.log("Going to update ...", this.settings)
game.settings.set("world", "character-summary-data", this.settings)
}
return formData
/* -------------------------------------------- */
_persist() {
const saved = game.settings.get("world", "character-summary-data")
game.settings.set("world", "character-summary-data", { ...saved, extraList: this.extraList })
}
/* -------------------------------------------- */
updateNPC() {
game.settings.set("world", "character-summary-data", game.system.malefices.charSummary.settings)
game.system.malefices.charSummary.close()
setTimeout(function () { game.system.malefices.charSummary.render(true) }, 500)
_saveAndRefresh() {
this.render({ force: true })
}
/* -------------------------------------------- */
async _onDrop(event) {
//console.log("Dragged data are : ", dragData)
let data = event.dataTransfer.getData('text/plain')
let dataItem = JSON.parse(data)
let actor = fromUuidSync(dataItem.uuid)
if (actor) {
game.system.malefices.charSummary.settings.npcList.push(actor.id)
game.system.malefices.charSummary.updateNPC()
} else {
ui.notifications.warn("Pas d'acteur trouvé")
}
}
/* -------------------------------------------- */
/** @override */
async activateListeners(html) {
super.activateListeners(html);
html.find('.actor-open').click((event) => {
const li = $(event.currentTarget).parents(".item")
const actor = game.actors.get(li.data("actor-id"))
actor.sheet.render(true)
})
html.find('.summary-roll').click((event) => {
const li = $(event.currentTarget).parents(".item")
const actor = game.actors.get(li.data("actor-id"))
let type = $(event.currentTarget).data("type")
let key = $(event.currentTarget).data("key")
actor.rollAttribut(key)
})
html.find('.actor-delete').click(event => {
const li = $(event.currentTarget).parents(".item");
let actorId = li.data("actor-id")
let newList = game.system.malefices.charSummary.settings.npcList.filter(id => id != actorId)
game.system.malefices.charSummary.settings.npcList = newList
game.system.malefices.charSummary.updateNPC()
})
try {
const dataItem = JSON.parse(event.dataTransfer.getData('text/plain'))
const actor = fromUuidSync(dataItem.uuid)
if (actor && !this.extraList.includes(actor.id)) {
this.extraList.push(actor.id)
this._persist()
this.render({ force: true })
}
} catch(e) { /* not a valid drag payload */ }
}
}