-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
160 lines (136 loc) · 5.13 KB
/
admin.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
let currentQuestionIndex = 0;
let gameWindowLoaded = false;
const teamScores = {
team1: 0,
team2: 0,
team3: 0,
team4: 0
};
// Attendre que la fenêtre du jeu soit chargée
if (gameWindow) {
gameWindow.onload = () => {
gameWindowLoaded = true;
displayQuestion(currentQuestionIndex);
};
}
const questionElement = document.getElementById('admin-current-question');
const answersListElement = document.getElementById('admin-answers-list');
const team1ScoreElement = document.getElementById('admin-team1-score');
const team2ScoreElement = document.getElementById('admin-team2-score');
const team3ScoreElement = document.getElementById('admin-team3-score');
const team4ScoreElement = document.getElementById('admin-team4-score');
function updateScore(team, points) {
teamScores[`team${team}`] += points;
document.getElementById(`admin-team${team}-score`).textContent = teamScores[`team${team}`];
// Synchroniser avec la fenêtre du jeu
if (gameWindowLoaded) {
gameWindow.postMessage({
type: 'updateScore',
team: team,
score: teamScores[`team${team}`]
}, '*');
}
}
function displayQuestion(index) {
if (index >= questions.length) {
alert('Fin du jeu !');
return;
}
const question = questions[index];
questionElement.textContent = question.question;
// Effacer les anciennes réponses
answersListElement.innerHTML = '';
// Créer les éléments de réponse
question.answers.forEach((answer, idx) => {
const answerCard = document.createElement('div');
answerCard.className = 'admin-answer-card';
// Créer le contenu de la réponse
const answerContent = document.createElement('div');
answerContent.className = 'answer-content';
answerContent.innerHTML = `
<span class="text">${answer.text}</span>
<span class="points">${answer.points} pts</span>
`;
// Créer les boutons d'équipe
const teamButtons = document.createElement('div');
teamButtons.className = 'team-buttons';
teamButtons.innerHTML = Object.entries(teamNames).map(([key, name], i) => `
<button class="team-button team${i + 1}" data-team="${i + 1}">${name}</button>
`).join('');
// Ajouter les gestionnaires d'événements pour les boutons d'équipe
teamButtons.querySelectorAll('.team-button').forEach(button => {
button.addEventListener('click', (e) => {
e.stopPropagation();
const team = parseInt(button.dataset.team);
updateScore(team, answer.points);
answerCard.classList.add('points-given');
teamButtons.querySelectorAll('.team-button').forEach(btn => btn.disabled = true);
});
});
answerCard.appendChild(answerContent);
answerCard.appendChild(teamButtons);
answerCard.addEventListener('click', () => handleAnswerClick(answerCard, answer, idx));
answersListElement.appendChild(answerCard);
});
// Synchroniser avec la fenêtre du jeu
if (gameWindowLoaded) {
gameWindow.postMessage({
type: 'newQuestion',
question: question.question,
answers: question.answers
}, '*');
}
}
function handleAnswerClick(answerCard, answer, index) {
if (!answerCard.classList.contains('revealed')) {
answerCard.classList.add('revealed');
// Envoyer le message à la fenêtre du jeu
if (gameWindowLoaded) {
gameWindow.postMessage({
type: 'revealAnswer',
index: index,
answer: answer
}, '*');
}
}
}
// Gestionnaire d'import de fichier
document.getElementById('questionFile').addEventListener('change', async (e) => {
const file = e.target.files[0];
if (!file) return;
const formData = new FormData();
formData.append('file', file);
try {
const response = await fetch('/import', {
method: 'POST',
body: formData
});
const data = await response.json();
if (response.ok) {
alert('Questions importées avec succès');
location.reload();
} else {
alert(`Erreur: ${data.error}`);
}
} catch (error) {
console.error('Error:', error);
alert('Une erreur est survenue lors de l\'import');
}
});
document.getElementById('admin-next-question').addEventListener('click', () => {
currentQuestionIndex++;
displayQuestion(currentQuestionIndex);
});
document.getElementById('admin-reset-question').addEventListener('click', () => {
const answerCards = document.querySelectorAll('.admin-answer-card');
answerCards.forEach(card => {
card.classList.remove('revealed', 'points-given');
card.querySelectorAll('.team-button').forEach(btn => btn.disabled = false);
});
// Réinitialiser la fenêtre du jeu
if (gameWindowLoaded) {
gameWindow.postMessage({
type: 'resetQuestion'
}, '*');
}
});