Skip to content

Commit

Permalink
Merge pull request #29 from gludington/075datastructure
Browse files Browse the repository at this point in the history
075datastructure
  • Loading branch information
gludington authored Nov 29, 2020
2 parents 57fb3e9 + 0035c80 commit 3855f28
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/conditional-visibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ Hooks.on("renderTokenHUD", (app, html, data) => {
ConditionalVisibility.INSTANCE.onRenderTokenHUD(app, html, data);
});

//synthetic actors go through this
Hooks.on("preUpdateToken", (scene, token, update, options, userId) => {
ConditionalVisibility.INSTANCE.onPreUpdateToken(scene, token, update, options, userId);
})

//real actors go through this
Hooks.on("preCreateActiveEffect", (actor, effect, options, userId) => {
ConditionalVisibility.INSTANCE.onPreCreateActiveEffect(actor, effect, options, userId);
})

Hooks.on("preDeleteActiveEffect", (actor, effect, options, userId) => {
ConditionalVisibility.INSTANCE.onPreDeleteActiveEffect(actor, effect, options, userId);
})
6 changes: 6 additions & 0 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"CONVIS.settings.autoStealth.name": "Auto hide on stealth rolls",
"CONVIS.settings.autoStealth.hint": "On supported game systems, set the hidden condition on a token's stealth roll",

"CONVIS.popup.dismissuntilupdated": "Dismiss until updated",
"CONVIS.popup.close": "Close",
"CONVIS.popup.version0910.1":"A compendium has is now part of this module, for those who wish to use macros instead of conditions icons to apply conditions, or to apply them in bulk.",
"CONVIS.popup.version0910.2":"There is also a script to migrate conditions of selected tokens from the Foundry 0.6.x model to the Foundry 0.7 model",
"CONVIS.popup.version0910.3":"Using these scripts as macros will require the installation of the Furnace module, as they are asynchronous",

