foundryvtt-reve-de-dragon/module/rdd-possession.js
2022-06-12 08:17:59 +02:00

192 lines
7.2 KiB
JavaScript

/* -------------------------------------------- */
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 {
/* -------------------------------------------- */
static init() {
}
/* -------------------------------------------- */
static searchPossessionFromEntite( attacker, defender) {
let poss = attacker.data.items.find( poss => poss.type == 'possession' && poss.data.data.possedeid == defender.data._id);
if (!poss) {
poss = defender.data.items.find( poss => poss.type == 'possession' && poss.data.data.possedeid == defender.data._id);
}
return poss && duplicate(poss) || undefined;
}
/* -------------------------------------------- */
static updateEtatPossession( possession ) {
possession.ptsConjuration = 0
possession.ptsPossession = 0
console.log("Possession", possession)
if ( possession.data.compteur > 0) {
possession.ptsPossession = possession.data.compteur
}
if ( possession.data.compteur < 0) {
possession.ptsConjuration = Math.abs(possession.data.compteur)
}
possession.isPosseder = false
possession.isConjurer = false
if (possession.ptsPossession >= 2 ) {
possession.isPosseder = true
}
if (possession.ptsConjuration >= 2 ) {
possession.isConjurer = true
}
}
/* -------------------------------------------- */
static async resultConjuration( rollData) {
let actor = game.actors.get(rollData.possession.data.possedeid)
if ( !rollData.rolled.isSuccess ) {
if( rollData.isECNIDefender) {
rollData.possession.data.compteur--
} else {
rollData.possession.data.compteur++
}
let update = { _id: rollData.possession._id, "data.compteur": rollData.possession.data.compteur }
await actor.updateEmbeddedDocuments('Item', [update])
}
this.updateEtatPossession(rollData.possession)
await RdDResolutionTable.displayRollData(rollData, this, 'chat-resultat-possession.html')
if ( rollData.possession.isPosseder || rollData.possession.isConjurer) {
actor.deleteEmbeddedDocuments("Item", [rollData.possession._id])
}
}
/* -------------------------------------------- */
static async onDefensePossession( attackerId, defenderId, possessionId) {
let attacker = game.actors.get(attackerId)
let defender = game.actors.get(defenderId)
let possession = attacker.items.find( item => item.type =='possession' && item.data.data.possessionid == possessionId)
if ( !possession ) {
possession = defender.items.find( item => item.type =='possession' && item.data.data.possessionid == possessionId)
if ( !possession) {
ui.notifications.warn("Une erreur s'est produite : Aucune possession trouvée !!")
return
}
}
possession = duplicate(possession)
// Update for draconic roll
let rollData = {
possession: possession
}
rollData.actor = defender
if ( defender.type == "personnage") {
rollData.competence = duplicate(defender.getDraconicOrZero())
rollData.competence.data.defaut_carac = 'reve-actuel'
rollData.forceCarac = { 'reve-actuel': { label: "Rêve Actuel", value: defender.getReveActuel() } }
rollData.selectedCarac = defender.data.data.carac.reve
rollData.isECNIDefender = false
} else {
rollData.competence = duplicate(defender.getCompetence("Possession"))
rollData.competence.data.defaut_carac = "reve"
rollData.forceCarac = { 'reve': { label: "Rêve", value: defender.data.data.carac.reve.value } }
rollData.selectedCarac = defender.data.data.carac.reve
rollData.isECNIDefender = true
//RdDItemCompetenceCreature.setRollDataCreature( rollData )
}
rollData.mode = "conjuration"
rollData.possesseur = attacker.name
const dialog = await RdDRoll.create(defender, rollData,
{
html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-defense-possession.html',
options: { height: 400 }
},
{
name: 'conjurer',
label: 'Conjurer une Possession',
callbacks: [
{ action: async r => await this.resultConjuration(r) }
]
}
);
dialog.render(true)
}
/* -------------------------------------------- */
static async _onRollPossession( rollData, isSuccess ) {
let possession = rollData.possession
possession.isSuccess = isSuccess
this.updateEtatPossession( possession)
await RdDResolutionTable.displayRollData(rollData, this, 'chat-resultat-possession.html');
}
/* -------------------------------------------- */
static async managePossession(attacker, competence, possession=undefined) {
const target = RdDCombat.getTarget()
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;
}
const defender = target.actor
if ( !possession) {
possession = this.searchPossessionFromEntite( attacker, defender)
if ( !possession) {
possession = await this.createPossession(attacker, defender)
}
}
possession = duplicate(possession)
this.updateEtatPossession(possession)
let rollData = {
competence: competence,
possession: possession,
possede: defender.name,
possesseur: attacker.name,
attackerId: attacker.data._id,
defenderId: defender.data._id,
mode: "possession"
};
if ( attacker.isCreature()) {
RdDItemCompetenceCreature.setRollDataCreature(rollData)
}
const dialog = await RdDRoll.create( attacker, rollData,
{
html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-competence.html',
options: { height: 540 }
}, {
name: 'jet-possession',
label: '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) },
]
});
dialog.render(true)
}
/* -------------------------------------------- */
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",
data: { description: "", typepossession: attacker.name, possede: false, possessionid: randomID(16), possesseurid: attacker.data._id, possedeid: defender.data._id, date: 0, compteur: 0 }
}
// Creates only the possession on the personnage side
let poss = await defender.createEmbeddedDocuments('Item', [possessionData])
return duplicate(poss[0])
}
}