From 7259d3dc216c303d413c2c34b8dc939247b40533 Mon Sep 17 00:00:00 2001 From: sladecraven Date: Sat, 11 Jun 2022 10:21:18 +0200 Subject: [PATCH] Review fight automation --- module/actor/actor.js | 11 +++++--- module/controllers/bol-rolls.js | 45 ++++++++++++++++++--------------- module/system/bol-action-hud.js | 2 +- module/system/bol-utility.js | 45 +++++++++++++++++++-------------- 4 files changed, 59 insertions(+), 44 deletions(-) diff --git a/module/actor/actor.js b/module/actor/actor.js index a9a891c..bb8d56e 100644 --- a/module/actor/actor.js +++ b/module/actor/actor.js @@ -363,6 +363,7 @@ export class BoLActor extends Actor { } } + /*-------------------------------------------- */ getAlchemistBonus() { let sorcerer = this.careers.find(item => item.data.properties.alchemist == true) if (sorcerer) { @@ -370,6 +371,7 @@ export class BoLActor extends Actor { } return 0; } + /*-------------------------------------------- */ getSorcererBonus() { let sorcerer = this.careers.find(item => item.data.properties.sorcerer == true) if (sorcerer) { @@ -378,6 +380,7 @@ export class BoLActor extends Actor { return 0; } + /*-------------------------------------------- */ heroReroll() { if (this.type == 'character') { return this.data.data.resources.hero.value > 0; @@ -550,7 +553,7 @@ export class BoLActor extends Actor { async manageHealthState() { let hpID = "lastHP" + this.id let lastHP = await this.getFlag("world", hpID) - if (lastHP != this.data.data.resources.hp.value && this.isOwner ) { + if (lastHP != this.data.data.resources.hp.value && game.user.isGM ) { // Only GM sends this await this.setFlag("world", hpID, this.data.data.resources.hp.value) if (this.data.data.resources.hp.value <= 0) { ChatMessage.create({ @@ -614,9 +617,9 @@ export class BoLActor extends Actor { /* -------------------------------------------- */ rollProtection(itemId) { - let armor = this.data.items.get(itemId) + let armor = duplicate(this.data.items.get(itemId)) if (armor) { - let armorFormula = "max("+armor.data.data.properties.soak.formula + ", 0)" + let armorFormula = "max("+armor.data.properties.soak.formula + ", 0)" let rollArmor = new Roll(armorFormula) rollArmor.roll({ async: false }).toMessage() } @@ -624,7 +627,7 @@ export class BoLActor extends Actor { /* -------------------------------------------- */ rollWeaponDamage(itemId) { - let weapon = this.data.items.get(itemId) + let weapon = duplicate(this.data.items.get(itemId)) if (weapon) { let r = new BoLDefaultRoll({ id: randomID(16), isSuccess: true, mode: "weapon", weapon: weapon, actorId: this.id, actor: this }) r.setSuccess(true) diff --git a/module/controllers/bol-rolls.js b/module/controllers/bol-rolls.js index 3bca815..0ecfd6d 100644 --- a/module/controllers/bol-rolls.js +++ b/module/controllers/bol-rolls.js @@ -72,7 +72,7 @@ export class BoLRoll { let target = BoLUtility.getTarget() - let weaponData = weapon.data.data + let weaponData = weapon.data let attribute = eval(`actor.data.data.attributes.${weaponData.properties.attackAttribute}`) let aptitude = eval(`actor.data.data.aptitudes.${weaponData.properties.attackAptitude}`) @@ -109,23 +109,25 @@ export class BoLRoll { /* -------------------------------------------- */ static weaponCheck(actor, event) { const li = $(event.currentTarget).parents(".item") - const weapon = actor.items.get(li.data("item-id")) + let weapon = actor.items.get(li.data("item-id")) if (!weapon) { ui.notifications.warn("Unable to find weapon !") return } + weapon = duplicate(weapon) return this.weaponCheckWithWeapon(actor, weapon) } /* -------------------------------------------- */ static alchemyCheck(actor, event) { const li = $(event.currentTarget).parents(".item"); - const alchemy = actor.items.get(li.data("item-id")); + let alchemy = actor.items.get(li.data("item-id")); if (!alchemy) { ui.notifications.warn("Unable to find Alchemy !"); return; } - let alchemyData = alchemy.data.data + alchemy = dupicate(alchemy) + let alchemyData = alchemy.data if (alchemyData.properties.pccurrent < alchemyData.properties.pccost) { ui.notifications.warn("Pas assez de Points de Cration investis dans la Préparation !") return @@ -154,7 +156,7 @@ export class BoLRoll { /* -------------------------------------------- */ static spellCheckWithSpell( actor, spell ) { - let spellData = spell.data.data + let spellData = spell.data let spellDef = { mode: "spell", actorId: actor.id, @@ -184,11 +186,12 @@ export class BoLRoll { return } const li = $(event.currentTarget).parents(".item") - const spell = actor.items.get(li.data("item-id")) + let spell = actor.items.get(li.data("item-id")) if (!spell) { ui.notifications.warn("Impossible de trouver ce sort !") return } + spell = duplicate(spell) return this.spellCheckWithSpell( actor, spell) } @@ -207,7 +210,7 @@ export class BoLRoll { $('#roll-nbdice').val("2 + " + String(Math.abs(this.rollData.bmDice)) + letter) } let rollbase = this.rollData.attrValue + "+" + this.rollData.aptValue - if ( this.rollData.weapon && this.rollData.weapon.data.data.properties.onlymodifier ) { + if ( this.rollData.weapon && this.rollData.weapon.data.properties.onlymodifier ) { rollbase = "" } $('#roll-modifier').val(rollbase + "+" + this.rollData.careerBonus + "+" + this.rollData.mod + "+" + @@ -337,8 +340,8 @@ export class BoLRoll { /* -------------------------------------------- */ static preProcessWeapon(rollData, defender) { if (rollData.mode == "weapon") { - rollData.weaponModifier = rollData.weapon.data.data.properties.attackModifiers ?? 0; - rollData.attackBonusDice = rollData.weapon.data.data.properties.attackBonusDice + rollData.weaponModifier = rollData.weapon.data.properties.attackModifiers ?? 0; + rollData.attackBonusDice = rollData.weapon.data.properties.attackBonusDice if (defender) { // If target is selected rollData.defence = defender.defenseValue rollData.armorMalus = defender.armorMalusValue @@ -429,7 +432,7 @@ export class BoLRoll { rollData.nbDice += (rollData.attackBonusDice) ? 1 : 0 let rollbase = rollData.attrValue + rollData.aptValue - if ( rollData.weapon && rollData.weapon.data.data.properties.onlymodifier ) { + if ( rollData.weapon && rollData.weapon.data.properties.onlymodifier ) { rollbase = 0 } const modifiers = rollbase + rollData.careerBonus + rollData.mod + rollData.weaponModifier - rollData.defence - rollData.modArmorMalus + rollData.shieldMalus + rollData.attackModifier + rollData.appliedArmorMalus @@ -454,7 +457,6 @@ export class BoLRoll { export class BoLDefaultRoll { constructor(rollData) { - BoLUtility.storeRoll(rollData) this.rollData = rollData if (this.rollData.isSuccess == undefined) { // First init this.rollData.isSuccess = false; @@ -500,7 +502,7 @@ export class BoLDefaultRoll { actor.spendPowerPoint(this.rollData.ppCost + this.rollData.ppCostArmor) } if (this.rollData.mode == "alchemy") { // PP cost management - actor.resetAlchemyStatus(this.rollData.alchemy.id) + actor.resetAlchemyStatus(this.rollData.alchemy._id) } await this.sendChatMessage() @@ -509,14 +511,15 @@ export class BoLDefaultRoll { /* -------------------------------------------- */ async sendChatMessage() { let actor = game.actors.get( this.rollData.actorId) - this._buildChatMessage(this.rollData).then(msgFlavor => { - this.rollData.roll.toMessage({ + this._buildChatMessage(this.rollData).then( async msgFlavor => { + let msg = await this.rollData.roll.toMessage({ user: game.user.id, rollMode: game.settings.get("core", "rollMode"), //whisper: BoLUtility.getWhisperRecipientsAndGMs(this.rollData.actor.name), flavor: msgFlavor, speaker: ChatMessage.getSpeaker({ actor: actor }), }) + msg.setFlag("world", "bol-roll-data", this.rollData) }) } @@ -555,14 +558,15 @@ export class BoLDefaultRoll { /* -------------------------------------------- */ async sendDamageMessage() { let actor = game.actors.get( this.rollData.actorId) - this._buildDamageChatMessage(this.rollData).then(msgFlavor => { - this.rollData.damageRoll.toMessage({ + this._buildDamageChatMessage(this.rollData).then(async msgFlavor => { + let msg = await this.rollData.damageRoll.toMessage({ user: game.user.id, flavor: msgFlavor, speaker: ChatMessage.getSpeaker({ actor: actor }), flags: { msgType: "default" } }) - }); + msg.setFlag("world", "bol-roll-data", this.rollData) + }) } /* -------------------------------------------- */ @@ -582,7 +586,7 @@ export class BoLDefaultRoll { /* -------------------------------------------- */ async rollDamage() { if (this.rollData.mode != "weapon") { // Only specific process in Weapon mode - return; + return } if (this.rollData.isSuccess) { @@ -594,8 +598,9 @@ export class BoLDefaultRoll { if (this.rollData.damageMode == 'damage-plus-12') { bonusDmg = 12 } - let attrDamageValue = this.getDamageAttributeValue(this.rollData.weapon.data.data.properties.damageAttribute) - let weaponFormula = BoLUtility.getDamageFormula(this.rollData.weapon.data.data, this.rollData.fightOption) + console.log("ROLLWEAPON: ", this.rollData) + let attrDamageValue = this.getDamageAttributeValue(this.rollData.weapon.data.properties.damageAttribute) + let weaponFormula = BoLUtility.getDamageFormula(this.rollData.weapon.data, this.rollData.fightOption) let damageFormula = weaponFormula + "+" + bonusDmg + "+" + attrDamageValue console.log("DAMAGE !!!", damageFormula, attrDamageValue, this.rollData) diff --git a/module/system/bol-action-hud.js b/module/system/bol-action-hud.js index 94a1c0e..6f996ff 100644 --- a/module/system/bol-action-hud.js +++ b/module/system/bol-action-hud.js @@ -31,7 +31,7 @@ export class BoLTokenHud { let actionIndex = Number(event.currentTarget.attributes['data-action-index'].value) let action = hudData.actionsList[actionIndex] const weapon = actor.items.get( action._id ) - BoLRoll.weaponCheckWithWeapon(hudData.actor, weapon) + BoLRoll.weaponCheckWithWeapon(hudData.actor, duplicate(weapon)) //console.log("Clicked", action) } ) diff --git a/module/system/bol-utility.js b/module/system/bol-utility.js index 1ce42df..a9747ad 100644 --- a/module/system/bol-utility.js +++ b/module/system/bol-utility.js @@ -63,12 +63,12 @@ export class BoLUtility { /* -------------------------------------------- */ static storeRoll(roll) { - this.lastRoll = roll + this.rollTab[roll.id] = roll } /* -------------------------------------------- */ - static getLastRoll() { - return this.lastRoll + static getRoll(rollId) { + return this.rollTab[roll.id] } /* -------------------------------------------- */ @@ -180,27 +180,34 @@ export class BoLUtility { } } + /* -------------------------------------------- */ + static getRollDataFromMessage( event ) { + let messageId = BoLUtility.findChatMessageId(event.currentTarget) + let message = game.messages.get(messageId) + return message.getFlag("world", "bol-roll-data") + } + /* -------------------------------------------- */ static async chatListeners(html) { // Damage handling html.on("click", '.chat-damage-apply', event => { - let rollData = BoLUtility.getLastRoll() + let rollData = BoLUtility.getRollDataFromMessage(event) + console.log("DATA !!!", rollData) $(`#${rollData.applyId}`).hide() BoLUtility.sendAttackSuccess(rollData) }); html.on("click", '.chat-damage-roll', event => { event.preventDefault(); - let rollData = BoLUtility.getLastRoll() - rollData.damageMode = event.currentTarget.attributes['data-damage-mode'].value; + let rollData = BoLUtility.getRollDataFromMessage(event) let bolRoll = new BoLDefaultRoll(rollData) bolRoll.rollDamage() }); html.on("click", '.transform-legendary-roll', event => { event.preventDefault(); - let rollData = BoLUtility.getLastRoll() + let rollData = BoLUtility.getRollDataFromMessage(event) let actor = game.actors.get( rollData.actorId) actor.subHeroPoints(1) let r = new BoLDefaultRoll(rollData) @@ -209,7 +216,7 @@ export class BoLUtility { html.on("click", '.transform-heroic-roll', event => { event.preventDefault(); - let rollData = BoLUtility.getLastRoll() + let rollData = BoLUtility.getRollDataFromMessage(event) let actor = game.actors.get( rollData.actorId) actor.subHeroPoints(1) let r = new BoLDefaultRoll(rollData) @@ -218,7 +225,7 @@ export class BoLUtility { html.on("click", '.hero-reroll', event => { event.preventDefault(); - let rollData = BoLUtility.getLastRoll() + let rollData = BoLUtility.getRollDataFromMessage(event) let actor = game.actors.get( rollData.actorId) actor.subHeroPoints(1) rollData.reroll = false // Disable reroll option for second roll @@ -264,15 +271,15 @@ export class BoLUtility { let armorFormula = defender.getArmorFormula() attackDef.rollArmor = new Roll(armorFormula) attackDef.rollArmor.roll( { async: false } ) - console.log("Armor roll ", attackDef.rollArmor) - attackDef.armorProtect = (attackDef.rollArmor.total < 0) ? 0 : attackDef.rollArmor.total; - attackDef.finalDamage = attackDef.damageRoll.total - attackDef.armorProtect; - attackDef.finalDamage = (attackDef.finalDamage < 0) ? 0 : attackDef.finalDamage; - defender.sufferDamage(attackDef.finalDamage); + attackDef.armorProtect = (attackDef.rollArmor.total < 0) ? 0 : attackDef.rollArmor.total + attackDef.finalDamage = attackDef.damageTotal - attackDef.armorProtect + attackDef.finalDamage = (attackDef.finalDamage < 0) ? 0 : attackDef.finalDamage + defender.sufferDamage(attackDef.finalDamage) + console.log("Armor roll -> result ", attackDef) } if (defenseMode == 'damage-without-armor') { - attackDef.finalDamage = attackDef.damageRoll.total; - defender.sufferDamage(attackDef.finalDamage); + attackDef.finalDamage = attackDef.damageTotal + defender.sufferDamage(attackDef.finalDamage) } if (defenseMode == 'hero-reduce-damage') { let armorFormula = defender.getArmorFormula() @@ -281,7 +288,7 @@ export class BoLUtility { attackDef.armorProtect = (attackDef.rollArmor.total < 0) ? 0 : attackDef.rollArmor.total attackDef.rollHero = new Roll("1d6") attackDef.rollHero.roll({ async: false }) - attackDef.finalDamage = attackDef.damageRoll.total - attackDef.rollHero.total - attackDef.armorProtect + attackDef.finalDamage = attackDef.damageTotal - attackDef.rollHero.total - attackDef.armorProtect attackDef.finalDamage = (attackDef.finalDamage < 0) ? 0 : attackDef.finalDamage defender.sufferDamage(attackDef.finalDamage) defender.subHeroPoints(1) @@ -417,8 +424,8 @@ export class BoLUtility { /* -------------------------------------------- */ static computeSpellCost(spell, nbOptCond = 0) { - let pp = spell.data.data.properties.ppcost - let minpp = __circle2minpp[spell.data.data.properties.circle] + let pp = spell.data.properties.ppcost + let minpp = __circle2minpp[spell.data.properties.circle] pp = (pp - nbOptCond < minpp) ? minpp : pp - nbOptCond return pp }