-
Notifications
You must be signed in to change notification settings - Fork 0
/
twoPlayer.html
263 lines (240 loc) · 9.47 KB
/
twoPlayer.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Rock, Paper, Scissors</title>
<link rel="stylesheet" type="text/css" href="./assets/css/reset.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="./assets/css/style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div class="lock">1</div>
<div class="container">
<div class="scoreRow row">
<div class="col-md-offset-6 col-md-6">
<div class="jumbotron jumbotron-fluid scoreboard">
<h2 class="roundArea">Round: <h2 class="roundArea nbr round">1</h2></h2>
<h3 class="currentScore">Current Score: </h3>
<div class="scoreLine">
<h4 class="scoreArea">Player1:
<h4 class="scoreArea nbr player1score"></h4>
</h4>
</div>
<div class="scoreLine">
<h4 class="scoreArea">Player2:
<h4 class="scoreArea nbr player2score"></h4>
</h4>
</div>
</div>
</div>
</div>
<div class="row multiPlayer">
<h4 class="col-md-12 multiInstructions instructions">Once you select your option, player 2 will need to select their option. </h4>
<h4 class="col-md-12 multiInstructions line2 instructions">The computer will let you know who won.
<div class="p1keyboard">Keyboard options:
<ul>
<li>rock: 1 or r</li>
<li>paper: 2 or p</li>
<li> scissors: 3 or s</li>
</ul>
</div>
</h4>
</div>
<div class="row">
<div class="col-md-offset-2 col-md-8">
<div class="jumbotron multiGame">
<h2>Player 1 picked <span id="player1"></span></h2>
<h2>Player 2 selected <span id="player2"></span></h2>
<h2 id="roundScore"></h2>
<div class="messageBox"></div>
</div>
</div>
</div>
<div class="winningPic">
<img src="./assets/images/rockWins.jpg" alt="rockWins" class="winningPic rock hide">
<img src="./assets/images/paperBeatsRock.jpg" alt="paperWins" class="winningPic paper hide">
<img src="./assets/images/scissorsBeatsPaper.jpg" alt="scissorsWins" class="winningPic scissors hide">
</div>
</div>
<script src="https://www.gstatic.com/firebasejs/6.0.4/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.0.4/firebase-database.js"></script>
<script type="text/javascript">
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "AIzaSyDQbIbuFuRhfmolhdnBfi_jpWyN1ZyNueo",
authDomain: "rockpaperscissors-d2102.firebaseapp.com",
databaseURL: "https://rockpaperscissors-d2102.firebaseio.com",
projectId: "rockpaperscissors-d2102",
storageBucket: "rockpaperscissors-d2102.appspot.com",
messagingSenderId: "600671659645",
appId: "1:600671659645:web:2233d1e8db5293cc54f314",
measurementId: "G-T8MZKXKGBB"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
var database = firebase.database();
function getScores() {
var player1spot = $(".player1score");
var player2spot = $(".player2score");
var ref = firebase.database().ref();
ref.on("value", function(snapshot) {
console.log(snapshot.val());
console.log(snapshot.val().RPSscore1);
console.log(snapshot.val().RPSscore2);
console.log(snapshot.val());
var pastPlayer1Score = snapshot.val().RPSscore1;
var pastPlayer2Score = snapshot.val().RPSscore2;
var currentRound = snapshot.val().RPSround;
player1spot.text(pastPlayer1Score);
player2spot.text(pastPlayer2Score);
$(".round").text(currentRound);
}, function (error) {
console.log("Error: " + error.code);
}) //end of error and get data
};
getScores();
function getRound(){
var ref = firebase.database().ref();
ref.on("value", function(snapshot) {
console.log(snapshot.val().RPSround);
var trackRound = snapshot.val().RPSround;
}, function (error) {
console.log("Error: " + error.code);
}) //end of error and get data
};
function trackScore() {
var round = Number($(".round").text());
var player1score = Number($(".player1score").text());
var player2score = Number($(".player2score").text());
console.log('round: ' + round);
console.log('Player1 score: ' + player1score);
console.log('Player2 score: ' + player1score);
//add a point to the winner
if (whoWon[0] === '1'){
console.log('player1 won');
database.ref().update({
RPSscore1: player1score + 1,
}); //end of update
} else if(whoWon[0] === '2'){
console.log('player2 won');
database.ref().update({
RPSscore2: player2score + 1,
}); //end of update
}
} //end of write data
var whoWon = [];
var bestOfThree = [];
var turn = [];
function userSelection(player) {
console.log('player: ' + player);
if(player === 'player1'){
var userText = document.getElementById("player1");
playerIndex = 0;
} else if (player === 'player2') {
var userText = document.getElementById("player2");
playerIndex = 1;
}
document.onkeyup = function(event) {
console.log($(".lock").text());
console.log($(".round").text());
if($(".lock").text() === $(".round").text()){
switch(event.key){
case 'r':
case '1':
userText.textContent = 'rock';
break;
case 'p':
case '2':
userText.textContent = 'paper';
break;
case 's':
case '3':
userText.textContent = 'scissors';
break;
}
var player1selection = document.getElementById("player1").innerText;
if(event.key === 'r' || 's' || 'p' || '1' || '2' || '3'){
userSelection('player2');
var player2selection = document.getElementById("player2").innerText;
turn[0] = player1selection;
turn[1] = player2selection;
bestOfThree.push(turn);
console.log(turn);
score(player1selection, player2selection);
trackScore();
console.log();
if(!(turn[0] === "") && !(turn[1] === "")){
console.log('reset');
var nextRound = $(".lock").text(Number($(".lock").text()) + 1);
$(".messageBox").append("<button class='reset' >Start the next round?</button>");
reset();
}
}
}
}
};
userSelection('player1');
function score(player1selection, player2selection) {
var scoreMessage = {
'rock-paper': 'Point goes to player 2.',
'rock-scissors': 'Point goes to player 1.',
'rock-rock': 'There was a tie.',
'paper-rock': 'Point goes to player 1.',
'paper-scissors': 'Point goes to player 2.',
'paper-paper': 'There was a tie.',
'scissors-rock': 'Point goes to player 2.',
'scissors-paper': 'Point goes to player 1.',
'scissors-scissors': 'There was a tie.'
};
var scoreBreakdown = {
'rock-paper': '2',
'rock-scissors': '1',
'rock-rock': '0',
'paper-rock': '1',
'paper-scissors': '2',
'paper-paper': '0',
'scissors-rock': '2',
'scissors-paper': '1',
'scissors-scissors': '0'
};
var gamePlay = [player1selection + '-' + player2selection];
switch(gamePlay[0]){
case 'rock-scissors':
case 'scissors-rock':
document.querySelector(".rock").classList.remove("hide");
document.querySelector(".rock").classList.add("win");
break;
case 'rock-paper':
case 'paper-rock':
document.querySelector(".paper").classList.remove("hide");
document.querySelector(".paper").classList.add("win");
break;
case 'scissors-paper':
case 'paper-scissors':
document.querySelector(".scissors").classList.remove("hide");
document.querySelector(".scissors").classList.add("win");
break;
}
document.getElementById("roundScore").textContent = (scoreMessage[player1selection + '-' + player2selection]);
whoWon[0] = scoreBreakdown[player1selection + '-' + player2selection];
console.log(whoWon);
};
function reset() {
$(".reset").on("click", function() {
document.getElementById("player1").innerText = '';
document.getElementById("player2").innerText = '';
document.getElementById("roundScore").textContent = '';
turn[0] = "";
turn[1] = "";
console.log(turn);
$(".messageBox").text("");
$(".round").text($(".lock").text());
document.querySelector(".win").classList.add("hide");
document.querySelector(".win").classList.remove("win");
userSelection('player1');
})
};
</script>
</body>
</html>