-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TableExport01.js
297 lines (269 loc) · 11.5 KB
/
TableExport01.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
// Github: https://github.com/shdwjk/Roll20API/blob/master/TableExport/TableExport.js
// By: The Aaron, Arcane Scriptomancer
// Contact: https://app.roll20.net/users/104025/the-aaron
var TableExport = TableExport || (function() {
'use strict';
var version = '0.2.3',
lastUpdate = 1453209012,
tableCache = {},
escapes = {
'[' : '<%%91%%>',
']' : '<%%93%%>',
'--' : '<%%-%%>',
' --' : '[TABLEEXPORT:ESCAPE]'
},
esRE = function (s) {
var escapeForRegexp = /(\\|\/|\[|\]|\(|\)|\{|\}|\?|\+|\*|\||\.|\^|\$)/g;
return s.replace(escapeForRegexp,"\\$1");
},
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 '';
},
checkInstall = function() {
log('-=> TableExport v'+version+' <=- ['+(new Date(lastUpdate*1000))+']');
},
showHelp = function() {
sendChat('',
'/w gm '
+'<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%;">'
+'TableExport v'+version
+'</div>'
+'<div style="padding-left:10px;margin-bottom:3px;">'
+'<p>This script dumps commands to the chat for reconstructing a rollable table on another campaign. While this can be done on your own campaigns via the transmogrifyer, this script allows you to pass those commands to a friend and thus share your own creative works with others.<p>'
+'<p><b>Caveat:</b> Avatar images that are not in your own library will be ignored by the API on import, but will not prevent creation of the table and table items.</p>'
+'</div>'
+'<b>Commands</b>'
+'<div style="padding-left:10px;">'
+'<b><span style="font-family: serif;">!export-table --'+ch('<')+'Table Name'+ch('>')+' [ --'+ch('<')+'Table Name'+ch('>')+' ...]</span></b>'
+'<div style="padding-left: 10px;padding-right:20px">'
+'<p>For all table names, case is ignored and you may use partial names so long as they are unique. For example, '+ch('"')+'King Maximillian'+ch('"')+' could be called '+ch('"')+'max'+ch('"')+' as long as '+ch('"')+'max'+ch('"')+' does not appear in any other table names. Exception: An exact match will trump a partial match. In the previous example, if a table named '+ch('"')+'Max'+ch('"')+' existed, it would be the only table matched for <b>--max</b>.</p>'
+'<ul>'
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">--'+ch('<')+'Table Name'+ch('>')+'</span></b> '+ch('-')+' This is the name of a table to export. You can specify as many tables as you like in a single command.'
+'</li> '
+'</ul>'
+'</div>'
+'</div>'
+'<div style="padding-left:10px;">'
+'<b><span style="font-family: serif;">!import-table --'+ch('<')+'Table Name'+ch('>')+' --'+ch('<')+'[ show | hide ]'+ch('>')+'</span></b>'
+'<div style="padding-left: 10px;padding-right:20px">'
+'<p>This is the command output by <b>!export-table</b> to create the new table. You likely will not need issue these commands directly.</p>'
+'<ul>'
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">--'+ch('<')+'Table Name'+ch('>')+'</span></b> '+ch('-')+' This is the name of the table to be create.'
+'</li> '
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">--'+ch('<')+'[ show | hide ]'+ch('>')+'</span></b> '+ch('-')+' This whether to show the table to players or hide it.'
+'</li> '
+'</ul>'
+'</div>'
+'</div>'
+'<div style="padding-left:10px;">'
+'<b><span style="font-family: serif;">!import-table-item --'+ch('<')+'Table Name'+ch('>')+' --'+ch('<')+'Table Item Name'+ch('>')+' --'+ch('<')+'Weight'+ch('>')+' --'+ch('<')+'Avatar URL'+ch('>')+'</span></b>'
+'<div style="padding-left: 10px;padding-right:20px">'
+'<p>This is the command output by <b>!export-table</b> to create the new table. You likely will not need issue these commands directly.</p>'
+'<ul>'
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">--'+ch('<')+'Table Name'+ch('>')+'</span></b> '+ch('-')+' This is the name of the table to add items to. <b>Note:</b> unlike for <b>!export-table</b>, this must be an exact name match to the created table.'
+'</li> '
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">--'+ch('<')+'Table Item Name'+ch('>')+'</span></b> '+ch('-')+' This is the name of the table item to create. <b>Note:</b> Because the string '+ch('"')+' --'+ch('"')+' may occur in a table item name, you may see '+ch('"')+'[TABLEEXPORT:ESCAPE]'+ch('"')+' show up as a replacement in these commands. This value is corrected internally to the correct '+ch('"')+' --'+ch('"')+' sequence on import.'
+'</li> '
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">--'+ch('<')+'Weight'+ch('>')+'</span></b> '+ch('-')+' This is the weight for this table item and should be an integer value.'
+'</li> '
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">--'+ch('<')+'Avatar URL'+ch('>')+'</span></b> '+ch('-')+' This is the URL for the avatar image of the table item.'
+'</li> '
+'</ul>'
+'</div>'
+'</div>'
+'</div>'
);
},
nameEscape = (function(){
var re=new RegExp('('+_.map(_.keys(escapes),esRE).join('|')+')','g');
return function(s){
return s.replace(re, function(c){ return escapes[c] || c; });
};
}()),
nameUnescape = (function(){
var sepacse = _.invert(escapes),
re=new RegExp('('+_.map(_.keys(sepacse),esRE).join('|')+')','g');
return function(s){
return s.replace(re, function(c){ return sepacse[c] || c; });
};
}()),
handleInput = function(msg) {
var args, matches, tables, tableIDs=[], errors=[], items, itemMatches, accum='';
if (msg.type !== "api" || !playerIsGM(msg.playerid)) {
return;
}
args = msg.content.split(/\s+/);
switch(args[0]) {
case '!import-table':
args = msg.content.split(/\s+--/);
if(args.length === 1) {
showHelp();
break;
}
if(_.has(tableCache,args[1])) {
sendChat('','/w gm '
+'<div style="border: 1px solid black; background-color: white; padding: 3px 3px;">'
+'<span style="font-weight:bold;color:#990000;">Warning:</span> '
+'Table ['+args[1]+'] already exists, skipping create.'
+'</div>'
);
} else {
tableIDs=findObjs({type: 'rollabletable', name: args[1]});
if(tableIDs.length) {
sendChat('','/w gm '
+'<div style="border: 1px solid black; background-color: white; padding: 3px 3px;">'
+'<span style="font-weight:bold;color:#990000;">Warning:</span> '
+'Table ['+args[1]+'] already exists, skipping create.'
+'</div>'
);
} else {
tableIDs=createObj('rollabletable',{
name: args[1],
showplayers: ('show'===args[2])
});
tableCache[args[1]]=tableIDs.id;
}
}
break;
case '!import-table-item':
args = msg.content.split(/\s+--/);
if(args.length === 1) {
showHelp();
break;
}
args[2] = nameUnescape(args[2]);
if(!_.has(tableCache,args[1])) {
tableIDs=findObjs({type: 'rollabletable', name: args[1]});
if(!tableIDs.length) {
sendChat('','/w gm '
+'<div style="border: 1px solid black; background-color: white; padding: 3px 3px;">'
+'<span style="font-weight:bold;color:#990000;">Error:</span> '
+'Table ['+args[1]+'] doesn not exist. Cannot create table item.'
+'</div>'
);
break;
} else {
tableCache[args[1]]=tableIDs[0].id;
}
}
createObj('tableitem',{
name: args[2],
rollabletableid: tableCache[args[1]],
weight: parseInt(args[3],10)||1,
avatar: args[4]||''
});
break;
case '!export-table':
args = msg.content.split(/\s+--/);
if(args.length === 1) {
showHelp();
break;
}
tables=findObjs({type: 'rollabletable'});
matches=_.chain(args)
.rest()
.map(function(n){
var l=_.filter(tables,function(t){
return t.get('name').toLowerCase() === n.toLowerCase();
});
return ( 1 === l.length ? l : _.filter(tables,function(t){
return -1 !== t.get('name').toLowerCase().indexOf(n.toLowerCase());
}));
})
.value();
_.each(matches,function(o,idx){
if(1 !== o.length) {
if(o.length) {
errors.push('Rollable Table [<b>'+args[idx+1]+'</b>] is ambiguous and matches '+o.length+' names: <b><i> '+_.map(o,function(e){
return e.get('name');
}).join(', ')+'</i></b>');
} else {
errors.push('Rollable Table [<b>'+args[idx+1]+'</b>] does not match any names.');
}
}
},errors);
if(errors.length) {
sendChat('','/w gm '
+'<div style="border: 1px solid black; background-color: white; padding: 3px 3px;">'
+'<div><span style="font-weight:bold;color:#990000;">Error:</span> '
+errors.join('</div><div><span style="font-weight:bold;color:#990000;">Error:</span> ')
+'</div>'
+'</div>'
);
break;
}
if( ! errors.length) {
matches=_.chain(matches)
.flatten(true)
.map(function(t){
tableIDs.push(t.id);
return t;
})
.value();
items=findObjs({type: 'tableitem'});
itemMatches=_.chain(items)
.filter(function(i){
return _.contains(tableIDs,i.get('rollabletableid'));
})
.reduce(function(memo,e){
if(!_.has(memo,e.get('rollabletableid'))) {
memo[e.get('rollabletableid')]=[e];
} else {
memo[e.get('rollabletableid')].push(e);
}
return memo;
},{})
.value();
_.each(matches, function(t){
accum+='!import-table --'+nameEscape(t.get('name'))+' --'+(t.get('showplayers') ? 'show' : 'hide')+'<br>';
_.each(itemMatches[t.id], function(i){
accum+='!import-table-item --'+nameEscape(t.get('name'))+' --'+nameEscape(i.get('name'))+' --'+i.get('weight')+' --'+i.get('avatar')+'<br>';
});
});
sendChat('', '/w gm '+accum);
}
break;
}
},
handleRemoveTable = function(obj){
tableCache = _.without(tableCache,obj.id);
},
registerEventHandlers = function() {
on('chat:message', handleInput);
on('destroy:rollabletable', handleRemoveTable);
};
return {
CheckInstall: checkInstall,
RegisterEventHandlers: registerEventHandlers
};
}());
on("ready",function(){
'use strict';
TableExport.CheckInstall();
TableExport.RegisterEventHandlers();
});