diff --git a/module/actor.js b/module/actor.js index 0e2e31f9..8669f918 100644 --- a/module/actor.js +++ b/module/actor.js @@ -273,24 +273,24 @@ export class RdDActor extends RdDBaseActor { getDraconicOuPossession() { const possession = this.items.filter(it => it.type == TYPES.competencecreature && it.system.categorie == 'possession') .sort(Misc.descending(it => it.system.niveau)) - .find(it=>true); + .find(it => true); if (possession) { return possession.clone(); } - const draconics = [...this.getDraconicList().filter(it => it.system.niveau >= 0).map(it =>it.clone()), + const draconics = [...this.getDraconicList().filter(it => it.system.niveau >= 0).map(it => it.clone()), POSSESSION_SANS_DRACONIC] .sort(Misc.descending(it => it.system.niveau)); return draconics[0]; } getPossession(possessionId) { - return this.items.find(it => it.type == 'possession' && it.system.possessionid == possessionId); + return this.items.find(it => it.type == TYPES.possession && it.system.possessionid == possessionId); } getPossessions() { - return this.items.filter(it => it.type == 'possession'); + return this.items.filter(it => it.type == TYPES.possession); } getEmpoignades() { - return this.items.filter(it => it.type == 'empoignade'); + return this.items.filter(it => it.type == TYPES.empoignade); } getDemiReve() { return this.system.reve.tmrpos.coord; @@ -961,7 +961,7 @@ export class RdDActor extends RdDBaseActor { /* -------------------------------------------- */ async updateCompetence(idOrName, compValue) { - const competence = this.getCompetence(idOrName); + const competence = this.getCompetence(idOrName); if (competence) { const toNiveau = compValue ?? RdDItemCompetence.getNiveauBase(competence.system.categorie, competence.getCategories()); this.notifyCompetencesTronc(competence, toNiveau); @@ -1362,21 +1362,38 @@ export class RdDActor extends RdDBaseActor { /* -------------------------------------------- */ async finDeRound(options = { terminer: false }) { + await this.$finDeRoundSuppressionEffetsTermines(options); + await this.$finDeRoundBlessuresGraves(); + await this.$finDeRoundSupprimerObsoletes(); + } + + async $finDeRoundSuppressionEffetsTermines(options) { for (let effect of this.getEffects()) { if (effect.duration.type !== 'none' && (effect.duration.remaining <= 0 || options.terminer)) { await effect.delete(); ChatMessage.create({ content: `${this.name} n'est plus ${Misc.lowerFirst(game.i18n.localize(effect.system.label))} !` }); } } + } + + async $finDeRoundBlessuresGraves() { if (this.isPersonnage() || this.isCreature()) { - const nbGraves = this.filterItems(it => it.isGrave(), 'blessure').length + const nbGraves = this.filterItems(it => it.isGrave(), 'blessure').length; if (nbGraves > 0) { // Gestion blessure graves : -1 pt endurance par blessure grave - await this.santeIncDec("endurance", - nbGraves); + await this.santeIncDec("endurance", -nbGraves); } } } + async $finDeRoundSupprimerObsoletes() { + const obsoletes = [] + .concat(this.itemTypes[TYPES.empoignade].filter(it => it.system.pointsemp <= 0)) + .concat(this.itemTypes[TYPES.possession].filter(it => it.system.compteur < -2 || it.system.compteur > 2)) + .map(it => it.id); + await this.deleteEmbeddedDocuments('Item', obsoletes); + } + /* -------------------------------------------- */ async setSonne(sonne = true) { if (this.isEntite()) { diff --git a/module/actor/base-actor.js b/module/actor/base-actor.js index d5901382..29e82b7e 100644 --- a/module/actor/base-actor.js +++ b/module/actor/base-actor.js @@ -144,6 +144,18 @@ export class RdDBaseActor extends Actor { .forEach(async it => await it.onFinPeriodeTemporel(oldTimestamp, newTimestamp)) } + async creerObjetParMJ(object){ + if (!Misc.isUniqueConnectedGM()) { + RdDBaseActor.remoteActorCall({ + actorId: this.id, + method: 'creerObjetParMJ', + args: [object] + }); + return; + } + await this.createEmbeddedDocuments('Item', [object]) + } + /* -------------------------------------------- */ getFortune() { return Monnaie.getFortune(this.itemTypes['monnaie']); diff --git a/module/chat-utility.js b/module/chat-utility.js index cf707dfc..7c244a4f 100644 --- a/module/chat-utility.js +++ b/module/chat-utility.js @@ -78,11 +78,7 @@ export class ChatUtility { /* -------------------------------------------- */ static async createChatWithRollMode(name, chatOptions) { - return await ChatUtility.createChatMessage(name, game.settings.get("core", "rollMode"), chatOptions); - } - - /* -------------------------------------------- */ - static async createChatMessage(name, rollMode, chatOptions) { + let rollMode = game.settings.get("core", "rollMode") switch (rollMode) { case "blindroll": // GM only if (!game.user.isGM) { diff --git a/module/rdd-empoignade.js b/module/rdd-empoignade.js index 3e46fd0e..83e2b6a4 100644 --- a/module/rdd-empoignade.js +++ b/module/rdd-empoignade.js @@ -4,6 +4,8 @@ import { RdDRoll } from "./rdd-roll.js"; import { RdDItemCompetenceCreature } from "./item-competencecreature.js"; import { ChatUtility } from "./chat-utility.js"; import { STATUSES } from "./settings/status-effects.js"; +import { ReglesOptionelles } from "./settings/regles-optionelles.js"; +import { TYPES } from "./item.js"; /* -------------------------------------------- */ @@ -14,6 +16,47 @@ export class RdDEmpoignade { static init() { } + /* -------------------------------------------- */ + static registerChatCallbacks(html) { + html.on("click", '.defense-empoignade-cac', event => { + const chatMessage = ChatUtility.getChatMessage(event); + const rollData = RdDEmpoignade.readRollEmpoignade(chatMessage); + let defenseMode = event.currentTarget.attributes['data-defense-mode'].value + RdDEmpoignade.onDefenseEmpoignade(rollData, defenseMode, "Corps à corps", "melee") + }); + html.on("click", '.defense-empoignade-esquive', event => { + const chatMessage = ChatUtility.getChatMessage(event); + const rollData = RdDEmpoignade.readRollEmpoignade(chatMessage); + let defenseMode = event.currentTarget.attributes['data-defense-mode'].value + RdDEmpoignade.onDefenseEmpoignade(rollData, defenseMode, "Esquive", "derobee") + }); + html.on("click", '.empoignade-poursuivre', event => { + let attackerId = event.currentTarget.attributes['data-attackerId'].value + let defenderId = event.currentTarget.attributes['data-defenderId'].value + RdDEmpoignade.onAttaqueEmpoignadeValidee(game.actors.get(attackerId), game.actors.get(defenderId)) + }); + html.on("click", '.empoignade-entrainer-sol', event => { + const chatMessage = ChatUtility.getChatMessage(event); + const rollData = RdDEmpoignade.readRollEmpoignade(chatMessage); + RdDEmpoignade.entrainerAuSol(rollData) + ChatUtility.removeChatMessageId(chatMessage.id) + }); + html.on("click", '.empoignade-projeter-sol', event => { + const chatMessage = ChatUtility.getChatMessage(event); + const rollData = RdDEmpoignade.readRollEmpoignade(chatMessage); + RdDEmpoignade.projeterAuSol(rollData) + ChatUtility.removeChatMessageId(chatMessage.id) + }); + html.on("change", '.empoignade-perte-endurance', event => { + const chatMessage = ChatUtility.getChatMessage(event); + const rollData = RdDEmpoignade.readRollEmpoignade(chatMessage); + if (event.currentTarget.value && event.currentTarget.value != "none") { + RdDEmpoignade.perteEndurance(rollData, event.currentTarget.value) + ChatUtility.removeChatMessageId(chatMessage.id) + } + }); + } + /* -------------------------------------------- */ static checkEmpoignadeEnCours(actor) { // TODO: autoriser la perception? la comédie/séduction? @@ -24,23 +67,38 @@ export class RdDEmpoignade { return false; } + /* -------------------------------------------- */ + static storeRollEmpoignade(msg, rollData) { + rollData.attacker = { id: rollData.attacker.id } + rollData.defender = { id: rollData.defender.id } + ChatUtility.setMessageData(msg, "empoignade-roll-data", rollData); + } + + /* -------------------------------------------- */ + static readRollEmpoignade(msg) { + const rollData = ChatUtility.getMessageData(msg, 'empoignade-roll-data'); + rollData.attacker = game.actors.get(rollData.attacker.id) + rollData.defender = game.actors.get(rollData.defender.id) + return rollData + } + /* -------------------------------------------- */ static isEmpoignadeEnCours(actor) { - return actor.itemTypes['empoignade'].find(it => it.system.pointsemp > 0) + return actor.itemTypes[TYPES.empoignade].find(it => it.system.pointsemp > 0) } /* -------------------------------------------- */ static getEmpoignadeById(actor, id) { - let emp = actor.itemTypes['empoignade'].find(it => it.system.empoignadeid == id) + let emp = actor.itemTypes[TYPES.empoignade].find(it => it.system.empoignadeid == id) return emp && duplicate(emp) || undefined; } /* -------------------------------------------- */ static getEmpoignade(attacker, defender) { - let emp = attacker.itemTypes['empoignade'].find(it => it.system.empoigneurid == attacker.id && it.system.empoigneid == defender.id) - if (!emp) { - emp = attacker.itemTypes['empoignade'].find(it => it.system.empoigneurid == defender.id && it.system.empoigneid == attacker.id) - } + let emp = attacker.itemTypes[TYPES.empoignade].find(it => + (it.system.empoigneurid == attacker.id && it.system.empoigneid == defender.id) || + (it.system.empoigneurid == defender.id && it.system.empoigneid == attacker.id) + ) if (emp) { return duplicate(emp); } @@ -68,15 +126,10 @@ export class RdDEmpoignade { let mode = (empoignade && empoignade.system.empoigneurid == attacker.id) ? "empoigner" : "liberer" let rollData = { - mode: mode, + mode, empoignade, attacker, defender, isEmpoignade: true, competence: attacker.getCompetence("Corps à corps").clone(), selectedCarac: attacker.system.carac.melee, - empoignade: empoignade, - attackerId: attacker.id, - attackerName: attacker.name, - defenderName: defender.name, - defenderId: defender.id, malusTaille: RdDEmpoignade.getMalusTaille(empoignade, attacker, defender) } if (attacker.isCreatureEntite()) { @@ -84,7 +137,7 @@ export class RdDEmpoignade { } if (empoignade.system.pointsemp >= 2) { let msg = await RdDResolutionTable.displayRollData(rollData, attacker, 'chat-empoignade-actions.html'); - ChatUtility.setMessageData(msg, "empoignade-roll-data", rollData) + RdDEmpoignade.storeRollEmpoignade(msg, rollData); } else { await RdDEmpoignade.$rollAttaqueEmpoignade(attacker, rollData, isNouvelle); } @@ -119,35 +172,36 @@ export class RdDEmpoignade { { name: 'jet-empoignade', label: 'Empoigner', - callbacks: [ - { condition: r => (r.rolled.isSuccess), action: async (r) => await RdDEmpoignade.$onRollEmpoignade(r, true, isNouvelle) }, - { condition: r => (r.rolled.isEchec), action: async (r) => await RdDEmpoignade.$onRollEmpoignade(r, false, isNouvelle) }, - ] + callbacks: [{ action: async (r) => await RdDEmpoignade.$onRollEmpoignade(r, isNouvelle) },] }); dialog.render(true); } /* -------------------------------------------- */ - static async $onRollEmpoignade(rollData, isSuccess, isNouvelle = false) { - let attacker = game.actors.get(rollData.attackerId) - let defender = game.actors.get(rollData.defenderId) + static async $onRollEmpoignade(rollData, isNouvelle = false) { + let attacker = game.actors.get(rollData.attacker.id) + let defender = game.actors.get(rollData.defender.id) - let empoignade = rollData.empoignade - empoignade.isSuccess = isSuccess; - if (isSuccess && isNouvelle) { + if (rollData.rolled.isSuccess && isNouvelle) { + const objectEmpoignade = rollData.empoignade.toObject(); // Creer l'empoignade sur attaquant/defenseur - await attacker.createEmbeddedDocuments('Item', [empoignade.toObject()]) - await defender.createEmbeddedDocuments('Item', [empoignade.toObject()]) + attacker.creerObjetParMJ(objectEmpoignade); + defender.creerObjetParMJ(objectEmpoignade); + } + + rollData.empoignade.isSuccess = rollData.rolled.isSuccess; + if (rollData.rolled.isPart) { + rollData.particuliere = "finesse"; } let msg = await RdDResolutionTable.displayRollData(rollData, attacker, 'chat-empoignade-resultat.html'); - ChatUtility.setMessageData(msg, "empoignade-roll-data", rollData) + RdDEmpoignade.storeRollEmpoignade(msg, rollData); } /* -------------------------------------------- */ - static async onDefenseEmpoignade(rollData, defenseMode, competenceName = "Corps à corps", carac = "melee") { - let attacker = game.actors.get(rollData.attackerId) - let defender = game.actors.get(rollData.defenderId) + static async onDefenseEmpoignade(attackerRoll, mode, competenceName = "Corps à corps", carac = "melee") { + let attacker = game.actors.get(attackerRoll.attacker.id) + let defender = game.actors.get(attackerRoll.defender.id) let empoignade = this.getEmpoignade(attacker, defender) if (!empoignade) { @@ -156,13 +210,18 @@ export class RdDEmpoignade { } empoignade = duplicate(empoignade) - rollData.mode = defenseMode - rollData.empoignade = empoignade - rollData.competence = defender.getCompetence(competenceName), - rollData.selectedCarac = defender.system.carac[carac], - rollData.malusTaille = RdDEmpoignade.getMalusTaille(empoignade, defender, attacker) - - await RdDEmpoignade.$rollDefenseEmpoignade(defender, rollData); + let defenderRoll = { + mode, attacker, defender, empoignade, attackerRoll, + diffLibre: attackerRoll.diffLibre, + attaqueParticuliere: attackerRoll.particuliere, + competence: defender.getCompetence(competenceName).clone(), + surprise: defender.getSurprise(true), + carac: defender.system.carac, + selectedCarac: defender.system.carac[carac], + malusTaille: RdDEmpoignade.getMalusTaille(empoignade, defender, attacker), + show: {} + }; + await RdDEmpoignade.$rollDefenseEmpoignade(defender, defenderRoll); } /* -------------------------------------------- */ @@ -196,7 +255,7 @@ export class RdDEmpoignade { if (empoignade.system.pointsemp >= 2) { let attacker = game.actors.get(empoignade.system.empoigneurid) let msg = await RdDResolutionTable.displayRollData(rollData, attacker, 'chat-empoignade-actions.html'); - ChatUtility.setMessageData(msg, "empoignade-roll-data", rollData) + RdDEmpoignade.storeRollEmpoignade(msg, rollData); } await RdDResolutionTable.displayRollData(rollData, rollData.defender, 'chat-empoignade-resultat.html') } @@ -231,8 +290,8 @@ export class RdDEmpoignade { /* -------------------------------------------- */ static async entrainerAuSol(rollData) { - let attacker = game.actors.get(rollData.attackerId) - let defender = game.actors.get(rollData.defenderId) + let attacker = game.actors.get(rollData.attacker.id) + let defender = game.actors.get(rollData.defender.id) let empoignade = this.getEmpoignade(attacker, defender) empoignade.system.ausol = true @@ -242,25 +301,25 @@ export class RdDEmpoignade { await defender.setEffect(STATUSES.StatusProne, true); let msg = await RdDResolutionTable.displayRollData(rollData, attacker, 'chat-empoignade-entrainer-sol.html'); - ChatUtility.setMessageData(msg, "empoignade-roll-data", rollData) + RdDEmpoignade.storeRollEmpoignade(msg, rollData); } /* -------------------------------------------- */ static async projeterAuSol(rollData) { - let attacker = game.actors.get(rollData.attackerId) - let defender = game.actors.get(rollData.defenderId) + let attacker = game.actors.get(rollData.attacker.id) + let defender = game.actors.get(rollData.defender.id) let empoignade = this.getEmpoignade(attacker, defender) await defender.setEffect(STATUSES.StatusProne, true); await this.$deleteEmpoignade(empoignade) let msg = await RdDResolutionTable.displayRollData(rollData, attacker, 'chat-empoignade-projeter-sol.html'); - ChatUtility.setMessageData(msg, "empoignade-roll-data", rollData) + RdDEmpoignade.storeRollEmpoignade(msg, rollData); } /* -------------------------------------------- */ static async perteEndurance(rollData, perteMode) { - let attacker = game.actors.get(rollData.attackerId) - let defender = game.actors.get(rollData.defenderId) + let attacker = game.actors.get(rollData.attacker.id) + let defender = game.actors.get(rollData.defender.id) let empoignade = this.getEmpoignade(attacker, defender) //console.log("Perte d'endurance :!!!", perteMode) @@ -278,7 +337,7 @@ export class RdDEmpoignade { await defender.santeIncDec("endurance", -(3 * Math.floor(endValue / 4))); } let msg = await RdDResolutionTable.displayRollData(rollData, attacker, 'chat-empoignade-perte-endurance.html'); - ChatUtility.setMessageData(msg, "empoignade-roll-data", rollData) + RdDEmpoignade.storeRollEmpoignade(msg, rollData); } /* -------------------------------------------- */ diff --git a/module/rdd-resolution-table.js b/module/rdd-resolution-table.js index 46700826..02a99b07 100644 --- a/module/rdd-resolution-table.js +++ b/module/rdd-resolution-table.js @@ -91,13 +91,17 @@ export class RdDResolutionTable { /* -------------------------------------------- */ static async displayRollData(rollData, actor = undefined, template = 'chat-resultat-general.html') { - return await ChatUtility.createChatWithRollMode(actor?.userName ?? game.user.name, { - content: await RdDResolutionTable.buildRollDataHtml(rollData, actor, template) + return await ChatUtility.createChatWithRollMode(RdDResolutionTable.actorChatName(actor), { + content: await RdDResolutionTable.buildRollDataHtml(rollData, template) }); } + static actorChatName(actor) { + return actor?.userName ?? game.user.name; + } + /* -------------------------------------------- */ - static async buildRollDataHtml(rollData, actor, template = 'chat-resultat-general.html') { + static async buildRollDataHtml(rollData, template = 'chat-resultat-general.html') { rollData.show = rollData.show || {}; return await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/${template}`, rollData); } diff --git a/module/rdd-utility.js b/module/rdd-utility.js index e69b9790..59a5f8be 100644 --- a/module/rdd-utility.js +++ b/module/rdd-utility.js @@ -668,6 +668,7 @@ export class RdDUtility { /* -------------------------------------------- */ static async chatListeners(html) { RdDCombat.registerChatCallbacks(html); + RdDEmpoignade.registerChatCallbacks(html); // Gestion spécifique message passeurs html.on("click", '.tmr-passeur-coord a', event => { @@ -694,44 +695,6 @@ export class RdDUtility { RdDPossession.onDefensePossession(attackerId, defenderId, possessionId) }); - html.on("click", '.defense-empoignade-cac', event => { - const chatMessage = ChatUtility.getChatMessage(event); - const rollData = ChatUtility.getMessageData(chatMessage, 'empoignade-roll-data'); - let defenseMode = event.currentTarget.attributes['data-defense-mode'].value - RdDEmpoignade.onDefenseEmpoignade(rollData, defenseMode, "Corps à corps", "melee") - }); - html.on("click", '.defense-empoignade-esquive', event => { - const chatMessage = ChatUtility.getChatMessage(event); - const rollData = ChatUtility.getMessageData(chatMessage, 'empoignade-roll-data'); - let defenseMode = event.currentTarget.attributes['data-defense-mode'].value - RdDEmpoignade.onDefenseEmpoignade(rollData, defenseMode, "Esquive", "derobee") - }); - html.on("click", '.empoignade-poursuivre', event => { - let attackerId = event.currentTarget.attributes['data-attackerId'].value - let defenderId = event.currentTarget.attributes['data-defenderId'].value - RdDEmpoignade.onAttaqueEmpoignadeValidee(game.actors.get(attackerId), game.actors.get(defenderId)) - }); - html.on("click", '.empoignade-entrainer-sol', event => { - const chatMessage = ChatUtility.getChatMessage(event); - const rollData = ChatUtility.getMessageData(chatMessage, 'empoignade-roll-data'); - RdDEmpoignade.entrainerAuSol(rollData) - ChatUtility.removeChatMessageId(chatMessage.id) - }); - html.on("click", '.empoignade-projeter-sol', event => { - const chatMessage = ChatUtility.getChatMessage(event); - const rollData = ChatUtility.getMessageData(chatMessage, 'empoignade-roll-data'); - RdDEmpoignade.projeterAuSol(rollData) - ChatUtility.removeChatMessageId(chatMessage.id) - }); - html.on("change", '.empoignade-perte-endurance', event => { - const chatMessage = ChatUtility.getChatMessage(event); - const rollData = ChatUtility.getMessageData(chatMessage, 'empoignade-roll-data'); - if (event.currentTarget.value && event.currentTarget.value != "none") { - RdDEmpoignade.perteEndurance(rollData, event.currentTarget.value) - ChatUtility.removeChatMessageId(chatMessage.id) - } - }); - // gestion bouton tchat Acheter html.on("click", '.button-acheter', event => { const venteData = DialogItemAchat.preparerAchat(event.currentTarget); diff --git a/templates/chat-empoignade-actions.html b/templates/chat-empoignade-actions.html index 85688188..6d9c30b8 100644 --- a/templates/chat-empoignade-actions.html +++ b/templates/chat-empoignade-actions.html @@ -1,12 +1,12 @@