-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcombot.js
324 lines (324 loc) · 12.6 KB
/
combot.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
/*
An initiative tracker
*/
var Discord = require('discord.js');
var token = require('./token.js');
var combot = new Discord.Client();
var version = '1.0.0';
combot.login(token);
combot.on('ready', function () {
Combat.setup();
console.log('ComBot ' + version + ' ready for battle');
});
combot.on('message', function (message) {
if (message.author.id !== combot.user.id) {
if (message.content.indexOf('!combot') === 0)
Combat.handle(message);
}
});
var Combatant = (function () {
function Combatant(user, name, dex, init) {
if (init === void 0) { init = null; }
this.init = null;
this.user = user;
this.name = name;
this.dex = parseInt(dex);
this.initMod = init ? parseInt(init) : Math.floor((this.dex - 10) / 2);
}
Combatant.prototype.rollInitiative = function () {
var roll = Math.ceil(Math.random() * 20);
this.init = { roll: roll, total: roll + this.initMod };
};
return Combatant;
}());
///<reference path="../main.ts" />
var Combat;
(function (Combat) {
var ongoing = false;
var hasStarted = false;
var channel = null;
var dmList = new Array();
var currentCombatant = null;
var combatantList = new Array();
var initList = new Array();
var delayList = new Array();
var fMap = new Map();
function setup() {
fMap.set('new', newCombat);
fMap.set('enter', enterCombat);
fMap.set('start', startCombat);
fMap.set('init', listInit);
fMap.set('end', endTurn);
fMap.set('delay', delayTurn);
fMap.set('act', actTurn);
fMap.set('finish', finishCombat);
fMap.set('ongoing', isOngoing);
fMap.set('list', listCombatants);
fMap.set('help', help);
fMap.set('info', info);
}
Combat.setup = setup;
function handle(message) {
var command = message.content.split(' ').slice(1);
var f = fMap.get(command[0].toLowerCase());
if (f)
f(message, command.slice(1));
else
message.channel.sendMessage(message.content + ' - command failed');
}
Combat.handle = handle;
function newCombat(message, command) {
if (ongoing) {
message.channel.sendMessage('There is already an ongoing combat.');
}
else {
if (message.channel.type === 'text') {
dmList.push(message.author);
channel = message.channel;
ongoing = true;
hasStarted = false;
message.channel.sendMessage(message.author + ' has started a new combat on channel ' + channel + '!');
}
else {
message.channel.sendMessage('You cannot start a combat via DM.');
}
}
}
function enterCombat(message, command) {
if ((ongoing) && (!hasStarted)) {
if (command.length < 2) {
message.channel.sendMessage('[ERROR] Not enough parameters.');
return;
}
var silent = false;
for (var c in command) {
if ((command[c].toLowerCase() === '-s') || (command[c].toLowerCase() === '-silent')) {
silent = true;
command.splice(c, 1);
break;
}
}
if (isNaN(parseInt(command[1]))) {
message.channel.sendMessage('[ERROR] Invalid parameter dexvalue "' + command[1] + '"');
return;
}
if ((command[2]) && (isNaN(parseInt(command[2])))) {
message.channel.sendMessage('[ERROR] Invalid parameter initmod "' + command[2] + '"');
return;
}
var combatant = new Combatant(message.author, command[0], command[1], command[2]);
combatantList.push(combatant);
if ((silent) && (dmList.indexOf(message.author !== -1))) {
message.author.dmChannel.sendMessage(command[0] + ' has entered the fray!');
}
else {
channel.sendMessage(command[0] + ' has entered the fray!');
}
}
else {
if (!ongoing)
message.channel.sendMessage('There is no ongoing combat.');
else
message.channel.sendMessage('Combat has already started. You cannot join.');
}
}
function startCombat(message, command) {
if (!hasStarted) {
hasStarted = true;
initList = new Array();
for (var _i = 0, combatantList_1 = combatantList; _i < combatantList_1.length; _i++) {
var c = combatantList_1[_i];
c.rollInitiative();
var i = initList.length - 1;
while ((i > -1) && ((initList[i].init.total < c.init.total) || ((initList[i].init.total === c.init.total) && (initList[i].dex < c.dex)))) {
i--;
}
initList.splice(i + 1, 0, c);
}
channel.sendMessage('Combat has begun!\n');
listInit(message, command);
startTurn();
}
else {
message.channel.sendMessage('Combat has already started.');
}
}
function listInit(message, command) {
if (hasStarted) {
var msg = 'Round Order:\n';
if (currentCombatant) {
msg += currentCombatant.name + ' (' + currentCombatant.init.roll + '+' + currentCombatant.initMod + ' = ' + currentCombatant.init.total + ')\n';
}
for (var _i = 0, initList_1 = initList; _i < initList_1.length; _i++) {
var c = initList_1[_i];
msg += c.name + ' (' + c.init.roll + '+' + c.initMod + ' = ' + c.init.total + ')\n';
}
if (delayList.length > 0) {
msg += 'Delayed: ';
for (var _a = 0, delayList_1 = delayList; _a < delayList_1.length; _a++) {
var c = delayList_1[_a];
msg += c.name;
msg += delayList.indexOf(c) < delayList.length - 1 ? ',' : '';
}
}
channel.sendMessage(msg);
}
else {
message.channel.sendMessage('Combat has not started yet.');
}
}
function startTurn(keyword) {
if (keyword === void 0) { keyword = 'ended'; }
var msg = '';
if (currentCombatant) {
if (initList.indexOf(currentCombatant) === -1) {
initList.push(currentCombatant);
}
msg += currentCombatant.name + ' has ' + keyword + ' their turn.\n';
}
currentCombatant = initList.shift();
if (delayList.indexOf(currentCombatant) !== -1) {
delayList.splice(delayList.indexOf(currentCombatant), 1);
}
msg += currentCombatant.user + ' ' + currentCombatant.name + '\'s turn!';
channel.sendMessage(msg);
}
function endTurn(message, command) {
if (hasStarted) {
if ((message.author === currentCombatant.user) || (dmList.indexOf(message.user) !== -1)) {
startTurn();
}
else {
message.channel.sendMessage('You cannot end someone else\'s turn.');
}
}
else {
message.channel.sendMessage('Combat has not started yet.');
}
}
function delayTurn(message, command) {
if (hasStarted) {
if ((message.author === currentCombatant.user) || (dmList.indexOf(message.user) !== -1)) {
delayList.push(currentCombatant);
startTurn('delayed');
}
else {
message.channel.sendMessage('You cannot delay someone else\'s turn.');
}
}
else {
message.channel.sendMessage('Combat has not started yet.');
}
}
function actTurn(message, command) {
if (hasStarted) {
var combatant = null;
for (var _i = 0, delayList_2 = delayList; _i < delayList_2.length; _i++) {
var c = delayList_2[_i];
if (c.user === message.author) {
combatant = c;
break;
}
}
if (combatant) {
initList.splice(0, 0, currentCombatant);
initList.splice(0, 0, combatant);
delayList.splice(delayList.indexOf(combatant), 1);
currentCombatant = null;
channel.sendMessage(combatant.name + ' is acting their delayed turn.');
startTurn();
}
else {
message.channel.sendMessage('You don\'t have a delayed turn.');
}
}
else {
message.channel.sendMessage('Combat has not started yet.');
}
}
function finishCombat(message, command) {
if (ongoing) {
if (dmList.indexOf(message.author) !== -1) {
if ((message.channel.type === 'dm') || (message.channel === channel)) {
ongoing = false;
currentCombatant = null;
dmList = new Array();
combatantList = new Array();
initList = new Array();
delayList = new Array();
channel.sendMessage(message.author + ' has ended the combat!');
}
else {
message.channel.sendMessage('You cannot finish the current combat from this channel. Use either ' + channel + ' or PM.');
}
}
else {
message.channel.sendMessage('You do not have permission to finish this combat.');
}
}
else {
message.channel.sendMessage('There is no ongoing combat.');
}
}
function isOngoing(message, command) {
if (ongoing) {
var msg = 'Current combat status: ';
if (hasStarted) {
msg += 'started. Characters cannot join.';
}
else {
msg += 'open. Characters can join by using !combat enter <charactername> <dexvalue>.';
}
msg += ' DM: ';
for (var _i = 0, dmList_1 = dmList; _i < dmList_1.length; _i++) {
var dm = dmList_1[_i];
msg += dm.username;
msg += dmList.indexOf(dm) !== dmList.length - 1 ? ' | ' : '.';
}
message.channel.sendMessage(msg);
}
else {
message.channel.sendMessage('There is no ongoing combat.');
}
}
function listCombatants(message, command) {
if (ongoing) {
var msg = '';
if (combatantList.length > 0) {
msg = 'Combatant list: ';
for (var _i = 0, combatantList_2 = combatantList; _i < combatantList_2.length; _i++) {
var c = combatantList_2[_i];
msg += c.name + '(' + c.user.username + ')';
msg += combatantList.indexOf(c) !== combatantList.length - 1 ? ', ' : '.';
}
}
else {
msg = 'There are no combatants registered yet.';
}
message.channel.sendMessage(msg);
}
else {
message.channel.sendMessage('There is no ongoing combat.');
}
}
function help(message, command) {
message.channel.sendMessage('**[PRE-COMBAT]**\n' +
'**!combot new** - Start a new combat, if there is none ongoing\n' +
'**!combot ongoing** - Check if there is any ongoing combats\n' +
'**!combot enter <charactername> <dexvalue> <initmod(optional)>** - Add a character to the combat. If not declared, initmod will be calculated based on dexvalue. ' +
'_Flags:_ -s|-silent _[DM ONLY]_\n' +
'**!combot list** - Returns the list of combatants engaged in battle\n' +
'**!combot start** - _[DM ONLY]_ Start the current combat\n' +
'\n**[COMBAT]**\n' +
'**!combot end** - _[DM OR CURRENT PLAYER ONLY]_ Ends current combatant\'s turn\n' +
'**!combot delay** - _[DM OR CURRENT PLAYER ONLY]_ Delay current combatant\'s turn\n' +
'**!combot act** - Act your combatant\'s delayed turn\n' +
'**!combot finish** - Ends current combat\n' +
'\n**[EXTRA]**\n' +
'**!combot help** - Shows command list\n' +
'**!combot info** - Check current bot version');
}
function info(message, command) {
message.channel.sendMessage('ComBot ' + version + ' ready for battle!');
}
})(Combat || (Combat = {}));