-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
279 lines (247 loc) · 7.02 KB
/
script.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
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
// global constants
var clueHoldTime = 700;
const cluePauseTime = 333;
const nextClueWaitTime = 1000;
const progressBar = document.querySelector(".progress-bar");
//global varible
var showAboutUsStatus = false;
var parternLength = 999;
var pattern = []
var progress = 0;
var gamePlaying = false;
var tonePlaying = false;
var volume = 0.6;
var guessCounter = 0;
var strikeCounter = 0;
var myVar = setInterval(Timer, 1000);
var ShowSignOutButton = false;
var script = document.createElement('script');
script.src = 'scripts/jquery-3.6.0.js';
document.getElementsByTagName('head')[0].appendChild(script);
clearInterval(myVar);
var time = 20;
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
var value = getCookie("user_id");
if(value == undefined){
document.getElementById("navibutton1").classList.remove("hidden");
document.getElementById("navibutton2").classList.add("hidden");
}else{
document.getElementById("navibutton1").classList.add("hidden");
document.getElementById("navibutton2").classList.remove("hidden");
}
function SignIn(){
window.location.href = "login.php";
}
function SignOut(){
document.cookie = "user_id=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
document.cookie = "user_name=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
document.getElementById("navibutton1").classList.remove("hidden");
document.getElementById("navibutton2").classList.add("hidden");
location.reload();
alert("Sign Out");
}
function SignOut_(){
document.cookie = "user_id=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
document.cookie = "user_name=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
window.location.href = "index.php";
alert("Sign Out");
}
function Timer(){
let timeRemain = Math.floor(time + clueHoldTime/1000)
document.getElementById("timer").innerHTML ="Time remain: " + timeRemain +"s";
time =timeRemain -1;
}
var check;
function showAboutus(){
// stopGame();
if(showAboutUsStatus){
document.getElementById("MyMuseGameContent").classList.add("hidden");
document.getElementById("MyMuseDashBoardContent").classList.add("hidden");
document.getElementById("MyMuseAboutusContent").classList.remove("hidden");
showAboutUsStatus = false;
}else{
document.getElementById("MyMuseAboutusContent").classList.add("hidden");
document.getElementById("MyMuseDashBoardContent").classList.add("hidden");
document.getElementById("MyMuseGameContent").classList.remove("hidden");
showAboutUsStatus = true;
}
}
function showAccount(){
var user =getCookie("user_id");
if(user == undefined){
alert("Please sign in first");
}else{
window.location.href = "account-info.php";
}
}
function showDashboard(){
// stopGame();
if(showAboutUsStatus){
document.getElementById("MyMuseGameContent").classList.add("hidden");
document.getElementById("MyMuseDashBoardContent").classList.add("hidden");
document.getElementById("MyMuseDashBoardContent").classList.remove("hidden");
showAboutUsStatus = false;
}else{
document.getElementById("MyMuseDashBoardContent").classList.add("hidden");
document.getElementById("MyMuseAboutusContent").classList.add("hidden");
document.getElementById("MyMuseGameContent").classList.remove("hidden");
showAboutUsStatus = true;
}
}
function timeOut(){
if(time<=0){
console.log("Time Out");
alert("Time Out")
stopGame();
location.reload();
clearInterval(check);
}
}
function startGame(){
//initialize game variables
strikeCounter = 0;
check = setInterval(timeOut,1);
progress = 0;
gamePlaying = true;
generatePattern();
// console.log("Partern generated is "+ pattern);
// swap the Start and Stop buttons
document.getElementById("startBtn").classList.add("hidden");
document.getElementById("stopBtn").classList.remove("hidden");
playClueSequence();
myVar = setInterval(Timer, 1000);
}
function insertScore()
{
$.ajax({
type: "POST",
url: 'send_score.php',
data: {score: progress},
success:function(data) {
alert("Score sent");
}
});
}
function stopGame(){
gamePlaying = false;
document.getElementById("timer").innerHTML ="Press Start to begin";
clearInterval(myVar);
document.getElementById("startBtn").classList.remove("hidden");
document.getElementById("stopBtn").classList.add("hidden");
$user = getCookie("user_id");
if ($user != undefined) {
insertScore();
}}
function lightButton(btn){
document.getElementById("button"+btn).classList.add("lit")
}
function clearButton(btn){
document.getElementById("button"+btn).classList.remove("lit")
}
function playSingleClue(btn){
if(gamePlaying){
lightButton(btn);
playTone(btn,clueHoldTime);
setTimeout(clearButton,clueHoldTime,btn);
}
}
function playClueSequence(){
guessCounter = 0;
let delay = nextClueWaitTime; //
for(let i=0;i<=progress;i++){ //
setTimeout(playSingleClue,delay,pattern[i])
// set a timeout to play that clue
delay += clueHoldTime
delay += cluePauseTime;
var clueHoldTime_new = clueHoldTime-clueHoldTime*0.1
clueHoldTime = Math.max(clueHoldTime_new,500)
time = 20
}
}
function loseGame(){
stopGame();
alert("Gameover!!Your score is " + progress);
// alert("Game Over. You lost.");
}
function winGame(){
stopGame();
alert("Game Over. You won!.");
}
function loseStrike(){
alert("Oops! That's wrong, you have "+ (3-strikeCounter)+ " strike left!");
}
function guess(btn){
console.log("user guessed: " + btn);
if(!gamePlaying){
clearInterval(myVar);
return;
}
if(pattern[guessCounter] == btn){
if(guessCounter == progress){
if(progress == pattern.length - 1){
winGame();
}else{
progress++;
playClueSequence();
}
}else{
guessCounter++;
}
}else{
if(strikeCounter==2){
loseGame();
}else{
strikeCounter +=1
loseStrike();
clueHoldTime = clueHoldTime+clueHoldTime*(0.2)
console.log(clueHoldTime);
playClueSequence();
}
}
}
function generatePattern(){
pattern =[]
for(let i = 0; i<parternLength; i++) {
var min = 1;
var max = 5;
pattern.push(Math.floor(Math.random() * (max - min + 1) + min))}
}
// Sound Synthesis Functions
const freqMap = {
1: 261.6,
2: 329.6,
3: 392,
4: 466.2,
5: 520
}
function playTone(btn,len){
document.getElementById("buttonsound"+btn).play();
tonePlaying = true
setTimeout(function(){
stopTone()
},len)
}
function startTone(btn){
if(!tonePlaying){
document.getElementById("buttonsound"+btn).play();
tonePlaying = true
}
}
function stopTone(){
g.gain.setTargetAtTime(0,context.currentTime + 0.05,0.025)
tonePlaying = false
}
// Page Initialization
// Init Sound Synthesizer
var AudioContext = window.AudioContext || window.webkitAudioContext
var context = new AudioContext()
var o = context.createOscillator()
var g = context.createGain()
g.connect(context.destination)
g.gain.setValueAtTime(0,context.currentTime)
o.connect(g)
o.start(0)