From 3a52ed28b40c7a09a9d88c963309ddb603587ecc Mon Sep 17 00:00:00 2001 From: kyamsil Date: Sun, 1 Aug 2021 22:48:08 +0100 Subject: [PATCH] Change the way conditions with values are handled --- src/systems/pf2e.js | 36 ++++++++++++++---------- src/systems/tokenHelper.js | 8 +++--- src/token.js | 57 +++++++++++++++----------------------- 3 files changed, 49 insertions(+), 52 deletions(-) diff --git a/src/systems/pf2e.js b/src/systems/pf2e.js index d6a5206..507efe9 100644 --- a/src/systems/pf2e.js +++ b/src/systems/pf2e.js @@ -103,25 +103,33 @@ export class pf2e{ return this.getCondition(token,condition) != undefined; } - getValuedCondition(token,condition) { + getConditionValue(token,condition) { const effect = this.getCondition(token, condition); - if (effect != undefined && effect?.data.value != null) return effect; + if (effect != undefined && effect?.value != null) return effect; } - async modifyValuedCondition(token,condition,delta) { - const effect = this.getValuedCondition(token,condition); - if (effect == undefined) { - if (delta > 0) { - const Condition = condition.charAt(0).toUpperCase() + condition.slice(1); - const newCondition = game.pf2e.ConditionManager.getCondition(Condition); - // newCondition.data.sources.hud = !0, - await game.pf2e.ConditionManager.addConditionToToken(newCondition, token); - } + async modifyConditionValue(token,condition,delta) { + if (condition == undefined) condition = 'removeAll'; + if (condition == 'removeAll'){ + for( let effect of token.actor.items.filter(i => i.type == 'condition')) + await effect.delete(); } else { - const currentValue = effect.value; - console.log(`Current Value: ${currentValue}`); - await game.pf2e.ConditionManager.updateConditionValue(effect, token, currentValue+delta); + const effect = this.getConditionValue(token,condition); + if (effect == undefined) { + if (delta > 0) { + const Condition = condition.charAt(0).toUpperCase() + condition.slice(1); + const newCondition = game.pf2e.ConditionManager.getCondition(Condition); + await game.pf2e.ConditionManager.addConditionToToken(newCondition, token); + } + } else { + try { + await game.pf2e.ConditionManager.updateConditionValue(effect.id, token, effect.value+delta); + } catch (error) { + //Do nothing. updateConditionValue will have an error about 'documentData is not iterable' when called from an NPC token. + } + } } + return true; } async toggleCondition(token,condition) { diff --git a/src/systems/tokenHelper.js b/src/systems/tokenHelper.js index d5df94b..f37a3d3 100644 --- a/src/systems/tokenHelper.js +++ b/src/systems/tokenHelper.js @@ -242,13 +242,13 @@ export class TokenHelper{ } /* PF2E */ - getValuedCondition(token,condition) { - return this.system.getValuedCondition(token,condition); + getConditionValue(token,condition) { + return this.system.getConditionValue(token,condition); } /* PF2E */ - modifyValuedCondition(token,condition,delta) { - return this.system.modifyValuedCondition(token,condition,delta); + modifyConditionValue(token,condition,delta) { + return this.system.modifyConditionValue(token,condition,delta); } /** diff --git a/src/token.js b/src/token.js index b97564a..b1e7ecc 100644 --- a/src/token.js +++ b/src/token.js @@ -153,7 +153,12 @@ export class TokenControl{ else if (stats == 'Advantage') txt += tokenHelper.getAdvantage(token) /* WFRP4e */ else if (stats == 'Resolve') txt += tokenHelper.getResolve(token) /* WFRP4e */ else if (stats == 'Resilience') txt += tokenHelper.getResilience(token) /* WFRP4e */ - + else if (stats == 'Condition') { + const valuedCondition = tokenHelper.getConditionValue(token, settings.condition); + if (valuedCondition != undefined) { + txt += valuedCondition?.value; + } + } if (settings.onClick == 'visibility') { //toggle visibility if (MODULE.getPermission('TOKEN','VISIBILITY') == false ) { @@ -195,7 +200,7 @@ export class TokenControl{ iconSrc = "fas fa-bullseye"; } } - else if (settings.onClick == 'condition') { //toggle condition + else if (settings.onClick == 'condition') { //handle condition if (MODULE.getPermission('TOKEN','CONDITIONS') == false ) { streamDeck.noPermission(context,device); return; @@ -210,21 +215,6 @@ export class TokenControl{ } } } - else if (settings.onClick == 'valuedCondition') { //modify valued condition - if (MODULE.getPermission('TOKEN','CONDITIONS') == false ) { - streamDeck.noPermission(context,device); - return; - } - ring = 1; - overlay = true; - if (icon == 'stats') { - iconSrc = tokenHelper.getConditionIcon(settings.valuedCondition); - if (tokenHelper.getValuedCondition(token,settings.valuedCondition)) { - ring = 2; - ringColor = "#FF7B00"; - } - } - } else if (settings.onClick == 'cubCondition') { //Combat Utility Belt conditions if (MODULE.getPermission('TOKEN','CONDITIONS') == false ) { streamDeck.noPermission(context,device); @@ -359,15 +349,6 @@ export class TokenControl{ overlay = true; if (icon == 'stats') iconSrc = tokenHelper.getConditionIcon(settings.condition); } - else if (settings.onClick == 'valuedCondition') { //modify condition value - if (MODULE.getPermission('TOKEN','CONDITIONS') == false ) { - streamDeck.noPermission(context,device); - return; - } - ring = 1; - overlay = true; - if (icon == 'stats') iconSrc = tokenHelper.getConditionIcon(settings.condition); - } else if (settings.onClick == 'cubCondition') { //Combat Utility Belt conditions if (MODULE.getPermission('TOKEN','CONDITIONS') == false ) { streamDeck.noPermission(context,device); @@ -526,15 +507,23 @@ export class TokenControl{ else if (onClick == 'target') { //Target token token.setTarget(!token.isTargeted,{releaseOthers:false}); } - else if (onClick == 'condition') { //Toggle condition + else if (onClick == 'condition') { //Handle condition if (MODULE.getPermission('TOKEN','CONDITIONS') == false ) return; - await tokenHelper.toggleCondition(token,settings.condition); - this.update(tokenId); - } - else if (onClick == 'valuedCondition') { //Modify Valued condition - if (MODULE.getPermission('TOKEN','CONDITIONS') == false ) return; - await tokenHelper.modifyValuedCondition(token,settings.valuedCondition,settings.valuedConditionDelta); - this.update(tokenId); + const func = settings.conditionFunction ? settings.conditionFunction : 'toggle'; + + if (func == 'toggle'){ //toggle + await tokenHelper.toggleCondition(token,settings.condition); + this.update(tokenId); + } + else if (func == 'increase'){ //increase + await tokenHelper.modifyConditionValue(token, settings.condition, +1) + this.update(tokenId); + } + else if (func == 'decrease'){ //decrease + await tokenHelper.modifyConditionValue(token, settings.condition, -1) + this.update(tokenId); + } + } else if (onClick == 'cubCondition') { //Combat Utility Belt conditions if (MODULE.getPermission('TOKEN','CONDITIONS') == false ) return;