2021-11-11 09:37:00 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
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";
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
/* On part du principe qu'une entité démarre tjs
|
|
|
|
une possession via le MJ (ie un joueur ne controle pas une entité)
|
|
|
|
Donc la compétence Possession ne peut être démarrée que par le MJ.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
export class RdDPossession {
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2022-07-02 01:41:55 +02:00
|
|
|
static init() {
|
2021-11-11 09:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2022-07-02 01:41:55 +02:00
|
|
|
static searchPossessionFromEntite(attacker, defender) {
|
|
|
|
let poss = attacker.data.items.find(poss => poss.type == 'possession' && poss.data.data.possedeid == defender.data._id);
|
2021-11-21 12:04:42 +01:00
|
|
|
if (!poss) {
|
2022-07-02 01:41:55 +02:00
|
|
|
poss = defender.data.items.find(poss => poss.type == 'possession' && poss.data.data.possedeid == defender.data._id);
|
2021-11-21 12:04:42 +01:00
|
|
|
}
|
|
|
|
return poss && duplicate(poss) || undefined;
|
2021-11-11 09:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2022-07-02 01:41:55 +02:00
|
|
|
static updateEtatPossession(possession) {
|
2022-05-14 20:25:47 +02:00
|
|
|
possession.ptsConjuration = 0
|
|
|
|
possession.ptsPossession = 0
|
|
|
|
console.log("Possession", possession)
|
2022-07-02 01:41:55 +02:00
|
|
|
if (possession.data.compteur > 0) {
|
2022-05-14 17:28:27 +02:00
|
|
|
possession.ptsPossession = possession.data.compteur
|
2022-07-02 01:41:55 +02:00
|
|
|
}
|
|
|
|
if (possession.data.compteur < 0) {
|
2022-05-14 17:28:27 +02:00
|
|
|
possession.ptsConjuration = Math.abs(possession.data.compteur)
|
2022-07-02 01:41:55 +02:00
|
|
|
}
|
2022-05-14 20:25:47 +02:00
|
|
|
possession.isPosseder = false
|
|
|
|
possession.isConjurer = false
|
2022-07-02 01:41:55 +02:00
|
|
|
if (possession.ptsPossession >= 2) {
|
2022-05-14 20:25:47 +02:00
|
|
|
possession.isPosseder = true
|
2021-11-21 12:04:42 +01:00
|
|
|
}
|
2022-07-02 01:41:55 +02:00
|
|
|
if (possession.ptsConjuration >= 2) {
|
2022-05-14 20:25:47 +02:00
|
|
|
possession.isConjurer = true
|
2021-11-21 12:04:42 +01:00
|
|
|
}
|
2021-11-11 09:37:00 +01:00
|
|
|
}
|
2021-11-21 12:04:42 +01:00
|
|
|
|
2021-11-11 09:37:00 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-07-02 01:41:55 +02:00
|
|
|
static async resultConjuration(rollData) {
|
2022-05-14 20:39:01 +02:00
|
|
|
let actor = game.actors.get(rollData.possession.data.possedeid)
|
2022-07-02 01:41:55 +02:00
|
|
|
if (!rollData.rolled.isSuccess) {
|
|
|
|
if (rollData.isECNIDefender) {
|
2022-05-14 17:28:27 +02:00
|
|
|
rollData.possession.data.compteur--
|
|
|
|
} else {
|
|
|
|
rollData.possession.data.compteur++
|
|
|
|
}
|
|
|
|
let update = { _id: rollData.possession._id, "data.compteur": rollData.possession.data.compteur }
|
2022-05-14 20:25:47 +02:00
|
|
|
await actor.updateEmbeddedDocuments('Item', [update])
|
2021-11-21 12:04:42 +01:00
|
|
|
}
|
2022-07-02 01:41:55 +02:00
|
|
|
|
2022-05-14 17:28:27 +02:00
|
|
|
this.updateEtatPossession(rollData.possession)
|
|
|
|
await RdDResolutionTable.displayRollData(rollData, this, 'chat-resultat-possession.html')
|
2022-07-02 01:41:55 +02:00
|
|
|
if (rollData.possession.isPosseder || rollData.possession.isConjurer) {
|
2022-05-14 20:39:01 +02:00
|
|
|
actor.deleteEmbeddedDocuments("Item", [rollData.possession._id])
|
|
|
|
}
|
2021-11-21 12:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2022-07-02 01:41:55 +02:00
|
|
|
static async onDefensePossession(attackerId, defenderId, possessionId) {
|
2022-05-14 17:28:27 +02:00
|
|
|
let attacker = game.actors.get(attackerId)
|
|
|
|
let defender = game.actors.get(defenderId)
|
2022-07-02 01:41:55 +02:00
|
|
|
let possession = attacker.getPossession(possessionId) ?? defender.getPossession(possessionId) ;
|
|
|
|
if (!possession) {
|
|
|
|
ui.notifications.warn("Une erreur s'est produite : Aucune possession trouvée !!")
|
|
|
|
return
|
2022-05-14 17:28:27 +02:00
|
|
|
}
|
2021-11-21 12:04:42 +01:00
|
|
|
// Update for draconic roll
|
|
|
|
let rollData = {
|
2022-07-02 01:41:55 +02:00
|
|
|
mode: "conjuration",
|
|
|
|
isECNIDefender: defender.type == "entite",
|
|
|
|
possession: duplicate(possession),
|
|
|
|
attacker: attacker,
|
|
|
|
defender: defender,
|
|
|
|
competence: defender.getDraconicOuPossession(),
|
|
|
|
selectedCarac: defender.data.data.carac.reve,
|
2021-11-21 12:04:42 +01:00
|
|
|
}
|
2022-07-02 01:41:55 +02:00
|
|
|
|
|
|
|
if (rollData.isECNIDefender) {
|
2022-05-14 17:28:27 +02:00
|
|
|
rollData.competence.data.defaut_carac = "reve"
|
|
|
|
rollData.forceCarac = { 'reve': { label: "Rêve", value: defender.data.data.carac.reve.value } }
|
2022-07-02 01:41:55 +02:00
|
|
|
} else {
|
|
|
|
rollData.competence.data.defaut_carac = 'reve-actuel'
|
|
|
|
rollData.forceCarac = { 'reve-actuel': { label: "Rêve Actuel", value: defender.getReveActuel() } }
|
2022-05-14 17:28:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const dialog = await RdDRoll.create(defender, rollData,
|
2021-11-21 12:04:42 +01:00
|
|
|
{
|
|
|
|
html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-defense-possession.html',
|
2022-07-02 01:41:55 +02:00
|
|
|
options: { height: 450 }
|
2021-11-21 12:04:42 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'conjurer',
|
|
|
|
label: 'Conjurer une Possession',
|
|
|
|
callbacks: [
|
2022-01-29 18:50:37 +01:00
|
|
|
{ action: async r => await this.resultConjuration(r) }
|
2021-11-21 12:04:42 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
);
|
2022-05-14 17:28:27 +02:00
|
|
|
dialog.render(true)
|
2021-11-21 12:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2022-07-02 01:41:55 +02:00
|
|
|
static async onAttaquePossession(attacker, competence, possession = undefined) {
|
2022-05-14 17:28:27 +02:00
|
|
|
const target = RdDCombat.getTarget()
|
2021-11-11 09:37:00 +01:00
|
|
|
if (target == undefined) {
|
|
|
|
ui.notifications.warn((game.user.targets?.size ?? 0) > 1
|
|
|
|
? "Vous devez choisir <strong>une seule</strong> cible à posséder!"
|
|
|
|
: "Vous devez choisir une cible à posséder!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-02 01:41:55 +02:00
|
|
|
const defender = target.actor;
|
|
|
|
possession = duplicate(possession ?? this.searchPossessionFromEntite(attacker, defender) ??(await this.createPossession(attacker, defender)));
|
|
|
|
|
2022-05-14 17:28:27 +02:00
|
|
|
this.updateEtatPossession(possession)
|
2021-11-11 09:37:00 +01:00
|
|
|
let rollData = {
|
2022-07-02 01:41:55 +02:00
|
|
|
mode: "possession",
|
|
|
|
isECNIDefender: defender.type == "entite",
|
|
|
|
competence: competence,
|
2021-11-21 12:04:42 +01:00
|
|
|
possession: possession,
|
2022-07-02 01:41:55 +02:00
|
|
|
attacker: attacker,
|
|
|
|
defender: defender
|
2021-11-11 09:37:00 +01:00
|
|
|
};
|
2022-07-02 01:41:55 +02:00
|
|
|
if (attacker.isCreature()) {
|
2022-05-14 17:28:27 +02:00
|
|
|
RdDItemCompetenceCreature.setRollDataCreature(rollData)
|
2021-11-11 09:37:00 +01:00
|
|
|
}
|
|
|
|
|
2022-07-02 01:41:55 +02:00
|
|
|
const dialog = await RdDRoll.create(attacker, rollData,
|
2021-11-11 09:37:00 +01:00
|
|
|
{
|
|
|
|
html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-competence.html',
|
|
|
|
options: { height: 540 }
|
|
|
|
}, {
|
|
|
|
name: 'jet-possession',
|
2022-07-02 01:41:55 +02:00
|
|
|
label: rollData.isECNIDefender ? 'Conjurer la possession' : 'Possession',
|
2021-11-11 09:37:00 +01:00
|
|
|
callbacks: [
|
2022-01-29 18:50:37 +01:00
|
|
|
{ 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) },
|
2021-11-11 09:37:00 +01:00
|
|
|
]
|
|
|
|
});
|
2022-05-14 17:28:27 +02:00
|
|
|
dialog.render(true)
|
2021-11-11 09:37:00 +01:00
|
|
|
}
|
|
|
|
|
2022-07-02 01:41:55 +02:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
static async _onRollPossession(rollData, isSuccess) {
|
|
|
|
rollData.possession.isSuccess = isSuccess;
|
|
|
|
this.updateEtatPossession(rollData.possession);
|
|
|
|
await RdDResolutionTable.displayRollData(rollData, this, 'chat-resultat-possession.html');
|
2022-06-25 18:59:20 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 09:37:00 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-07-02 01:41:55 +02:00
|
|
|
static async createPossession(attacker, defender) {
|
2022-05-14 17:28:27 +02:00
|
|
|
let possessionData = {
|
|
|
|
name: "Possession en cours de " + attacker.name, type: 'possession',
|
2021-11-21 12:04:42 +01:00
|
|
|
img: "systems/foundryvtt-reve-de-dragon/icons/entites/possession2.webp",
|
2022-05-14 17:28:27 +02:00
|
|
|
data: { description: "", typepossession: attacker.name, possede: false, possessionid: randomID(16), possesseurid: attacker.data._id, possedeid: defender.data._id, date: 0, compteur: 0 }
|
2021-11-21 12:04:42 +01:00
|
|
|
}
|
2022-05-14 17:28:27 +02:00
|
|
|
// Creates only the possession on the personnage side
|
|
|
|
let poss = await defender.createEmbeddedDocuments('Item', [possessionData])
|
|
|
|
return duplicate(poss[0])
|
2021-11-11 09:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|