-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
187 lines (168 loc) · 5.4 KB
/
script.js
File metadata and controls
187 lines (168 loc) · 5.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
// Pour johnny
// let params = new URLSearchParams(document.location.search);
// let idPartie = params.get('idPartie');
// let idModule = params.get('idModule');
let rn_div = document.querySelector("#random_number");
let stepTitle = document.querySelector("#step_title");
let currentStep = 1;
let clickedButton;
// Générer le chiffre aléatoire initial
regenerateRandom();
updateDisplay();
updateStepImages();
document.querySelectorAll('.button').forEach(item => {
item.addEventListener('click', () => conditions_buttons(item.id));
});
// function resolve() {
// console.log(idPartie);
// console.log(idModule);
// $.ajax({
// url: `../functions/functionsDatabase.php?action=resolve&idPartie=${idPartie}&idModule=${idModule}`,
// success: function(data) {
// $('#result').html(data);
// }
// });
// }
// function incrementError() {
// $.ajax({
// url: `../functions/functionsDatabase.php?action=incrementError&idPartie=${idPartie}&idModule=${idModule}`,
// success: function(data) {
// $('#result').html(data);
// }
// });
// }
function ledOnClickSound() {
const ledSound = new Audio('audios/ledOn.wav');
ledSound.play();
}
function mistakeSound() {
const mistakeAudio = new Audio('audios/error.wav');
mistakeAudio.play();
}
function conditions_buttons(idButtonCurrent) {
const button = document.querySelector(`#${idButtonCurrent}`);
// Vérification de l'action en fonction de l'étape et du chiffre aléatoire
switch (currentStep) {
case 1:
if (checkStep1(button)) {
clickedButton = button;
nextStep();
ledOnClickSound();
} else {
resetSteps();
}
break;
case 2:
if (checkStep2(button)) {
clickedButton = button;
nextStep();
ledOnClickSound();
} else {
resetSteps();
}
break;
case 3:
if (checkStep3(button)) {
clickedButton = button;
document.querySelector("#led_fin").setAttribute("src", "assets/led_fin_vert.png");
ledOnClickSound();
//Pour johnny
//resolve();
resetSteps();
} else {
resetSteps();
}
break;
default:
resetSteps();
break;
}
}
function nextStep() {
currentStep++;
regenerateRandom();
updateDisplay();
updateStepImages();
}
function resetSteps() {
mistakeSound();
currentStep = 1;
regenerateRandom();
updateDisplay();
updateStepImages();
//Pour Johnny
//incrementError();
}
function regenerateRandom() {
random_number = Math.floor(Math.random() * 4) + 1;
rn_div.innerHTML = `<h1>${random_number}</h1>`;
}
function updateDisplay() {
stepTitle.innerHTML = `Etape ${currentStep}`;
}
function updateStepImages() {
const stepImages = document.querySelectorAll('.steps');
for (let i = stepImages.length - 1; i >= 0; i--) {
const stepNumber = stepImages.length - i;
if (stepNumber < currentStep) {
stepImages[i].setAttribute("src", "assets/boutonEtapeVert.png");
} else {
stepImages[i].setAttribute("src", "assets/boutonEtapeNeutre.png");
}
}
}
// Étape 1
// Si l’écran affiche 1, appuyez sur le bouton en deuxième position.
// Si l’écran affiche 2, appuyez sur le bouton en deuxième position.
// Si l’écran affiche 3, appuyez sur le bouton en troisième position.
// Si l’écran affiche 4, appuyez sur le bouton en quatrième position.
function checkStep1(button) {
switch (random_number) {
case 1:
case 2:
return button.id === 'button2';
case 3:
return button.id === 'button3';
case 4:
return button.id === 'button4';
default:
return false;
}
}
// Étape 2
// Si l’écran affiche 1, appuyez sur le bouton portant le chiffre « 4 ».
// Si l’écran affiche 2, appuyez sur le bouton à la même position qu’à l’étape 1.
// Si l’écran affiche 3, appuyez sur le bouton en première position.
// Si l’écran affiche 4, appuyez sur le bouton à la même position qu’à l’étape 1.
function checkStep2(button) {
switch (random_number) {
case 1:
return button.id === 'button4';
case 2:
case 4:
return button === clickedButton;
case 3:
return button.id === 'button1';
default:
return false;
}
}
// Étape 3
// Si l’écran affiche 1, appuyez sur le bouton ayant le même chiffre qu’à l’étape 2.
// Si l’écran affiche 2, appuyez sur le bouton ayant le même chiffre qu’à l’étape 1.
// Si l’écran affiche 3, appuyez sur le bouton en troisième position.
// Si l’écran affiche 4, appuyez sur le bouton portant le chiffre « 4 ».
function checkStep3(button) {
switch (random_number) {
case 1:
return button.id === 'button' + random_number;
case 2:
return button.id === 'button' + random_number;
case 3:
return button.id === 'button3';
case 4:
return button.id === 'button4';
default:
return false;
}
}