-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeBomb.js
300 lines (262 loc) · 13.3 KB
/
TimeBomb.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
/*
TimeBomb
Add a "time bomb" to the Combat Tracker in Roll20
On Github: https://github.com/blawson69
Contact me: https://app.roll20.net/users/1781274/ben-l
Like this script? Become a patron:
https://www.patreon.com/benscripts
*/
var TimeBomb = TimeBomb || (function () {
'use strict';
//---- INFO ----//
var version = '0.1',
debugMode = false,
styles = {
box: 'background-color: #fff; border: 1px solid #000; padding: 8px 10px; border-radius: 6px; margin-left: -40px; margin-right: 0px;',
title: 'padding: 0 0 10px 0; color: ##591209; font-size: 1.5em; font-weight: bold; font-variant: small-caps; font-family: "Times New Roman",Times,serif;',
button: 'background-color: #000; border-width: 0px; border-radius: 5px; padding: 5px 8px; color: #fff; text-align: center;',
textButton: 'background-color: transparent; border: none; padding: 0; color: #591209; text-decoration: underline;',
buttonWrapper: 'text-align: center; margin: 10px 0; clear: both;',
code: 'font-family: "Courier New", Courier, monospace; background-color: #ddd; color: #000; padding: 2px 4px;',
alert: 'color: #C91010; font-size: 1.5em; font-weight: bold; font-variant: small-caps; text-align: center;'
},
checkInstall = function () {
if (!_.has(state, 'TimeBomb')) state['TimeBomb'] = state['TimeBomb'] || {};
if (typeof state['TimeBomb'].bombs == 'undefined') state['TimeBomb'].bombs = [];
log('--> TimeBomb v' + version + ' <-- Primed and ready. Let\'s blow sh*t up!');
if (debugMode) {
var d = new Date();
showDialog('Debug Mode', 'TimeBomb v' + version + ' loaded at ' + d.toLocaleTimeString() + '.', 'GM');
}
},
//----- INPUT HANDLER -----//
handleInput = function (msg) {
if (msg.type == 'api' && msg.content.startsWith('!bomb') && playerIsGM(msg.playerid)) {
var parms = msg.content.split(/\s+/i);
if (parms[1]) {
switch (parms[1]) {
case 'create':
commandCreate(msg);
break;
case 'add':
commandAdd(msg);
break;
case 'reset':
commandReset(msg);
break;
case 'hide':
commandHideResult(msg);
break;
case 'destroy':
commandDestroy(msg);
break;
case 'help':
default:
commandHelp(msg);
}
} else {
commandHelp(msg);
}
}
},
commandCreate = function (msg) {
if (_.size(msg.selected) !== 2) {
showDialog('Error', 'You have not selected the right number of tokens! Please select a "Bomb" token and a "Result" token.', msg.who);
return;
}
var bomb, result;
_.each(msg.selected, function (obj) {
var token = getObj(obj._type, obj._id);
if (token) {
var nameVal = token.get("bar1_value") ;
if (nameVal === 'Bomb') bomb = token;
if (nameVal === 'Result') result = token;
}
});
if (typeof bomb != 'undefined' &&_.find(state['TimeBomb'].bombs, function (x) { return x == bomb.get('id'); })) {
showDialog('Error', '"' + bomb.get('name') + '" has already been created.', msg.who);
return;
}
if (typeof bomb != 'undefined' && typeof result != 'undefined') {
var lOffset = parseInt(result.get('left')) - parseInt(bomb.get('left'));
var tOffset = parseInt(result.get('top')) - parseInt(bomb.get('top'));
bomb.set({bar3_value: result.get('id'), bar2_value: (bomb.get('bar2_value') == '' || !isNum(bomb.get('bar2_value')) ? 10 : bomb.get('bar2_value')), bar3_max: lOffset + '|' + tOffset, showplayers_bar1: false, showplayers_bar2: false, showplayers_bar3: false, playersedit_bar1: false, playersedit_bar2: false, playersedit_bar3: false, playersedit_name: false});
result.set({layer: 'walls', bar3_value: bomb.get('id'), showplayers_bar1: false, showplayers_bar2: false, showplayers_bar3: false, showplayers_name: false, playersedit_bar1: false, playersedit_bar2: false, playersedit_bar3: false, playersedit_name: false});
state['TimeBomb'].bombs.push(bomb.get('id'));
showDialog('Success', '"' + bomb.get('name') + '" was created successfully! When ready to trigger the countdown (add it to the Turn Tracker), use <span style=\'' + styles.code + '\'>!bomb add</span> with the bomb token selected.', msg.who);
} else {
var err = "Couldn't find a required piece:<ul>"
+ (typeof bomb == 'undefined' ? '<li>A token named "Bomb".</li>' : '')
+ (typeof result == 'undefined' ? '<li>A token named "Result".</li>' : '')
+ '</ul>';
showDialog('Error', err, msg.who);
}
},
commandAdd = function (msg) {
var bomb = getObj(msg.selected[0]._type, msg.selected[0]._id);
var to = getTurnOrder();
if (bomb && _.find(state['TimeBomb'].bombs, function (x) { return x == bomb.get('id'); }) && !_.find(to, function (y) { return y.id == bomb.get('id'); })) {
to.push({
id: bomb.get('id'),
pr: bomb.get('bar2_value'),
formula: -1
});
setTurnOrder(to);
} else {
var err = (_.find(to, function (x) { return x.id == bomb.get('id'); })) ? '"' + ((bomb) ? bomb.get('name') : 'unknown') + '" is already in the Turn Tracker' : 'Your Bomb must be created before it can be added to the Turn Tracker.';
showDialog('Error', err, msg.who);
}
},
commandReset = function (msg) {
var parms = msg.content.split(/\s+/i);
var token = getObj('graphic', parms[2]), bomb, result;
if (token) {
bomb = token;
result = getObj('graphic', bomb.get('bar3_value'));
}
if (bomb && result) {
var lOffset = parseInt(result.get('left')) - parseInt(bomb.get('left'));
var tOffset = parseInt(result.get('top')) - parseInt(bomb.get('top'));
result.set({layer: 'walls'});
bomb.set({layer: 'objects', bar3_value: result.get('id'), bar3_max: lOffset + '|' + tOffset, showplayers_bar1: false});
state['TimeBomb'].bombs.push(bomb.get('id'));
showDialog('Bomb Reset', '"' + bomb.get('name') + '" has been successfully reset. When ready to trigger the countdown (add it to the Turn Tracker), use <span style=\'' + styles.code + '\'>!bomb add</span> with the bomb token selected.', msg.who);
var bomb = getObj('graphic', result.get('bar3_value'));
} else {
var err = "Couldn't find a required piece:<ul>"
+ (typeof bomb == 'undefined' ? '<li>A token named "Bomb".</li>' : '')
+ (typeof result == 'undefined' ? '<li>A token named "Result".</li>' : '')
+ '</ul>';
showDialog('Error', err, msg.who);
}
},
commandHideResult = function (msg) {
var parms = msg.content.split(/\s+/i);
var result = getObj('graphic', parms[2]);
if (result) {
result.set({layer: 'walls'});
}
},
commandDestroy = function (msg) {
if (_.size(msg.selected) == 0) {
showDialog('Error', 'You must select a valid Bomb "Result" token.', msg.who);
return;
}
var token = getObj(msg.selected[0]._type, msg.selected[0]._id);
if (token && (token.get('bar1_value') == 'Bomb' || token.get('bar1_value') == 'Result')) {
var bomb, result;
if (token.get('bar1_value') == 'Bomb') {
bomb = token;
result = getObj('graphic', bomb.get('bar3_value'));
} else {
result = token;
bomb = getObj('graphic', result.get('bar3_value'));
}
result.set({layer: 'objects', bar3_value: '', bar3_max: ''});
bomb.set({layer: 'objects', bar3_value: '', bar3_max: ''});
state['TimeBomb'].bombs = _.filter(state['TimeBomb'].bombs, function (x) { return x !== bomb.get('id') });
showDialog('Bomb Destroyed', '"' + bomb.get('name') + '" has been successfully destroyed.', msg.who);
} else {
showDialog('Error', 'The selected token was not a Bomb token.', msg.who);
}
},
getTurnOrder = function () {
return (Campaign().get('turnorder') === '') ? [] : Array.from(JSON.parse(Campaign().get('turnorder')));
},
setTurnOrder = function (to) {
// Make sure Turn Tracker is open first
if (Campaign().get('initiativepage') == false)
Campaign().set({initiativepage: Campaign().get('playerpageid')});
Campaign().set('turnorder', JSON.stringify(to));
},
getCurrentTurn = function () {
return _.first(getTurnOrder());
},
processGMNotes = function (notes) {
var retval, text = unescape(notes).trim();
if (text.search('{template:') != -1) {
text = removeFormatting(text);
text = text.replace('&{template', '&{template');
}
return text;
},
removeFormatting = function (html) {
html = html.replace(/<p[^>]*>/gi, '<p>').replace(/\n(<p>)?/gi, '</p><p>').replace(/<br>/gi, '</p><p>').replace(/<\/?(span|div|pre|img|code|a|b|i|h1|h2|h3|h4|h5|hr)[^>]*>/gi, '');
if (html != '' && /<p>.*?<\/p>/g.test(html)) {
html = html.match(/<p>.*?<\/p>/g).map( l => l.replace(/^<p>(.*?)<\/p>$/,'$1'));
html = html.join(/\n/);
}
return html;
},
rollDice = function (exp) {
exp = exp.split(/\D/gi);
var roll, num = (exp[0]) ? parseInt(exp[0]) : 1,
die = (exp[1]) ? parseInt(exp[1]) : 6,
plus = (exp[2]) ? parseInt(exp[2]) : 0;
roll = (num == 1) ? randomInteger(die) : randomInteger(die * num - (num - 1)) + (num - 1);
return roll + plus;
},
isNum = function (txt) {
var nr = /^\d+$/;
return nr.test(txt);
},
commandHelp = function (msg) {
var message = '<span style=\'' + styles.code + '\'>!bomb help</span><br>Sends this dialog to the chat window.<br><br>';
message += 'More information here...';
showDialog('TimeBomb Help', message, msg.who);
},
showDialog = function (title, content, whisperTo = '') {
var gm = /\(GM\)/i;
title = (title == '') ? '' : '<div style=\'' + styles.title + '\'>' + title + '</div>';
var body = '<div style=\'' + styles.box + '\'>' + title + '<div>' + content + '</div></div>';
if (whisperTo.length > 0) {
whisperTo = '/w ' + (gm.test(whisperTo) ? 'GM' : '"' + whisperTo + '"') + ' ';
sendChat('TimeBomb', whisperTo + body, null, {noarchive:true});
} else {
sendChat('TimeBomb', body);
}
},
checkTimer = function () {
var curr = getCurrentTurn();
if (curr && curr.id !== 'undefined' && _.find(state['TimeBomb'].bombs, function (x) { return x == curr.id; }) && curr.pr == 0) {
var bomb = getObj('graphic', curr.id);
if (bomb) {
var result = getObj('graphic', bomb.get('bar3_value')),
offsets = bomb.get('bar3_max').split('|');
var left = parseInt(bomb.get('left')) + parseInt(offsets[0]),
top = parseInt(bomb.get('top')) + parseInt(offsets[1]);
result.set({left: left, top: top});
_.delay(function () {
var effect = processGMNotes(bomb.get('gmnotes'));
if (effect !== '') showDialog('', effect);
bomb.set({layer: 'walls'});
if (result.get('bar1_max') == 'false' || result.get('bar1_max') == 'tokens' || result.get('bar1_max') == 'objects') {
result.set({layer: 'objects'});
toFront(result);
} else {
result.set({layer: 'map'});
}
var nto = _.filter(getTurnOrder(), function (obj) { return obj.id !== bomb.get('id'); });
setTurnOrder(nto);
state['TimeBomb'].bombs = _.filter(state['TimeBomb'].bombs, function (x) { return x !== bomb.get('id') });
}, 350);
_.delay(function () {
showDialog('', '"' + bomb.get('name') + '" has been detonated.<div style=\'' + styles.buttonWrapper + '\'><a style=\'' + styles.button + '\' href="!bomb hide ' + result.get('id') + '" title="Hide Detonation Token">Hide</a> <a style=\'' + styles.button + '\' href="!bomb reset ' + bomb.get('id') + '" title="Reset Bomb">Reset</a></div>', 'GM');
}, 500);
} else showDialog('', 'Bomb not found!', 'GM');
}
},
//---- PUBLIC FUNCTIONS ----//
registerEventHandlers = function () {
on('chat:message', handleInput);
on("change:campaign:turnorder", checkTimer);
};
return {
checkInstall: checkInstall,
registerEventHandlers: registerEventHandlers
};
}());
on("ready", function () {
TimeBomb.checkInstall();
TimeBomb.registerEventHandlers();
});