-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
284 lines (243 loc) · 8 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
280
281
282
283
284
//To Do:
//store score in local memory
//include download/play/buy button
function reload() {
window.location.reload();
}
//animation functions
function shake(elementID) {
var element = document.getElementById(elementID);
var interval = 100;
var distance = 10;
var times = 4;
this.$(element).css('position', 'relative');
for (var i = 0; i < (times + 1); i++) {
this.$(element).animate({
left: ((i % 2 == 0 ? distance : distance * -1))
}, interval);
}
this.$(element).animate({
left: 0
}, interval);
}
function bounce(elementID) {
var element = document.getElementById(elementID);
var interval = 100;
var distance = 10;
var times = 4;
this.$(element).css('position', 'relative');
for (var i = 0; i < (times + 1); i++) {
this.$(element).animate({
top: ((i % 2 == 0 ? distance : distance * -1))
}, interval);
}
this.$(element).animate({
top: 0
}, interval);
}
//Song Constructor
function Song(options) {
this.name = options.name; //string
this.preview_url = options.url || options.preview_url; //string url
};
//Player Constructor
function Player(name) {
this.name = name || "";
this.guess = "";
this.score = 0;
this.$score_value = $("#score-value");
};
//Game Constructor
function Game(playerObjectArray, songObjectArray) {
this.maxRoundLength = 30;
this.roundCount = 0;
this.roundStart = this.now();
this.roundTime = 0;
this.players = playerObjectArray || [];
this.songs = songObjectArray || [];
this.winStatements = ["You got it!",
"Watch out, Adele!",
"I thought it was The Beatles...",
"Keep it going!",
"Highest score EVER?",
"Lightning Round!",
"That's my favorite song!",
"Rockstar!",
"Notice me Senpai.",
"Call 911! 'Cuz you are on FIRE",
"You're my hero.",
"Making Beyonce proud!",
"Grand Prize!",
"That one took me a second.",
"OK. But can you get the next one?",
"I bet you make babies smile.",
"Stylin'!",
"DANCE BREAK",
"Me 'What's your number?' You: 'Number 1.'",
"Annie are you OK?!" //20
].shuffle();
};
//GAME METHODS
Game.prototype.cacheDom = function() {
this.$document = $(document);
this.$display = this.$document.find("#display");
this.$audio = this.$document.find("audio");
this.$song_guess = this.$document.find("#song-guess");
this.$start_button = this.$document.find("#start");
this.$restart_button = this.$document.find("#restart");
this.$visualizer = this.$document.find("#visualizer");
}
Game.prototype.currentPlayer = function() {
return this.players[this.roundCount % this.players.length];
}
//Note: game.roundCount should be iterated to get the next item
Game.prototype.currentWinStatement = function() {
return this.winStatements[this.roundCount % this.winStatements.length];
}
//Note: game.roundCount should be iterated to get the next item
Game.prototype.currentSong = function() {
return this.songs[this.roundCount % this.songs.length];
}
//Note: game.roundCount should be iterated to get the next item
Game.prototype.currentSongName = function() {
return this.currentSong()["name"];
}
Game.prototype.cleanString = function(string) {
var cleanedString = string
.replace(/\s*\(.*?\)\s*/g, '')
//removes secondary song titles in parenthesis
.replace(/[^\w\s]|_/g, "")
//removes punctuation
.replace(/feat.*this.$/g, "");
//removes anything after "feat. "
cleanedString.trim();
//removes leading and trailing whitespace
return cleanedString;
}
Game.prototype.checkGuess = function() {
if (this.players.length > 0) {
var cleanedSong = this.cleanString(this.currentSongName());
var cleanedGuess = this.cleanString(this.$song_guess.val());
console.log(cleanedSong);
return (new RegExp(cleanedSong, "i").test(cleanedGuess));
//ignores case
}
}
//Checks if final song has been reached
Game.prototype.checkEnd = function() {
return this.roundCount == this.songs.length && this.roundCount > 0;
}
//Ends game if final song has been reached, updates view
Game.prototype.endGame = function() {
if (this.checkEnd()) {
this.$display.html(this.currentWinStatement());
this.$audio.attr("src", "#");
this.$song_guess.hide();
this.$visualizer.hide();
this.$restart_button.show();
this.$restart_button.on("click touchstart", reload);
}
}
Game.prototype.handleCorrect = function() {
this.currentPlayer().incrementScore();
this.$display.html(this.currentWinStatement());
bounce("display");
}
Game.prototype.handleMissed = function() {
this.$display.html("You missed '" + this.currentSongName() + "'");
shake("display");
}
Game.prototype.handleRound = function() {
this.roundTime = this.getRoundLength();
if (this.checkGuess() && this.roundTime <= this.maxRoundLength) {
this.handleCorrect();
this.newRound();
} else if (this.roundTime > this.maxRoundLength) {
this.handleMissed();
this.newRound();
}
}
//Iterates the song and game.roundCount, resetting timer
//updates view
Game.prototype.newRound = function() {
this.roundCount++;
this.roundStart = this.now();
this.$song_guess.val("");
this.$audio.attr("src", this.currentSong()["preview_url"]);
};
//updates view to pre-game state, initalizes player
Game.prototype.beginGame = function() {
this.$document.on('keyup', this.handleRound.bind(this));
this.players.push(new Player(this.$song_guess.val()));
this.$start_button.hide();
this.$song_guess.val("");
this.$song_guess.focus();
this.$song_guess.attr("placeholder", "guess the song name");
this.$audio.attr("src", this.currentSong()["preview_url"]);
this.$visualizer.show();
this.roundStart = this.now();
this.$display.html("You have " + this.maxRoundLength + " seconds for each song.");
}
Game.prototype.initialize = function() {
var Game = this;
this.cacheDom();
this.$start_button.on("click touchstart", this.beginGame.bind(this));
this.$restart_button.hide();
this.$visualizer.hide();
setInterval(this.checkTime.bind(this), 100);
setInterval(this.endGame.bind(this), 100);
$.ajax({
type: "GET",
url: "https://api.spotify.com/v1/search?q=year:2016&type=track",
success: function(spotifyJSON) {
Game.songs = spotifyJSON.tracks.items.shuffle();
}
})
}
Game.prototype.isLastRound = function() {
return this.roundCount > 0 && (this.roundCount + 1) != this.songs.length;
}
Game.prototype.isRoundOver = function() {
return this.getRoundLength() > this.maxRoundLength;
}
Game.prototype.checkTime = function() {
if (!this.isLastRound() && this.isRoundOver()) {
this.handleMissed();
this.newRound();
} else if (!this.isLastRound() && this.getRoundLength() > 25) {
this.$display.html(this.maxRoundLength - Math.trunc(this.getRoundLength()));
}
}
Game.prototype.now = function() {
return $.now() / 1000;
}
Game.prototype.getRoundLength = function() {
if (this.players.length > 0) {
return this.now() - this.roundStart;
} else {
return -1;
}
}
//PLAYER METHODS
//Increments player.score and updates view
Player.prototype.incrementScore = function() {
this.score++;
this.$score_value.html(this.score);
}
//Array method to shuffle items using built in RNG
//Fisher-Yates (Knuth) Shuffle
Array.prototype.shuffle = function() {
var currentIndex = this.length,
temporaryValue, randomIndex;
//until shuffled
while (0 !== currentIndex) {
//randomly grab an element
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
//swap grabbed element with current element
temporaryValue = this[currentIndex];
this[currentIndex] = this[randomIndex];
this[randomIndex] = temporaryValue;
}
return this;
}