Séparation défense et encaissement

This commit is contained in:
Vincent Vandemeulebrouck 2020-12-18 22:15:17 +01:00
parent 3975d7f529
commit ea4b7a7b22

View File

@ -20,7 +20,9 @@ export class RdDCombat {
if (target == undefined) {
ui.notifications.warn("Vous devez choisir une seule cible à attaquer!");
}
return this.create(attacker, target ? target.actor : undefined, target)
const defender = target ? target.actor : undefined;
const defenderTokenId = target ? target.data._id : undefined;
return this.create(attacker, defender, defenderTokenId, target)
}
/* -------------------------------------------- */
@ -34,8 +36,8 @@ export class RdDCombat {
}
/* -------------------------------------------- */
static create(attacker, defender, target = undefined) {
return new RdDCombat(attacker, defender, target)
static create(attacker, defender, defenderTokenId, target = undefined) {
return new RdDCombat(attacker, defender, defenderTokenId, target)
}
/* -------------------------------------------- */
@ -45,22 +47,40 @@ export class RdDCombat {
const dataDefenderTokenId = event.currentTarget.attributes['data-defenderTokenId'];
if (dataDefenderTokenId) {
let defenderToken = canvas.tokens.get(dataDefenderTokenId.value);
const defenderTokenId = dataDefenderTokenId.value;
let defenderToken = canvas.tokens.get(defenderTokenId);
let defender = defenderToken.actor;
return this.create(attacker, defender);
return RdDCombat.create(attacker, defender, defenderTokenId);
}
return this.createUsingTarget(attacker)
return RdDCombat.createUsingTarget(attacker)
}
/* -------------------------------------------- */
constructor(attacker, defender, target) {
this.attacker = attacker;
this.defender = defender;
this.target = target;
this.attackerId = this.attacker.data._id;
this.defenderId = this.defender.data._id;
this.defenderTokenId = target ? target.data._id : undefined;
static _sendRollMessage(sender, recipient, defenderTokenId, topic, message, rollData) {
let chatMessage = {
content: message,
whisper: ChatUtility.getWhisperRecipients("blindroll", recipient.name),
};
// envoyer le message au destinataire
if (!game.user.isGM || recipient.hasPlayerOwner) {
let data = {
attackerId: sender ? sender.data._id : undefined,
defenderId: recipient ? recipient.data._id : undefined,
defenderTokenId: defenderTokenId,
rollData: duplicate(rollData),
rollMode: true
};
mergeObject(data, chatMessage);
game.socket.emit("system.foundryvtt-reve-de-dragon", { msg: topic, data: data});
} else {
chatMessage.whisper = [game.user];
}
if (game.user.isGM) { // Always push the message to the MJ
ChatMessage.create(chatMessage);
}
}
/* -------------------------------------------- */
@ -73,6 +93,16 @@ export class RdDCombat {
}
}
/* -------------------------------------------- */
constructor(attacker, defender, defenderTokenId, target) {
this.attacker = attacker;
this.defender = defender;
this.target = target;
this.attackerId = this.attacker.data._id;
this.defenderId = this.defender.data._id;
this.defenderTokenId = defenderTokenId;
}
/* -------------------------------------------- */
async onEvent(button, event) {
let rollData = game.system.rdd.rollDataHandler[this.attackerId];
@ -91,7 +121,6 @@ export class RdDCombat {
}
}
/* -------------------------------------------- */
static isEchec(rollData) {
switch (rollData.surprise) {
@ -240,55 +269,43 @@ export class RdDCombat {
ChatUtility.chatWithRollMode(chatOptions, this.attacker.name)
if (this.target) {
this._messageDefenseur(rollData);
this._sendMessageDefense(rollData);
}
}
/* -------------------------------------------- */
_messageDefenseur(rollData) {
console.log("RdDCombat._messageDefenseur", rollData, " / ", this.attacker, this.target, this.target.actor.isToken, this.attacker.data._id, rollData.competence.data.categorie);
_sendMessageDefense(rollData) {
console.log("RdDCombat._sendMessageDefense", rollData, " / ", this.attacker, this.target, this.attackerId, rollData.competence.data.categorie);
let content = "<strong>" + this.defender.name + "</strong> doit se défendre :<span class='chat-card-button-area'>";
let message = this._buildMessageDefense(rollData);
// encaisser
message += this._buildMessageEncaisser(rollData) + "</span>";
RdDCombat._sendRollMessage(this.attacker, this.defender, this.defenderTokenId, "msg_defense", message, rollData);
}
_buildMessageDefense(rollData) {
let message = "<strong>" + this.defender.name + "</strong> doit se défendre :<span class='chat-card-button-area'>";
if (this.defender.getSurprise() != 'totale') {
// parades
for (const arme of this._filterArmesParade(this.defender.data.items, rollData.competence.data.categorie)) {
content += "<br><a class='chat-card-button' id='parer-button' data-attackerId='" + this.attackerId + "' data-defenderTokenId='" + this.defenderTokenId + "' data-armeid='" + arme._id + "'>Parer avec " + arme.name + "</a>";
message += "<br><a class='chat-card-button' id='parer-button' data-attackerId='" + this.attackerId + "' data-defenderTokenId='" + this.defenderTokenId + "' data-armeid='" + arme._id + "'>Parer avec " + arme.name + "</a>";
}
// corps à corps
if (rollData.dmg.mortalite != 'mortel') {
content += "<br><a class='chat-card-button' id='parer-button' data-attackerId='" + this.attackerId + "' data-defenderTokenId='" + this.defenderTokenId + "'>Parer à mains nues</a>";
message += "<br><a class='chat-card-button' id='parer-button' data-attackerId='" + this.attackerId + "' data-defenderTokenId='" + this.defenderTokenId + "'>Parer à mains nues</a>";
}
// esquive
if (rollData.competence.data.categorie == 'melee' || rollData.competence.data.categorie == "lancer" || rollData.competence.data.categorie == 'competencecreature') {
content += "<br><a class='chat-card-button' id='esquiver-button' data-attackerId='" + this.attackerId + "' data-defenderTokenId='" + this.defenderTokenId + "'>Esquiver</a>";
message += "<br><a class='chat-card-button' id='esquiver-button' data-attackerId='" + this.attackerId + "' data-defenderTokenId='" + this.defenderTokenId + "'>Esquiver</a>";
}
}
return message;
}
// encaisser
content += "<br><a class='chat-card-button' id='encaisser-button' data-attackerId='" + this.attackerId + "' data-defenderTokenId='" + this.defenderTokenId + "'>Encaisser à " + Misc.toSignedString(rollData.dmg.total) + " !</a>";
content += "</span>"
let defense = {
title: "Défense en combat",
content: content,
whisper: ChatUtility.getWhisperRecipients("blindroll", this.defender.name),
attackerId: this.attackerId,
defenderTokenId: this.defenderTokenId,
rollMode: true,
rollData: duplicate(rollData)
};
// envoyer le message de defense
if (!game.user.isGM || this.defender.hasPlayerOwner) {
game.socket.emit("system.foundryvtt-reve-de-dragon", { msg: "msg_defense", data: defense });
} else {
defense.whisper = [game.user];
}
if (game.user.isGM) { // Always push the message to the MJ
ChatMessage.create(defense);
}
_buildMessageEncaisser(rollData) {
return "<br><a class='chat-card-button' id='encaisser-button' data-attackerId='" + this.attackerId + "' data-defenderTokenId='" + this.defenderTokenId + "'>Encaisser à " + Misc.toSignedString(rollData.dmg.total) + " !</a>";
}
/* -------------------------------------------- */
@ -479,7 +496,8 @@ export class RdDCombat {
await this.computeRecul(rollData, true);
// TODO: gestion message pour chance/encaissement
this.encaisser(rollData.attackerRoll);
this._sendMessageEncaisser(rollData.attackerRoll);
}
/* -------------------------------------------- */
@ -575,7 +593,7 @@ export class RdDCombat {
ChatUtility.chatWithRollMode(chatOptions, this.defender.name)
await this.computeRecul(rollData, true);
this.encaisser(rollData.attackerRoll);
this._sendMessageEncaisser(rollData.attackerRoll);
}
@ -661,11 +679,10 @@ export class RdDCombat {
}
}
/* -------------------------------------------- */
encaisser(attackerRoll) {
// TODO: gestion message pour chance/encaissement
this.encaisser(attackerRoll, this.defenderTokenId);
_sendMessageEncaisser(rollData){
let message = "<strong>" + this.defender.name + "</strong> doit:" + this._buildMessageEncaisser(rollData);
RdDCombat._sendRollMessage(this.attacker, this.defender, this.defenderTokenId, "msg_encaisser", message, rollData);
}
/* -------------------------------------------- */