Skip to content

Instantly share code, notes, and snippets.

@BigWhale
Created September 2, 2018 23:00
Show Gist options
  • Save BigWhale/0363e41dd99651edb56345778e05acc7 to your computer and use it in GitHub Desktop.
Save BigWhale/0363e41dd99651edb56345778e05acc7 to your computer and use it in GitHub Desktop.
Glitch check for Roll20 Shadowrun 5E advanced template
on("ready", function() {
log("Woo, I am here!");
on('chat:message', function(msg) {
var ones = 0,
total = 0,
hits = 0,
res,
who,
re = new RegExp("^{{name=(.*?)}}");
if (msg.type !== "api") {
if (msg.inlinerolls) {
log("Getting results.");
// Extract name of the character from the message
// then use it to send a message in chat
var speaking;
var name = re.exec(msg.content);
var characters = findObjs({_type: 'character'});
characters.forEach(function(chr) { if(chr.get('name') == name[1]) speaking = chr; });
// Last inlineroll is the one we want. Not really safe.
if (msg.inlinerolls[msg.inlinerolls.length - 1].signature !== false) {
log(msg.inlinerolls[msg.inlinerolls.length - 1]);
res = msg.inlinerolls[msg.inlinerolls.length - 1].results.rolls[0].results;
// This often fails, because results is missing reduce() !?
// Sometimes there's an extra {"d": 1, v: "..."} in the results?
// That's why all the extra counters
// ones = res.reduce((a, b) => a + (b["v"] == 1 ? 1 : 0), 0)
for (var j = 0; j < res.length; j++) {
if (res[j]["d"]) {
log('Ignore me.');
} else {
total++;
if (res[j]["v"] == 1) {
ones++;
}
if (res[j]["v"] >= 5) {
hits++;
}
}
}
}
log("Dice: " + total + " Hits: " + hits + " | Ones: " + ones);
if (ones > (Math.floor(total / 2))) {
if (hits < 1) {
sendChat('character|' + speaking.id, '/me CRITICALLY GLITCHED!');
log("BIIIIG!");
} else {
sendChat('character|' + speaking.id, '/me GLITCHED!');
}
log("Whoopsie!");
}
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment