-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscripts.js
212 lines (179 loc) · 4.96 KB
/
scripts.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
var interval;
var seconds = 0;
var currentLetter = '';
var letters = ['q','w','e','a','s','d','z','x','c'];
var totalPoints = 15;
var freeData = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var timedData = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var plot;
var currentData = timedData;
var currentPoint = 1;
var best = 0;
var average = 0;
var attempts = 0;
var breaks = 0;
window.onload = function() {
var data = [ { label: "Successful Pulls Per Attempt", data: indexData(timedData) }];
plot = $.plot($("#flot"), data, {
points: {show: true},
lines: {show: true},
yaxis: {
min: 0
},
xaxis: {
show: false
}
});
update();
};
function addDataPoint(data, point) {
if (data.length > 0)
data = data.slice(1);
data.push(point);
return data;
}
function updateLastDataPoint(data, point) {
data[data.length-1] = point;
return data;
}
function indexData(data) {
var res = [];
for (var i = data.length-totalPoints; i < data.length; ++i) {
res.push([i-data.length, data[i]])
}
return res;
}
function update() {
plot.setData([indexData(currentData)]);
plot.setupGrid();
plot.draw();
setTimeout(update, 100);
}
$('#start').click(function(){
startGame();
});
$('#freePlay').click(function(){
if($('#freePlay')[0].checked){
clearInterval(interval);
seconds = 0;
startGame();
$("#timeInput").prop('disabled', true);
$("#gameTimer").css('visibility', 'hidden');
}
else {
$("#timeInput").prop('disabled', false);
$("#gameTimer").css('visibility', 'inherit');
$('#gameTimer').html("");
removeSecond();
}
});
function countdown() {
clearInterval(interval);
removeSecond();
interval = setInterval(function() {
removeSecond();
}, 1000);
}
function removeSecond() {
if(seconds == 0) {
$('#gameTimer').html("Finished with " + currentPoint + " pulls!");
$("#gameText").text("Press Space to Begin");
if(currentPoint > 0) {
recordGame(false);
}
currentLetter = "";
updateLastDataPoint(currentData, currentPoint);
clearInterval(interval);
return;
}
var second_text = seconds > 1 ? 'seconds' : 'second';
$('#gameTimer').html("Time Remaining: " + seconds + " " + second_text);
seconds--;
}
$(document).on("keypress", function(e){registerKeypress(e)});
function startGame() {
if(!$('#freePlay')[0].checked) {
seconds = $("#timeInput").val();
if(currentPoint != 0){
currentData= addDataPoint(currentData, 0);
}
currentPoint = 0;
updateLastDataPoint(currentData, currentPoint);
countdown();
}
else {
}
challengeLetter();
}
function registerKeypress(key){
if(key.which==32 && !$('#freePlay')[0].checked)
{
if(seconds > 0) {
currentPoint = 0;
}
startGame();
}
else if(String.fromCharCode(key.which).toLowerCase() == currentLetter){
$("#gameText").css("color", "green");
currentLetter = "";
setTimeout(function(){
$("#gameText").css("color", "black");
if(seconds > 0 || $('#freePlay')[0].checked) {
challengeLetter();
}
}, 250);
currentPoint++;
if(!$('#freePlay')[0].checked) {
updateLastDataPoint(currentData, currentPoint);
}
}
else if(seconds > 0 || $('#freePlay')[0].checked) {
if(!$('#freePlay')[0].checked) {
var tempPoint = currentPoint;
seconds = 0;
currentLetter = "";
updateLastDataPoint(currentData, currentPoint);
clearInterval(interval);
$("#gameText").text("Press Space to Begin");
if(tempPoint > 0) {
recordGame(true);
}
$('#gameTimer').html("<span style='color: red'>Line Broke after " + tempPoint + " pulls!</span>");
}
else {
$('#gameTimer').html("<span style='color: red'>Line Broke after " + currentPoint + " pulls!</span>");
$('#gameTimer').css('visibility', 'inherit');
currentPoint = 0;
$("#gameTimer").removeClass('newLetter');
setTimeout(function(){
$("#gameTimer").addClass('newLetter');
},0);
setTimeout(function(){$('#gameTimer').css('visibility', 'hidden'); $("#gameTimer").removeClass('newLetter');}, 1000);
challengeLetter();
}
}
}
function challengeLetter() {
currentLetter = letters[Math.floor((Math.random() * 9))];
$("#gameText").text("Press " + currentLetter.toUpperCase());
}
function recordGame(broke) {
if(broke) {
breaks++;
}
if(currentPoint > best) {
best = currentPoint;
}
attempts++;
average = ((average * (attempts-1)) + currentPoint) / attempts;
var lastFive = currentData.filter(function(val){return val != 0;})
lastFive = lastFive.slice(Math.max(lastFive.length - 5, 0));
var lastFiveSum = 0;
for( var i = 0; i < lastFive.length; i++ ){
lastFiveSum += lastFive[i];
}
$("#statsBest").html(best);
$("#statsAverage").html(average.toFixed(1) + "<br>(Last 5: " + (lastFiveSum/lastFive.length).toFixed(1) + ")");
$("#statsAttempts").html(attempts);
$("#statsBreaks").html(breaks + "<br>(" + Math.round(breaks / attempts * 100) + "%)")
}