Skip to content

Commit 30714f0

Browse files
Centralized options
1 parent 254ecb7 commit 30714f0

File tree

2 files changed

+103
-58
lines changed

2 files changed

+103
-58
lines changed

html/game.html

Lines changed: 102 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ <h1 class="win2" style="display: none"></h1> <!--// Cre
7979
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.8/sweetalert2.min.js"></script>
8080
<!-- // End JS imports -->
8181
<script>
82-
var demoMode = false;
83-
if (demoMode) {
84-
$(".words").css({ "border": "10px solid red"})
85-
}
8682
var fruits = [
8783
{
8884
name: "Lemon",
@@ -105,11 +101,32 @@ <h1 class="win2" style="display: none"></h1> <!--// Cre
105101
key: "Digit3"
106102
}
107103
]
104+
var options = {
105+
gamelength: 10, // Seconds
106+
loadingscreenlength: 2, // Seconds
107+
time_remaining_bar: true,
108+
loadingscreen: true,
109+
camo: true,
110+
randomMove: true,
111+
demo: false,
112+
leaderboard: false,
113+
sounds: {
114+
success: true,
115+
fail: true
116+
},
117+
lang: {
118+
points: "You got $score points!",
119+
scoreboard_entry: "$name - $score"
120+
}
121+
};
122+
if (options.demo) {
123+
$(".words").css({ "border": "10px solid red"})
124+
}
108125
var soundUrl = "https://freesound.org/people/LittleRainySeasons/sounds/335908/download/335908__littlerainyseasons__correct.mp3";
109126
var badSound = "https://freesound.org/people/RICHERlandTV/sounds/216090/download/216090__richerlandtv__bad-beep-incorrect.mp3";
110127
var out = []; // Initialise array of names of fruits
111128
var mappings = {};
112-
var options = {
129+
var nanooptions = {
113130
id: 'my-id',
114131
target: document.getElementById('tehbody')
115132
};
@@ -133,21 +150,39 @@ <h1 class="win2" style="display: none"></h1> <!--// Cre
133150
} // TODO: Why is this method so HACK y? It doesn't need to be
134151
return out[name] // Return the choice from the array
135152
}
136-
setTimeout(function() { // DO NOT LOOK AT THIS CODE
137-
setTimeout(function(){ // THIS IS NOT A FAKE LOADING SCREEN AT ALL
138-
initPic(getImageFromName(choose(out)), choose(out)) // Set a random background and text
139-
}, 200)
140-
var nanobar = new Nanobar( options ); // Create the time bar
141-
var b = 0;
142-
var meee = setInterval(function () { // And increment it
143-
b++
144-
nanobar.go( Math.round( (b / 6000 ) * 100 ) )
145-
if(b > 6000) {
146-
clearInterval(meee)
153+
if (options.loadingscreen) {
154+
setTimeout(function() { // DO NOT LOOK AT THIS CODE
155+
setTimeout(function(){ // THIS IS NOT A FAKE LOADING SCREEN AT ALL
156+
initPic(getImageFromName(choose(out)), choose(out)) // Set a random background and text
157+
}, 100)
158+
if (options.time_remaining_bar){
159+
var nanobar = new Nanobar( nanooptions ); // Create the time bar
160+
var b = 0;
161+
var meee = setInterval(function () { // And increment it
162+
b++
163+
nanobar.go( Math.round( (b / (options.gamelength * 10) ) * 100 ) )
164+
if(b > options.gamelength * 10) { // If the time has elapsed
165+
clearInterval(meee) // DELETE IT
166+
}
167+
}, 100) // And do that every 100 ms
147168
}
148-
}, 10)
169+
$(".loading").css({"display": "none"}) // Remove the *cough cough* loading screen
170+
}, options.loadingscreenlength * 1000) // <- This does not set the time the loading screen displays for
171+
} else {
149172
$(".loading").css({"display": "none"})
150-
}, 2000)
173+
initPic(getImageFromName(choose(out)), choose(out)) // Set a random background and text
174+
if (options.time_remaining_bar){
175+
var nanobar = new Nanobar( nanooptions ); // Create the time bar
176+
var b = 0;
177+
var meee = setInterval(function () { // And increment it
178+
b++
179+
nanobar.go( Math.round( (b / 600 ) * 100 ) )
180+
if(b > 600) { // If the time has elapsed
181+
clearInterval(meee) // DELETE IT
182+
}
183+
}, 100) // And do that every 100 ms
184+
}
185+
}
151186

152187
function choose(choices) { // Makes a random choice from Array, eg. [1,2,5] => 1 or 2 or 5
153188
var index = Math.floor(Math.random() * choices.length);
@@ -168,12 +203,18 @@ <h1 class="win2" style="display: none"></h1> <!--// Cre
168203
function initPic(pic, word) { // Function that sets the background and word
169204
$("html").css("background-image", pic); // Set page background to pic
170205
$(".words").text(word); // Set "words" element to the name of what you gotta click
171-
setTimeout(function () { // After 70ms, so that the background loads first
172-
getColor(pic.replace("url('", "").replace("')", ""), fruits[2].pics.indexOf(pic) !== -1, function (correctColor) { // Get the average image color
173-
$(".words").css("color", correctColor); // Then set the word's color to it
174-
})
175-
}, 10)
176-
randomMove($(".words")) // CHANGED: Now moves text each time
206+
if (options.camo) {
207+
setTimeout(function () { // After 70ms, so that the background loads first
208+
getColor(pic.replace("url('", "").replace("')", ""), fruits[2].pics.indexOf(pic) !== -1, function (correctColor) { // Get the average image color
209+
$(".words").css("color", correctColor); // Then set the word's color to it
210+
})
211+
}, 10)
212+
} else {
213+
$(".words").css("color", "black");
214+
}
215+
if(options.randomMove){
216+
randomMove($(".words")) // CHANGED: Now moves text each time
217+
}
177218
current.pic = pic;
178219
current.word = word;
179220
}
@@ -182,38 +223,40 @@ <h1 class="win2" style="display: none"></h1> <!--// Cre
182223
$("html").css("background-image", ""); // Remove background image
183224
$("html").css("background-color", "black"); // Set the background black
184225
$(".words").remove() // Remove the current word (ie banana)
185-
$(".win2").text("You scored " + (correct - incorrect) + " points.") // Set winning text
226+
$(".win2").text(options.lang.points.replace("$score", (correct - incorrect).toString())) // Set winning text
186227
$(".win2").fadeIn() // Fade in the winning text
187-
$(".sboard").fadeIn() // Fade in the scoreboard
188-
$.get("/get-score/", function (d) { // Download the updated scoreboard
189-
var arr = JSON.parse(d); // Parse what we've been given
190-
for (var i = 0; i < arr.length ; i++) { // Go through all the top 20 scores
191-
$(".leaderboard").append("<li class='list-group-item'> " + arr[i].name + " - " + arr[i].score + " </li>") // And add them to the leaderboard
192-
}
193-
})
194-
swal({ // Create a dialog box:
195-
title: 'What\'s your name?',
196-
input: 'text', // With an input box
197-
showCancelButton: true,
198-
confirmButtonText: 'Submit',
199-
showLoaderOnConfirm: true,
200-
allowOutsideClick: false
201-
}).then(function (name) { // Then when user clicks submit
202-
$.post("/add-score/", {name: name, score: (correct - incorrect)}, function (data) { // Then when that data is successfully sent
203-
console.info(data);
204-
setTimeout(function () {
205-
$.get("/get-score/", function (d) { // Download the updated scoreboard
206-
var arr = JSON.parse(d); // Parse what we've been given
207-
$(".leaderboard").html("") // Remove old scores
208-
for (var i = 0; i < arr.length ; i++) { // Go through all the top 20 scores
209-
$(".leaderboard").append("<li class='list-group-item'> " + arr[i].name + " - " + arr[i].score + " </li>") // And add them to the leaderboard
210-
}
211-
})
212-
}, 500)
228+
if(options.leaderboard){
229+
$(".sboard").fadeIn() // Fade in the scoreboard
230+
$.get("/get-score/", function (d) { // Download the updated scoreboard
231+
var arr = JSON.parse(d); // Parse what we've been given
232+
for (var i = 0; i < arr.length ; i++) { // Go through all the top 20 scores
233+
$(".leaderboard").append("<li class='list-group-item'> " + options.lang.scoreboard_entry.replace("$name", arr[i].name).replace("$score", arr[i].score) + " </li>") // And add them to the leaderboard
234+
}
213235
})
214-
});
236+
swal({ // Create a dialog box:
237+
title: 'What\'s your name?',
238+
input: 'text', // With an input box
239+
showCancelButton: true,
240+
confirmButtonText: 'Submit',
241+
showLoaderOnConfirm: true,
242+
allowOutsideClick: false
243+
}).then(function (name) { // Then when user clicks submit
244+
$.post("/add-score/", {name: name, score: (correct - incorrect)}, function (data) { // Then when that data is successfully sent
245+
console.info(data);
246+
setTimeout(function () {
247+
$.get("/get-score/", function (d) { // Download the updated scoreboard
248+
var arr = JSON.parse(d); // Parse what we've been given
249+
$(".leaderboard").html("") // Remove old scores
250+
for (var i = 0; i < arr.length ; i++) { // Go through all the top 20 scores
251+
$(".leaderboard").append("<li class='list-group-item'> " + arr[i].name + " - " + arr[i].score + " </li>") // And add them to the leaderboard
252+
}
253+
})
254+
}, 500)
255+
})
256+
});
257+
};
215258
document.onkeyup = function () {}; // Finally, set the "onkeyup" function to empty so that users can't continue playing
216-
}, 62000)
259+
}, options.loadingscreen ? (options.gamelength * 1000) + (options.loadingscreenlength * 1000) : options.gamelength * 1000)
217260

