-
Notifications
You must be signed in to change notification settings - Fork 1
/
initiative-handler.js
115 lines (99 loc) · 5.37 KB
/
initiative-handler.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
var DiceRoller = require('./diceRoller.js');
var Initiative = require('./initiative.js');
var initiativeHelpMatch = /^!Init Help/i;
var initiativeRollMatch = /^!Init (PC|NPC)[ ]?\[([adpcfbs]*)\][ ]?\[?([sfatrdkl]*)\]?/i;
var initiativeAddMatch = /^!Init (?:Add|A) (PC|NPC) (\d+):\s?(\d+)?/i;
var initiativeRemoveMatch = /^!Init (?:Remove|R|Delete|D) (PC|NPC) (\d+)/i;
var initiativeShowMatch = /^!Init (?:Show|S|View|V)$/i;
var initiativeNextMatch = /^!Init (?:Next|N)$/i;
var initiativeStartMatch = /^!Init (?:Start)$/i;
var initiativeClearMatch = /^!Init (?:Clear|C|Reset)/i;
var channelInit = {};
exports.handleMessage = function(message, channelID, serverDice, serverSymbols, emojiServerID, sendMessages, printSymbols) {
if (message.match(initiativeRollMatch)) {
if (!channelInit[channelID]){
channelInit[channelID] = new Initiative();
}
let roller = new DiceRoller(serverDice, serverSymbols);
var initRollMatch = initiativeRollMatch.exec(message);
roller.roll(initRollMatch[2].toLowerCase(), initRollMatch[3].toLowerCase());
var symbols = DiceRoller.countSymbols(roller.cancelledSymbols);
channelInit[channelID].addSlot(initRollMatch[1], symbols['Success'], symbols['Advantage']);
var returnMessage = "";
roller.diceResults.forEach(function(result) {
returnMessage += result.code + " ";
});
resultsMessage = printSymbols(roller.cancelledSymbols, emojiServerID);
sendMessages(channelID, [returnMessage + '\n\n' + resultsMessage, 'Adding '+ initRollMatch[1] +' slot to initative. New order: ' + '\n\n' + channelInit[channelID].unicodeOrder]);
}
if (message.match(initiativeAddMatch)) {
if (!channelInit[channelID]){
channelInit[channelID] = new Initiative();
}
var addMatch = initiativeAddMatch.exec(message);
channelInit[channelID].addSlot(addMatch[1], parseInt(addMatch[2]), parseInt(addMatch[3]));
sendMessages(channelID, ['Adding '+ addMatch[1] +' slot to initative. New order: ' + '\n\n' + channelInit[channelID].unicodeOrder]);
}
if (message.match(initiativeRemoveMatch)) {
if (!channelInit[channelID]){
sendMessages(channelID, ['No initiative is currently being tracked for this channel. Use `!Init (PC|NPC) []` to get started']);
} else {
var removeMatch = initiativeRemoveMatch.exec(message);
if (channelInit[channelID].deleteSlot(removeMatch[1], parseInt(removeMatch[2]))) {
sendMessages(channelID, ['Removing ' + removeMatch[1] + ' ' + removeMatch[2] + ' from initative. New order: ' + '\n\n' + channelInit[channelID].unicodeOrder]);
} else {
sendMessages(channelID, ['No '+ removeMatch[1] + ' ' + removeMatch[2] + ' to remove.']);
}
}
}
if (message.match(initiativeShowMatch)) {
if (!channelInit[channelID]){
sendMessages(channelID, ['No initiative is currently being tracked for this channel. Use `!Init (PC|NPC) []` to get started']);
} else {
sendMessages(channelID, ['Round: ' + channelInit[channelID].currentRound + ' | ' + channelInit[channelID].unicodeOrder]);
}
}
if (message.match(initiativeNextMatch)) {
if (!channelInit[channelID]){
sendMessages(channelID, ['No initiative is currently being tracked for this channel. Use `!Init (PC|NPC) []` to get started']);
} else {
channelInit[channelID].nextSlot();
sendMessages(channelID, ['Round: ' + channelInit[channelID].currentRound + ' | ' + channelInit[channelID].unicodeOrder]);
}
}
if (message.match(initiativeStartMatch)) {
if (!channelInit[channelID]){
sendMessages(channelID, ['No initiative is currently being tracked for this channel. Use `!Init (PC|NPC) []` to get started']);
} else {
channelInit[channelID].beginInitiative();
sendMessages(channelID, ['Round: ' + channelInit[channelID].currentRound + ' | ' + channelInit[channelID].unicodeOrder]);
}
}
if (message.match(initiativeClearMatch)) {
if (!channelInit[channelID]){
sendMessages(channelID, ['No initiative is currently being tracked for this channel. Use `!Init (PC|NPC) []` to get started']);
} else {
channelInit[channelID] = null;
sendMessages(channelID, ['Initiative for this channel has been cleared']);
}
}
if (message.match(initiativeHelpMatch)) {
var message = "```\n";
message += "Init Help:\n";
message += "To add a slot: !Init (PC|NPC) [Some Dice Characters]\n";
message += "See '!Roll Help' for more details on the dice characters\n";
message += "* Example: !Init PC [aa]\n";
message += "Init supports rolling Additional Symbols as well.\n";
message += "* Example: !Init PC [aa][sa]\n\n";
message += "!Init Add (PC|NPC) Successes:Advantages\n";
message += "* Example: !Init Add PC 1:2\n\n";
message += "!Init Remove (PC|NPC) Occurrence\n";
message += "* Example: !Init Remove NPC 3\n";
message += "This will remove the 3rd NPC slot in the order.\n\n";
message += "!Init Next\n";
message += "!Init Start\n";
message += "!Init Show\n";
message += "!Init Clear```";
sendMessages(channelID, [message]);
}
}