Last active
December 16, 2015 20:39
-
-
Save rlittlefield/5493842 to your computer and use it in GitHub Desktop.
Roll20 API: reveal command by class
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
var getByClasses = function(classes, criteria) { | |
var graphics = findObjs({ | |
_pageid: Campaign().get("playerpageid"), | |
_type: "graphic", | |
}); | |
var cclasses = {}; | |
for (var i = 0, il = classes.length; i < il; i++) { | |
cclasses['.'+classes[i]] = true; | |
} | |
var output = []; | |
_.each(graphics, function(item) { | |
var item_name = item.get('name'); | |
if (criteria) { | |
var matches_criteria = true; | |
for (var clabel in criteria) { | |
var cvalue = criteria[clabel]; | |
if (item.get(clabel) != cvalue) { | |
return; | |
} | |
} | |
} | |
var item_classes = item_name.match(/\.\w+/); | |
_.each(item_classes, function(cclass) { | |
if (cclasses[cclass]) { | |
output[output.length] = item; | |
} | |
}); | |
}); | |
return output; | |
} | |
var getRandomByClasses = function(classes, criteria) { | |
var items = getByClasses(classes, criteria); | |
if (!items.length) { | |
return; | |
} | |
var item = items[_.random(0, items.length-1)]; | |
return item; | |
} | |
var getRandomByClass = function(klass, criteria) { | |
return getRandomByClasses([klass], criteria) | |
} | |
var getByClass = function(klass, criteria) { | |
return getByClasses([klass], criteria); | |
} | |
var processCommand = function(command, argv) { | |
switch(command) { | |
case '!reveal': | |
case '!show': | |
/* | |
Usage: !reveal foo bar | |
Result: | |
will find all graphics with a name that contains .foo or .bar and | |
then move them to the objects layer. This may be useful for ambushes, | |
cutscenes, etc. | |
*/ | |
_.each(getByClasses(argv), function(item) { | |
item.set({ | |
'layer': 'objects' | |
}); | |
}); | |
break; | |
case '!conceal': | |
case '!hide': | |
/* | |
Usage: !conceal foo bar | |
Result: | |
will find all graphics with a name that contains .foo or .bar and | |
then move them to the gm layer. This may be useful for ambushes, | |
cutscenes, etc. | |
*/ | |
_.each(getByClasses(argv), function(item) { | |
item.set({ | |
'layer': 'gmlayer' | |
}); | |
}); | |
break; | |
case '!revealrandom': | |
case '!showrandom': | |
// Usage: !revealrandom foo bar | |
// Result: picks a random item with .foo or .bar in the name and shows | |
var item = getRandomByClasses(argv, {'layer':'gmlayer'}); | |
item.set({ | |
'layer': 'objects' | |
}); | |
break; | |
case '!concealrandom': | |
case '!hiderandom': | |
// Usage: !revealrandom foo bar | |
// Result: picks a random item with .foo or .bar in the name and hides | |
var item = getRandomByClasses(argv, {'layer':'objects'}); | |
item.set({ | |
'layer': 'gmlayer' | |
}); | |
break; | |
case '!alter': | |
case '!change': | |
// Usage: !alter foo bar > rotation=24.1 aura1_radius=5 aura1_color="#44FF00" | |
// Result: changes properties of all graphics with .foo or .bar | |
// Note that this is not a proper parser, so you need to keep it to | |
// a single space, don't forget the '>' character to separate the | |
// classes from the properties, and put quotes around strings. | |
// Additionally, don't put spaces in values, as it will not work. | |
var classes = []; | |
var alterations = []; | |
var found = false; | |
for (var i = 0, il = argv.length; i < il; i++) { | |
var klass = argv[i]; | |
if (klass == '>') { | |
found = true; | |
continue; | |
} | |
if (!found) { | |
classes[classes.length] = klass; | |
} else { | |
alterations[alterations.length] = klass; | |
} | |
} | |
alterations = alterations.join(' '); | |
log(alterations); | |
eval('var changes = ' + alterations); | |
log(changes); | |
_.each(getByClasses(classes), function(item) { | |
item.set(changes); | |
}); | |
break; | |
} | |
} | |
on("chat:message", function(msg) { | |
var argv = msg.content.split(' '); | |
var command = argv.shift(); | |
if (msg.type != 'api') { | |
return; | |
} | |
log(msg.content); | |
return processCommand(command, argv); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment