diff --git a/module/rdd-possession.js b/module/rdd-possession.js index 1bda5b76..0de88bf0 100644 --- a/module/rdd-possession.js +++ b/module/rdd-possession.js @@ -3,6 +3,7 @@ import { RdDCombat } from "./rdd-combat.js"; import { RdDResolutionTable } from "./rdd-resolution-table.js"; import { RdDRoll } from "./rdd-roll.js"; import { RdDItemCompetenceCreature } from "./item-competencecreature.js"; +import { Targets } from "./targets.js"; /* -------------------------------------------- */ /* On part du principe qu'une entité démarre tjs @@ -27,30 +28,34 @@ export class RdDPossession { } /* -------------------------------------------- */ - static async onAttaquePossession(target, attacker, competence, possession = undefined) { + static async onAttaquePossession(target, attacker, competence, suitePossession = undefined) { const defender = target.actor; - possession = duplicate(possession ?? this.searchPossessionFromEntite(attacker, defender) ?? (await this.createPossession(attacker, defender))); + const fromEntite = RdDPossession.searchPossessionFromEntite(attacker, defender); + const isNouvelle = !suitePossession && ! fromEntite; + const possession = (suitePossession ?? fromEntite ?? (await RdDPossession.createPossession(attacker, defender))); - this.$updateEtatPossession(possession) + RdDPossession.$updateEtatPossession(possession) + let rollData = { mode: "possession", isECNIDefender: false, competence: competence, possession: possession, attacker: attacker, - defender: defender + defender: defender, + targetToken: Targets.extractTokenData(target) }; if (attacker.isCreature()) { RdDItemCompetenceCreature.setRollDataCreature(rollData) } - await RdDPossession.$rollAttaquePossession(attacker, rollData); + await RdDPossession.$rollAttaquePossession(attacker, rollData, isNouvelle); } /* -------------------------------------------- */ static async onConjurerPossession(attacker, competence, possession) { possession = duplicate(possession); - this.$updateEtatPossession(possession) + RdDPossession.$updateEtatPossession(possession) let rollData = { mode: "possession", isECNIDefender: true, @@ -74,11 +79,12 @@ export class RdDPossession { ui.notifications.warn("Une erreur s'est produite : Aucune possession trouvée !!") return } + possession = duplicate(possession) // Update for draconic roll let rollData = { mode: "conjuration", isECNIDefender: defender.type == "entite", - possession: duplicate(possession), + possession: possession, attacker: attacker, defender: defender, competence: defender.getDraconicOuPossession(), @@ -87,48 +93,49 @@ export class RdDPossession { } rollData.competence.system.defaut_carac = 'reve-actuel' - await RdDPossession.$rollDefensePossesion(defender, rollData); + await RdDPossession.$rollDefensePossession(defender, rollData); } /* -------------------------------------------- */ - static async $rollAttaquePossession(attacker, rollData) { + static async $rollAttaquePossession(attacker, rollData, isNouvelle = false) { const dialog = await RdDRoll.create(attacker, rollData, + { html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-competence.html' }, { - html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-competence.html' - }, { name: 'jet-possession', label: rollData.isECNIDefender ? 'Conjurer la possession' : 'Possession', callbacks: [ - { condition: r => (r.rolled.isSuccess), action: async (r) => await this.$onRollPossession(r, true) }, - { condition: r => (r.rolled.isEchec), action: async (r) => await this.$onRollPossession(r, false) }, + { condition: r => (r.rolled.isSuccess), action: async (r) => await RdDPossession.$onRollPossession(r, true, isNouvelle) }, + { condition: r => (r.rolled.isEchec), action: async (r) => await RdDPossession.$onRollPossession(r, false, isNouvelle) }, ] }); dialog.render(true); } /* -------------------------------------------- */ - static async $rollDefensePossesion(defender, rollData) { + static async $onRollPossession(rollData, isSuccess, isNouvelle = false) { + rollData.possession.isSuccess = isSuccess; + RdDPossession.$updateEtatPossession(rollData.possession); + if (isNouvelle) { + // Creer la possession sur le defenseur + rollData.defender.createEmbeddedDocuments('Item', [rollData.possession.toObject()]) + } + await RdDResolutionTable.displayRollData(rollData, rollData.attacker, 'chat-resultat-possession.html'); + } + + /* -------------------------------------------- */ + static async $rollDefensePossession(defender, rollData) { const dialog = await RdDRoll.create(defender, rollData, - { - html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-defense-possession.html' - }, + { html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-defense-possession.html' }, { name: 'conjurer', label: 'Conjurer une Possession', callbacks: [ - { action: async (r) => await this.$onRollConjuration(r) } - ] - } - ); + { action: async (r) => await RdDPossession.$onRollConjuration(r) } + ] + } + ); dialog.render(true); - } - - /* -------------------------------------------- */ - static async $onRollPossession(rollData, isSuccess) { - rollData.possession.isSuccess = isSuccess; - this.$updateEtatPossession(rollData.possession); - await RdDResolutionTable.displayRollData(rollData, this, 'chat-resultat-possession.html'); - } + } /* -------------------------------------------- */ static async $onRollConjuration(rollData) { @@ -143,9 +150,9 @@ export class RdDPossession { await actor.updateEmbeddedDocuments('Item', [update]) } - this.$updateEtatPossession(rollData.possession) + RdDPossession.$updateEtatPossession(rollData.possession) - await RdDResolutionTable.displayRollData(rollData, this, 'chat-resultat-possession.html') + await RdDResolutionTable.displayRollData(rollData,rollData.defender, 'chat-resultat-possession.html') if (rollData.possession.isPosseder || rollData.possession.isConjurer) { // conjuration actor.deleteEmbeddedDocuments("Item", [rollData.possession._id]) @@ -175,14 +182,14 @@ export class RdDPossession { /* -------------------------------------------- */ static async createPossession(attacker, defender) { - let possessionData = { - name: "Possession en cours de " + attacker.name, type: 'possession', - img: "systems/foundryvtt-reve-de-dragon/icons/entites/possession2.webp", - system: { description: "", typepossession: attacker.name, possede: false, possessionid: randomID(16), possesseurid: attacker.id, possedeid: defender.id, date: 0, compteur: 0 } - } - // Creates only the possession on the personnage side - let poss = await defender.createEmbeddedDocuments('Item', [possessionData]) - return duplicate(poss[0]) + return await Item.create({ + name: "Possession en cours de " + attacker.name, type: 'possession', + img: "systems/foundryvtt-reve-de-dragon/icons/entites/possession2.webp", + system: { description: "", typepossession: attacker.name, possede: false, possessionid: randomID(16), possesseurid: attacker.id, possedeid: defender.id, date: 0, compteur: 0 } + }, + { + temporary: true + }) } } \ No newline at end of file