-
Notifications
You must be signed in to change notification settings - Fork 0
/
dc.js
259 lines (221 loc) · 6.52 KB
/
dc.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
$( document ).ready(function() {
// Handler for .ready() called.
$.ajax({
method: "GET",
url: "http://128.199.78.95:8899/internetBest",
})
.done(function( msg ) {
// alert( "Best: " + msg );
$("#best").text(msg.trim());
});
});
var allowedValues = [2,4,8];
var gameOver = false;
var timerRunning = false;
var userBest = 0;
var userInputs = [];
function updateTime2() {
var curTime = parseInt($("#time").text());
$("#time").text(curTime - 1);
checkGameOver();
updateTime();
}
function updateTime() {
if(!gameOver)
setTimeout(function(){
updateTime2()
}
, 1000);
}
function updateScore() {
var curScore = parseInt($("#curScore").text());
var lValue = parseInt($("#ltext").text());
var rValue = parseInt($("#rtext").text());
var incr = Math.abs(lValue - rValue);
if(incr > 0) {
$("#curScore").removeClass("fontkaboom");
$("#curScore").text(curScore + incr);
$("#curScore").replaceWith($("#curScore").clone(true));
$("#curScore").addClass("fontkaboom");
}
// feedback
if(incr == 8 || incr == 0) {
$("#feedback").removeClass("boomdissapear");
if(incr == 8)
$("#feedback").text("Sweeet!");
if(incr == 0)
$("#feedback").text("Meh!");
$("#feedback").replaceWith($("#feedback").clone(true));
$("#feedback").addClass("boomdissapear");
}
var newScore = parseInt($("#curScore").text());
if(newScore > userBest) {
userBest = newScore;
$("#userbest").removeClass("colordissapear");
$("#userbest").text(userBest);
$("#userbest").replaceWith($("#userbest").clone(true));
$("#userbest").addClass("colordissapear");
}
}
function checkGameOver() {
if(gameOver) return;
var lValue = parseInt($("#ltext").text());
var rValue = parseInt($("#rtext").text());
var timesup = false;
var exceeded8 = false;
if(parseInt($("#time").text()) <= 0) {
timesup = true;
}
if(Math.abs(lValue - rValue) > 8) {
exceeded8 = true;
}
gameOver = exceeded8 || timesup;
if(timesup) {
$("#timesup").removeClass("invisible");
}
if(exceeded8) {
$("#exceeded8").removeClass("invisible");
}
if(gameOver){
$("#restart").removeClass("invisible");
$.ajax({
method: "GET",
url: "http://128.199.78.95:8899/sendGame?data=".concat(userInputs.toString()),
})
.done(function( msg ) {
console.log("Game result saved hopefully");
});
userInputs = [];
}
}
function updateNextRandom() {
$("#cur").removeClass("kaboom");
$("#cur").text(allowedValues[Math.floor(Math.random()*3)]);
$("#cur").replaceWith($("#cur").clone(true));
$("#cur").addClass("kaboom");
}
function doAnimation(ele, val) {
var el = $(ele);
var plus = el.find("#plus");
plus.removeClass("anim");
plus.text("+"+val);
plus.replaceWith(plus.clone(true));
plus = el.find("#plus");
plus.addClass("anim");
}
function leftPressed() {
var curVal = parseInt($("#cur").text());
userInputs.push("L"+"-"+curVal);
if(!timerRunning) {
timerRunning = true;
updateTime();
}
var lboxValue = parseInt($("#ltext").text());
$("#ltext").text(curVal + lboxValue);
doAnimation("#lbox", curVal);
checkGameOver();
updateNextRandom();
if(!gameOver)
updateScore();
}
function rightPressed() {
var curVal = parseInt($("#cur").text());
userInputs.push("R"+"-"+curVal);
if(!timerRunning) {
timerRunning = true;
updateTime();
}
var rboxValue = parseInt($("#rtext").text());
$("#rtext").text(curVal + rboxValue);
doAnimation("#rbox", curVal);
checkGameOver();
updateNextRandom();
if(!gameOver)
updateScore();
}
function restart() {
gameOver = false;
timerRunning = false;
$("#timesup").addClass("invisible");
$("#exceeded8").addClass("invisible");
$("#restart").addClass("invisible");
$("#curScore").text(0);
$("#time").text(60);
$("#ltext").text(0);
$("#rtext").text(0);
}
$("body").on("keydown", function(event) {
if(!gameOver) {
console.log(event.which);
switch(event.which) {
case 37:
leftPressed();
break;
case 65:
leftPressed();
break;
case 39:
rightPressed();
break;
case 68:
rightPressed();
break;
}
}
if(event.which == 32) {
restart();
}
});
$("#restart").on('click', function() {
restart();
})
function swipedetect(el, callback){
var touchsurface = el,
swipedir,
startX,
startY,
distX,
distY,
threshold = 30, //required min distance traveled to be considered swipe
restraint = 100, // maximum distance allowed at the same time in perpendicular direction
allowedTime = 300, // maximum time allowed to travel that distance
elapsedTime,
startTime,
handleswipe = callback || function(swipedir){}
touchsurface.addEventListener('touchstart', function(e){
var touchobj = e.changedTouches[0]
swipedir = 'none'
dist = 0
startX = touchobj.pageX
startY = touchobj.pageY
startTime = new Date().getTime() // record time when finger first makes contact with surface
e.preventDefault()
}, false)
touchsurface.addEventListener('touchmove', function(e){
e.preventDefault() // prevent scrolling when inside DIV
}, false)
touchsurface.addEventListener('touchend', function(e){
var touchobj = e.changedTouches[0]
distX = touchobj.pageX - startX // get horizontal dist traveled by finger while in contact with surface
distY = touchobj.pageY - startY // get vertical dist traveled by finger while in contact with surface
elapsedTime = new Date().getTime() - startTime // get time elapsed
if (elapsedTime <= allowedTime){ // first condition for awipe met
if (Math.abs(distX) >= threshold && Math.abs(distY) <= restraint){ // 2nd condition for horizontal swipe met
swipedir = (distX < 0)? 'left' : 'right' // if dist traveled is negative, it indicates left swipe
}
else if (Math.abs(distY) >= threshold && Math.abs(distX) <= restraint){ // 2nd condition for vertical swipe met
swipedir = (distY < 0)? 'up' : 'down' // if dist traveled is negative, it indicates up swipe
}
}
handleswipe(swipedir)
e.preventDefault()
}, false)
}
var el = document.getElementById('gamearea');
swipedetect(el, function(swipedir){
//swipedir contains either "none", "left", "right", "top", or "down"
if (swipedir =='left')
leftPressed();
if(swipedir == 'right')
rightPressed();
});