-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecursiveTable.js
402 lines (362 loc) · 15.1 KB
/
RecursiveTable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// 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.2.1',
lastUpdate = 1500986989,
schemaVersion = 0.1,
defaults = {
maxdepth: 10,
delimiter: ', ',
dropempty: true,
sort: false,
prefaceuniquespace: false,
emptydefault: '',
who: ''
},
regex = {
rtCmd: /^(!rt)(?:\[([^\]]*)\])?\s*/,
inlineRoll: /\[\[.*\]\]/
},
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
};
}
},
sendChatP = function(msg){
return new Promise((resolve) =>{
sendChat('',msg.replace(/\[\[\s+/,'[[').replace(/\[\[\s+\[\[/,'[[[['),(res)=>{
resolve(res[0]);
});
});
},
ch = function (c) {
var entities = {
'<' : 'lt',
'>' : 'gt',
"'" : '#39',
'@' : '#64',
'{' : '#123',
'|' : '#124',
'}' : '#125',
'[' : '#91',
']' : '#93',
'&' : 'amp',
'"' : 'quot',
'-' : 'mdash',
' ' : 'nbsp'
};
if(_.has(entities,c) ){
return ('&'+entities[c]+';');
}
return '';
},
esRE = function (s) {
var escapeForRegexp = /(\\|\/|\[|\]|\(|\)|\{|\}|\?|\+|\*|\||\.|\^|\$)/g;
return s.replace(escapeForRegexp,"\\$1");
},
HE = (function(){
var entities={
//' ' : '&'+'nbsp'+';',
'<' : '&'+'lt'+';',
'>' : '&'+'gt'+';',
"'" : '&'+'#39'+';',
'@' : '&'+'#64'+';',
'{' : '&'+'#123'+';',
'|' : '&'+'#124'+';',
'}' : '&'+'#125'+';',
'[' : '&'+'#91'+';',
']' : '&'+'#93'+';',
'"' : '&'+'quot'+';'
},
re=new RegExp('('+_.map(_.keys(entities),esRE).join('|')+')','g');
return function(s){
return s.replace(re, function(c){ return entities[c] || c; });
};
}()),
makeSuffixer = function(suffix){
let n = 0;
return (val)=>val+suffix.repeat(n++);
},
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. Now with options and support for whispering Roll Templates.</p>'+
'<p>When using Rolltemplates, your message must have at least one <code>{{</code> that in not coming from a Rollable Table. When using the <code>PrefaceUniqueSpace</code> option, be sure your <code>{{name=something}}</code> is first.</p>'+
'</div>'+
'<b>Commands</b>'+
'<div style="padding-left:10px;">'+
'<b><span style="font-family: serif;">!rt[options] [--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.'+
'</li> '+
'</ul>'+
'</div>'+
'<div style="padding-left: 10px;padding-right:20px">'+
'<p><b>Options</b> - These are inline settings to adjust how the rolls are put together. Options are specified in <code>'+ch('[')+'</code> <code>'+ch(']')+'</code> right after the <code>!rt</code>:</p>'+
'<pre style="white-space:normal;word-break:normal;word-wrap:normal;">'+
'!rt'+ch('[')+'delimiter:-|maxdepth:20'+ch(']')+' something'+
'</pre>'+
'<p>Options are separated with the verticle pipe symbol (<code>'+ch('|')+'</code>) and have an optional argument separated by a <b>:</b>. Omitting the argument causes <b>true</b> to be used for switch options, or the default value. All Options are case insenstive. Options are one of 3 types: Number (any integer), Boolean (true values: <code>on</code>, <code>yes</code>, <code>y</code>, <code>true</code>. false values: <code>off</code>, <code>no</code>, <code>n</code>, <code>false</code>), or text (any value except <code>]</code>, use <code>\\'+ch('|')+'</code> for <code>'+ch('|')+'</code>) </p>'+
'<ul>'+
'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'+
'<b><span style="font-family: serif;">MaxDepth</span></b> '+ch('-')+' Specifies the number of recursions to perform. <b>Default: 10 (Number)</b>'+
'</li> '+
'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'+
'<b><span style="font-family: serif;">Delimiter</span></b> '+ch('-')+' A string of text to put between table items. The special value <code>BR</code> will cause html line breaks to be used. <b>Default: <code>, </code>(String)</b>'+
'</li> '+
'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'+
'<b><span style="font-family: serif;">DropEmpty</span></b> '+ch('-')+' Causes empty table items to be dropped before joining with the delimiter. <b>Default: on (Boolean)</b>'+
'</li> '+
'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'+
'<b><span style="font-family: serif;">Sort</span></b> '+ch('-')+' Causes table items to be sorted before being joined by the delimiter. Note that this happens at a single layer of recursion, so if you have table items made of of lists of table items, the sorting will only be at each level. <b>Default: off (Boolean)</b>'+
'</li> '+
'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'+
'<b><span style="font-family: serif;">PrefaceUniqueSpaces</span></b> '+ch('-')+' Causes the final message to have a unique number of spaces inserted after each <code>'+ch('{')+ch('{')+'</code>. This is useful if you'+ch("'")+'re building Roll Templates and might have multiple lines with the same label. <b>Default: off (Boolean)</b>'+
'</li> '+
'</ul>'+
'</div>'+
'<b>Examples</b>'+
'<div style="padding-left:10px;">'+
'<p>Basic usage, whispering treasure to the gm.</p>'+
'<pre style="white-space:normal;word-break:normal;word-wrap:normal;">'+
'!rt /w gm '+ch('[')+ch('[')+'1t'+ch('[')+'treasure-table'+ch(']')+ch(']')+ch(']')+
'</pre>'+
'<p>Whispering a roll template:</p>'+
'<pre style="white-space:normal;word-break:normal;word-wrap:normal;">'+
'!rt /w gm '+ch('&')+ch('{')+'template:default'+ch('}')+ch('{')+ch('{')+'treasure='+ch('[')+ch('[')+'1t'+ch('[')+'treasure-table'+ch(']')+ch(']')+ch(']')+''+ch('}')+ch('}')+''+
'</pre>'+
'<p>Whispering a roll template, with each item on a separate line:</p>'+
'<pre style="white-space:normal;word-break:normal;word-wrap:normal;">'+
'!rt'+ch('[')+'Delimiter:BR'+ch(']')+' /w gm '+ch('&')+ch('{')+'template:default'+ch('}')+ch('{')+ch('{')+'treasure='+ch('[')+ch('[')+'1t'+ch('[')+'treasure-table'+ch(']')+ch(']')+ch(']')+''+ch('}')+ch('}')+''+
'</pre>'+
'<p>Whispering a roll template, with each item on a separate line, with empty results replaced by <code>Nothing</code>:</p>'+
'<pre style="white-space:normal;word-break:normal;word-wrap:normal;">'+
'!rt'+ch('[')+'Delimiter:BR|EmptyDefault:Nothing'+ch(']')+' /w gm '+ch('&')+ch('{')+'template:default'+ch('}')+ch('{')+ch('{')+'treasure='+ch('[')+ch('[')+'1t'+ch('[')+'treasure-table'+ch(']')+ch(']')+ch(']')+''+ch('}')+ch('}')+''+
'</pre>'+
'<p>Whispering a roll template, with each item on a separate line, with a table that is returning <code>{{label=values'+ch('}')+ch('}')+'</code>:</p>'+
'<pre style="white-space:normal;word-break:normal;word-wrap:normal;">'+
'!rt'+ch('[')+'Delimiter:BR|PrefaceUniqueSpace'+ch(']')+' '+ch('&')+ch('{')+'template:default'+ch('}')+ch('{')+ch('{')+'name=Treasure Bundles'+ch('}')+ch('}')+''+ch('[')+ch('[')+'5t'+ch('[')+'treasure-bundle'+ch(']')+ch(']')+ch(']')+
'</pre>'+
'</div>'+
'</div>'+
'</div>'
);
},
getAsBoolean = function(val,defVal){
let isTrue = _.contains(['on','yes','y','true'],val.toLowerCase()),
isFalse = _.contains(['off','no','n','false'],val.toLowerCase());
log({isTrue,isFalse});
if(isTrue || isFalse){
return !isFalse;
}
return !_.isUndefined(defVal) ? defVal : val;
},
parseOptions = function(cmdOpts){
return _.chain((cmdOpts||'').replace(/((?:\\.|[^|])*)\|/g,'$1\n').replace(/\\/,'').split(/\n/))
.filter((a)=>a.length)
.reduce((m,o)=>{
let tok=o.split(/:/),
c=tok.shift().toLowerCase(),
a=tok.join(':')||true;
switch(c){
case 'maxdepth':
a=parseInt(a,10)||defaults[c];
break;
case 'dropempty':
case 'sort':
case 'prefaceuniquespace':
a=getAsBoolean(a,defaults[c]);
break;
case 'emptydefault':
break;
case 'delimiter':
switch(a.toLowerCase()){
case 'br':
a = '<br>';
break;
default:
}
break;
}
m[c]=a;
return m;
},_.clone(defaults))
.value();
},
rollAndParseInlines = function(roll,opts){
return new Promise((returnText)=>{
sendChatP(roll)
.then((msg)=>{
parseInlines(msg.inlinerolls,opts)
.then((subs)=>{
returnText(_.reduce(subs,(m,v,k)=>m.replace(k,v),msg.content));
})
.catch((e)=>{
let eRoll=HE(roll);
sendChat(`RecursiveTables`,`/w "${opts.who}" <div>An error occured while filling the results of this roll: <code>${eRoll}</code></div><div>Error: <code>${e.message}</code></div>`);
});
})
.catch((e)=>{
let eRoll=HE(roll);
sendChat(`RecursiveTables`,`/w "${opts.who}" <div>An error occured parsing this roll: <code>${eRoll}</code></div><div>Error: <code>${e.message}</code></div>`);
});
});
},
parseInlines = function(inlines,opts){
return new Promise((returnSubs)=>{
let subOpts = _.clone(opts),
subs = {},
context = {},
promises=[];
--subOpts.maxdepth;
_.each(inlines,(rollRecord,msgIdx)=>{
const key=`$[[${msgIdx}]]`,
result = rollRecord.results.total;
context[key]={
result: rollRecord.results.total
};
_.each(rollRecord.results.rolls,(roll)=>{
if(_.has(roll,'table')){
context[key].hasText=false;
context[key].parts=[];
context[key].sentinal=0;
_.each(roll.results, (die,dieIdx)=>{
if(_.has(die,'tableItem') && _.isString(die.tableItem.name) && !die.tableItem.name.match(/^\d+$/)){
if( regex.inlineRoll.test(die.tableItem.name)){
if(subOpts.maxdepth){
++context[key].sentinal;
promises.push(new Promise((done)=>{
rollAndParseInlines(die.tableItem.name,subOpts)
.then((text)=>{
context[key].parts[dieIdx]=text;
--context[key].sentinal;
if(!context[key].sentinal){
subs[key]=_.compose(
(opts.sort ? (x)=>x.sort() : _.identity),
(opts.dropempty ? (x)=>_.filter(x,(v)=>`${v}`.trim().length) : _.identity)
)(context[key].parts).join(opts.delimiter);
}
done(true);
})
.catch((e)=>{
let eRoll=HE(die.tableItem.name);
sendChat(`RecursiveTables`,`/w "${opts.who}" <div>An Error occured with this TableItem: <code>${eRoll}</code></div><div>Error: <code>${e.message}</code></div>`);
});
}));
} else {
context[key].parts[dieIdx]=`${die.tableItem.name}`.trim()||opts.emptydefault;
}
} else {
context[key].parts[dieIdx]=`${die.tableItem.name}`.trim()||opts.emptydefault;
}
context[key].hasText=true;
} else {
context[key].parts[dieIdx]=die.v;
}
});
if(context[key].hasText){
subs[key]=_.compose(
(opts.sort ? (x)=>x.sort() : _.identity),
(opts.dropempty ? (x)=>_.filter(x,(v)=>`${v}`.trim().length) : _.identity)
)(context[key].parts).join(opts.delimiter);
} else {
subs[key]=result;
}
} else {
subs[key]=result;
}
});
});
if(promises.length){
Promise.all(promises)
.then(()=>{
returnSubs(subs);
})
.catch((e)=>{
let eRoll=HE(_.pluck(inlines,'expression').join(', '));
sendChat(`RecursiveTables`,`/w "${opts.who}" <div>An Error occurred: <code>${eRoll}</code></div><div>Error: <code>${e.message}</code></div>`);
});
} else {
returnSubs(subs);
}
});
},
parseMessage = function(msg, opts){
parseInlines(msg.inlinerolls, opts)
.then((subs)=>{
msg.content= _.reduce(subs,(m,v,k)=>m.replace(k,v),msg.content);
sendChat(msg.who||'[BLANK]',(msg.content||'[EMPTY]').replace(/\{\{/g,(opts.prefixuniquespaces ? makeSuffixer(' ') : _.identity)));
})
.catch((e)=>{
let eRoll=HE(msg.content);
sendChat(`RecursiveTables`,`/w "${opts.who}" <div>An Error occured with this message: <code>${eRoll}</code></div><div>Error: <code>${e.message}</code></div>`);
});
},
handleInput = function(msg_orig) {
var msg = _.clone(msg_orig),
args, who, cmd, opts;
try {
if (msg.type !== "api") {
return;
}
who=(getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname');
args = msg.content.split(/\s+/);
cmd = (args.shift().match(regex.rtCmd)||[]).splice(1);
switch(cmd[0]) {
case '!rt':
if('--help' === args[0]){
showHelp(who);
} else {
opts = parseOptions(cmd[1]);
opts.who = who;
msg.content = msg.content.replace(regex.rtCmd,'');
if(_.has(msg,'rolltemplate') && _.isString(msg.rolltemplate) && msg.rolltemplate.length){
msg.content = msg.content.replace(/\{\{/,'&{template:'+msg.rolltemplate+'} {{');
}
parseMessage(msg,opts);
}
break;
}
} catch (e) {
let who=(getObj('player',msg_orig.playerid)||{get:()=>'API'}).get('_displayname');
sendChat('RecursiveTables',`/w "${who}" `+
`<div style="border:1px solid black; background-color: #ffeeee; padding: .2em; border-radius:.4em;" >`+
`<div>There was an error while trying to run your command:</div>`+
`<div style="margin: .1em 1em 1em 1em;"><code>${msg_orig.content}</code></div>`+
`<div>Please <a class="showtip tipsy" title="The Aaron's profile on Roll20." style="color:blue; text-decoration: underline;" href="https://app.roll20.net/users/104025/the-aaron">send me this information</a> so I can make sure this doesn't happen again (triple click for easy select in most browsers.):</div>`+
`<div style="font-size: .6em; line-height: 1em;margin:.1em .1em .1em 1em; padding: .1em .3em; color: #666666; border: 1px solid #999999; border-radius: .2em; background-color: white;">`+
JSON.stringify({msg: msg_orig, stack: e.stack})+
`</div>`+
`</div>`
);
}
},
registerEventHandlers = function() {
on('chat:message', handleInput);
};
return {
CheckInstall: checkInstall,
RegisterEventHandlers: registerEventHandlers
};
}());
on('ready',function() {
'use strict';
RecursiveTable.CheckInstall();
RecursiveTable.RegisterEventHandlers();
});