-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
397 lines (361 loc) · 13.4 KB
/
index.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
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
var DEBUG_MODE = false;
var cornerTextPipes = ["╚", "╔","╗","╝"];
var cornerTextLines = ["┗", "┏","┓","┛"];
var cornerText = cornerTextLines;
var boardHeight = 36, boardWidth = 80;
var gameBoard;
var gameLoop = false;
var highScore = 0;
var serverScore = false;
var skipNameDialog = false;
function pageLoad() {
highScore = getScoreCookie();
logPageHit();
setupScoreStream();
document.getElementById("highScoreText").textContent = highScore;
document.getElementById("username").value = getNameCookie();
resetBoard();
}
function setupScoreStream() {
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("getHighScore.php");
source.onmessage = function(event) {
document.getElementById("serverHighScoreText").textContent = event.data;
serverScore = event.data | 0;
console.log(event.data);
};
} else {
console.log("Event streams aren't available for your web browser. Consider updating to a modern web browser.")
getServerScore();
setInterval(getServerScore,30000);
}
}
function getServerScore() {
var http = new XMLHttpRequest();
var url = "getHighScore.php";
var params = "";
http.open("GET", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
if (isNaN(http.responseText.substring(17))){
console.log(http.responseText); //responseText should be a string that is a number. If it isnt, responseText is likely an error message
serverScore === false;
} else {
serverScore = parseInt(http.responseText.substring(17));
document.getElementById("serverHighScoreText").textContent = serverScore;
}
}
}
http.send(params);
}
function resetBoard(){
if(!gameLoop){
gameBoard = genRandomBoard(boardHeight, boardWidth);
setFontSize();
document.getElementById("gameText").innerHTML = boardToHTML(gameBoard);
}
}
function resetScore(){
if (confirm("Are you sure you want to reset your high score?") == true) {
highScore = 0;
document.getElementById("highScoreText").textContent = highScore;
setScoreCookie();
}
}
function setScoreCookie() {
document.cookie = "highScore=" + highScore + "; expires=Tue, 19 Jan 2038 03:14:07 UTC";
};
function setNameCookie(username) {
document.cookie = "username=" + username + "; expires=Tue, 19 Jan 2038 03:14:07 UTC";
};
function boardToHTML(arr){
var lr = boardHeight, lc = boardWidth;
var output = "";
for(var r = 0; r < lr; r++){
for(var c = 0; c < lc; c++){
output += "<span id='" + r + "," + c + "' onclick='charClicked(" + r + "," + c + ")'>";
output += cornerText[arr[r][c]];
output += "</span>";
}
output += "<br>";
}
return output;
}
window.addEventListener("resize", function(event) {
setFontSize();
})
document.getElementById("username").onkeypress = function(e) {
var chr = String.fromCharCode(e.which);
if ("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890".indexOf(chr) < 0){
return false;
} else {
setNameCookie(document.getElementById("username").value + chr);
}
};
function setFontSize(){
var viewWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var fontSize = Math.floor(viewWidth/boardWidth + .5);
document.getElementById("gameText").style.fontSize = Math.min(fontSize, 20) +"px";
document.getElementById("globalScoreAlign").style.width = Math.floor(viewWidth/3.2)+"px";
document.getElementById("currentScoreAlign").style.width = Math.floor(viewWidth/3.2)+"px";
document.getElementById("highScoreAlign").style.width = Math.floor(viewWidth/3.2)+"px";
}
function genRandomBoard(lr, lc){
var out = [];
for(var r = 0; r < lr; r++){
var tempRow = [];
for(var c = 0; c < lc; c++){
tempRow.push( (Math.random()*4) | 0);
}
out.push(tempRow);
}
return out;
}
function charClicked(r, c){
var newGame = new GameWorker();
newGame.startReaction(r, c);
}
function getScoreCookie() {
var name = "highScore=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return parseInt(c.substring(name.length, c.length));
}
}
return 0;
}
function getNameCookie() {
var name = "username=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function logPageHit() {
var http = new XMLHttpRequest();
var url = "logPageHit.php";
var params = "";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
console.log(http.responseText);
}
}
http.send(params);
}
function implodeBoard(inputBoard) {
var boardStr = "";
for(var r=0; r<inputBoard.length; r++){
var rowStr = "";
for(var c = 0; c < inputBoard[0].length; c++){
rowStr += inputBoard[r][c] + ",";
}
boardStr += rowStr.substring(0, rowStr.length-1) + "."; //Remove extrenuous comma, add period seperator
}
boardStr = boardStr.substring(0, boardStr.length-1); //Remove extrenuous period
return boardStr;
}
var GameWorker = function (){
var score = 0;
var neighbors = [];
var oldNeighbors = [];
var boardStr;
var inputRow;
var inputCol;
var turnCell = function(r, c){
gameBoard[r][c] = (gameBoard[r][c]+1) % 4;
document.getElementById(r + "," + c).innerHTML = cornerText[gameBoard[r][c]];
document.getElementById(r + "," + c).style = "color:blue;";
score++;
};
var fadeCell = function(r, c){
document.getElementById(r + "," + c).style = "color:red;";
};
var chainReact = function(){
if(neighbors.length>0){
setTimeout(chainReact, document.getElementById("delaySlider").value | 0);
for(var n = 0, l = oldNeighbors.length; n<l; n++){
fadeCell(oldNeighbors[n][0],oldNeighbors[n][1]);
}
for(var n = 0, l = neighbors.length; n<l; n++){
turnCell(neighbors[n][0],neighbors[n][1]);
}
oldNeighbors = JSON.parse(JSON.stringify(neighbors));
neighbors = getNeighbors(neighbors, gameBoard);
document.getElementById("scoreText").textContent = score;
if(score>highScore){
highScore=score;
document.getElementById("highScoreText").textContent = highScore;
setScoreCookie();
}
} else {
gameLoop = false;
if(!(serverScore === false)){
if(score==highScore){
getServerScore();
}
if(!(serverScore === false) && score>=1000){
if(document.getElementById("username").value == ""){
if(!skipNameDialog && confirm("That was a pretty good score! However, since you haven't set a display name, your score can't appear on the leaderboard. Would you like to set your display name now?")){
var str = prompt("Enter your display name\n(Alphanumeric characters only)");
while(str!="" && /[^a-zA-Z0-9]/.test(str)) {
str = prompt("Enter your display name\n(Alphanumeric characters ONLY)");
}
if (str == null || str == ""){
alert("Your score wasn't logged on the server.\nFeel free to set your display name at any time.");
skipNameDialog = true;
} else {
document.getElementById("username").value = str;
setScoreCookie(str);
sendGame(boardStr, inputRow, inputCol);
}
} else if(!skipNameDialog) {
alert("Your score wasn't logged on the server.\nFeel free to set your display name at any time.");
skipNameDialog = true;
}
} else {
sendGame(boardStr, inputRow, inputCol);
}
} else {
if(!(serverScore === false)){
console.log("Score was not logged on the server. (Reason: score was too low)");
} else {
console.log("Score was not logged on the server. (Reason: serverScore === false. Perhaps there was an error on the server within the last 30 seconds?)");
}
}
}
ga('send', 'event', 'Game', 'play', undefined, score); //Log game in google analytics
}
};
var getNeighbors = function(parents, arr){
var out = [];
var h = arr.length, w = arr[0].length;
for(var n = 0, l = parents.length; n<l; n++){
var r = parents[n][0], c = parents[n][1];
if(r-1>=0){
if(arr[r][c] % 3 == 0 && arr[r-1][c] % 3 != 0){
out.push([r-1,c]);
}
}
if(c-1>=0){
if(arr[r][c] >= 2 && arr[r][c-1] <= 1){
out.push([r,c-1]);
}
}
if(r+1<h){
if(arr[r][c] % 3 != 0 && arr[r+1][c] % 3 == 0){
out.push([r+1,c]);
}
}
if(c+1<w){
if(arr[r][c] <= 1 && arr[r][c+1] >= 2){
out.push([r,c+1]);
}
}
}
return removeDuplicates(out);
};
/**
* Removes duplicate values from an array
* have the same resulting string
*/
var removeDuplicates = function(arr) {
var len=arr.length;
var out=[];
var strings = [];
for(var i = 0; i<len; i++){
strings.push(JSON.stringify(arr[i]));
}
for (var i=0;i<len;i++) {
var flag = true;
for (var j=i+1; j<len && flag; j++) {
if (strings[i] == strings[j]){
flag = false;
}
}
if (flag){
out.push(arr[i]);
}
}
return out;
};
var sendGame = function(board, row, col){
//Calculate score on the server, and log that score in the database
var http = new XMLHttpRequest();
var url = "playGame.php";
var params = "board=" + board + "&row=" + row + "&col=" + col + "&name=" + document.getElementById("username").value;
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
console.log(http.responseText);
}
}
http.send(params);
}
var self = this;
self.startReaction = function(r, c){
if(!gameLoop){
document.getElementById("gameText").innerHTML = boardToHTML(gameBoard);
inputRow=r;
inputCol=c;
boardStr=implodeBoard(gameBoard)
neighbors = [[r,c]];
oldNeighbors = [[r,c]];
gameLoop = true;
chainReact();
}
};
}
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
getLeaderboard();
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
function getLeaderboard(){
var tableStr = "Loading leaderboard from the server...";
var http = new XMLHttpRequest();
var url = "getLeaderboard.php";
var params = "";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
var leaderboardDiv = document.getElementById('leaderboardDiv');
leaderboardDiv.innerHTML = http.responseText;
}
}
http.send(params);
}