Change the way conditions with values are handled

This commit is contained in:
kyamsil
2021-08-01 22:48:08 +01:00
parent 6c840cbf59
commit 3a52ed28b4
3 changed files with 49 additions and 52 deletions

View File

@@ -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) {

View File

@@ -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);
}
/**

View File

@@ -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;