forked from KieronDowie/TestingGrounds
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsocketstuff.js
More file actions
1404 lines (1391 loc) · 36.8 KB
/
socketstuff.js
File metadata and controls
1404 lines (1391 loc) · 36.8 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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Variable to track connection
connection = false;
//Players on list
var users = [];
//Mod
var mod = false;
var paused = false;
var currentphase = undefined;
var daynumber = 1;
//Connect attempts
var connectAttempt = 0;
var kicked = false;
//Notify sound
var hey = new Audio('6ballStart.mp3');
//Halloween
//var hey = new Audio('Giratina.wav');
var mpregame = new Audio('Alfheim.mp3');
var whoami = new Audio('Cascades.mp3');
var mmodtime = new Audio('Valkyrie.mp3');
//Halloween
//var mmodtime = new Audio('Bewitching.mp3');
var mdaytime = new Audio('Piglets.mp3');
var mvoting = new Audio('Deceitful.mp3');
var mtrial = new Audio('Truth.mp3');
var mnight = new Audio('Chaos.mp3');
var musicon = 1;
mpregame.loop = true;
whoami.loop = true;
mdaytime.loop = true;
mnight.loop = true;
mmodtime.loop = true;
function clearAllInfo()
{
var all = $('.controlbutton');
for (var i=0; i<all.length;i++)
{
var classes = all[i].className.split(' ');
for (x in classes)
{
if (classes[x].indexOf('buttondown') != -1)
{
classes[x] = '';
}
}
all[i].className = classes.join(' ');
}
}
function modInterface()
{
//Add play/pause button to the clock
if ($('#clock').length > 0)
{
addPauseButton(currentphase);
}
addModControls();
for (x = 0; x < users.length; x++)
{
var li = $("<li></li>");
var num = (x==0)?'MOD':x;
if ($($('#userlist li')[x]).find('.dev').length)
{
var name = '<span class="name dev">'+users[x]+'</span>';
}
else
{
var name = '<span class="name">'+users[x]+'</span>';
}
var info = $(`<div class="info" id="p-${users[x]}"><span class="num">${num}</span>${name}</div>`);
$('#userlist li')[x].innerHTML='';
$($('#userlist li')[x]).removeClass('deadplayer');
//Add in a rolelist button if it is does not already exist
if ($('#rolelistbutton').length == 0)
{
var rlbutton = $('<div id="rolelistbutton"></div>');
rlbutton.click(function()
{
openRolelist();
});
$('#inputarea').append(rlbutton);
}
//Add in an automod settings button if it doesn't exist
if ($('#automodsettingsbutton').length == 0)
{
var ambutton = $('<div id="automodsettingsbutton"></div>');
ambutton.click(function()
{
autoModSettings();
});
$('#inputarea').append(ambutton);
}
//Addition to the top row
var kill = $('<div class="controlbutton killbutton"><span>Kill</span></div>');
kill.click(function()
{
var index = $('.killbutton, .revivebutton').index($(this))
socket.sendMessage(Type.TOGGLELIVING,users[index],!$(this).hasClass('killbutton'));
});
var jail= $('<div class="controlbutton jailbutton"><span>Jail</span></div>');
jail.click(function()
{
var index = $('.jailbutton, .releasebutton').index($(this))
socket.sendMessage(Type.TOGGLE,users[index],'jailed',$(this).hasClass('jailbutton'));
});
var will = $('<div class="controlbutton modwillbutton"><span>W</span></div>');
var more = $('<div class="controlbutton more"></div>');
var arrow = $('<span class="downarrow"></span>');
more.append(arrow);
more.click(function(e)
{
openModList(e.target);
});
will.click(function(e)
{
openUserWill(e.target);
});
info.append(more);
info.append(will);
info.append(jail);
info.append(kill);
//Adding bottom row
var modcontrols = $('<div class="modcontrols"></div>');
var rolechanger = $('<input type="text" class="role"></li>');
rolechanger.keydown(function(e)
{
if (e.keyCode==13) //Enter
{
var index = $('.role').index($(this));
var name = $('.name')[index].innerHTML;
socket.sendMessage(Type.SETROLE,name,this.value);
this.style.background='teal';
this.old = this.value;
}
});
rolechanger.keyup(function(e){
if (this.old != this.value)
{
this.style.background='white';
}
else
{
this.style.background='teal';
}
});
modcontrols.append(rolechanger);
var entangle= $('<div class="controlbutton entanglebutton"><span>Entangle</span></div>');
entangle.click(function()
{
var index = $('.entanglebutton, .disentanglebutton').index($(this))
socket.sendMessage(Type.TOGGLE,users[index],'entangled',$(this).hasClass('entanglebutton'));
});
modcontrols.append(entangle);
$($('#userlist li')[x]).append(info);
$($('#userlist li')[x]).append(modcontrols);
}
$('.name').addClass('shorten');
}
var urlParams = new URLSearchParams(window.location.search);
var player_name = urlParams.get("name");
if(!player_name) window.location = '/';
var listeners = {};
function addSocketListener(type, callback)
{
listeners[type] = callback;
}
var socket;
function connectSocket(reconnecting)
{
var protocol = (window.location.protocol === 'https:') ? 'wss:' : 'ws:';
socket = new WebSocket(protocol+'//'+window.location.host+'/');
socket.addEventListener('open', function()
{
socket.sendMessage(Type.JOIN, player_name, reconnecting);
});
socket.addEventListener('close',function()
{
kittyReconnect();
});
socket.addEventListener('message', function(event)
{
var [type, ...args] = JSON.parse(event.data);
var display = msgToHTML(type, args);
if(display) {
addMessage(display);
}
if(listeners[type]) {
listeners[type].apply(socket, args);
}
});
socket.sendMessage = function()
{
this.send(JSON.stringify(Array.prototype.slice.call(arguments)));
}
}
connectSocket();
addSocketListener(Type.HELP,function(commands)
{
var helpmsgs = [
"The Mod does not bite, they are only killing people.",
"The Mod can make mistakes, if you believe there has been a mod error, message them using /mod.",
"Please keep your last will closed during Modtime.",
"You can tell the mod what you would like to do at night using /mod or /target",
"Be patient as Modtime can last for a while, depending on the player number and roles in play.",
];
var tldrchanges = [
"None at the moment lol.",
];
var controls = $("<div class='helppanel shrink aChanges' id='helpListPanel'></div>");
var controldetails = [
'You can kill or jail a player using the bigger buttons.',
'Assign a player a role manually using the textbox under their name.',
'You have access to a player\'s will using the W button. Use this to forge or clean a will.',
'The white button labelled with a V allows you to send preset messages to a player. You can also access the modifiers and actions here.'
];
for (i in controldetails)
{
controls.append('<li>'+controldetails[i]+'</li>');
}
var controlslink = $("<a href='#'>As the mod, you have control over every player.</a>");
var rlinfolink= $("<a href='#'>You can access the rolelist using the button to the right of the chatbox.</a>");
var rlinfo = [
"You can edit each entry in the rolelist using the pencil button to the right of each role.",
"The button with a dice will roll a role for all players.",
"The green ticlk will assign the roles to each player.",
"Show List will show the rolelist to all of the players in the game. It is recommended that you use this before starting.",
"Show Roles will show the roles of all players. It is recommended that you use this after the game is over.",
"'Custom roles' allows you to toggle the rolling of non-standard roles. When unchecked, only roles actually in the game will roll.",
"The Autolist button sets the rolelist to the recommended list for that amount of players."
];
var rlpanel = $("<div class='helppanel shrink aChanges' id='helpListPanel'></div>");
for (i in rlinfo)
{
rlpanel.append('<li>'+rlinfo[i]+'</li>');
}
rlinfolink.click(function(){
showPanel(rlpanel);
});
controlslink.click(function(){
showPanel(controls);
});
var modhelp = [
"The following information is only neccessary if you are planning to mod a Testing Grounds game.",
"If you are using manual mode, have a separate file open to take note of night actions and results to check if you have given everyone their feedback.",
controlslink,
controls,
rlinfolink,
rlpanel,
'Each game begins with a "Roles" phase. In this phase you distribute the roles, set the modifiers and wait for everyone to confirm their role.',
'You may need to tell them about the /confirm command, if there are new players present.',
'After everyone has confirmed, select Day 1 to start the game.',
'Phases will end in Modtime, where it is your call to perform the actions before setting the phase to Day/Night.',
];
var changespan = $("<div class='helppanel shrink aChanges' id='helpListPanel'></div>");
for (i in tldrchanges)
{
changespan.append('<li>'+tldrchanges[i]+'</li>');
}
var abridgedChanges = $('<a href="#" class="backline">Or see the tldr version.</a>');
abridgedChanges.click(function(){
showPanel(changespan);
});
//Help
var helpmsg = "";
for (i in helpmsgs)
{
helpmsg += '<li>'+helpmsgs[i]+"</li>";
}
var com = $("<div class='helppanel shrink helpPanel' id='helpListPanel'>"+helpmsg+"</div>");
com.prepend(changespan);
com.prepend(abridgedChanges);
com.prepend("<li>Read the standard changes of the Testing Grounds <a target='_blank' href='https://docs.google.com/document/d/1d_a-R-lhKQpQe_fYD3XyBnCx4WQETI9GokBjscB96mk/edit'>here</a>.</li>");
var com2 = $("<div class='helppanel shrink' id='modListPanel'></div>");
for ( i in modhelp)
{
var li = $('<li></li>');
li.append(modhelp[i]);
com2.append(li);
}
var com3 = $("<div class='helppanel shrink' id='commandListPanel'></div>");
var txt1 = $('<a href="#">General help</a>');
var txt2 = $('<a href="#">Modding help</a>');
var txt3 = $('<a href="#">List of commands</a>');
//Command list.
for (i in commands)
{
var f = i[0].toUpperCase() + i.substring(1,i.length);
com3.append($('<li class="commandheader"><b>'+f+'</b></li>'));
for (j in commands[i])
{
com3.append($('<li><b>/'+j+': </b>'+commands[i][j]+'</li>'));
}
}
txt1.click(function(){
showPanel(com);
});
txt2.click(function(){
showPanel(com2);
});
txt3.click(function(){
showPanel(com3);
});
[txt1,com,txt2,com2,txt3,com3].map(function(msg) {
var li = $('<li></li>');
li.append(msg);
addMessage(li);
});
});
addSocketListener(Type.PING,function()
{
socket.sendMessage(Type.PONG);
});
addSocketListener(Type.HEY,function(){
hey.play();
});
addSocketListener(Type.JOIN,function(name)
{
users.push(name);
var num = $('#userlist').children().length;
if (num==0)
{
num='MOD';
//Player is first. They are mod.
mod=true;
//Add in a rolelist button if it is does not already exist
if ($('#rolelistbutton').length == 0)
{
var rlbutton = $('<div id="rolelistbutton"></div>');
rlbutton.click(function()
{
openRolelist();
});
$('#inputarea').append(rlbutton);
}
//Add in an automod settings button if it doesn't exist
if ($('#automodsettingsbutton').length == 0)
{
var ambutton = $('<div id="automodsettingsbutton"></div>');
ambutton.click(function()
{
autoModSettings();
});
$('#inputarea').append(ambutton);
}
addModControls();
}
//Top row, normal users.
var li = $('<li></li>');
var info = $(`<div class="info" id="p-${name}"></div>`);
var name = $('<span class="name">'+name+'</span>');
var num = $('<span class="num">'+num+'</span>');
info.append(num);
info.append(name);
//Bottom row
if (mod)
{
//Addition to the top row
var kill = $('<div class="controlbutton killbutton"><span>Kill</span></div>');
kill.click(function()
{
var index = $('.killbutton, .revivebutton').index($(this))
socket.sendMessage(Type.TOGGLELIVING,users[index],!$(this).hasClass('killbutton'));
});
var jail= $('<div class="controlbutton jailbutton"><span>Jail</span></div>');
jail.click(function()
{
var index = $('.jailbutton, .releasebutton').index($(this))
socket.sendMessage(Type.TOGGLE,users[index],'jailed',$(this).hasClass('jailbutton'));
});
var will = $('<div class="controlbutton modwillbutton"><span>W</span></div>');
var more = $('<div class="controlbutton more"><span class="downarrow"></span></div>');
more.click(function(e)
{
openModList(e.target);
});
will.click(function(e)
{
openUserWill(e.target);
});
info.append(more);
info.append(will);
info.append(jail);
info.append(kill);
//Adding bottom row
var modcontrols = $('<div class="modcontrols"></div>');
var rolechanger = $('<input type="text" class="role"></li>');
rolechanger.keydown(function(e)
{
if (e.keyCode==13) //Enter
{
var index = $('.role').index($(this));
var name = $('.name')[index].innerHTML;
socket.sendMessage(Type.SETROLE,name,this.value);
this.style.background='teal';
this.old = this.value;
}
});
rolechanger.keyup(function(e){
if (this.old != this.value)
{
this.style.background='white';
}
else
{
this.style.background='teal';
}
});
modcontrols.append(rolechanger);
var entangle= $('<div class="controlbutton entanglebutton"><span>Entangle</span></div>');
entangle.click(function()
{
var index = $('.entanglebutton, .disentanglebutton').index($(this))
socket.sendMessage(Type.TOGGLE,users[index],'entangled',$(this).hasClass('entanglebutton'));
});
modcontrols.append(entangle);
}
li.append(info);
if (mod) {
li.append(modcontrols);
}
$('#userlist').append(li);
if (mod)
{
$('.name').addClass('shorten');
}
});
addSocketListener(Type.LEAVE,function(name)
{
var index = users.indexOf(name);
$($('#userlist').children()[index]).remove();
//Recalculate the numbering.
var nums = $('.num');
for (var i = index; i < nums.length; i++ )
{
if (i!=0)
{
nums[i].innerHTML=''+i;
}
else
{
nums[i].innerHTML='MOD';
}
}
//Remove from list
users.splice(index,1);
});
addSocketListener(Type.DISCONNECT,function(name)
{
$(`#p-${name}`).append(`<span class="emoji" id="${name}-disconnected">🚫</span>`);
});
addSocketListener(Type.RECONNECT,function(name)
{
$(`#${name}-disconnected`).remove();
});
addSocketListener(Type.SETROLE,function(role)
{
if(!mod)
{
var li = $(`#p-${player_name}`).closest('li');
li.find('.roledisplay').remove();
var role_safe = sanitize(role.role);
li.append(`<div class="roledisplay"><span style="color: ${role.rolecolor}">${role_safe}</span></div>`);
}
});
addSocketListener(Type.SETMOD,function(val)
{
if (val && !mod)
{
mod = true;
modInterface();
}
else if (mod && !val)
{
$('.pausebutton, .playbutton').remove();
$('#modnumbering').empty();
mod = false;
var buttons = $('.killbutton, .revivebutton');
var roles = $('.role');
for (i = 0; i < users.length; i++)
{
var num = i==0?'MOD':i;
if ($($('#userlist li')[i]).find('.dev').length)
{
var name ='<span class="name dev">'+users[i]+'</span>';
}
else
{
var name ='<span class="name">'+users[i]+'</span>';
}
if ($(buttons[i]).hasClass('killbutton'))
{
//Top row, normal users.
var info = $(`<div class="info" id="p-${users[i]}"><span class="num">${num}</span>${name}</div>`);
}
else
{
var role = roles[i].value==''?'NoRole':roles[i].value;
var info = $(`<div class="info" id="${users[i]}"><span class="num">${num}</span><span class="name">${name}</span></div><div>${role}</div>`);
$($('#userlist li')[i]).addClass('deadplayer');
}
$('#userlist li')[i].innerHTML='';
$($('#userlist li')[i]).append(info);
}
if ($('#rolelist').length != 0)
{
$('#rolelist').remove();
}
if ($('#rolelistbutton').length != 0)
{
$('#rolelistbutton').remove();
}
if ($('#automodsettingsbutton').length != 0)
{
$('#automodsettingsbutton').remove();
}
$('.name').removeClass('shorten');
}
});
addSocketListener(Type.ROOMLIST,function(list)
{
if (!mod)
{
users = [];
$('#userlist').empty();
for (i in list)
{
var num = (i==0)?'MOD':i; //Num is MOD if i is 0, otherwise num is equal to i.
if (list[i].role)
{
//Player is dead.
var role_safe = sanitize(list[i].role);
$('#userlist').append(`<li class="deadplayer"><div class="info" id="p-${list[i].name}"><span class="num">${num}</span><span class="name">${list[i].name}</span></div><div class="roledisplay"><span style="color: ${list[i].rolecolor}">${role_safe}</span></div></li>`);
if(list[i].haswill) {
$(`#p-${list[i].name}`).append(`<span class="emoji" id="${list[i].name}-will">📜</span>`);
$(`#${list[i].name}-will`).click((e) => {
openUserWill(e.target);
});
}
}
else
{
$('#userlist').append(`<li><div class="info" id="p-${list[i].name}"><span class="num">${num}</span><span class="name">${list[i].name}</span></div></li>`);
}
if(list[i].spectate)
{
$($('#userlist').children()[i]).addClass('spectator');
}
if(list[i].dev)
{
$($('#userlist').children()[i]).find('.name').addClass('dev');
}
users.push(list[i].name);
}
}
else
{
var user_names = {};
$('#userlist').children().each((i,el)=>user_names[users[i]] = el);
users = [];
for(i in list)
{
if(user_names[list[i].name])
{
$(user_names[list[i].name]).find('.num').text(+i || 'MOD');
$('#userlist').append(user_names[list[i].name]);
delete user_names[list[i].name];
users.push(list[i].name);
}
else
{
listeners[Type.JOIN](list[i].name);
}
if(list[i].spectate)
{
$($('#userlist').children()[i]).addClass('spectator');
}
else
{
$($('#userlist').children()[i]).removeClass('spectator');
}
if(list[i].dev)
{
$($('#userlist').children()[i]).find('.name').addClass('dev');
}
}
for(i in user_names)
{
$(user_names[i]).remove();
}
}
});
addSocketListener(Type.TOGGLELIVING,function(p)
{
if (!mod)
{
var index = users.indexOf(p.name);
var li = $($('#userlist').children()[index]);
index = index==0?'MOD':index;
if (p.hasOwnProperty('role'))
{
var role_safe = sanitize(p.role);
li.addClass('deadplayer');
li.find('.roledisplay').remove();
li.append(`<div class="roledisplay"><span style="color: ${p.rolecolor}">${role_safe}</span></div>`);
if(p.haswill) {
$(`#p-${p.name}`).append(`<span class="emoji" id="${p.name}-will">📜</span>`);
$(`#${p.name}-will`).click(() => {
openUserWill($(`#p-${p.name}`));
});
}
}
else
{
li.removeClass('deadplayer');
if(p.name != player_name) {
li.find('.roledisplay').remove();
}
li.find(`#${p.name}-will`).remove();
}
}
else
{
var index = users.indexOf(p.name);
var li = $($('#userlist').children()[index]);
var button = li.find('.killbutton, .revivebutton');
if (p.hasOwnProperty('role'))
{
button.removeClass('killbutton');
button.addClass('revivebutton');
button.html('<span>Revive</span>');
}
else
{
button.removeClass('revivebutton');
button.addClass('killbutton');
button.html('<span>Kill</span>');
}
}
});
addSocketListener(Type.KICK,function()
{
kicked = true;
});
addSocketListener(Type.DENY,function(reason){
addMessage('<li><b>'+reason+'</b></li>');
kicked = true;
});
addSocketListener(Type.SETDAYNUMBER,function(num){
daynumber = num;
$('#dayli').html('Day '+num);
if (num % 2 == 0)
{
$('#nightli').html('Night '+num+'<div class="moonimg" />');
}
else
{
$('#nightli').html('Night '+num);
}
});
addSocketListener(Type.SETPHASE,function(phase,silent,time)
{
currentphase = phase;
if (phase == 0)
{
if (musicon == 1)
{
mpregame.currentTime = 0;
mpregame.volume = 1;
}
}
else
{
mpregame.volume = 0;
}
if (phase == 1)
{
whoami.play();
whoami.currentTime = 0;
if (musicon == 1)
{
whoami.volume = 1;
}
}
else
{
whoami.volume = 0;
}
if (phase == 2)
{
if (musicon == 1)
{
mmodtime.currentTime = 0;
mmodtime.volume = 1;
}
}
else
{
mmodtime.volume = 0;
}
if (phase == 3 || phase == 9)
{
mdaytime.play();
mdaytime.currentTime = 0;
if (musicon == 1)
{
mdaytime.volume = 1;
}
}
else
{
mdaytime.volume = 0;
}
if (phase == 4)
{
mvoting.play();
mvoting.currentTime = 0;
if (musicon == 1)
{
mvoting.volume = 1;
}
}
else
{
mvoting.volume = 0;
}
if (phase >= 5 && phase <= 7)
{
if (phase == 5)
{
mtrial.play();
mtrial.currentTime = 0;
if (musicon == 1)
{
mtrial.volume = 1;
}
}
}
else
{
mtrial.volume = 0;
}
if (phase == 8)
{
mnight.play();
mnight.currentTime = 0;
if (musicon == 1)
{
mnight.volume = 1;
}
}
else
{
mnight.volume = 0;
}
//Remove any remaining voting interfaces
$('.votinginterface').remove();
//Remove any remaining night interfaces
$('.nightinterface').remove();
//Remove any remaining verdict interfaces
$('.verdictinterface').remove();
$('header ul li').removeClass('current');
$($('header ul li')[phase]).addClass('current');
$('#clock').remove();
$('.pausebutton, .playbutton').remove();
//Move the clock.
if (time > 0)
{
$($('header ul li')[phase]).append('<div id="clock">'+time+'</div>');
if (mod)
{
addPauseButton(phase);
}
}
if (phase == 4 && !mod) //Voting
{
//Add the voting interface
for (i = 1; i < users.length; i++)
{
if (!$($('#userlist li')[i]).is('.deadplayer, .spectator'))
{
var li = $('#userlist').children()[i];
var votinginterface = $('<div class="votinginterface"></div>');
if(users[i] !== player_name && !$($('#userlist li')[i]).find('.angel').length && !$(`#p-${player_name}`).closest('li').is('.deadplayer, .spectator')) {
var button = $('<div class="votebutton">Vote</div>');
button.click(function()
{
var index = $('#userlist li').index(this.parentNode.parentNode.parentNode);
var name = users[index];
socket.sendMessage(Type.VOTE,name);
});
votinginterface.append(button);
}
var count = $('<div class="votecount">0</div>');
votinginterface.append(count);
$($(li).children()[0]).append(votinginterface);
}
}
}
if (phase == 6 && !mod && !$(`#p-${player_name}`).closest('li').is('.deadplayer, .spectator')) //Verdicts, guilty/inno/abstain
{
//Add verdict interface
var verdict = $('<div class="verdictinterface"></div>');
var guilty = $('<div class="verdictbutton guiltybutton"></div>');
guilty.click(function()
{
socket.sendMessage(Type.VERDICT,false); //false for guilty
});
var inno = $('<div class="verdictbutton innobutton"></div>');
inno.click(function()
{
socket.sendMessage(Type.VERDICT,true); //true for inno
});
verdict.append(guilty);
verdict.append(inno);
$('#main').append(verdict);
}
//Initially start all songs.
mpregame.play();
whoami.play();
mmodtime.play();
mdaytime.play();
mvoting.play();
mtrial.play();
mnight.play();
});
addSocketListener(Type.TARGETING_OPTIONS,function(legal_targets,current_targets) {
$('.nightinterface').remove();
if(mod) {
// The mod cannot target
return;
}
if(!legal_targets.length || !legal_targets[0].length) {
// No legal targets
return;
}
var n, i;
var targets_chosen = legal_targets.map(()=>'');
for(n = legal_targets.length-1; n >= 0; n--) {
targetlist = legal_targets[n];
//Add the night buttons
for (i = 1; i < users.length; i++)
{
var name = users[i];
var player_el = $(`#p-${name}`);
var nightinterface = $('<div class="nightinterface"></div>');
if(targetlist.includes(users[i])) {
var button = $('<div class="nightbutton">Target</div>');
button.data('target-name', name);
button.data('target-num', n);
if(current_targets && current_targets[n] == name) {
targets_chosen[n] = name;
button.addClass('pressed');
}
button.click(function()
{
var n = $(this).data('target-num');
var name = $(this).data('target-name');
if($(this).hasClass('pressed')) {
targets_chosen[n] = '';
$(this).removeClass('pressed');
} else {
if(targets_chosen[n]) {
$('#p-'+targets_chosen[n]).find('.nightbutton').filter((i,a)=>$(a).data('target-num') == n).removeClass('pressed');
}
targets_chosen[n] = name;
$(this).addClass('pressed');
}
socket.sendMessage(Type.TARGET, targets_chosen.filter(a=>a).join(' ') || '0');
});
nightinterface.append(button);
}
player_el.append(nightinterface);
}
}
});
addSocketListener(Type.SWITCH,function(name1,name2)
{
var i1=users.indexOf(name1);
var i2=users.indexOf(name2);
users[i1] = name2;
users[i2] = name1;
//Swap li's
var a = $($('#userlist li')[i1]);
var b = $($('#userlist li')[i2]);
//Swap list items
b.before(a);
$('#userlist li:first-child').before(b);
//Swap numbers
$('.num')[i1].innerHTML = (i1==0)?'MOD':i1;
$('.num')[i2].innerHTML = (i2==0)?'MOD':i2;
});
addSocketListener(Type.TOGGLE, function(to, chat, state) {
if(!mod)
return;
var li = $(`#p-${to}`).closest('li');
switch(chat) {
case 'jailed':
var button = li.find('.jailbutton, .releasebutton');
if (state)
{
button.removeClass('jailbutton');
button.addClass('releasebutton');
button.html('<span>Release</span>');
}
else
{
button.removeClass('releasebutton');
button.addClass('jailbutton');
button.html('<span>Jail</span>');
}
break;
case 'entangled':
var button = li.find('.entanglebutton, .disentanglebutton');
if (state)
{
button.removeClass('entanglebutton');
button.addClass('disentanglebutton');
button.html('<span>Disentangle</span>');
}
else
{
button.removeClass('disentanglebutton');
button.addClass('entanglebutton');
button.html('<span>Entangle</span>');
}
break;
case 'douse':
if(state) {
$(`#p-${to}`).append(`<span class="emoji" id="${to}-fire">🔥</span>`);
$(`#${to}-fire`).click(() => {
if (mod) {
socket.sendMessage(Type.TOGGLE, to, 'douse', false);
}
});
} else {
$(`#${to}-fire`).remove();
}
break;
default:
var controlbutton = li.find('.controlbutton');
if (state)
{
controlbutton.addClass(chat+'buttondown');
}
else
{
controlbutton.removeClass(chat+'buttondown');
}
}
});
addSocketListener(Type.MAYOR, function(name) {
$(`#p-${name}`).append(`<span class="emoji" id="${name}-mayor" style="color:#7fff00">(Mayor)</span>`)
$(`#${name}-mayor`).click(() => {
if (mod) {
$(`#${name}-mayor`).remove();
socket.sendMessage(Type.REMOVE_EMOJI, `${name}-mayor`);
}
});
});
addSocketListener(Type.GARDENIA, function(name) {
$(`#p-${name}`).append(`<span class="emoji" id="${name}-gardenia" style="color:#228f41">(Gardenia)</span>`)
$(`#${name}-gardenia`).click(() => {
if (mod) {
$(`#${name}-gardenia`).remove();
socket.sendMessage(Type.REMOVE_EMOJI, `${name}-gardenia`);
}
});
});
addSocketListener(Type.VOTE,function(voter,msg,voted,prev,power)
{
if (!mod)
{
if (prev)
{
var index = users.indexOf(prev);
var li =$('#userlist li')[index];
if (li.childNodes[0].childNodes[2])
{
var count = li.querySelector('.votecount');
var num = parseInt(count.innerHTML);
num -= power;
count.innerHTML=num;
}
}
if (voted)
{
var index = users.indexOf(voted);
var li =$('#userlist li')[index];
var count = li.querySelector('.votecount');
var num = parseInt(count.innerHTML);
num += power;