Instantly share code, notes, and snippets.
Created
March 15, 2023 23:54
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save BaldarSilveraxe/e72614a969ba536221d99890026d3556 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on('ready', () => { | |
const tokenDefaults = { | |
exclude: ["_id", "_pageid", "left", "top", "imgsrc", "width", "height", "layer", "sides"], | |
areFalse: ["aura1_square", "aura2_square", "emits_bright_light", "emits_low_light", "fliph", "flipv", "has_bright_light_vision", "has_directional_bright_light", "has_directional_dim_light", "has_limit_field_of_night_vision", "has_limit_field_of_vision", "has_night_vision", "isdrawing", "light_hassight", "light_otherplayers", "lockMovement", "show_tooltip", "showname", "showplayers_aura1", "showplayers_aura2", "showplayers_bar1", "showplayers_bar2", "showplayers_bar3", "showplayers_name"], | |
areTrue: ["playersedit_aura1", "playersedit_aura2", "playersedit_bar1", "playersedit_bar2", "playersedit_bar3", "playersedit_name"], | |
areNull: ["bar_location", "compact_bar", "night_vision_effect", "night_vision_tint"], | |
areEmpty: ["_cardid", "adv_fow_view_distance", "aura1_radius", "aura2_radius", "bar1_link", "bar1_max", "bar1_value", "bar2_link", "bar2_max", "bar2_value", "bar3_link", "bar3_max", "bar3_value", "controlledby", "gmnotes", "lastmove", "light_angle", "light_dimradius", "light_losangle", "light_radius", "name", "represents", "statusmarkers", "tooltip"], | |
areZero: ["bright_light_distance", "currentSide", "dim_light_opacity", "directional_bright_light_center", "directional_bright_light_total", "directional_dim_light_center", "directional_dim_light_total", "limit_field_of_night_vision_center", "limit_field_of_night_vision_total", "limit_field_of_vision_center", "limit_field_of_vision_total", "low_light_distance", "night_vision_distance", "rotation"], | |
areMisc: { | |
aura1_color: "#FFFF99", | |
aura2_color: "#59E594", | |
tint_color: "transparent", | |
light_multiplier: 1, | |
_type: "graphic", | |
_subtype: "token", | |
light_sensitivity_multiplier: 100, | |
lightColor: "transparent" | |
} | |
}; | |
const simpleObj = (o) => JSON.parse(JSON.stringify(o)); | |
const getCleanImgsrc = (imgsrc) => { | |
let parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^?]*)(\?[^?]+)?$/); | |
if (parts) { | |
return parts[1] + 'thumb' + parts[3] + (parts[4] ? parts[4] : `?${Math.round(Math.random()*9999999)}`); | |
} | |
return; | |
}; | |
const getTablesFromToken = (token) => { | |
let array = []; | |
let result = []; | |
_.each(findObjs({type: 'tableitem', avatar: token.imgsrc}), (e) => { | |
array.push(e.get("_rollabletableid")); | |
}); | |
_.each(array, (e) => { | |
if (findObjs({type: 'tableitem', _rollabletableid: e})[0].get("avatar") == token.imgsrc) { | |
result.push(e); | |
} | |
}); | |
return result; | |
} | |
const checkSides = (token) => { | |
return (token.sides.length != 0 && | |
decodeURIComponent(token.sides) != token.side && | |
getCleanImgsrc(decodeURIComponent(token.imgsrc)) == getCleanImgsrc(decodeURIComponent(token.sides.split(/\|/)[0]))); | |
}; | |
const checkDefaults = (token) => { | |
const copy = _.omit(token, tokenDefaults.exclude); | |
const compareValue = (obj, val, str) => { | |
return Object.keys(_.omit(obj, function(value, key, object) { | |
return value !== val | |
})).sort().toString() === str.sort().toString(); | |
}; | |
const compareMisc = (obj) => { | |
const lastProps = _.omit(obj, function(value, key, object) { | |
return value === false || value === true || value === null || value === "" || value === 0; | |
}); | |
return Object.keys(lastProps).every(key => lastProps[key] === tokenDefaults.areMisc[key]); | |
}; | |
return compareValue(copy, false, tokenDefaults.areFalse) && | |
compareValue(copy, true, tokenDefaults.areTrue) && | |
compareValue(copy, null, tokenDefaults.areNull) && | |
compareValue(copy, "", tokenDefaults.areEmpty) && | |
compareValue(copy, 0, tokenDefaults.areZero) && | |
compareMisc(copy); | |
}; | |
const isTableToken = (obj) => { | |
let token = simpleObj(obj); | |
if (checkSides(token) && checkDefaults(token)) { | |
let tables = getTablesFromToken(token); | |
switch (tables.length) { | |
case 0: | |
sendChat('', `/w gm <div style="color: #993333;font-weight:bold;">No table found!</div>`); | |
break; | |
case 1: | |
sendChat('', `/w gm <div style="color: #993333;font-weight:bold;">One table found:<br>${tables.toString()}</div>`); | |
break; | |
default: | |
sendChat('', `/w gm <div style="color: #993333;font-weight:bold;">More than one table found:<br>${tables.toString()}</div>`); | |
break; | |
} | |
} | |
}; | |
on('add:token', (obj) => { | |
isTableToken(obj); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment