forked from Roll20/roll20-api-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathditv.js
341 lines (321 loc) · 11.2 KB
/
ditv.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
var DitV = DitV || {
GRID_SIZE: 70,
BOX_WIDTH: 6,
BOX_HEIGHT: 4,
CHIP_HEIGHT: 4,
CHIP_START: 2,
MAX_VALUE: 10,
DEFAULT_URLS: {
//1: "https://s3.amazonaws.com/files.d20.io/images/USER_ID/SOME_RANDOM_CHARACTERS/thumb.png"
},
GENERIC_URL: "https://s3.amazonaws.com/files.d20.io/images/8713360/xSx7ppXB9KD2hIvGf64TaQ/thumb.png?1428459149",
init: function(){
if (!state.hasOwnProperty('DitV')){ state.DitV = {}; }
if (!state.DitV.hasOwnProperty('characters')){ state.DitV.characters = {}; }
if (!state.DitV.hasOwnProperty('chipImages')){ state.DitV.chipImages = {}; }
if (!state.DitV.CHIP_URLS){ state.DitV.CHIP_URLS = {}; }
for (var i = 1; i <= DitV.MAX_VALUE; i++){
if ((!state.DitV.CHIP_URLS[i]) && (DitV.DEFAULT_URLS[i])){
state.DitV.CHIP_URLS[i] = DitV.DEFAULT_URLS[i];
}
}
DitV.MAX_STACK = Math.floor(DitV.GRID_SIZE / DitV.CHIP_HEIGHT);
},
addCharacter: function(name, x, y, color){
var pageId = Campaign().get('playerpageid');
if (!pageId){ return "Unable to determine player page."; }
if (state.DitV.characters[name]){ return "Character '" + name + "' already registered."; }
if (!x){ x = 0; }
if (!y){ y = 0; }
if (!color){ color = "#ffff00"; }
var path = [["M", 0, 0], ["L", DitV.BOX_WIDTH * DitV.GRID_SIZE, 0], ["L", DitV.BOX_WIDTH * DitV.GRID_SIZE, DitV.BOX_HEIGHT * DitV.GRID_SIZE],
["L", 0, DitV.BOX_HEIGHT * DitV.GRID_SIZE], ["L", 0, 0]];
var box = createObj("path", {
_pageid: pageId,
_path: JSON.stringify(path),
stroke: color,
left: x * DitV.GRID_SIZE + DitV.BOX_WIDTH * DitV.GRID_SIZE / 2,
top: y * DitV.GRID_SIZE + DitV.BOX_HEIGHT * DitV.GRID_SIZE / 2,
width: DitV.BOX_WIDTH * DitV.GRID_SIZE,
height: DitV.BOX_HEIGHT * DitV.GRID_SIZE,
layer: "map"});
state.DitV.characters[name] = {
'x': x,
'y': y,
'box': box.id,
'chips': {}
};
},
removeCharacter: function(name){
if (!state.DitV.characters[name]){ return "Character '" + name + "' not registered."; }
var error = DitV.clearChips(name);
if (state.DitV.characters[name]['box']){
var box = getObj("path", state.DitV.characters[name]['box']);
if (box){ box.remove(); }
delete state.DitV.characters[name]['box'];
}
delete state.DitV.characters[name];
return error;
},
clearChips: function(name){
if (!state.DitV.characters[name]){ return "Character '" + name + "' not registered."; }
for (var value in state.DitV.characters[name]['chips']){
while ((state.DitV.characters[name]['chips'][value]) && (state.DitV.characters[name]['chips'][value].length > 0)){
var token = getObj("graphic", state.DitV.characters[name]['chips'][value].pop());
if (token){ token.remove(); }
}
}
state.DitV.characters[name]['chips'] = {};
},
addChip: function(name, value){
var pageId = Campaign().get('playerpageid');
if (!pageId){ return "Unable to determine player page."; }
if (!state.DitV.characters[name]){ return "Character '" + name + "' not registered."; }
var character = state.DitV.characters[name];
var pos = value - 1 + DitV.CHIP_START; // position in wrapped list of 1x2 stacks (CHIP_START is the offset past the reserved top-left area)
var x = (pos % DitV.BOX_WIDTH) + character['x'];
var y = Math.floor(pos / DitV.BOX_WIDTH) * 2 + character['y'] + 1;
// translate from squares to pixels, and adjust for the fact that Roll20 uses center instead of top-left for position
x = (x * DitV.GRID_SIZE) + (DitV.GRID_SIZE / 2);
y = (y * DitV.GRID_SIZE) + (DitV.GRID_SIZE / 2);
// offset chip image upwards to place on top of stack
y -= Math.min((character['chips'][value] ? character['chips'][value].length : 0), DitV.MAX_STACK) * DitV.CHIP_HEIGHT;
var token = createObj("graphic", {
_subtype: "token",
_pageid: pageId,
imgsrc: (state.DitV.CHIP_URLS[value] || DitV.GENERIC_URL),
left: x,
top: y,
width: DitV.GRID_SIZE,
height: DitV.GRID_SIZE,
layer: "objects",
isdrawing: true,
/////
//
controlledby: "all"
//
/////
});
if (!token){ return "Failed to create token for chip with value " + value; }
/////
//
if(!state.DitV.CHIP_URLS[value]){ token.set("status_blue", (value < 10 ? value : true)); }
//
/////
if (!character['chips'][value]){ character['chips'][value] = []; }
character['chips'][value].push(token.id);
},
addChips: function(name, counts){
if (!state.DitV.characters[name]){ return "Character '" + name + "' not registered."; }
for (var i = 0; i < counts.length; i++){
for (var j = 0; j < counts[i]; j++){
var error = DitV.addChip(name, i + 1);
if (error){ return error; }
}
}
},
rollChips: function(name, rollSpec){
var specArray = rollSpec.split(/[+\s]/);
var rollCounts = new Array(DitV.MAX_VALUE);
for (var i = 0; i < specArray.length; i++){
var spec = specArray[i].split("d");
if (spec.length != 2){
return "Malformed roll specification: " + specArray[i];
}
var diceCount = parseInt(spec[0]);
var dieSize = parseInt(spec[1]);
for (j = 0; j < diceCount; j++){
var roll = randomInteger(dieSize);
rollCounts[roll - 1] = (rollCounts[roll - 1] || 0) + 1;
}
}
var error = DitV.addChips(name, rollCounts);
return error || rollCounts;
},
countChips: function(name){
if (!state.DitV.characters[name]){ return "Character '" + name + "' not registered."; }
var chips = state.DitV.characters[name]['chips'];
var counts = {};
for (var value in chips){
// prune references to chips whose tokens have been deleted before counting remaining chips
chips[value] = chips[value].filter(function(tokenId){ return getObj("graphic", tokenId); });
counts[value] = chips[value].length;
}
return counts;
},
/////
//
//chip maintenance (reclaim and restack chips)?
//
/////
write: function(s, who, style, from){
if (who){
who = "/w " + who.split(" ", 1)[0] + " ";
}
sendChat(from, who + s.replace(/</g, "<").replace(/>/g, ">").replace(/\n/g, "<br>"));
},
showHelp: function(who, cmd){
DitV.write(cmd + " commands:", who, "", "DitV");
var helpMsg = "";
helpMsg += "help: display this help message\n";
helpMsg += "add NAME X Y [COLOR] add character with specified name, coordinates, and color\n";
helpMsg += "remove NAME remove specified character\n";
helpMsg += "clear NAME clear specified character's chips\n";
helpMsg += "roll NAME DICE roll DICE (e.g. \"3d4+1d6\") and add to specified character's chips\n";
helpMsg += "count NAME count specified character's chips and display for all to see\n";
/////
//
//chip maintenance?
//
/////
helpMsg += "setimage VALUE [URL] set the image URL for the specified chip value (uses selected token's image if no URL specified)\n";
helpMsg += "images display a table of chip images\n";
DitV.write(helpMsg, who, "font-size: small; font-family: monospace", "DitV");
},
handleDitVMessage: function(tokens, msg){
if (tokens.length < 2){
return DitV.showHelp(msg.who, tokens[0]);
}
var error = "";
switch (tokens[1]){
case "add":
if (tokens.length <= 4){
error = "The 'add' command requires at least three arguments: character name, x coordinate, and y coordinate";
break;
}
error = DitV.addCharacter(tokens[2], parseInt(tokens[3]), parseInt(tokens[4]), tokens[5]);
break;
case "remove":
if (tokens.length <= 2){
error = "The 'remove' command requires one argument: character name";
break;
}
error = DitV.removeCharacter(tokens[2]);
break;
case "clear":
if (tokens.length <= 2){
error = "The 'clear' command requires one argument: character name";
break;
}
error = DitV.clearChips(tokens[2]);
break;
case "roll":
if (tokens.length <= 3){
error = "The 'roll' command requires two argumenst: character name and dice specification";
break;
}
var rollSpec = tokens.slice(3).join(" ");
var rollCounts = DitV.rollChips(tokens[2], rollSpec);
if (typeof(rollCounts) == typeof("")){
error = rollCounts;
}
else{
var rollMsg = tokens[2] + " rolling " + rollSpec.replace(/ /g, "+") + ":";
var joinStr = " ";
for (var i = 0; i <rollCounts.length; i++){
for (var j = 0; j < rollCounts[i]; j++){
rollMsg += joinStr + (i + 1);
joinStr = ", ";
}
}
DitV.write(rollMsg, "", "", msg.who);
}
break;
case "count":
if (tokens.length <= 2){
error = "The 'count' command requires one argument: character name";
break;
}
var counts = DitV.countChips(tokens[2]);
if (typeof(counts) == typeof("")){
error = counts;
break;
}
var countMsg = "&{template:default} {{name=" + tokens[2] + " Chip Count}}";
for (var i = 1; i <= DitV.MAX_VALUE; i++){
if (counts[i]){
countMsg += " {{" + i + "=" + counts[i] + "}}";
}
}
DitV.write(countMsg, "", "", msg.who);
break;
case "setimage":
if (tokens.length <= 2){
error = "The 'setimage' command requires at least one argument: chip value";
break;
}
var value = parseInt(tokens[2]);
if ((value <= 0) || (value > DitV.MAX_VALUE)){
error = "Chip value must be between 1 and " + DitV.MAX_VALUE;
break;
}
var imgUrl = "";
if (tokens.length > 3){
imgUrl = tokens[3];
}
else{
if ((!msg.selected) || (msg.selected.length != 1) || (msg.selected[0]._type != "graphic")){
error = "Must either pass a URL or call setimage with exactly one token selected";
break;
}
var imgToken = getObj(msg.selected[0]._type, msg.selected[0]._id);
if (!imgToken){
error = "Unable to get selected token";
break;
}
log("Token URL for value " + value + ": " + imgToken.get('imgsrc'));
imgUrl = imgToken.get('imgsrc').replace(/[/][^/.]*[.](jpg|png)/, "/thumb.$1");
}
state.DitV.CHIP_URLS[value] = imgUrl;
break;
case "images":
var imgMsg = "&{template:default} {{name=Chip Images}} {{Default=";
if (DitV.GENERIC_URL){
imgMsg += "[" + DitV.GENERIC_URL + "](" + DitV.GENERIC_URL + ")";
}
else{
imgMsg += "undefined";
}
imgMsg += "}}";
for (var i = 1; i <= DitV.MAX_VALUE; i++){
imgMsg += "{{" + i + "=";
if (state.DitV.CHIP_URLS[i]){
var imgUrl = state.DitV.CHIP_URLS[i].replace(/[?]\d+$/, "");
imgMsg += "[" + state.DitV.CHIP_URLS[i] + "](" + state.DitV.CHIP_URLS[i] + ")";
}
else{
imgMsg += "undefined";
}
imgMsg += "}}";
}
DitV.write(imgMsg, msg.who, "", "DitV");
break;
case "help":
DitV.showHelp(msg.who, tokens[0]);
break;
default:
DitV.write("Error: Unrecognized command: " + tokens[0], msg.who, "", "DitV");
DitV.showTrackerHelp(msg.who, tokens[0]);
}
if (error){
DitV.write("Error: " + error, msg.who, "", "DitV");
}
},
handleChatMessage: function(msg){
if ((msg.type != "api") || (msg.content.indexOf("!ditv") !=0 )){ return; }
return DitV.handleDitVMessage(msg.content.split(" "), msg);
},
registerDitV: function(){
DitV.init();
if ((typeof(Shell) != "undefined") && (Shell) && (Shell.registerCommand)){
Shell.registerCommand("!ditv", "!ditv <subcommand> [args]", "Dogs in the Vineyard dice tracker", DitV.handleDitVMessage);
if (Shell.write){
DitV.write = Shell.write;
}
}
else{
on("chat:message", DitV.handleChatMessage);
}
}
};
on("ready", function(){ DitV.registerDitV(); });