"CONVIS.help.readmeat": "More instructions can be found at the",
"CONVIS.help.gamesystem": "Game System",
"CONVIS.help.hasStealth": "Has Stealth",
Expand Down
60 changes: 59 additions & 1 deletion src/module/ConditionalVisibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ConditionalVisibility {
if (isVisible === false) {
return false;
}
if (game.user.isGM || this._controlled || !canvas.sight.tokenVision) {
if (game.user.isGM || this.owner || !canvas.sight.tokenVision) {
return true;
}
return ConditionalVisibility.canSee(this);
Expand All @@ -39,6 +39,24 @@ export class ConditionalVisibility {
system.initializeStatusEffects();
}

public isSemvarGreater(first:string, second:string):boolean {
const firstSemVar:Array<number> = this.splitOnDot(first);
const secondSemVar:Array<number> = this.splitOnDot(second);
if (firstSemVar.length != secondSemVar.length) {
throw new Error("bad semvar first " + first +", second" + second);
}
for (let i = 0; i < firstSemVar.length;i++ ){
if (firstSemVar[i] > secondSemVar[i]) {
return true;
}
}
return false;
}

private splitOnDot(toSplit:string):Array<number> {
return toSplit.split(".").map(str => isNaN(Number(str)) ? 0 : Number(str));
}

/**
* A static method that will be replaced after initialization with the appropriate system specific method.
* @param token the token to test
Expand Down Expand Up @@ -156,6 +174,32 @@ export class ConditionalVisibility {
// update sight layer, as custom decisons will not be executed the
// first time through, and cannot be forced in setup
this.draw();

const popupVersion = game.settings.get(MODULE_NAME, "popup-version");
const currentVersion = game.modules.get(MODULE_NAME).data.version === "@tagVersion@" ? "0.0.3" : game.modules.get(MODULE_NAME).data.version;

if (this.isSemvarGreater(currentVersion, popupVersion)) {
renderTemplate("modules/conditional-visibility/templates/version_popup.html", {
version: currentVersion,
}).then(content => {
let d = new Dialog({
title: "Conditional Visibility",
content: content,
buttons: {
one: {
icon: '<i class="fas fa-check"></i>',
label: game.i18n.localize('CONVIS.popup.dismissuntilupdated'),
callback: () => game.settings.set(MODULE_NAME, 'popup-version', currentVersion)
},
two: {
icon: '<i class="fas fa-times"></i>',
label: game.i18n.localize('CONVIS.popup.close')
}
}
});
d.render(true);
});
}
}

public onRenderTokenConfig(tokenConfig: any, jQuery:JQuery, data: any) {
Expand Down Expand Up @@ -190,6 +234,20 @@ export class ConditionalVisibility {
});
}

public onPreCreateActiveEffect(actor, effect, options, userId) {
const status:Constants.StatusEffect = this._conditionalVisibilitySystem.getEffectByIcon(effect);
if (status) {
actor.setFlag(Constants.MODULE_NAME, status.visibilityId, true);
}
}

public onPreDeleteActiveEffect(actor, effect, options, userId) {
const status:Constants.StatusEffect = this._conditionalVisibilitySystem.getEffectByIcon(effect);
if (status) {
actor.unsetFlag(Constants.MODULE_NAME, status.visibilityId);
}
}

public onPreUpdateToken(scene:any, token:any, update:any, options:any, userId:string) {
const effectsFromUpdate = this._conditionalVisibilitySystem.effectsFromUpdate(update);
if (effectsFromUpdate) {
Expand Down
14 changes: 6 additions & 8 deletions src/module/ConditionalVisibilityFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,11 @@ export class ConditionalVisibilityFacadeImpl implements ConditionalVisibilityFac
}

private has(token, condition):boolean {
console.error("OKdddd");
console.error(condition);
let flags = token?.data?.flags?.[Constants.MODULE_NAME];
if (flags) {
return flags[condition.visibilityId] === true;
} else {
return false;
}
const flags = token.data.actorLink ? token.actor?.data?.flags?.[Constants.MODULE_NAME] : token?.data?.flags?.[Constants.MODULE_NAME];
if (flags) {
return flags[condition.visibilityId] === true;
} else {
return false;
}
}
}
7 changes: 7 additions & 0 deletions src/module/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export const registerSettings = function() {
onChange: value => console.log(Constants.MODULE_NAME + ' | autoStealth set to ' + value)
})

game.settings.register(Constants.MODULE_NAME, "popup-version", {
scope: "world",
config: false,
type: String,
default: "0.0.9",
});

// Register any custom module settings here
ConditionalVisibility.onInit();
}
12 changes: 12 additions & 0 deletions src/module/systems/ConditionalVisibilitySystem5e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ export class ConditionalVisibilitySystem5e extends DefaultConditionalVisibilityS
object.data.flags[Constants.MODULE_NAME] = {};
}
object.data.flags[Constants.MODULE_NAME]._ste = result;
if (object.actor) {
if (!object.actor.data) {
object.actor.data = {};
}
if (!object.actor.data.flags) {
object.actor.data.flags = {};
}
if (!object.actor.data.flags[Constants.MODULE_NAME]) {
object.actor.data.flags[Constants.MODULE_NAME] = {};
}
object.actor.data.flags[Constants.MODULE_NAME]._ste = result;
}
return realOnToggleEffect(event, opts);
});
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/module/systems/DefaultConditionalVisibilitySystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DefaultConditionalVisibilitySystem implements ConditionalVisibility
_effectsByCondition: Map<string, StatusEffect>;

hasStatus(token:Token, id:string, icon:string): boolean {
return token.data?.flags?.[MODULE_NAME]?.[id] === true;
return token.data.actorLink ? token.actor?.data?.flags?.[MODULE_NAME]?.[id] === true : token.data?.flags?.[MODULE_NAME]?.[id] === true;
}

constructor() {
Expand Down
3 changes: 2 additions & 1 deletion src/packs/visibility-scripts.db
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
{"_id":"ioE9JiWntRzjeDFr","name":"In Darkness on","permission":{"default":0},"type":"script","sort":100003,"flags":{},"scope":"global","command":"//Sets selected tokens to in darkness\nConditionalVisibility.setCondition(canvas.tokens.controlled, 'indarkness', true)","img":"icons/svg/dice-target.svg","actorIds":[]}
{"_id":"pkE9JiWntRzjeACr","name":"In Darkness off","permission":{"default":0},"type":"script","sort":100004,"flags":{},"scope":"global","command":"//Removes indarkness condition from selected tokens.\nConditionalVisibility.setCondition(canvas.tokens.controlled, 'indarkness', false)","img":"icons/svg/dice-target.svg","actorIds":[]}
{"_id":"ioE9JiWntRzjeBBr","name":"Obscured on","permission":{"default":0},"type":"script","sort":100005,"flags":{},"scope":"global","command":"//Sets selected tokens to obscured\nConditionalVisibility.setCondition(canvas.tokens.controlled, 'obscured', true)","img":"icons/svg/dice-target.svg","actorIds":[]}
{"_id":"pkE9JiWntRzjeBBr","name":"Obscured off","permission":{"default":0},"type":"script","sort":100006,"flags":{},"scope":"global","command":"//Removes obscured from selected tokens.\nConditionalVisibility.setCondition(canvas.tokens.controlled, 'obscured', false)","img":"icons/svg/dice-target.svg","actorIds":[]}
{"_id":"pkE9JiWntRzjeBBr","name":"Obscured off","permission":{"default":0},"type":"script","sort":100006,"flags":{},"scope":"global","command":"//Removes obscured from selected tokens.\nConditionalVisibility.setCondition(canvas.tokens.controlled, 'obscured', false)","img":"icons/svg/dice-target.svg","actorIds":[]}
{"_id":"pkA4JiWntARaeBBr","name":"Migrate Foundry 0.6 to 0.7","permission":{"default":0},"type":"script","sort":100017,"flags":{},"scope":"global","command":"// Removes statusEffects set in Foundry 06 and applies the equivalent effect for Foundry 0.7\n\nlet update0607ConvisMatches = {\n\t'modules/conditional-visibility/icons/unknown.svg': 'invisible',\n\t'modules/conditional-visibility/icons/newspaper.svg': 'hiding',\n\t'modules/conditional-visibility/icons/foggy.svg': 'obscured',\n\t'modules/conditional-visibility/icons/moon.svg': 'indarkness'\n}\n\nconst popper = async function(token, effects) {\n\tawait effects.reduce(async (promise, eff) => {\n\t\tawait promise;\n\t\tif (eff.endsWith('newspaper.svg')) {\n\t\t\tconsole.info('Migrating 0.6->0.7 hiding for ' + token.data.name);\n\t\t\ttoken.data.effects.splice(token.data.effects.indexOf(eff), 1);\n\t\t\tconst ste = token.data?.flags['conditional-visibility']?._ste;\n\t\t\tif (ste) {\n\t\t\t\tawait ConditionalVisibility.hide([token], ste);\n\t\t\t} else {\n\t\t\t\tawait ConditionalVisibility.hide([token]);\n\t\t\t}\n\t\t} else {\n\t\t\tconsole.info('Migrating 0.6->0.7 ' + update0607ConvisMatches[eff] + ' for ' + token.data.name);\n\t\t\ttoken.data.effects.splice(token.data.effects.indexOf(eff), 1);\n\t\t\tawait ConditionalVisibility.setCondition([token], update0607ConvisMatches[eff], true);\n\t\t}\n\t}, Promise.resolve());\n}\n\nfor (const token of canvas.tokens.placeables) {\n\tif (token.data.effects.length > 0) {\n\t\tconst name = token.data.name;\n\t\tconst effectsToToggle = token.data.effects.filter(eff => {\n\t\t\tlet toCheck;\n\t\t\tif (typeof eff === 'string') {\n\t\t\t\ttoCheck = eff;\n\t\t\t} else {\n\t\t\t\ttoCheck = eff.icon;\n\t\t\t}\n\t\t\treturn toCheck && update0607ConvisMatches[toCheck] !== undefined;\n\t\t});\n\t\tpopper(token, effectsToToggle);\n\t};\n}","img":"icons/svg/dice-target.svg","actorIds":[]}
4 changes: 4 additions & 0 deletions src/templates/version_popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h2>Conditional Visibility {{version}}</h2>
<p class="notes">{{localize "CONVIS.popup.version0910.1"}}</p>
<p class="notes">{{localize "CONVIS.popup.version0910.2"}}</p>
<p class="notes">{{localize "CONVIS.popup.version0910.3"}}</p>

0 comments on commit 3855f28

Please sign in to comment.