Skip to content

Instantly share code, notes, and snippets.

@Kurohyou
Last active July 19, 2016 23:51
Show Gist options
  • Save Kurohyou/1079a21cbceac57c562fbbeb2b8fe468 to your computer and use it in GitHub Desktop.
Save Kurohyou/1079a21cbceac57c562fbbeb2b8fe468 to your computer and use it in GitHub Desktop.
A modification of The Aaron's Recursive Tables mod. Adds the ability to roll on tables with a modification by adding it after the table call in the inline roll like so: [[1t[TableName]mod]] mod needs to be an expression like +2, -4, +2+5, +[[1d6]]. The script also now respects whispers when the message contains a roll template.
// Github: https://github.com/shdwjk/Roll20API/blob/master/RecursiveTable/RecursiveTable.js
// By: The Aaron, Arcane Scriptomancer
// Contact: https://app.roll20.net/users/104025/the-aaron
var RecursiveTable = RecursiveTable || (function() {
'use strict';
var version = '0.1.2',
lastUpdate = 1453302547,
schemaVersion = 0.1,
maxParseDepth = 10,
tableRoll,
checkInstall = function() {
log('-=> RecursiveTable v'+version+' <=- ['+(new Date(lastUpdate*1000))+']');
if( ! _.has(state,'RecursiveTable') || state.RecursiveTable.version !== schemaVersion) {
log(' > Updating Schema to v'+schemaVersion+' <');
state.RecursiveTable = {
version: schemaVersion
};
}
},
ch = function (c) {
var entities = {
'<' : 'lt',
'>' : 'gt',
"'" : '#39',
'@' : '#64',
'{' : '#123',
'|' : '#124',
'}' : '#125',
'[' : '#91',
']' : '#93',
'"' : 'quot',
'-' : 'mdash',
' ' : 'nbsp'
};
if(_.has(entities,c) ){
return ('&'+entities[c]+';');
}
return '';
},
showHelp = function(who) {
sendChat('','/w "'+who+'" '
+'<div style="border: 1px solid black; background-color: white; padding: 3px 3px;">'
+'<div style="font-weight: bold; border-bottom: 1px solid black;font-size: 130%;">'
+'RecursiveTable v'+version
+'</div>'
+'<div style="padding-left:10px;margin-bottom:3px;">'
+'<p>RecursiveTable provides a way to expand the results of Rollable Tables which have inline rolls within them.</p>'
+'</div>'
+'<b>Commands</b>'
+'<div style="padding-left:10px;">'
+'<b><span style="font-family: serif;">!rt [--help| ... ]</span></b>'
+'<div style="padding-left: 10px;padding-right:20px">'
+'<p>Performs all inline rolls, then continues to expand inline rolls (to a maximum depth of around 10).</p>'
+'<ul>'
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">--help</span></b> '+ch('-')+' Shows the Help screen'
+'</li> '
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">...</span></b> '+ch('-')+' Anything following !rt will be expanded, then sent to to the chat. '
+'To roll on a table with a modifier add the modifier after the table call in the inline roll(including a + or -). e.g. [[1t[TableName]+mod]]'
+'</li> '
+'</ul>'
+'</div>'
+'</div>'
+'</div>'
);
},
parseMessage = function(msgs, context ){
var msg = msgs.shift();
if(_.has(msg,'inlinerolls')){
msg.content = _.chain(msg.inlinerolls)
.reduce(function(m,v,k){
if(v.results.rolls.length>1)
var mod;
_.each(v.results.rolls, function(obj){
if(obj.expr){
mod = eval(obj.expr);
};
});
var ti=_.reduce(v.results.rolls,function(m2,v2){
if(_.has(v2,'table')){
log(v2.results[0].tableidx);
var tab = findObjs({
type: 'rollabletable',
name: v2.table
})[0],
entries = findObjs({
type: 'tableitem',
rollabletableid: tab.id,
}),
items = [],
weight;
if(tab){
var rollresult;
_.each(findObjs({
type: 'tableitem',
rollabletableid: tab.id
}), function(element, index, list) {
weight = parseInt(element.get('weight'));
for (var i = 0; i < weight; i++) {
items.push(element);
}
});
}
if(!mod){
mod=0;
};
m2.push(_.reduce(v2.results,function(m3,v3){
rollresult = v2.results[0].tableidx+mod;
log('contextdepth:'+context.depth);
log(rollresult)
if(rollresult>(items.length-1)){
tableRoll = items[items.length-1].get('name');
}else if(rollresult<0){
tableRoll = items[0].get('name');
}else{
tableRoll = items[rollresult].get('name');
};
m3.push(tableRoll)
return m3;
},[]).join(''));
}
return m2;
},[]).join(', ');
m['$[['+k+']]']= (ti.length && ti) || v.results.total || 0;
return m;
},{})
.reduce(function(m,v,k){
return m.replace(k,v);
},msg.content)
.value();
}
if(context.depth < maxParseDepth && msg.content.match(/\[\[.+\]\]/)){
++context.depth;
sendChat('',msg.content,function(msg){parseMessage(msg,context);});
} else {
sendChat(context.who, (context.rolltemplate ? context.whisper+'&{template:'+context.rolltemplate+'} ':'')+msg.content.replace(/%%SLASH%%/,'/'));
}
},
handleInput = function(msg_orig) {
var msg = _.clone(msg_orig),
args, who,
context = {
depth: 0,
who: msg.who,
rolltemplate: msg.rolltemplate,
whisper: '',
};
if (msg.type !== "api") {
return;
}
who=getObj('player',msg.playerid).get('_displayname');
args = msg.content.split(/\s+/);
switch(args[0]) {
case '!rt':
if('--help' === args[1] || args.length===1){
showHelp(who);
} else {
msg.content = msg.content.replace(/^!rt\s+/,'').replace(/\//,'%%SLASH%%');
if(args[1]){
if(args[1].indexOf('/')===0){
context.whisper = args[1] + ' ';
if(args[1]==='/w'){
context.whisper += args[2]+' ';
}
}
}
parseMessage([msg],context);
}
break;
}
},
registerEventHandlers = function() {
on('chat:message', handleInput);
};
return {
CheckInstall: checkInstall,
RegisterEventHandlers: registerEventHandlers
};
}());
on('ready',function() {
'use strict';
RecursiveTable.CheckInstall();
RecursiveTable.RegisterEventHandlers();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment