-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
330 lines (276 loc) · 11.1 KB
/
app.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
var main = function() {
'use strict';
$('#login').show();
$('#usersOnline').hide();
//View Model
function viewModel(){
var self = this;
self.imagePath = ko.observable('');
}
var vm = new viewModel(); //Hold the viewmodel in a variable so that it can be reffered inside socket.io events
ko.applyBindings(vm);
var currPrice;
var socket = io.connect('http://localhost:3000', {
reconnect: true
});
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', '/the-price-is-right-sound-byte_cutted.mp3');
var homePageAudio = document.createElement('audio');
homePageAudio.setAttribute('src', '/price_is_right.mp3');
audioElement.addEventListener('ended', function() {
homePageAudio.pause();
homePageAudio.currentTime = 0;
}, false)
homePageAudio.addEventListener('ended', function() {
homePageAudio.pause();
homePageAudio.currentTime = 0;
}, false)
homePageAudio.play();
$('#stopMusic').on('click', function(event) {
homePageAudio.pause();
homePageAudio.currentTime = 0;
});
$('#frontPageImage').click(function(){
homePageAudio.pause();
homePageAudio.currentTime = 0;
audioElement.play();
setTimeout(function(){
window.location.href = '/gamePage.html'; },4000);
});
$('#inputBid').click( function(event) {
if ($('#bid').val() !== '')
{
var usrBid = $('#bid').val();
socket.emit('bid', usrBid);
}
});
socket.on("update-bid", function(playerBids, userPlaying)
{
$('.game').show();
$('.game #gameplay').show();
if(playerBids[userPlaying[0]] !== null)
{
$('#p1-bid').text(playerBids[userPlaying[0]]);
}
if(playerBids[userPlaying[1]] !== null)
{
$('#p2-bid').text(playerBids[userPlaying[1]]);
}
if(playerBids[userPlaying[2]] !== null)
{
$('#p3-bid').text(playerBids[userPlaying[2]]);
}
if(playerBids[userPlaying[3]] !== null)
{
$('#p4-bid').text(playerBids[userPlaying[3]]);
}
if(playerBids[userPlaying[4]] !== null)
{
$('#p5-bid').text(playerBids[userPlaying[4]]);
}
});
// Add New User
$('#createUser').on('click', function(event) {
if ($('#username').val() !== '')
{
$('#login').hide();
homePageAudio.pause();
homePageAudio.currentTime = 0;
audioElement.play();
var username = $('#username').val();
socket.emit('join', username);
$('.game').show();
$('.game #gameplay').hide();
$('#usersOnline').show();
}
return false;
});
socket.on("update-users", function(user) {
$("#users").empty();
$.each(user, function(clientID, name) {
$('#users').append("<li>" + name + "</li>");
$('#users').css("color", "#FC8832");
$('#users').css("font-size", "20px");
});
});
socket.on("game_started", function(input) {
vm.imagePath(input.image);
currPrice = input.price;
});
socket.on("game_over", function(input) {
$('#winnerCircle').text("Game Over");
});
socket.on("roundWinner", function(input) {
var winnerCount = 0;
$.each(input, function(clientID, name) {
if(winnerCount == 0)
$('#firstWinner').text(name);
if(winnerCount == 1)
$('#secondWinner').text(name);
if(winnerCount == 2)
$('#thirdWinner').text(name);
winnerCount = winnerCount + 1;
});
});
socket.on("playing", function(users, userPlaying, input)
{
$('.game').show();
$('.game #gameplay').show();
$('.game #gameplay #winnersCircle').show();
$('#roundBid').show();
$('#p1').text(users[userPlaying[0]]);
$('#p2').text(users[userPlaying[1]]);
$('#p3').text(users[userPlaying[2]]);
$('#p4').text(users[userPlaying[3]]);
$('#p5').text(users[userPlaying[4]]);
});
socket.on("spectating", function(users, userPlaying)
{
$('.game').show();
$('.game #gameplay').show();
$('.game #gameplay #winnersCircle').show();
$('#roundBid').hide();
$('#p1').text(users[userPlaying[0]]);
$('#p2').text(users[userPlaying[1]]);
$('#p3').text(users[userPlaying[2]]);
$('#p4').text(users[userPlaying[3]]);
$('#p5').text(users[userPlaying[4]]);
});
socket.on("waiting-room", function(users, userPlaying)
{
$('.game').show();
$('.game #gameplay').show();
$('.game #gameplay #winnersCircle').show();
$('#roundBid').hide();
});
};
/*
//TRYING SOMETHING OUT
// Create new wheel object specifying the parameters at creation time.
var theWheel = new Winwheel({
'numSegments' : 20, // Specify number of segments.
'outerRadius' : 150, // Set outer radius so wheel fits inside the background.
'textFontSize' : 20, // Set font size as desired.
'segments' : // Define segments including colour and text.
[
{'fillStyle' : 'green', 'text' : '5¢'},
{'fillStyle' : 'black', 'text' : '$1', 'textFillStyle' : 'white'},
{'fillStyle' : 'green', 'text' : '15¢'},
{'fillStyle' : 'black', 'text' : '80¢', 'textFillStyle' : 'white'},
{'fillStyle' : 'white', 'text' : '35¢', 'textFillStyle' : 'black'},
{'fillStyle' : 'black', 'text' : '60¢', 'textFillStyle' : 'white'},
{'fillStyle' : 'white', 'text' : '20¢', 'textFillStyle' : 'black'},
{'fillStyle' : 'black', 'text' : '40¢', 'textFillStyle' : 'white'},
{'fillStyle' : 'white', 'text' : '75¢', 'textFillStyle' : 'black'},
{'fillStyle' : 'black', 'text' : '55¢', 'textFillStyle' : 'white'},
{'fillStyle' : 'white', 'text' : '95¢', 'textFillStyle' : 'black'},
{'fillStyle' : 'black', 'text' : '50¢', 'textFillStyle' : 'white'},
{'fillStyle' : 'white', 'text' : '85¢', 'textFillStyle' : 'black'},
{'fillStyle' : 'black', 'text' : '30¢', 'textFillStyle' : 'white'},
{'fillStyle' : 'white', 'text' : '65¢', 'textFillStyle' : 'black'},
{'fillStyle' : 'black', 'text' : '10¢', 'textFillStyle' : 'white'},
{'fillStyle' : 'white', 'text' : '45¢', 'textFillStyle' : 'black'},
{'fillStyle' : 'black', 'text' : '70¢', 'textFillStyle' : 'white'},
{'fillStyle' : 'white', 'text' : '25¢', 'textFillStyle' : 'black'},
{'fillStyle' : 'black', 'text' : '90¢', 'textFillStyle' : 'white'}
],
'animation' : // Specify the animation to use.
{
'type' : 'spinToStop',
'duration' : 5, // Duration in seconds.
'spins' : 8, // Number of complete spins.
'callbackFinished' : 'alertPrize()'
}
});
// Vars used by the code in this page to do power controls.
var wheelPower = 0;
var wheelSpinning = false;
// -------------------------------------------------------
// Function to handle the onClick on the power buttons.
// -------------------------------------------------------
function powerSelected(powerLevel)
{
// Ensure that power can't be changed while wheel is spinning.
if (wheelSpinning == false)
{
// Reset all to grey incase this is not the first time the user has selected the power.
document.getElementById('pw1').className = "";
document.getElementById('pw2').className = "";
document.getElementById('pw3').className = "";
// Now light up all cells below-and-including the one selected by changing the class.
if (powerLevel >= 1)
{
document.getElementById('pw1').className = "pw1";
}
if (powerLevel >= 2)
{
document.getElementById('pw2').className = "pw2";
}
if (powerLevel >= 3)
{
document.getElementById('pw3').className = "pw3";
}
// Set wheelPower var used when spin button is clicked.
wheelPower = powerLevel;
// Light up the spin button by changing it's source image and adding a clickable class to it.
document.getElementById('spin_button').src = "spin_on.png";
document.getElementById('spin_button').className = "clickable";
}
}
// -------------------------------------------------------
// Click handler for spin button.
// -------------------------------------------------------
function startSpin()
{
// Ensure that spinning can't be clicked again while already running.
if (wheelSpinning == false)
{
// Based on the power level selected adjust the number of spins for the wheel, the more times is has
// to rotate with the duration of the animation the quicker the wheel spins.
if (wheelPower == 1)
{
theWheel.animation.spins = 3;
}
else if (wheelPower == 2)
{
theWheel.animation.spins = 8;
}
else if (wheelPower == 3)
{
theWheel.animation.spins = 15;
}
// Disable the spin button so can't click again while wheel is spinning.
document.getElementById('spin_button').src = "spin_off.png";
document.getElementById('spin_button').className = "";
// Begin the spin animation by calling startAnimation on the wheel object.
theWheel.startAnimation();
// Set to true so that power can't be changed and spin button re-enabled during
// the current animation. The user will have to reset before spinning again.
wheelSpinning = true;
}
}
// -------------------------------------------------------
// Function for reset button.
// -------------------------------------------------------
function resetWheel()
{
theWheel.stopAnimation(false); // Stop the animation, false as param so does not call callback function.
theWheel.rotationAngle = 0; // Re-set the wheel angle to 0 degrees.
theWheel.draw(); // Call draw to render changes to the wheel.
document.getElementById('pw1').className = ""; // Remove all colours from the power level indicators.
document.getElementById('pw2').className = "";
document.getElementById('pw3').className = "";
wheelSpinning = false; // Reset to false to power buttons and spin can be clicked again.
}
// -------------------------------------------------------
// Called when the spin animation has finished by the callback feature of the wheel because I specified callback in the parameters.
// -------------------------------------------------------
function alertPrize()
{
// Get the segment indicated by the pointer on the wheel background which is at 0 degrees.
var winningSegment = theWheel.getIndicatedSegment();
// Do basic alert of the segment text. You would probably want to do something more interesting with this information.
alert("You have won " + winningSegment.text);
}
*/
$(document).ready(main);