forked from Roll20/roll20-api-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsa41-crit-checker.js
152 lines (135 loc) · 5.35 KB
/
dsa41-crit-checker.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
/* jshint undef: true */
/* globals
sendChat,
randomInteger,
_,
on
*/
var DSA41CritChecker = DSA41CritChecker || (function()
{
'use strict';
const messageTemplate = '<div style="border: 1px solid gray; background-color: {{color}}"><span>{{message}}</span></div>';
function getWhisperTarget(options) {
var nameProperty, targets, type;
options = options || {};
if (options.player) {
nameProperty = 'displayname';
type = 'player';
} else if (options.character) {
nameProperty = 'name';
type = 'character';
} else {
return '';
}
if (options.id) {
targets = [getObj(type, options.id)];
if (targets[0]) {
return '/w ' + targets[0].get(nameProperty).split(' ')[0] + ' ';
}
}
if (options.name) {
// Sort all players or characters (as appropriate) whose name *contains* the supplied name,
// then sort them by how close they are to the supplied name.
targets = _.sortBy(filterObjs(function(obj) {
if (obj.get('type') !== type) return false;
return obj.get(nameProperty).indexOf(options.name) >= 0;
}), function(obj) {
return Math.abs(levenshteinDistance(obj.get(nameProperty), options.name));
});
if (targets[0]) {
return '/w ' + targets[0].get(nameProperty).split(' ')[0] + ' ';
}
}
return '';
}
function sendMessage(content, originalMessage) {
if (originalMessage.target) {
var msgContent = "/w " + originalMessage.target + " " + content;
sendChat("Game", "/w " + originalMessage.target + " " + content, null, { noarchive: true });
if (!(originalMessage.target === "gm" && playerIsGM(originalMessage.playerid))) {
msgContent = DSA41CritChecker.getWhisperTarget( { player: true, id: originalMessage.playerid }) + " " + content;
sendChat("Game", DSA41CritChecker.getWhisperTarget( { player: true, id: originalMessage.playerid }) + " " + content, null, { noarchive: true });
}
} else {
sendChat("Game", content, null, { noarchive: true });
}
}
function handleMainRoll(mainRoll, msg) {
const totalRoll = mainRoll.results.rolls[0];
const totalResult = totalRoll.results[0].v;
var critCount = 0;
var fumbleCount = 0;
var rolls = [];
for (let i = 0; i < totalRoll.rolls[0].length; i++) {
const subRoll = totalRoll.rolls[0][i];
if (subRoll.resultType === "sum") {
rolls.push(subRoll.rolls[0][0].results[0].v);
if (subRoll.rolls[0][0].results[0].v === 1) {
critCount++;
} else if (subRoll.rolls[0][0].results[0].v === 20) {
fumbleCount++;
}
}
}
var shouldSendMessage = false;
var message = null;
var color = null;
if (critCount > 1) {
color = '#d7f2cd';
if (totalResult < 0 && rolls[0] === 1 && rolls[1] === 1) {
shouldSendMessage = true;
message = 'Probe kritisch gelungen trotz negativer TaP*, da 1 bei ersten beiden Würfel. Gesamtzahl gewürfelte "1": ' + critCount;
} else if (totalResult >= 0) {
shouldSendMessage = true;
message = 'Probe kritisch gelungen. Anzahl gewürfelte "1": ' + critCount;
}
} else if (fumbleCount > 1) {
if (totalResult >= 0 && rolls[0] === 20 && rolls[1] === 20) {
shouldSendMessage = true;
color = '#f2cdcd'
message = 'Probe kritisch fehlgeschlagen trotz positiver TaP*, da 20 bei ersten beiden Würfel. Gesamtzahl gewürfelte "20": ' + fumbleCount;
} else if (totalResult < 0) {
shouldSendMessage = true;
color = '#f2e3cd'
message = 'Probe kritisch fehlgeschlagen. Anzahl gewürfelte "20": ' + fumbleCount;
}
}
if (shouldSendMessage) {
DSA41CritChecker.sendMessage(messageTemplate.replace("{{color}}", color).replace("{{message}}", message), msg);
}
}
function registerEventHandlers()
{
on('chat:message', DSA41CritChecker.handleChatMessage);
}
/**
* Grab chat message objects
*
* @param {object} msg
*/
function handleChatMessage(msg)
{
if (msg.rolltemplate === "DSA-Talente" || msg.rolltemplate === "DSA-Zauber" || msg.rolltemplate === "DSA-Talente-eBE" || msg.rolltemplate === "DSA-Liturgien") {
for (let i = 0; i < msg.inlinerolls.length; i++) {
const mainRoll = msg.inlinerolls[i];
if ((mainRoll.expression.match(/1d20cs1cf20/g) || []).length === 3) {
DSA41CritChecker.handleMainRoll(mainRoll, msg);
}
}
}
}
return {
registerEventHandlers: registerEventHandlers,
handleChatMessage: handleChatMessage,
sendMessage: sendMessage,
handleMainRoll: handleMainRoll,
getWhisperTarget: getWhisperTarget
}
}());
/**
* Fires when the page has loaded.
*/
on("ready", function()
{
DSA41CritChecker.registerEventHandlers();
});