-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
252 lines (215 loc) · 8.71 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
function checkSession() { // This function is checking if the user is logged in
const isLoggedIn = document.getElementById('isLoggedIn').value;
const UserName = document.getElementById('usernm').value;
if (isLoggedIn === 'true') {
document.getElementById('userStatus').textContent = 'Welcome, ' + UserName;
document.getElementById('logout-btn').style.display = 'block';
} else {
document.getElementById('userStatus').textContent = 'You are not logged in.';
}
}
document.getElementById('player1-ready').addEventListener('click', () => {
const player1Color = document.getElementById('player1-color').value;
const player3Color = document.getElementById('player3-color').value;
const p1ColorOut = fetchGameInfo(player1Color);
const p3ColorOut = fetchGameInfo(player3Color);
const gameId = document.getElementById('global_gameid').value;
const playerId = document.getElementById('global_playerid').value;
const player1id = document.getElementById('player1id_hidden').value;
console.log(player1Color, player3Color, gameId, playerId, player1id);
if (validateColors(player1Color,player3Color)) {
const xhr = new XMLHttpRequest();
xhr.open('POST', 'update_player_status.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
const data = new URLSearchParams({
gameId: gameId,
playerId: playerId,
player1id: player1id,
player1Color: player1Color,
player3Color: player3Color
});
xhr.send(data.toString());
xhr.onload = function() {
if (xhr.status === 200) {
// Handle successful response
alert(xhr.responseText);
// document.getElementById('player1-choice').textContent = p1ColorOut + " & " + p3ColorOut;
} else {
// Handle error
console.error('Request failed. Returned status of ' + xhr.status);
alert(xhr.responseText);
}
};
// Send an AJAX request to update the database
// fetch('update_player_status.php', {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json'
// },
// body: JSON.stringify({
// gameId: gameId,
// playerId: playerId,
// player1id: player1id,
// player1Color: player1Color,
// player3Color: player3Color
// })
// })
// .then(response => response.text())
// .then(data => {
// if (data === 'success') {
// // Handle successful update
// alert('Your choices have been saved.');
// document.getElementById('player1-choice').textContent = player1Color + " & " + player3Color;
// } else {
// // Handle error
// alert(data);
// // alert('Error saving choices. ' + data.message);
// }
// });
} else {
alert('Please select different colors for Player 1.');
}
});
document.getElementById('player2-ready').addEventListener('click', () => {
const player2Color = document.getElementById('player2-color').value;
const player4Color = document.getElementById('player4-color').value;
const p2ColorOut = fetchGameInfo(player2Color);
const p4ColorOut = fetchGameInfo(player4Color);
const gameId = document.getElementById('global_gameid').value;
const playerId = document.getElementById('global_playerid').value;
const player1id = document.getElementById('player1id_hidden').value;
if (validateColors(player2Color,player4Color)) {
const xhr = new XMLHttpRequest();
xhr.open('POST', 'update_player_status.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
const data = new URLSearchParams({
gameId: gameId,
playerId: playerId,
player1id: player1id,
player2Color: player2Color,
player4Color: player4Color
});
xhr.send(data.toString());
xhr.onload = function() {
if (xhr.status === 200) {
// Handle successful response
alert(xhr.responseText);
// document.getElementById('player2-choice').textContent = p2ColorOut + " & " + p4ColorOut;
} else {
// Handle error
console.error('Request failed. Returned status of ' + xhr.status);
}
};
} else {
alert('Please select different colors for Player 2.');
}
});
function validateColors(color1, color2){ // Check if both colors per user are selected and different
return color1 !== color2 && color1 !== '' && color2 !== '';
}
function fetchGameInfo(color){ // Returns the full name of every color
const inputColor = color;
if (inputColor == 'r'){
return 'Red';
} else if (inputColor == 'b'){
return 'Blue';
} else if (inputColor == 'g'){
return 'Green';
} else if (inputColor == 'y'){
return 'Yellow';
} else {
return 'No Color';
}
}
function updatePlayerColors (){ // Will update player color status inside lobby.php
// Check if the current page is lobby.php
if (window.location.href.indexOf('lobby.php') !== -1) {
const gameId = document.getElementById('global_gameid').value;
const xhr = new XMLHttpRequest();
xhr.open('POST', 'updateColors.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
const data = new URLSearchParams({
gameId: gameId
});
xhr.send(data.toString());
xhr.onload = function() {
if (xhr.status === 200) {
const responseData = JSON.parse(xhr.responseText)[0];
if (responseData.error) {
console.error(responseData.error);
// Handle error message from PHP
} else {
// Update player choices with retrieved colors
document.getElementById('player1-choice').textContent = fetchGameInfo(responseData.player1Color) + " & " + fetchGameInfo(responseData.player3Color);
document.getElementById('player2-choice').textContent = fetchGameInfo(responseData.player2Color) + " & " + fetchGameInfo(responseData.player4Color);
}
} else {
console.error('Request failed. Returned status of ' + xhr.status);
}
};
// Schedule the next execution after 3 second
setTimeout(updatePlayerColors, 3000);
}
}
// Start the initial execution
updatePlayerColors();
function checkReadiness (){ // Will activate start game button if both players are ready
// Check if the current page is lobby.php
if (window.location.href.indexOf('lobby.php') !== -1) {
const gameId = document.getElementById('global_gameid').value;
const xhr = new XMLHttpRequest();
xhr.open('POST', 'checkReadiness.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
const data = new URLSearchParams({
gameId: gameId
});
xhr.send(data.toString());
xhr.onload = function() {
if (xhr.status === 200) {
const responseData = JSON.parse(xhr.responseText)[0];
if (responseData.error) {
console.error(responseData.error);
// Handle error message from PHP
} else {
// Check if players are ready
if(responseData.player1ready == '1' && responseData.player2ready == '1'){
document.getElementById('startGameButton').disabled = false;
}
}
} else {
console.error('Request failed. Returned status of ' + xhr.status);
}
};
// Schedule the next execution after 2 second
setTimeout(checkReadiness, 2000);
}
}
// Start the initial execution
checkReadiness();
document.getElementById('startGameButton').addEventListener('click', () => { //Redirects players to game based on its id
const gameId = document.getElementById('global_gameid').value;
window.location.href = `game.php?game_id=${gameId}`;
});
// window.addEventListener('beforeunload', function (event) { // Tried to build a function that logout player everytime closes tab or window
// const gameId = document.getElementById('global_gameid').value;
// if (event.target.location.href !== window.location.href) {
// return; // Don't trigger the logout process for navigation
// }
// event.preventDefault();
// // Send a request to the server to update the game status
// const xhr = new XMLHttpRequest();
// xhr.open('POST', 'logout.php');
// const data = new URLSearchParams({
// gameId: gameId
// });
// xhr.send(data.toString());
// // xhr.onload = function() {
// // if (xhr.status === 200) {
// // // Handle successful response
// // alert(xhr.responseText);
// // } else {
// // // Handle error
// // console.error('Request failed. Returned status of ' + xhr.status);
// // }
// // };
// });