-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
1081 lines (915 loc) · 27.6 KB
/
app.js
File metadata and controls
1081 lines (915 loc) · 27.6 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
// Server-side code (Node.js with Express and Socket.IO)
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const { v4: uuidv4 } = require('uuid');
const app = express();
const server = http.createServer(app);
const io = new Server(server);
// Serve static files
app.use(express.static('public'));
// Store sessions and their game lists
const sessions = {};
io.on('connection', (socket) => {
console.log('A user connected');
let currentSession = null;
let username = null;
// Gérer l'inscription à une session
socket.on('join-session', (data) => {
currentSession = data.sessionId;
username = data.username; // Récupérer le nom d'utilisateur fourni par le client
socket.join(data.sessionId);
console.log(`User ${username} joined session: ${data.sessionId}`);
});
// Gérer les messages de chat
socket.on('chat-message', (message) => {
if (currentSession) {
socket.to(currentSession).emit('chat-message', {
userId: username || socket.id, // Utiliser le nom d'utilisateur si disponible
message,
isSender: false,
});
// Émettre un message pour l'expéditeur uniquement
socket.emit('chat-message', {
userId: 'You', // Identifie le message comme venant de l'utilisateur
message,
isSender: true,
});
}
});
// Assign username
socket.on('setUsername', (data) => {
username = data.username;
currentSession = data.sessionId;
if (!sessions[currentSession]) {
sessions[currentSession] = { creator: socket.id, users: {}, games: [] };
}
sessions[currentSession].users[socket.id] = { username, isCreator: socket.id === sessions[currentSession].creator };
socket.join(currentSession);
io.to(currentSession).emit('updateUsers', sessions[currentSession].users);
socket.emit('updateList', sessions[currentSession].games);
});
// Add a game
socket.on('addGame', (game) => {
if (currentSession && sessions[currentSession]) {
sessions[currentSession].games.push(game);
io.to(currentSession).emit('updateList', sessions[currentSession].games);
}
});
// Remove a game
socket.on('removeGame', (gameIndex) => {
if (currentSession && sessions[currentSession] && sessions[currentSession].creator === socket.id) {
sessions[currentSession].games.splice(gameIndex, 1);
io.to(currentSession).emit('updateList', sessions[currentSession].games);
}
});
// Clear game list
socket.on('clearGames', () => {
if (currentSession && sessions[currentSession] && sessions[currentSession].creator === socket.id) {
sessions[currentSession].games = [];
io.to(currentSession).emit('updateList', sessions[currentSession].games);
}
});
// Pick a random game
socket.on('pickRandom', () => {
if (currentSession && sessions[currentSession] && sessions[currentSession].creator === socket.id) {
const games = sessions[currentSession].games;
if (games.length > 0) {
const randomGame = games[Math.floor(Math.random() * games.length)];
io.to(currentSession).emit('randomPicked', randomGame);
}
}
});
// Handle user disconnection
socket.on('disconnect', () => {
if (currentSession && sessions[currentSession]) {
delete sessions[currentSession].users[socket.id];
if (sessions[currentSession].creator === socket.id) {
const remainingUsers = Object.keys(sessions[currentSession].users);
if (remainingUsers.length > 0) {
const newCreator = remainingUsers[0];
sessions[currentSession].creator = newCreator;
sessions[currentSession].users[newCreator].isCreator = true;
} else {
delete sessions[currentSession];
}
}
io.to(currentSession).emit('updateUsers', sessions[currentSession]?.users || {});
}
});
});
// Start the server
const PORT = process.env.PORT || 8080;
server.listen(PORT, () => {
console.log('Server running on http://localhost:' + PORT);
});
// Client-side code (public/index.html)
const htmlContent = `
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Game Picker</title>
<style>
body {
font-family: 'Arial', sans-serif;
text-align: center;
margin: 0;
padding: 0;
background-color: #212121;
color: #333;
overflow-x: hidden;
}
h1 {
margin-top: 20px;
font-size: 2.5rem;
color: #4CAF50;
}
h2 {
color: #e6e6e6;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 20px;
}
input, button {
padding: 10px;
margin: 10px;
border: none;
border-radius: 5px;
font-size: 1rem;
}
input {
width: 250px;
border: 1px solid #ccc;
}
button {
background-color: #4CAF50;
color: white;
cursor: pointer;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
opacity: 0.7;
}
button:hover:not(:disabled) {
background-color: #45a049;
}
.buttonBlue {
background-color: #046bda;
}
.buttonBlue:hover:not(:disabled) {
background-color: #0351a6;
}
.buttonRed {
background-color: #da0704;
}
.buttonRed:hover:not(:disabled) {
background-color: #a60503;
}
#game-list {
margin: 20px auto;
padding: 0;
max-width: 400px;
}
#game-list li {
list-style: none;
margin: 5px 0;
padding: 10px;
background: white;
border: 1px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
display: flex;
justify-content: space-between;
}
#game-list li span {
cursor: pointer;
color: red;
font-weight: bold;
}
#selected-game {
margin-top: 20px;
font-size: 1.5rem;
font-weight: bold;
color: #4CAF50;
transition: all 0.3s ease;
}
.session {
margin-top: 20px;
}
.session input {
width: 200px;
}
#user-list {
position: fixed;
bottom: 10px;
right: 10px;
background: rgba(0, 0, 0, 0.6);
color: white;
padding: 10px;
border-radius: 5px;
font-size: 0.9rem;
text-align: left;
min-width: 150px;
}
#user-list h3 {
margin: 0 0 10px;
font-size: 1rem;
color: #fff;
}
.logo-container {
text-align: center;
margin: 20px 0;
}
.logo {
max-width: 230px;
height: auto;
border-radius: 10px;
}
.toCenter {
text-align: center;
}
.shake {
animation: shake 0.5s;
}
@keyframes shake {
0%, 100% {
transform: translateX(0);
}
25% {
transform: translateX(-5px);
}
50% {
transform: translateX(5px);
}
75% {
transform: translateX(-5px);
}
}
/* Fireworks Animation Styles */
.firework-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 10001;
}
.firework {
position: absolute;
width: 6px;
height: 6px;
border-radius: 50%;
box-shadow: 0 0 10px 4px;
animation: explode 1.5s ease-out forwards;
}
.particle {
position: absolute;
width: 4px;
height: 4px;
border-radius: 50%;
box-shadow: 0 0 8px 3px;
animation: spread 1.5s ease-out forwards;
}
@keyframes explode {
0% {
transform: translate(0, 100vh);
opacity: 1;
}
20% {
opacity: 1;
}
30% {
opacity: 0.8;
}
100% {
transform: translate(0, 30vh);
opacity: 0;
}
}
@keyframes spread {
0% {
opacity: 1;
}
40% {
opacity: 0.8;
}
100% {
transform: translate(var(--tx), var(--ty));
opacity: 0;
}
}
.highlight-selection {
animation: highlight 1.5s ease-in-out;
}
@keyframes highlight {
0% {
transform: scale(1);
text-shadow: 0 0 0 transparent;
}
50% {
transform: scale(1.3);
text-shadow: 0 0 20px #4CAF50, 0 0 40px #4CAF50;
}
100% {
transform: scale(1);
text-shadow: 0 0 10px #4CAF50;
}
}
/* Styles pour le décompte */
#countdown {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
color: white;
font-size: 6rem;
justify-content: center;
align-items: center;
z-index: 9999;
}
/* Styles pour la popup */
.popup-container {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10000;
justify-content: center;
align-items: center;
}
.popup {
background-color: white;
padding: 30px;
border-radius: 10px;
max-width: 80%;
text-align: center;
box-shadow: 0 0 30px rgba(76, 175, 80, 0.5);
}
.popup h2 {
color: #4CAF50;
margin-top: 0;
}
.popup p {
font-size: 1.8rem;
margin: 20px 0;
}
.popup button {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
font-size: 1.2rem;
cursor: pointer;
}
.popup button:hover {
background-color: #45a049;
}
.share-button {
position: fixed;
top: 20px;
right: 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 50%;
width: 50px;
height: 50px;
font-size: 24px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
z-index: 1000;
}
.share-button:hover {
background-color: #45a049;
}
.copy-button {
background-color: #046bda;
color: white;
border: none;
border-radius: 5px;
padding: 5px 10px;
font-size: 0.8rem;
cursor: pointer;
margin-left: 5px;
}
.copy-button:hover {
background-color: #0351a6;
}
.copy-success {
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border-radius: 5px;
z-index: 2000;
display: none;
animation: fadeInOut 2s ease-in-out;
}
@keyframes fadeInOut {
0% { opacity: 0; }
10% { opacity: 1; }
90% { opacity: 1; }
100% { opacity: 0; }
}
.input-with-button {
position: relative;
display: inline-block;
width: 200px;
}
.input-with-button input {
padding-right: 40px; /* Espace pour le bouton à droite */
width: 100%;
box-sizing: border-box;
}
.input-with-button button {
position: absolute;
right: -10px;
top: 11px;
height: 65%;
width: 35px;
background-color: transparent;
border: none;
border-left: 0px solid #ccc;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
margin: 0;
padding: 0;
border-radius: 0 5px 5px 0;
}
.input-with-button button:hover {
background-color: #f0f0f0;
color: #046bda;
}
#chat-container {
position: absolute;
bottom: 10px;
left: 10px;
width: 300px;
background: rgba(0, 0, 0, 0.6);
color: white;
padding: 10px;
border-radius: 5px;
display: flex;
flex-direction: column;
}
#chat-box {
text-align: left;
height: 200px;
overflow-y: auto;
margin-bottom: 10px;
padding: 5px;
border: 1px solid #cccccc21;
border-radius: 5px;
background: #ffffff1c;
color: #e6e6e6;
}
#chat-input {
flex: 1;
padding: 5px;
border: 1px solid #ccc;
border-radius: 5px;
}
.txt_chat_user {
font-weight: bold;
color: #53a6ff;
}
.txt_chat_user_me {
font-weight: bold;
color: #ffffff;
}
</style>
</head>
<body>
<div class="logo-container">
<img src="/img/logo.png" alt="Game Picker Logo" class="logo">
</div>
<!-- Notification de copie réussie -->
<div id="copy-success" class="copy-success">Copié !</div>
<!-- Bouton de partage -->
<button id="share-button" class="share-button" title="Partager la session">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="18" cy="5" r="3"></circle>
<circle cx="6" cy="12" r="3"></circle>
<circle cx="18" cy="19" r="3"></circle>
<line x1="8.59" y1="13.51" x2="15.42" y2="17.49"></line>
<line x1="15.41" y1="6.51" x2="8.59" y2="10.49"></line>
</svg>
</button>
<!-- Popup de partage -->
<div class="popup-container" id="share-popup">
<div class="popup">
<h2>Partager cette session</h2>
<p>Copiez ce lien pour inviter d'autres joueurs :</p>
<input type="text" id="share-link" readonly style="width: 100%; margin-bottom: 15px; padding: 8px;">
<button id="copy-share-link">Copier le lien</button>
<button id="close-share-popup" style="margin-left: 10px;">Fermer</button>
</div>
</div>
<!--<h1>Game Picker</h1>-->
<div class="container">
<div class="session">
<input id='username' type='text' placeholder='Enter your username' />
<div class="input-with-button">
<input id='session-id' type='text' placeholder='Enter session ID' />
<button id="copy-session-id" title="Copier l'ID">📋</button>
</div>
<button class="buttonBlue" id='join-session' style='margin-left: 28px;'>Join/Create Session</button>
</div>
<div id='main-content' style='display: none;'>
<input id='game-input' type='text' placeholder='Enter a game' />
<button id='add-game'>Add Game</button>
<button class="buttonRed" id='clear-games'>Clear List</button>
<button id='pick-random'>Pick Random</button>
<h2>Game List</h2>
<ul id='game-list'></ul>
<h2 id='selected-game'></h2>
</div>
</div>
<div id="chat-container">
<div id="chat-box"></div>
<form id="chat-form">
<input id="chat-input" type="text" placeholder="Type your message..." />
<button type="submit">Send</button>
</form>
</div>
<div id="user-list">
<h3 class="toCenter">Participants</h3>
<hr>
</div>
<!-- Élément de décompte -->
<div id="countdown"></div>
<!-- Popup pour afficher le résultat -->
<div class="popup-container" id="result-popup">
<div class="popup">
<h2>Game Selected!</h2>
<p id="popup-game"></p>
<button id="close-popup">OK</button>
</div>
</div>
<script src='/socket.io/socket.io.js'></script>
<script>
const socket = io();
const usernameInput = document.getElementById('username');
const sessionIdInput = document.getElementById('session-id');
const joinSessionButton = document.getElementById('join-session');
const mainContent = document.getElementById('main-content');
const gameInput = document.getElementById('game-input');
const addGameButton = document.getElementById('add-game');
const clearGamesButton = document.getElementById('clear-games');
const pickRandomButton = document.getElementById('pick-random');
const gameList = document.getElementById('game-list');
const selectedGame = document.getElementById('selected-game');
const userList = document.getElementById('user-list');
const countdown = document.getElementById('countdown');
const resultPopup = document.getElementById('result-popup');
const popupGame = document.getElementById('popup-game');
const closePopupButton = document.getElementById('close-popup');
//Ajouter les sons
const beepSound = '/assets/sounds/beep.mp3';
const finalSound = '/assets/sounds/final.mp3';
const fireworkExplodeSound = [
'/assets/sounds/firework.mp3',
'/assets/sounds/firework2.mp3',
'/assets/sounds/firework3.mp3'
];
let currentSession = '';
let isCreator = false;
let countdownActive = false;
// Fonction pour jouer un son spécifique
function playSound(src, vol) {
const audio = new Audio(src);
audio.volume = vol;
audio.play();
}
// Generate random session ID
function generateSessionId() {
return Math.random().toString(36).substring(2, 10);
}
// Shake element
function shakeElement(element) {
element.classList.add('shake');
element.style.borderColor = 'red';
setTimeout(() => {
element.classList.remove('shake');
element.style.borderColor = '';
}, 500);
}
// Handle join session button click
joinSessionButton.addEventListener('click', () => {
const username = usernameInput.value.trim();
let sessionId = sessionIdInput.value.trim();
if (!username) {
shakeElement(usernameInput);
return;
}
if (!sessionId) {
sessionId = generateSessionId();
sessionIdInput.value = sessionId; // Update input field with the generated session ID
}
currentSession = sessionId;
// Joindre une session (assurez-vous que sessionId est défini)
socket.emit('join-session', { sessionId, username });
socket.emit('setUsername', { username, sessionId });
mainContent.style.display = 'block';
//sessionIdInput.disabled = true;
//joinSessionButton.disabled = true;
//usernameInput.disabled = true;
});
// Envoyer un message de chat
const chatInput = document.getElementById('chat-input');
const chatForm = document.getElementById('chat-form');
chatForm.addEventListener('submit', (e) => {
e.preventDefault();
const message = chatInput.value;
if (message) {
socket.emit('chat-message', message);
chatInput.value = '';
}
});
// Recevoir un message de chat
const chatBox = document.getElementById('chat-box');
socket.on('chat-message', (data) => {
const messageElement = document.createElement('div');
const messageElement_User = document.createElement('span');
if (data.isSender) {
messageElement_User.classList.add('txt_chat_user_me');
} else {
messageElement_User.classList.add('txt_chat_user');
}
const messageElement_Msg = document.createElement('span');
messageElement_User.textContent = data.userId;
messageElement_Msg.textContent = ' : ' + data.message;
messageElement.appendChild(messageElement_User);
messageElement.appendChild(messageElement_Msg);
chatBox.appendChild(messageElement);
chatBox.scrollTop = chatBox.scrollHeight; // Faire défiler vers le bas
});
// Update game list
socket.on('updateList', (games) => {
gameList.innerHTML = '';
games.forEach((game, index) => {
const li = document.createElement('li');
li.textContent = game;
if (isCreator) {
const removeButton = document.createElement('span');
removeButton.textContent = '❌';
removeButton.addEventListener('click', () => {
if (!countdownActive) {
socket.emit('removeGame', index);
}
});
li.appendChild(removeButton);
}
gameList.appendChild(li);
});
});
// Update user list
socket.on('updateUsers', (users) => {
userList.innerHTML = '<h3 class="toCenter">Participants</h3><hr>';
isCreator = false;
for (const userId in users) {
const user = users[userId];
const userItem = document.createElement('div');
userItem.textContent = user.username;
if (user.isCreator) {
userItem.textContent += ' ★';
if (socket.id === userId) {
isCreator = true;
}
}
userList.appendChild(userItem);
}
// Mettre à jour l'interface en fonction du rôle de l'utilisateur
updateUserInterface();
});
// Mettre à jour l'interface en fonction du rôle de l'utilisateur
function updateUserInterface() {
// Activer/désactiver les contrôles en fonction du rôle
//addGameButton.disabled = !isCreator || countdownActive;
clearGamesButton.disabled = !isCreator || countdownActive;
pickRandomButton.disabled = !isCreator || countdownActive;
//gameInput.disabled = !isCreator || countdownActive;
}
// Fonction de décompte
function startCountdown(callback) {
countdownActive = true;
updateUserInterface();
countdown.style.display = 'flex';
let count = 3;
countdown.textContent = count;
playSound(beepSound, 0.1);
const interval = setInterval(() => {
count--;
countdown.textContent = count;
if (count > 0) {
playSound(beepSound, 0.1);
} else {
clearInterval(interval);
playSound(finalSound, 1);
countdown.style.display = 'none';
countdownActive = false;
if (callback) callback();
}
}, 1000);
}
// Create fireworks animation
function createFireworks() {
const fireworkContainer = document.createElement('div');
fireworkContainer.className = 'firework-container';
fireworkContainer.style.zIndex = '10001'; // Assurez-vous que le z-index est supérieur à celui de la popup (10000)
document.body.appendChild(fireworkContainer);
// Créer plusieurs feux d'artifice avec des délais différents
for (let i = 0; i < 12; i++) {
setTimeout(() => {
launchFirework(fireworkContainer);
}, i * 300);
}
// Supprimer le conteneur après la fin des animations
setTimeout(() => {
fireworkContainer.remove();
}, 8000);
}
function launchFirework(container) {
// Créer le feu d'artifice principal
const firework = document.createElement('div');
firework.className = 'firework';
// Position aléatoire
const xPos = Math.random() * 80 + 10; // 10-90% de la largeur de l'écran
firework.style.left = xPos + "%";
// Couleur aléatoire
const hue = Math.floor(Math.random() * 360);
const saturation = 90 + Math.floor(Math.random() * 10);
const lightness = 50 + Math.floor(Math.random() * 10);
firework.style.backgroundColor = 'hsl('+hue+', '+saturation+'%, '+lightness+'%)';
firework.style.boxShadow = '0 0 10px 4px hsl('+hue+', '+saturation+'%, '+lightness+'%)';
container.appendChild(firework);
// Créer des particules pour l'effet d'explosion
setTimeout(() => {
const x = firework.offsetLeft;
const y = firework.offsetTop;
// Créer plusieurs particules avec des formes et tailles variées
const particleCount = 50 + Math.floor(Math.random() * 30);
for (let i = 0; i < particleCount; i++) {
const particle = document.createElement('div');
particle.className = 'particle';
// Direction aléatoire
const angle = Math.random() * Math.PI * 2;
const distance = Math.random() * 150 + 80;
const tx = Math.cos(angle) * distance;
const ty = Math.sin(angle) * distance;
// Variation de taille pour chaque particule
const size = 2 + Math.random() * 4;
particle.style.width = size + 'px';
particle.style.height = size + 'px';
particle.style.setProperty('--tx', tx + "px");
particle.style.setProperty('--ty', ty + "px");
// Couleur aléatoire de la même famille de teinte
const particleHue = hue + Math.random() * 40 - 20;
const particleSat = saturation + Math.floor(Math.random() * 10);
const particleLit = lightness + Math.floor(Math.random() * 30);
particle.style.backgroundColor = 'hsl('+particleHue+', '+particleSat+'%, '+particleLit+'%)';
particle.style.boxShadow = '0 0 8px 3px hsl('+particleHue+', '+particleSat+'%, '+particleLit+'%)';
// Position au point d'explosion du feu d'artifice
particle.style.left = x + "px";
particle.style.top = y + "px";
container.appendChild(particle);
}
// Supprimer le feu d'artifice principal après l'explosion
playSound(fireworkExplodeSound[Math.floor(Math.random() * fireworkExplodeSound.length)], 0.1);
firework.remove();
}, 800);
}
// Display random picked game with popup
socket.on('randomPicked', (game) => {
if (countdownActive) return;
// Démarrer le décompte
startCountdown(() => {
// Après le décompte, afficher la popup
popupGame.textContent = game;
resultPopup.style.display = 'flex';
resultPopup.classList.add('highlight-selection');
createFireworks();
// Mettre à jour le texte en-dessous aussi
selectedGame.textContent = 'Selected Game: ' + game;
//selectedGame.classList.add('highlight-selection');
setTimeout(() => {
//selectedGame.classList.remove('highlight-selection');
resultPopup.classList.remove('highlight-selection');
}, 1500);
});
});
// Fermer la popup et réactiver les contrôles
closePopupButton.addEventListener('click', () => {
resultPopup.style.display = 'none';
countdownActive = false;
updateUserInterface();
});
// Add game to list
addGameButton.addEventListener('click', () => {
if (countdownActive) return;
const game = gameInput.value.trim();
if (game) {
socket.emit('addGame', game);
gameInput.value = '';
}
});
// Clear game list
clearGamesButton.addEventListener('click', () => {
if (!isCreator || countdownActive) return;
socket.emit('clearGames');
});
// Pick random game
pickRandomButton.addEventListener('click', () => {
if (!isCreator || countdownActive) return;