forked from WaveAqualei/TestingGrounds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
600 lines (596 loc) · 16.4 KB
/
common.js
File metadata and controls
600 lines (596 loc) · 16.4 KB
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
var Type = {
PING:0,
PONG:1,
MSG:2,
ROOMLIST:3,
TOGGLE:4,
JOINROOM:5,
JOIN:6,
LEAVE:7,
SYSTEM:9,
SETROLE:10,
HIGHLIGHT:11,
SETPHASE:12,
WHISPER:13,
MOD:14,
TOGGLELIVING:15,
PRENOT:16,
VOTE:17,
CLEARVOTES:18,
VERDICT:19,
TICK:20,
JUDGEMENT:21,
SETDEV:22,
WILL:23,
SETMOD:24,
SWITCH:25,
ACCEPT:26,
ROLEUPDATE:27,
DENY:28,
KICK:29,
ROLECARD:30,
ROLL:31,
SETROLESBYLIST:32,
MASSROLEUPDATE:33,
SHOWLIST:34,
SHOWALLROLES:35,
LATENCIES:36,
GETWILL:37,
HEY:38,
TARGET:39,
HUG:40,
ME:41,
ROLELIST:42,
AUTOLEVEL:43,
SUGGESTIONS:44,
SYSSENT:45,
CUSTOMROLES:46,
HELP:47,
PAUSEPHASE:48,
SETDAYNUMBER:49,
SETSPEC:50,
REMSPEC: 51,
LOGINDEXI: 52,
LOGINDEXO: 53,
MAYOR: 54,
GUARDIAN_ANGEL: 55,
REMOVE_EMOJI: 56,
NOTES: 57,
GETNOTES: 58,
DISCONNECT: 59,
RECONNECT: 60,
GARDENIA: 61,
PHLOX: 62,
TARGETING_OPTIONS: 63,
ROLERESULTS: 64,
};
/*
* 0 = No auto
* 1 = Semi auto
* 2 = Full auto
* */
var Phase = {
PREGAME: 0,
ROLES: 1,
MODTIME: 2,
DAY: 3,
VOTING: 4,
TRIAL: 5,
VERDICTS: 6,
LASTWORDS: 7,
NIGHT: 8,
FIRSTDAY: 9,
};
if(typeof jQuery !== 'undefined') {
function sanitize(msg) {
return $("<span>").text(msg).html()
}
} else {
function sanitize(msg) {
msg = msg.replace(/&/g, '&'); //This needs to be replaced first, in order to not mess up the other codes.
msg = msg.replace(/</g, '<');
msg = msg.replace(/>/g, '>');
msg = msg.replace(/\"/g, '"');
msg = msg.replace(/\'/g, ''');
msg = msg.replace(/:/g, ':');
return msg;
}
}
function referenceToName(from) {
if(from.num) {
return '<span class="playernum">'+from.num+'</span> '+from.name;
} else if(from.num === 0) {
return '<span class="playernum modnum">M</span> '+from.name;
} else if(from.name) {
return from.name;
} else if(typeof from === 'string') {
return from;
} else {
return '';
}
}
function processMessage(msg, type)
{
switch (type)
{
case 'me':
return '<li class="me">*<em>'+msg+'</em>*</li>';
break;
case 'rolecard':
return '<li class="rolecardcontainer">'+msg+'</li>';
break;
case 'hug':
return '<li>*<b>'+msg.name+' hugs '+msg.target+'</b>*</li>';
break;
case 'system':
return '<li><b>'+msg+'</b></li>';
break;
case 'information':
msg.styling = msg.styling || 'information';
return '<li class="'+msg.styling+'">'+msg.msg+'</li>';
break;
case 'highlight':
msg.msg = (function(msg) {
var changes = ['maf','cov','arso','jester','ww','town','sk','neut'];
for (i in changes)
{
var start = '['+changes[i]+']';
var end = '[/'+changes[i]+']';
var a = msg.indexOf(start);
var b = msg.indexOf(end);
while ( a!=-1 && b!=-1)
{
var a = msg.indexOf(start);
var b = msg.indexOf(end);
msg = msg.replace(start,'<'+changes[i]+'>');
msg = msg.replace(end,'</'+changes[i]+'>');
}
}
return msg;
})(msg.msg);
msg.styling = msg.styling || 'highlight';
return '<li class="'+msg.styling+'"><b>'+msg.msg+'</b></li>';
break;
case 'whisper':
if (!msg.msg)
{
return '<li><b>'+referenceToName(msg.from)+'</b><span class="whisper"> is whispering to </span><b>'+referenceToName(msg.to)+'</b></span>.</li>';
}
else if (msg.from && msg.to)
{
return '<li><span class="whispermessage">From</span> <b>'+referenceToName(msg.from)+'</b> <span class="whispermessage">to</span> <b>'+referenceToName(msg.to)+'</b> <span class="whispermessage"> '+msg.msg+' </span></li>';
}
else if (msg.from)
{
return '<li><span class="whispermessage">From</span> <b>'+referenceToName(msg.from)+'</b><span class="whispermessage"> '+msg.msg+' </span></li>';
}
else if (msg.to)
{
return '<li><span class="whispermessage">To</span> <b>'+referenceToName(msg.to)+'</b><span class="whispermessage"> '+msg.msg+' </span></li>';
}
else
{
return '<li><span class="whisper">A whisper in the wind</span></li>';
}
break;
case 'mod':
if (msg.from)
{
return '<li><span class="mod">From</span> <b>'+referenceToName(msg.from)+':</b><span class="mod"> '+msg.msg+' </span></li>';
}
else if (msg.to)
{
return '<li><span class="mod">To</span> <b>'+referenceToName(msg.to)+':</b><span class="mod"> '+msg.msg+' </span></li>';
}
else
{
return '<li><span class="mod">Malformed message.</span></li>';
}
break;
case 'target':
if (msg.target != '')
{
if (msg.role)
{
msg.role = '('+msg.role+') is';
}
else if(msg.role === '')
{
msg.role = ' is';
}
else
{
msg.role = ' are';
}
var str = msg.name+msg.role+' now targeting <b>'+msg.target+'</b>.';
}
else
{
if (msg.role)
{
var str = msg.name+'('+msg.role+') has changed their mind.';
}
else
{
var str = 'You have changed your mind.';
}
}
return '<li><span class="mod">'+str+'</span></li>';
break;
case 'prenot':
return '<li class="'+msg.styling+'">'+msg.msg+'</li>';
break;
case 'vote':
if(msg.prev && msg.voted) {
return '<li><b>'+msg.voter+'</b><span class="vote"> has changed their vote to </span><b>'+msg.voted+'</b></li>';
} else if(msg.voted) {
return '<li><b>'+msg.voter+'</b><span class="vote"> has voted for </span><b>'+msg.voted+'</b></li>';
} else if(msg.prev) {
return '<li><b>'+msg.voter+'</b><span class="vote"> has cancelled their vote.</span></li>';
}
break;
case 'verdict':
if (msg.val==0)
{
return '<li><b>'+msg.name+'</b> <span class="vote">has voted.</span>';
}
else if (msg.val==1)
{
return '<li><b>'+msg.name+'</b> <span class="vote">has changed their vote.</span>';
}
else
{
return '<li><b>'+msg.name+'</b> <span class="vote">has cancelled their vote.</span>';
}
break;
case 'judgement':
var message = '';
for (i in msg.votes)
{
switch (msg.votes[i])
{
case -1: message += '<li>'+i+' <span class="vote">voted</span> <span class="guilty">guilty</span>.</li>'; break;
case 0: message += '<li>'+i+' <span class="abstain">abstained</span>.</li>'; break;
case 1: message += '<li>'+i+' <span class="vote">voted</span> <span class="inno">innocent</span>.</li>'; break;
}
}
message += '<li class="vote">The Town has decided to '+(msg.result ? 'lynch' : 'pardon')+' '+msg.name+' by a vote of <span class="inno"><b>'+msg.innos+'</b></span> to <span class="guilty"><b>'+msg.guilties+'</b></span>.</li>';
return message;
break;
case 'will':
if (msg == '')
{
return '<li class="highlight"><span>We could not find a Last Will.</span></li>';
}
else
{
return '<li class="highlight"><span>We found a Will next to their body.</span></li><li><div class="will">'+msg+'</div></li>';
}
break;
case 'allroles':
var message = '';
for (i in msg)
{
message += '<li class="displaylistitem allroles"><div class="innerlistitem">'+msg[i].name+'</div><div class="innerlistitem">'+msg[i].role+'</div></li>';
}
return '<li><ul class="allroleslist">'+message+'</ul></li>';
break;
case 'rolelist':
var message = '';
for (i in msg)
{
message += '<li class="displaylistitem">'+msg[i]+'</li>';
}
return '<li><ul class="displaylist">'+message+'</ul></li>';
break;
default:
return '<li>Bad message type: '+type+'</li>';
}
}
var messageHandlers = {};
function addMessageHandler(type,callback) {
messageHandlers[type] = callback;
}
function msgToHTML(type, args) {
if(messageHandlers[type]) {
return messageHandlers[type].apply(this, args);
}
}
addMessageHandler(Type.MSG,function(from,msg)
{
from = referenceToName(from)+': ';
if(msg.styling) {
msg = '<span class="'+msg.styling+'">'+from+msg.msg+'</span>';
} else if(msg.msg) {
msg = from+msg.msg;
} else {
msg = from+msg;
}
return '<li>'+msg+'</li>';
});
addMessageHandler(Type.ME,function(name,msg)
{
return processMessage(name+' '+msg,'me');
});
addMessageHandler(Type.HIGHLIGHT,function(msg, styling)
{
return processMessage({msg: msg, styling: styling}, 'highlight');
});
addMessageHandler(Type.ROLERESULTS,function(msg, styling)
{
return processMessage({msg: msg, styling: styling}, 'information');
});
addMessageHandler(Type.JOIN,function(name)
{
return processMessage({
msg: name +' has joined.',
styling: 'msgsystem',
},'information');
});
addMessageHandler(Type.DISCONNECT,function(name)
{
return processMessage({
msg: name +' has left.',
styling: 'msgsystem',
},'information');
});
addMessageHandler(Type.RECONNECT,function(name)
{
return processMessage({
msg: name +' has reconnected.',
styling: 'msgsystem',
},'information');
});
addMessageHandler(Type.SYSTEM,function(msg)
{
return processMessage(msg,'system');
});
addMessageHandler(Type.SYSSENT,function(to,msg,styling)
{
return processMessage({
msg: 'To '+to+': '+msg,
styling,
},'information');
});
var phasetext = [
'Pregame',
'Roles',
'Modtime',
'Day',
'Voting',
'Trial',
'Verdict',
'Last Words',
'Night',
'Day 1',
];
addMessageHandler(Type.SETDAYNUMBER,function(num){
phasetext[3] = 'Day '+num;
phasetext[8] = 'Night '+num;
});
addMessageHandler(Type.SETPHASE,function(phase,silent,time)
{
if (!silent)
{
return processMessage({msg: phasetext[phase], styling: 'phasechange'}, 'highlight');
}
});
addMessageHandler(Type.WHISPER,function(msg)
{
return processMessage(msg,'whisper');
});
addMessageHandler(Type.MOD,function(msg)
{
return processMessage(msg,'mod');
});
addMessageHandler(Type.PRENOT,function(notification)
{
switch (notification)
{
case 'GUARDIAN_ANGEL':
return processMessage({msg: "The Guardian Angel was watching over you!", styling: 'reviving'}, 'prenot');
break;
case 'PHLOX':
return processMessage({msg: "A Phlox has purified you!", styling: 'reviving'}, 'prenot');
break;
case 'TRANSPORT':
return processMessage({msg: "You were transported to another location.", styling: 'dying'}, 'prenot');
break;
case 'POISON_CURABLE':
return processMessage({msg: "You were poisoned. You will die tomorrow unless you are cured!", styling: 'dying'}, 'prenot');
break;
case 'POISON_UNCURABLE':
return processMessage({msg: "You were poisoned. You will die tomorrow!", styling: 'dying'}, 'prenot');
break;
case 'PROTECTED':
return processMessage({msg: "You were attacked but someone protected you!", styling: 'reviving'}, 'prenot');
break;
case 'SAVED_BY_BG':
return processMessage({msg: "You were attacked but someone fought off your attacker!", styling: 'reviving'}, 'prenot');
break;
case 'SAVED_BY_TRAP':
return processMessage({msg: "You were attacked but a trap saved you!", styling: 'reviving'}, 'prenot');
break;
case 'SAVED_BY_GA':
return processMessage({msg: "You were attacked but the Guardian Angel saved you!", styling: 'reviving'}, 'prenot');
break;
case 'TARGET_ATTACKED':
return processMessage({msg: "Your target was attacked!", styling: 'dying'}, 'prenot');
break;
case 'MEDUSA_STONE':
return processMessage({msg: "You turned someone to stone.", styling: 'dying'}, 'prenot');
break;
case 'INNO':
return processMessage({msg: "You could not find evidence of wrongdoing. Your target seems innocent.", styling: 'dying'}, 'prenot');
break;
case 'SUS':
return processMessage({msg: "Your target is suspicious!", styling: 'dying'}, 'prenot');
break;
case 'DEAD':
return processMessage({msg:'You have died!',styling:'dying'},'prenot');
break;
case 'BLACKMAIL':
return processMessage({msg:'Someone threatened to reveal your secrets. You are blackmailed!',styling:'dying'},'prenot');
break;
case 'DOUSE':
return processMessage({msg:'You were doused in gas!',styling:'dying'},'prenot');
break;
case 'TARGETIMMUNE':
return processMessage({msg:'Your target\'s defense was too strong to kill.',styling:'dying'},'prenot');
break;
case 'IMMUNE':
return processMessage({msg:'Someone attacked you but your defense was too strong!',styling:'dying'},'prenot');
break;
case 'JESTER':
return processMessage({msg:'The Jester will have their revenge from the grave!',styling:'dying'},'prenot');
break;
case 'SHOTVET':
return processMessage({msg:'You were shot by the Veteran you visited!',styling:'dying'},'prenot');
break;
case 'VETSHOT':
return processMessage({msg:'You shot someone who visited you last night!',styling:'dying'},'prenot');
break;
case 'RB':
return processMessage({msg:'Someone occupied your night. You were roleblocked!',styling:'dying'},'prenot');
break;
case 'WITCHED':
return processMessage({msg:'You felt a mystical power dominating you. You were controlled by a Witch!',styling:'dying'},'prenot');
break;
case 'REVIVE':
return processMessage({msg:'You were revived!',styling:'reviving'},'prenot');
break;
case 'HEAL':
return processMessage({msg:'You were attacked but someone nursed you back to health!',styling:'reviving'},'prenot');
break;
case 'JAILED':
return processMessage({msg:'You were hauled off to jail!',styling:'hauled'},'prenot');
break;
case 'JAILING':
return processMessage({msg:'You dragged your target off to jail!',styling:'reviving'},'prenot');
break;
case 'ENTANGLED':
return processMessage({msg:'You were locked away in the Garden!',styling:'captured'},'prenot');
break;
case 'ENTANGLING':
return processMessage({msg:'You have locked away your target!',styling:'reviving'},'prenot');
break;
case 'LINKED':
return processMessage({msg:'You have been linked!',styling:'reviving'},'prenot');
break;
case 'FULLMOON':
return processMessage({msg:'There is a full moon out tonight.',styling:'moon'},'prenot');
break;
}
});
addMessageHandler(Type.TARGET,function(name,role,target)
{
if(typeof role === 'string') {
role = sanitize(role);
}
return processMessage({
name,
role,
target,
},'target');
});
addMessageHandler(Type.MAYOR, function(name) {
return processMessage({msg: '🎩' +name+' has revealed themselves as the Mayor!', styling: 'mayor_reveal'}, "highlight");
});
addMessageHandler(Type.GARDENIA, function(name) {
return processMessage({msg: '🌼' +name+' has unveiled themselves as the Gardenia!', styling: 'gardenia_reveal'}, "highlight");
});
addMessageHandler(Type.HUG,function(name,target)
{
return processMessage({name:name,target:target},'hug');
});
addMessageHandler(Type.VOTE,function(voter,display,voted,prev)
{
if(display) {
return processMessage({voter,voted,prev},'vote');
}
});
addMessageHandler(Type.VERDICT,function(name,val)
{
return processMessage({name:name,val:val},'verdict');
});
addMessageHandler(Type.GUARDIAN_ANGEL, function(name, yourName) {
return processMessage({msg: '👼 The Guardian Angel has protected '+name+'.', styling: 'guardian_angel'}, "highlight");
});
addMessageHandler(Type.PHLOX, function(name, yourName) {
return processMessage({msg: '🌸 A Phlox has purified '+name+'.', styling: 'phlox_li'}, "highlight");
});
addMessageHandler(Type.JUDGEMENT,function(msg)
{
return processMessage(msg,'judgement');
});
addMessageHandler(Type.ROLECARD,function(card)
{
return processMessage(card,'rolecard');
});
addMessageHandler(Type.WILL,function(will)
{
return processMessage(will,'will');
});
addMessageHandler(Type.NOTES, function (notes) {
return processMessage(notes, 'notes');
});
addMessageHandler(Type.LATENCIES,function(p)
{
if (typeof p == "number")
{
return processMessage('Ping: '+p+'ms','system');
}
else
{
var output = '';
for (i in p)
{
output += processMessage(i+': '+p[i]+'ms','system');
}
return output;
}
});
addMessageHandler(Type.SHOWLIST,function(list)
{
return processMessage(list,'rolelist');
});
addMessageHandler(Type.SHOWALLROLES,function(list)
{
return processMessage(list,'allroles');
});
addMessageHandler(Type.SUGGESTIONS,function(results){
if(typeof jQuery !== 'undefined') {
//Skip this; socketstuff.js has its own (more complex) handler.
return '';
}
//Target list
var rows = [];
rows.push(['<b>Name</b>','<b>Role</b>','<b>Target</b>']); //Header
for (i in results.targets)
{
var data = [];
data.push('<span class="playername">'+i+'</span>'); //Name
data.push(sanitize(results.targets[i][0])); //Role
if (results.targets[i][1] && results.targets[i][1].length != 0)
{
data.push(results.targets[i][1].join(', ')); //Target
}
else
{
data.push('No Action');
}
rows.push(data);
data = [];
}
var table = '<table class="actiontable">'+rows.map(data=>'<tr>'+data.map(a=>'<td>'+a+'</td>').join('')+'</tr>').join('')+'</table>';
var container = '<div class="automodcontainer"><header><p>Automod</p></header>'+table+'</div>';
return container;
});
if(typeof module !== 'undefined') {
module.exports = {
Type: Type,
Phase: Phase,
sanitize: sanitize,
msgToHTML: msgToHTML,
};
}