-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
361 lines (306 loc) · 10.2 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
Requirements:
Only two players can play at the same time.
Both player has to pic either rock, papaer or scissors.
Game will display for any tie or defeat
Game will track players wins and loss.
When the connection of one player goes another player can join and continue playing.
It will keep track of the connection.
*/
// Initialize Firebase.
var config = {
apiKey: "AIzaSyCcZF0mY8mli-PcmjFs4tx31JtTJPNvvp0",
authDomain: "projectrps-ce3c2.firebaseapp.com",
databaseURL: "https://projectrps-ce3c2.firebaseio.com",
projectId: "projectrps-ce3c2",
storageBucket: "",
messagingSenderId: "353009623962"
};
firebase.initializeApp(config);
// Create references.
var database = firebase.database();
var chatRef = database.ref("/chat");
var playersRef = database.ref("/players");
var connectedRef = database.ref(".info/connected");
// Global variables to keep track of all the players data locally.
var playerName,
playerNumber,
player1LoggedIn = false,
player2LoggedIn = false,
playerObject,
player1Object = {
playerId:"",
name: name,
choice: "",
wins: 0,
loss: 0
},
player2Object = {
playerId:"",
name: name,
choice: "",
wins: 0,
loss: 0
},
player1ChatObject = {
userId: "",
name: "",
text: ""
},
player2ChatObject = {
userId: "",
name: "",
text: ""
},
playerChatObject,
resetId;
// show the login screen
showLoginScreen();
/**
* Reset the round
*/
function reset() {
clearTimeout(resetId);
playerObject.choice = "";
database.ref("/players/" + playerNumber).set(playerObject);
$(".selection-reveal").hide();
$("#feedback").empty();
}
// Update the loggedIn status the Players.
playersRef.on("child_added",function(snapshot){
window["player"+snapshot.key+"LoggedIn"] = true;
window["player"+snapshot.key+"Object"] = snapshot.val();
}, errorHandler);
// update the playerObject when the child is changed
playersRef.on("child_changed",function(snapshot){
window["player"+snapshot.key+"Object"] = snapshot.val();
updateStats();
}, errorHandler);
// Updates when a child is removed.
playersRef.on("child_removed",function(snapshot){
var listText = "System:"+snapshot.val().name+" has disconnected";
var listString = $("<li>").addClass("system");
listString.text(listText);
$("#chat-log").append(listString);
window["player"+snapshot.key+"LoggedIn"] = false;
window["player"+snapshot.key+"Object"] = {
name: "",
choice:"",
wins: 0,
loss: 0
};
// When both palyers are disconnected.
if(!player1LoggedIn && !player2LoggedIn){
chatRef.remove();
}
},errorHandler);
// When a new chat is added.
chatRef.on("child_added", function(snapshot){
window["player"+snapshot.key+"ChatObject"] = snapshot.val();
updateChat();
});
// When a child is changed.
chatRef.on("child_changed", function(snapshot){
window["player"+snapshot.key+"ChatObject"] = snapshot.val();
updateChat();
});
// if a chat message is removed, remove it from the DOM
chatRef.on("child_removed", function (chatSnap) {
$("#" + chatSnap.key).remove();
}, errorHandler);
// When any general change is made.
playersRef.on("value",function(snapshot){
// Update the player if logged in
$("#player-1").text(player1Object.name || "Waiting for player1");
$("#player-2").text(player2Object.name || "Waiting for Player2");
// update which part of the player box is showing based on whether a selection has been made
updatePlayerBox("1", snapshot.child("1").exists(), snapshot.child("1").exists() && snapshot.child("1").val().choice);
updatePlayerBox("2", snapshot.child("2").exists(), snapshot.child("2").exists() && snapshot.child("2").val().choice);
// Display the right screen depending on the logged in status.
if (player1LoggedIn && player2LoggedIn && !playerNumber){
showLoginPending();
} else if (playerNumber && !playerObject.choice){
showLoggedInScreen();
} else if((!player1LoggedIn && !playerNumber) || (!player2LoggedIn&&!playerNumber)){
showLoginScreen();
}
// If both the players selected a choice call rps function.
if(player1Object.choice && player2Object.choice){
rps(player1Object.choice,player2Object.choice);
}
},errorHandler);
// Funtion to handle errors.
function errorHandler(error){
console.log("ERROR: "+error.code);
}
/**
* Update the player box state
* @param {string} playerNum 1 or 2
* @param {boolean} exists
* @param {boolean} choice
*/
function updatePlayerBox(playerNum, exists, choice) {
if (exists) {
if (playerNumber != playerNum) {
if (choice) {
$(".p" + playerNum + "-selection-made").show();
$(".p" + playerNum + "-pending-selection").hide();
} else {
$(".p" + playerNum + "-selection-made").hide();
$(".p" + playerNum + "-pending-selection").show();
}
}
} else {
$(".p" + playerNum + "-selection-made").hide();
$(".p" + playerNum + "-pending-selection").hide();
}
}
/**
* Update status for both players based off most recently-pulled data
*/
function updateStats() {
["1", "2"].forEach(playerNum => {
var obj = window["player" + playerNum + "Object"];
$("#p" + playerNum + "-wins").text(obj.wins);
$("#p" + playerNum + "-loss").text(obj.loss);
});
player1LoggedIn ? $(".p1-status").show() : $(".p1-status").hide();
player2LoggedIn ? $(".p2-status").show() : $(".p2-status").hide();
}
/**
* Update the chat list
*/
function updateChat() {
["1", "2"].forEach(playerNum => {
var obj = window["player" + playerNum + "ChatObject"];
var chatText = obj.text;
var listText = $("<li>").attr("id",playerNum);
if(playerNum == obj.userId){
listText.addClass("current-user");
}
if(playerNum != obj.userId){
listText.addClass("other-user");
}
chatText = "<strong>"+obj.name +": <strong>" +chatText;
listText.html(chatText);
$("#chat-log").append(listText);
});
// Scroll to bottom
$("#chat-log").scrollTop($("#chat-log")[0].scrollHeight);
}
/**
* Compares 2 choices and determines a tie or winner
* @param {string} p1choice rock, paper, scissors
* @param {string} p2choice rock, paper, scissors
*/
// Function to compute the game.
function rps(p1choice,p2choice){
$(".p1-selection-reveal").text(p1choice);
$(".p2-selection-reveal").text(p2choice);
showSelections();
if (p1choice == p2choice) {
//tie
$("#feedback").text("TIE");
}
else if ((p1choice == "rock" && p2choice == "scissors") || (p1choice == "paper" && p2choice == "rock") || (p1choice == "scissors" && p2choice == "paper")) {
// p1 wins
$("#feedback").html("<small>" + p1choice + " beats " + p2choice + "</small><br/><br/>" + player1Object.name + " wins!");
if (playerNumber == "1") {
playerObject.wins++;
} else {
playerObject.loss++;
}
} else {
// p2 wins
$("#feedback").html("<small>" + p2choice + " beats " + p1choice + "</small><br/><br/>" + player2Object.name + " wins!");
if (playerNumber == "2") {
playerObject.wins++;
} else {
playerObject.loss++;
}
}
resetId = setTimeout(reset, 10*1000);
}
/**
* When the login button is clicked, new player is added to the available player slot.
*/
$("#login-btn").on("click",function(e){
e.preventDefault();
// Check for the available slot.
if(!player1LoggedIn){
playerNumber = "1";
playerObject = player1Object;
playerChatObject = player1ChatObject;
}
else if(!player2LoggedIn){
playerNumber = "2";
playerObject = player2Object;
playerChatObject = player2ChatObject;
}
else {
playerNumber = null;
playerObject = null;
playerChatObject = null;
}
// If slot available, put the values to firebase.
if(playerNumber){
playerName = $(".login-input").val().trim();
playerObject.playerId = playerNumber;
playerObject.name = playerName;
playerChatObject.userId = playerNumber;
playerChatObject.name = playerName;
$(".player-name-display").text(playerName);
$(".player-number-display").text(playerNumber);
database.ref("/players/"+playerNumber).set(playerObject);
database.ref("/players/"+playerNumber).onDisconnect().remove();
}
$(".login-input").val("");
})
/**
* Update the firebase database when a selection is made.
*/
$(".selection").on("click",function(){
// failsafe for if the player isn't logged in.
if(!playerNumber) return;
playerObject.choice = this.id;
database.ref("/players/"+playerNumber).set(playerObject);
$(".p"+playerNumber+"-selections").hide();
$(".p"+playerNumber+"-selection-reveal").text(this.id).show();
});
// when the Chat send is clicked.
$("#send-chat").on("click", function(event){
event.preventDefault();
// Push the chats into the database.
var text = $("#chat").val().trim();
playerChatObject.text = text;
database.ref("/chat/"+playerNumber).set(playerChatObject);
database.ref("/chat/"+playerNumber).onDisconnect().remove();
$("#chat").val("");
});
// Display functions
function showLoginScreen(){
$(".pre-connection,.post-login,.pending-login,.selections").hide();
$(".pre-login").show();
}
function showLoginPending(){
$(".pre-connection,.post-login,.pre-login,.selections").hide();
$(".pending-login").show();
}
function showLoggedInScreen(){
$(".pre-connection,.pre-login,.pending-login").hide();
$(".post-login").show();
if(playerNumber == "1"){
$(".p1-selections").show();
} else {
$(".p1-selections").hide();
}
if(playerNumber == "2"){
$(".p2-selections").show();
} else {
$(".p2-selections").hide();
}
}
function showSelections() {
$(".selections, .pending-selection, .selection-made").hide();
$(".selection-reveal").show();
}