-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.js
415 lines (354 loc) · 8.87 KB
/
utility.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
403
404
405
406
407
408
409
410
411
412
413
414
415
function getCharacter(playername)
{
var rtn = null;
try
{
var characters = findObjs({
_type: "character",
name: playername
});
if (characters.length > 0)
rtn = _.first(characters);
}
catch (ex) {}
return rtn;
}
function setAttributeValue(attribute, value)
{
if (attribute)
attribute.set("current", value);
}
function createAttribute(character, attributename)
{
var rtn = null;
if (character && attributename)
{
rtn = createObj("attribute", {
name: attributename.toUpperCase(),
characterid: character.get("_id")
});
}
return rtn;
}
function getAttribute(attributes, attributename)
{
var rtn = null;
for (var i = 0; i < attributes.length; i++)
{
if (attributes[i].get("name").toUpperCase() == attributename.toUpperCase())
{
rtn = attributes[i];
break;
}
}
return rtn;
}
function getAttributeValue(attributes, attributename)
{
var rtn = null;
for (var i = 0; i < attributes.length; i++)
{
if (attributes[i].get("name").toUpperCase() == attributename.toUpperCase())
{
rtn = parseInt(attributes[i].get("current"), 10);
break;
}
}
if (isNaN(rtn))
rtn = null;
return rtn;
}
function getAttributes(character)
{
var rtn = null;
if (character)
{
rtn = findObjs({
_type: "attribute",
_characterid: character.get('_id')
});
}
return rtn;
}
function createAbility(character, abilityname)
{
var rtn = null;
if (character && abilityname)
{
rtn = createObj("ability", {
name: abilityname,
characterid: character.get("id")
});
}
return rtn;
}
function getAbility(abilities, abilityname)
{
var rtn = null;
for (var i = 0; i < abilities.length; i++)
{
if (abilities[i].get("name").toUpperCase() == abilityname.toUpperCase())
{
rtn = abilities[i];
break;
}
}
return rtn;
}
function getAbilities(character)
{
var rtn = null;
if (character)
{
rtn = findObjs({
_type: "ability",
_characterid: character.get('_id')
});
}
return rtn;
}
function getInitiative(token)
{
var initiative = null;
// check to see if character
var represents = token.get("_represents");
if (represents && represents !== "")
{
var character = getObj("character", represents);
var attributes = getAttributes(character);
var MOB = getAttributeValue(attributes, "MOB");
var AG = getAttributeValue(attributes, "AG");
var INIMOD = getAttributeValue(attributes, "INI%");
if (!MOB)
MOB = 0;
if (!AG)
AG = 0;
if (!INIMOD)
INIMOD = 0;
initiative = MOB + AG + INIMOD;
}
else
{
var mobInitiative = token.get("bar3_value");
if (mobInitiative && (mobInitiative !== ""))
initiative = parseInt(token.get("bar3_value"), 10);
}
return initiative;
}
function setInitiative(turnOrder)
{
turnOrder.sort(function(a,b)
{
return b.pr - a.pr;
});
Campaign().set("turnorder", JSON.stringify(turnOrder));
}
function isNumber(n)
{
return !isNaN(parseFloat(n)) && isFinite(n);
}
function sign(n)
{
return n ? n < 0 ? -1 : 1 : 0;
}
function tallyLeftovers(msg)
{
rtn = null;
if (msg.length > 0)
{
var bonus = 0;
var tokens = msg.split(" ");
for (var i = 0; i < tokens.length; i++)
{
if (isNumber(tokens[i]))
bonus += parseInt(tokens[i], 10);
}
if (bonus !== 0)
rtn = bonus;
}
return rtn;
}
// Roll against a CTN and auto max damage/result, a null is a fumble
function roll(ctn)
{
rtn = null;
if (+ctn > 0)
{
var dice = Math.ceil(+ctn / 20);
var origRolls = [];
var cloneRolls = [];
var slayingRolls = [];
var ctns = [];
var count = 0;
var slaying = 0;
// Get all dice roll values and set all die CTN's
for (var i = 0; i < dice; i++)
{
origRolls[i] = randomInteger(20);
ctns[i] = 20;
if (i == (dice - 1))
{
if (ctn % 20 !== 0)
ctns[i] = ctn % 20;
}
}
// Test for slaying dice
if (origRolls[0] == 1)
{
var tmpRoll = randomInteger(20);
i = 0;
// Keep rolling on coups
while (tmpRoll == 1)
{
slaying += ctns[0];
slayingRolls[i] = tmpRoll;
tmpRoll = randomInteger(20);
i++;
}
slayingRolls[i] = tmpRoll;
if (tmpRoll <= ctns[0])
slaying += tmpRoll;
}
// Test for fumble
if (origRolls[0] == 20)
rtn = {total: 0, checks: ctns, dice: origRolls, slayingTotal: 0, slayingDice: slayingRolls};
else
{
cloneRolls = origRolls.slice();
// Re-arrange rolls
cloneRolls.sort(function (a, b){
if (a == 1 && b != 1)
return -1;
if (b == 1 && a != 1)
return 1;
if (a > b)
return -1;
if (a < b)
return 1;
return 0;
});
for (i = 0; i < dice; i++)
{
if (cloneRolls[i] <= ctns[i])
count += cloneRolls[i] == 1 ? ctns[i] : cloneRolls[i];
}
rtn = {total: count, checks: ctns, dice: cloneRolls, slayingTotal: slaying, slayingDice: slayingRolls};
}
}
return rtn;
}
function ctnFormat(ctnData)
{
var rtn = null;
if (ctnData)
{
var color1 = "background-color: #ABFFA3;";
var color2 = "background-color: #FFB8B8;";
var plus = "background-color: #ABFFA3;";
var minus = "background-color: #FFB8B8;";
var ctn = "background-color: #E3A6FF;";
var norm = "padding: 1px 2px 1px 2px; -webkit-border-radius: 3px; border: 1px solid #555555;";
var i = 0;
rtn = "<table style='font-size: 0.875em; border-collapse: separate; border-spacing: 2px; font-weight: bold;'><tr><td style='" + norm + ctn + "'>CTN</td><td> = </td>";
for (i = 0; i < ctnData.length; i++)
{
if (i !== 0)
rtn += "<td style='" + "'>" + (ctnData[i][2] === 1 ? "+" : "-") + "</td>";
rtn += "<td style='" + norm + (ctnData[i][2] === 1 ? plus : minus) + "'>" + ctnData[i][0] + "(" + ctnData[i][1] + ")" + "</td>";
}
rtn += "</tr></table>";
}
return rtn;
}
function rollFormat(who, msg, ctnData, ctn, rollData)
{
var rtn = null;
if (rollData)
{
var miss = "background-color: #FFB8B8;";
var hit = "background-color: #C4C4C4;";
var coup = "background-color: #ABFFA3;";
var fumble = "background-color: #E3A6FF;";
var outside = "padding: 4px; -webkit-border-radius: 4px; border: 1px solid #555555;";
var i = 0;
var j = 0;
rtn = ctnData;
rtn += "<table style='margin=top: 7px; border-collapse: separate; border-spacing: 2px; font-weight: bold;'><tr>";
for(i = 0; i < rollData.checks.length; i++)
{
if (i === 0 && rollData.total === 0 && rollData.dice[i] === 20)
{
rtn += "<td style='" + fumble + outside + "'>";
}
else if (rollData.dice[i] <= rollData.checks[i])
{
if (rollData.dice[i] == 1)
rtn += "<td style='" + coup + outside + "'>";
else
rtn += "<td style='" + hit + outside + "'>";
}
else
rtn += "<td style='" + miss + outside + "'>";
rtn += (rollData.dice[i] < 10 ? " " + rollData.dice[i] : rollData.dice[i]) + "<span style=' font-weight: normal;'> vs </span>" + (rollData.checks[i] < 10 ? " " + rollData.checks[i] : rollData.checks[i]) + "</td>";
if (i === 0 && rollData.slayingDice.length > 0)
{
for (j = 0; j < rollData.slayingDice.length; j++)
{
if (rollData.slayingDice[j] <= rollData.checks[i])
{
if (rollData.slayingDice[j] == 1)
rtn += "<td style='" + coup + outside + "'>S ";
else
rtn += "<td style='" + hit + outside + "'>S ";
}
else
rtn += "<td style='" + miss + outside + "'>S ";
rtn += (rollData.slayingDice[i] < 10 ? " " + rollData.slayingDice[i] : rollData.slayingDice[i]) + "<span style=' font-weight: normal;'> vs </span>" + (rollData.checks[i] < 10 ? " " + rollData.checks[i] : rollData.checks[i]) + "</td>";
}
}
}
rtn += "</tr></table>";
rtn += "<span>" + msg + " and " + (rollData.total > 0 ? "<span style='font-weight: bold';>SUCCEED</span>" : (rollData.dice[0] == 20 ? "<span style='font-weight: bold';>FUMBLE</span>" : "<span style='font-weight: bold';>MISS</span>")) + (rollData.total > 0 ? (" for <span style='font-weight: bold';>" + (rollData.total + rollData.slayingTotal) + "</span>!") : "") + "</span>";
}
return rtn;
}
/*on("chat:message", function(msg)
{
if (msg.type == "api" && msg.content.indexOf("!setup") !== -1)
{
var character = getCharacter(msg.who);
var attributes = null;
var message = "";
var i = 0;
var attribList = ["BOD","MND","MOB","ST","CO","AG","DX","IN","AU","AV","WB","SPC%","TSC%","INI%"];
var attrib = null;
var abilities = null;
var ability = null;
if (character)
{
message = msg.content.replace("!setup", "").toLowerCase();
attributes = getAttributes(character);
if (attributes)
{
for (i = 0; i < attribList.length; i++)
{
attrib = getAttribute(attributes, attribList[i]);
if (!attrib)
{
attrib = createAttribute(character, attribList[i]);
setAttributeValue(attrib, 0);
}
}
}
if ((abilities = getAbilities(character)))
{
if (!(ability = getAbility(abilities, "End Turn")))
{
ability = createAbility(character, "End Turn");
ability.set("action", "!endTurn");
}
}
sendChat(msg.who, "/w " + msg.who + " finished with initial setup.");
}
}
});*/