Appel à la chance sur les défenses

Suppressions de ChatMessage selon contenu

En ayant un <div id=""> avec id unique, on peut retrouver et supprimer
les messages obsoletes (par exemple, les choix dans les combats).
This commit is contained in:
Vincent Vandemeulebrouck 2021-01-07 00:32:22 +01:00
parent 212a94afee
commit 47fb2d511e
6 changed files with 212 additions and 67 deletions

View File

@ -3,6 +3,12 @@
* Class providing helper methods to get the list of users, and
*/
export class ChatUtility {
static removeMyChatMessageContaining(part) {
const toDelete = game.messages.filter(it => it.user._id == game.user._id)
.filter(it => it.data.content.includes(part))
.forEach(it => it.delete());
}
/* -------------------------------------------- */
static chatWithRollMode(chatOptions, name) {

View File

@ -94,7 +94,14 @@ export class RdDCombat {
/* -------------------------------------------- */
static registerChatCallbacks(html) {
for (let button of ['#parer-button', '#esquiver-button', '#particuliere-attaque', '#encaisser-button']) {
for (let button of [
'#parer-button',
'#esquiver-button',
'#particuliere-attaque',
'#encaisser-button',
'#appel-chance-defense',
'#appel-destinee-defense',
]) {
html.on("click", button, event => {
event.preventDefault();
RdDCombat.createForEvent(event).onEvent(button, event);
@ -119,22 +126,81 @@ export class RdDCombat {
/* -------------------------------------------- */
async onEvent(button, event) {
let rollData = game.system.rdd.rollDataHandler[this.attackerId];
if (!rollData) {
let attackerRoll = game.system.rdd.rollDataHandler.attaques[this.attackerId];
if (!attackerRoll) {
ui.notifications.warn("Action automatisée impossible, le jet de l'attaquant a été perdu (suite à un raffraichissement?)")
return;
}
const defenderTokenId = event.currentTarget.attributes['data-defenderTokenId'].value;
let defenderRoll = this._consumeDefense(attackerRoll.passeArme);
switch (button) {
case '#particuliere-attaque': return await this.choixParticuliere(rollData, event.currentTarget.attributes['data-mode'].value);
case '#particuliere-attaque': return await this.choixParticuliere(attackerRoll, event.currentTarget.attributes['data-mode'].value);
case '#parer-button': {
const armeId = event.currentTarget.attributes['data-armeid'];
return this.parade(rollData, armeId?.value);
return this.parade(attackerRoll, armeId?.value);
}
case '#esquiver-button': return this.esquive(rollData);
case '#encaisser-button': return this.encaisser(rollData, event.currentTarget.attributes['data-defenderTokenId'].value);
case '#esquiver-button': return this.esquive(attackerRoll);
case '#encaisser-button': return this.encaisser(attackerRoll, defenderTokenId);
case '#appel-chance-defense': return this.defender.rollAppelChance(
() => this.rejouerDefense(defenderRoll, { chance: true }),
() => this.afficherOptionsDefense(attackerRoll, { chance: true }));
case '#appel-destinee-defense': return this.defender.appelDestinee(
() => this.defenseSignificative(defenderRoll),
() => this.afficherOptionsDefense(attackerRoll, { destinee: true }));
}
}
_consumeDefense(passeArme) {
let defenderRoll = game.system.rdd.rollDataHandler.defenses[passeArme];
game.system.rdd.rollDataHandler.defenses[passeArme] = undefined;
return defenderRoll;
}
_storeDefense(defenderRoll) {
game.system.rdd.rollDataHandler.defenses[defenderRoll.passeArme] = defenderRoll;
}
rejouerDefense(defenderRoll, tentatives) {
ui.notifications.info("La défense est rejouée grâce à la chance")
const attackerRoll = defenderRoll.attackerRoll;
this.removeChatMessageActionsPasseArme(attackerRoll.passeArme);
this.addTentatives(attackerRoll, tentatives);
if (defenderRoll.arme) {
this.parade(attackerRoll, defenderRoll.arme._id);
}
else{
this.esquive(attackerRoll);
}
}
afficherOptionsDefense(attackerRoll, tentatives) {
ui.notifications.info("La chance n'est pas avec vous")
this._sendMessageDefense(attackerRoll, tentatives);
}
defenseSignificative(defenderRoll){
ui.notifications.info('defense significative grâce à la destinée')
const attackerRoll = defenderRoll.attackerRoll;
RdDResolutionTable.forceSignificative(defenderRoll.rolled);
this.removeChatMessageActionsPasseArme(attackerRoll.passeArme);
if (defenderRoll.arme) {
this._onParadeNormale(defenderRoll);
}
else{
this._onEsquiveNormale(defenderRoll);
}
}
/* -------------------------------------------- */
removeChatMessageActionsPasseArme(passeArme) {
ChatUtility.removeMyChatMessageContaining(`<div data-passearme="${passeArme}">`);
}
/* -------------------------------------------- */
static isEchec(rollData) {
switch (rollData.surprise) {
@ -185,6 +251,7 @@ export class RdDCombat {
label: 'Attaque: ' + (arme?.name ?? competence.name),
callbacks: [
this.attacker.createCallbackExperience(),
{ action: r => this.removeChatMessageActionsPasseArme(r.passeArme) },
{ condition: r => (RdDCombat.isReussite(r) && !RdDCombat.isParticuliere(r)), action: r => this._onAttaqueNormale(r) },
{ condition: RdDCombat.isParticuliere, action: r => this._onAttaqueParticuliere(r) },
{ condition: RdDCombat.isEchec, action: r => this._onAttaqueEchec(r) },
@ -197,10 +264,12 @@ export class RdDCombat {
/* -------------------------------------------- */
_prepareAttaque(competence, arme) {
let rollData = {
passeArme: randomID(16),
coupsNonMortels: false,
competence: competence,
surprise: this.attacker.getSurprise(),
surpriseDefenseur: this.defender.getSurprise()
surpriseDefenseur: this.defender.getSurprise(),
tentatives: { chance: false, defense: false }
};
if (this.attacker.isCreature()) {
@ -229,7 +298,7 @@ export class RdDCombat {
}
message += `<br><a class='chat-card-button' id='particuliere-attaque' data-mode='finesse' data-attackerId='${this.attackerId}'>Attaquer en Finesse</a>`;
}
game.system.rdd.rollDataHandler[this.attackerId] = rollData;
game.system.rdd.rollDataHandler.attaques[this.attackerId] = duplicate(rollData);
// TODO: use a dialog?
ChatMessage.create({ content: message, whisper: ChatMessage.getWhisperRecipients(this.attacker.name) });
}
@ -241,7 +310,7 @@ export class RdDCombat {
rollData.dmg = RdDBonus.dmg(rollData, this.attacker.getBonusDegat(), this.defender.isEntiteCauchemar());
// Save rollData for defender
game.system.rdd.rollDataHandler[this.attackerId] = duplicate(rollData);
game.system.rdd.rollDataHandler.attaques[this.attackerId] = duplicate(rollData);
rollData.show = {
cible: this.target ? this.defender.data.name : 'la cible',
@ -254,45 +323,35 @@ export class RdDCombat {
}
if (this.target) {
this._sendMessageDefense(rollData);
await this._sendMessageDefense(rollData);
}
}
/* -------------------------------------------- */
_sendMessageDefense(rollData) {
console.log("RdDCombat._sendMessageDefense", rollData, " / ", this.attacker, this.target, this.attackerId, rollData.competence.data.categorie);
async _sendMessageDefense(attackerRoll, tentatives = {}) {
console.log("RdDCombat._sendMessageDefense", attackerRoll, tentatives, " / ", this.attacker, this.target, this.attackerId, attackerRoll.competence.data.categorie);
let message = this._buildMessageDefense(rollData);
// encaisser
message += this._buildMessageEncaisser(rollData) + "</span>";
this.removeChatMessageActionsPasseArme(attackerRoll.passeArme);
RdDCombat._sendRollMessage(this.attacker, this.defender, this.defenderTokenId, "msg_defense", message, rollData);
this.addTentatives(attackerRoll, tentatives);
let message = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-demande-defense.html`, {
passeArme: attackerRoll.passeArme,
tentatives: attackerRoll.tentatives,
surprise: this.defender.getSurprise(),
defender: this.defender,
attackerId: this.attackerId,
defenderTokenId: this.defenderTokenId,
mainsNues: attackerRoll.dmg.mortalite != 'mortel' && this.defender.getCompetence("Corps à corps"),
armes: this._filterArmesParade(this.defender.data.items, attackerRoll.competence, attackerRoll.arme),
dmg: attackerRoll.dmg
});
RdDCombat._sendRollMessage(this.attacker, this.defender, this.defenderTokenId, "msg_defense", message, attackerRoll);
}
/* -------------------------------------------- */
_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, rollData.arme)) {
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' && this.defender.getCompetence("Corps à corps")) {
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 != 'tir') {
message += "<br><a class='chat-card-button' id='esquiver-button' data-attackerId='" + this.attackerId + "' data-defenderTokenId='" + this.defenderTokenId + "'>Esquiver</a>";
}
}
return message;
}
/* -------------------------------------------- */
_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>";
addTentatives(attackerRoll, tentatives) {
mergeObject(attackerRoll.tentatives, tentatives, { overwrite: true });
}
/* -------------------------------------------- */
@ -355,6 +414,7 @@ export class RdDCombat {
label: 'Parade: ' + (arme ? arme.name : rollData.competence.name),
callbacks: [
this.defender.createCallbackExperience(),
{ action: r => this.removeChatMessageActionsPasseArme(r.passeArme) },
{ condition: RdDCombat.isReussite, action: r => this._onParadeNormale(r) },
{ condition: RdDCombat.isParticuliere, action: r => this._onParadeParticuliere(r) },
{ condition: RdDCombat.isEchec, action: r => this._onParadeEchec(r) },
@ -370,6 +430,7 @@ export class RdDCombat {
const armeAttaque = attackerRoll.arme;
let rollData = {
passeArme: attackerRoll.passeArme,
forceValue: this.defender.getForceValue(),
diffLibre: attackerRoll.diffLibre,
attackerRoll: attackerRoll,
@ -403,7 +464,7 @@ export class RdDCombat {
if (!rollData.attackerRoll.isPart) {
// TODO: attaquant doit jouer résistance et peut être désarmé p132
ChatUtility.chatWithRollMode({
content: `L'attaquant doit jouer résistance et peut être désarmé (p132)`
content: `(à gérer) L'attaquant doit jouer résistance et peut être désarmé (p132)`
}, this.defender.name)
}
}
@ -416,7 +477,6 @@ export class RdDCombat {
await this.computeDeteriorationArme(rollData);
await RdDResolutionTable.displayRollData(rollData, this.defender, 'chat-resultat-parade.html');
}
/* -------------------------------------------- */
@ -435,11 +495,11 @@ export class RdDCombat {
async _onParadeEchec(rollData) {
console.log("RdDCombat._onParadeEchec >>>", rollData);
await this.computeRecul(rollData);
await RdDResolutionTable.displayRollData(rollData, this.defender, 'chat-resultat-parade.html');
this._sendMessageEncaisser(rollData.attackerRoll);
this._storeDefense(rollData);
this.removeChatMessageActionsPasseArme(rollData.passeArme);
this._sendMessageDefense(rollData.attackerRoll, { defense: true });
}
/* -------------------------------------------- */
@ -458,6 +518,7 @@ export class RdDCombat {
label: 'Esquiver',
callbacks: [
this.defender.createCallbackExperience(),
{ action: r => this.removeChatMessageActionsPasseArme(r.passeArme) },
{ condition: RdDCombat.isReussite, action: r => this._onEsquiveNormale(r) },
{ condition: RdDCombat.isParticuliere, action: r => this._onEsquiveParticuliere(r) },
{ condition: RdDCombat.isEchec, action: r => this._onEsquiveEchec(r) },
@ -470,6 +531,7 @@ export class RdDCombat {
/* -------------------------------------------- */
_prepareEsquive(attackerRoll, competence) {
let rollData = {
passeArme: attackerRoll.passeArme,
forceValue: this.defender.getForceValue(),
diffLibre: attackerRoll.diffLibre,
attackerRoll: attackerRoll,
@ -509,7 +571,7 @@ export class RdDCombat {
// https://gitlab.com/LeRatierBretonnien/foundryvtt-reve-de-dragon/-/issues/85
console.log("RdDCombat._onEsquiveEchecTotal >>>", rollData);
let chatOptions = {
content: "<strong>Echec total à l'esquive'!</strong> "
content: "<strong>Echec total à l'esquive!</strong> "
+ await RdDRollTables.getMaladresse({ arme: false })
}
ChatUtility.chatWithRollMode(chatOptions, this.defender.name)
@ -518,11 +580,11 @@ export class RdDCombat {
async _onEsquiveEchec(rollData) {
console.log("RdDCombat._onEsquiveEchec >>>", rollData);
await this.computeRecul(rollData);
await RdDResolutionTable.displayRollData(rollData, this.defender, 'chat-resultat-esquive.html');
this._sendMessageEncaisser(rollData.attackerRoll);
this._storeDefense(rollData);
this.removeChatMessageActionsPasseArme(rollData.passeArme);
this._sendMessageDefense(rollData.attackerRoll, { defense: true })
}
/* -------------------------------------------- */
@ -613,13 +675,16 @@ export class RdDCombat {
}
/* -------------------------------------------- */
encaisser(attackerRoll, defenderTokenId) {
async encaisser(attackerRoll, defenderTokenId) {
defenderTokenId = defenderTokenId || this.defenderTokenId;
console.log("RdDCombat.encaisser >>>", attackerRoll, defenderTokenId);
if (game.user.isGM) { // Current user is the GM -> direct access
attackerRoll.attackerId = this.attackerId;
attackerRoll.defenderTokenId = defenderTokenId;
let defenderRoll = this._consumeDefense(attackerRoll.passeArme);
await this.computeRecul(defenderRoll);
this.defender.encaisserDommages(attackerRoll, this.attacker);
} else { // Emit message for GM
game.socket.emit("system.foundryvtt-reve-de-dragon", {

View File

@ -21,6 +21,7 @@ import { RdDResolutionTable } from "./rdd-resolution-table.js";
import { RdDTokenHud } from "./rdd-token-hud.js";
import { RdDCommands } from "./rdd-commands.js";
import { RdDCombat } from "./rdd-combat.js";
import { ChatUtility } from "./chat-utility.js";
/* -------------------------------------------- */
/* Foundry VTT Initialization */
@ -104,7 +105,10 @@ Hooks.once("init", async function() {
// Create useful storage space
game.system.rdd = {
rollDataHandler: {},
rollDataHandler: {
attaques: {},
defenses: {}
},
TMRUtility: TMRUtility
}
@ -213,9 +217,7 @@ Hooks.once("init", async function() {
/* -------------------------------------------- */
function messageDeBienvenue(){
game.messages
.filter(it => it.user._id == game.user._id && it.data.content.match(/^<div id="message-bienvenue-rdd/))
.forEach(it => it.delete());
ChatUtility.removeMyChatMessageContaining('<div id="message-bienvenue-rdd">');
ChatMessage.create( {
user: game.user._id,
whisper: [game.user._id],

View File

@ -127,7 +127,7 @@ export class RdDResolutionTable {
/* -------------------------------------------- */
static _updateChancesFactor(chances, diviseur) {
if (diviseur && diviseur > 1) {
let newScore = Math.floor(Number(chances.score) / diviseur);
let newScore = Math.floor(chances.score / diviseur);
mergeObject(chances, this._computeCell(null, newScore), { overwrite: true });
}
}
@ -135,10 +135,14 @@ export class RdDResolutionTable {
/* -------------------------------------------- */
static _updateChancesWithBonus(chances, bonus) {
if (bonus) {
let newScore = Number(chances.score) + Number(bonus);
let newScore = chances.score + bonus;
mergeObject(chances, this._computeCell(null, newScore), { overwrite: true });
}
}
static forceSignificative(chances) {
chances.roll = Math.floor(chances.score /2);
mergeObject(chances, reussites.find(x => x.code == 'sign'), { overwrite: true });
}
/* -------------------------------------------- */
static async rollChances(chances) {
@ -146,7 +150,7 @@ export class RdDResolutionTable {
myRoll.showDice = chances.showDice;
await RdDDice.show(myRoll);
chances.roll = myRoll.total;
mergeObject(chances, this._computeReussite(chances, chances.roll));
mergeObject(chances, this._computeReussite(chances, chances.roll), { overwrite: true });
return chances;
}

View File

@ -206,6 +206,7 @@ export class RdDUtility {
'systems/foundryvtt-reve-de-dragon/templates/hud-actor-attaque.html',
// messages tchat
'systems/foundryvtt-reve-de-dragon/templates/chat-infojet.html',
'systems/foundryvtt-reve-de-dragon/templates/chat-demande-defense.html',
'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-appelchance.html',
'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-attaque.html',
'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-parade.html',
@ -784,7 +785,7 @@ export class RdDUtility {
}
if ((game.user.isGM && !defenderToken.actor.hasPlayerOwner) || (defenderToken.actor.hasPlayerOwner && (game.user.character.id == defenderToken.actor.data._id))) {
//console.log("User is pushing message...", game.user.name);
game.system.rdd.rollDataHandler[data.attackerId] = duplicate(data.rollData);
game.system.rdd.rollDataHandler.attaques[data.attackerId] = duplicate(data.rollData);
data.whisper = [game.user];
data.blind = true;
data.rollMode = "blindroll";
@ -886,9 +887,12 @@ export class RdDUtility {
/* -------------------------------------------- */
static _handleMsgEncaisser(data) {
if (game.user.isGM) { // Seul le GM effectue l'encaissement sur la fiche
let attackerRoll = game.system.rdd.rollDataHandler[data.attackerId]; // Retrieve the rolldata from the store
let defenderToken = canvas.tokens.get(data.defenderTokenId);
defenderToken.actor.encaisserDommages(attackerRoll);
let attackerRoll = game.system.rdd.rollDataHandler.attaques[data.attackerId]; // Retrieve the rolldata from the store
game.system.rdd.rollDataHandler.attaques[data.attackerId] = undefined;
game.system.rdd.rollDataHandler.defenses[attackerRoll.passeArme] = undefined;
let defender = canvas.tokens.get(data.defenderTokenId).actor;
defender.encaisserDommages(attackerRoll);
}
}

View File

@ -0,0 +1,64 @@
<div data-passearme="{{passeArme}}">
{{#if (eq surprise 'totale')}}
<span><strong>{{defender.name}}</strong> est totalement surpris</span>
{{else if tentatives.defense}}
<span><strong>{{defender.name}}</strong> doit</span>
{{else}}
<span><strong>{{defender.name}}</strong> doit se défendre
{{#if (eq surprise 'demi')}} avec une significative {{/if}} :
</span>
{{/if}}
<span class='chat-card-button-area'>
<br>
{{#unless (eq surprise 'totale')}}
{{#if tentatives.defense}}
<br>
{{#unless tentatives.chance}}
{{#if (eq defender.data.type 'personnage')}}
<a class='chat-card-button' id='appel-chance-defense' data-attackerId='{{attackerId}}'
data-defenderTokenId='{{defenderTokenId}}'>Faire appel à la chance</a>
</a>
<br>
{{/if}}
{{/unless}}
{{#if (eq defender.data.type 'personnage')}}
{{#if (gt defender.data.data.compteurs.destinee.value 0)}}
<a class='chat-card-button' id='appel-destinee-defense' data-attackerId='{{attackerId}}'
data-defenderTokenId='{{defenderTokenId}}'>Utiliser la destinée</a>
</a>
<br>
{{/if}}
{{/if}}
{{else}}
{{#each armes as |arme key|}}
<a class='chat-card-button' id='parer-button' data-attackerId='{{../attackerId}}'
data-defenderTokenId='{{../defenderTokenId}}' data-armeid='{{arme._id }}'>Parer avec {{arme.name}}
</a>
<br>
{{/each}}
{{#if mainsNues}}
<a class='chat-card-button' id='parer-button' data-attackerId='{{attackerId}}'
data-defenderTokenId='{{defenderTokenId}}'>
Parer à mains nues
</a>
<br>
{{/if}}
{{#if (ne attaqueCategorie 'tir')}}
<a class='chat-card-button' id='esquiver-button' data-attackerId='{{attackerId}}'
data-defenderTokenId='{{defenderTokenId}}'>
Esquiver
</a>
<br>
{{/if}}
{{/if}}
{{/unless}}
<a class='chat-card-button' id='encaisser-button' data-attackerId='{{attackerId}}'
data-defenderTokenId='{{defenderTokenId}}'>
Encaisser à {{#if (eq dmg.mortalite 'non-mortel')~}}
({{numberFormat dmg.total decimals=0 sign=true}})
{{~else~}}
{{numberFormat dmg.total decimals=0 sign=true}}
{{~/if}} !
</a>
</span>
</div>