218261
var correct = 0; // Start at 0 wins
219262
var incorrect = 0
@@ -226,19 +269,21 @@ <h1 class="win2" style="display: none"></h1> <!--// Cre
226269
var key2 = choose(out) // ^
227270
correct++; // Add 1 to the win counter
228271
$("#scorecount").text(correct - incorrect)
229-
playSound(soundUrl);
272+
if(options.sounds.success){
273+
playSound(soundUrl);
274+
}
230275
if ((correct - incorrect) >= 40 && (correct - incorrect) <= 42) {
231276
initPic("url('legendary.png')", key)
232277
} else {
233278
initPic(getImageFromName(key2), key) // And set the background and text to those random fruits
234279
}
235280
} else {
236-
playSound(badSound)
281+
if(options.sounds.fail){
282+
playSound(badSound);
283+
}
237284
incorrect = incorrect + 1; // If the fruit is wrong, add one to the incorrect score
238285
$("#scorecount").text(correct - incorrect) // And update visual counter
239286
}
240-
241-
242287
}
243288
</script>
244289
</html>

storage/leaderboard.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"name":"Jim *******","score":41},{"name":"jay","score":26},{"name":"Demo","score":20}]
1+
[{"name":"Jim *******","score":41},{"name":"jay","score":26},{"name":"Demo","score":20},{"name":"Jeff","score":39}]

0 commit comments

Comments
 (0)