Spaces:
Paused
Paused
Update static/js/game.js
Browse files- static/js/game.js +64 -46
static/js/game.js
CHANGED
@@ -1,62 +1,49 @@
|
|
1 |
let sessionId = null;
|
2 |
-
let
|
|
|
3 |
|
4 |
document.addEventListener('DOMContentLoaded', (event) => {
|
5 |
-
document.getElementById('playButton')
|
6 |
-
|
7 |
-
document.getElementById('submitWordButton').addEventListener('click', submitWord);
|
8 |
});
|
9 |
|
10 |
-
function
|
11 |
fetch('/play', { method: 'POST' })
|
12 |
.then(response => response.json())
|
13 |
.then(data => {
|
14 |
sessionId = data.session_id;
|
15 |
-
|
16 |
document.getElementById('sessionId').textContent = sessionId;
|
17 |
-
document.getElementById('
|
18 |
-
|
19 |
-
|
20 |
-
checkGameStatus();
|
21 |
-
});
|
22 |
-
}
|
23 |
-
|
24 |
-
function joinGame() {
|
25 |
-
const joinSessionId = document.getElementById('joinSessionId').value;
|
26 |
-
fetch(`/join/${joinSessionId}`, { method: 'POST' })
|
27 |
-
.then(response => response.json())
|
28 |
-
.then(data => {
|
29 |
-
if (data.error) {
|
30 |
-
alert(data.error);
|
31 |
-
} else {
|
32 |
-
sessionId = joinSessionId;
|
33 |
-
player = 'player2';
|
34 |
-
document.getElementById('sessionId').textContent = sessionId;
|
35 |
-
document.getElementById('startOptions').style.display = 'none';
|
36 |
-
document.getElementById('gameInfo').style.display = 'block';
|
37 |
-
document.getElementById('statusMessage').textContent = 'Joined the game. Starting soon...';
|
38 |
checkGameStatus();
|
|
|
|
|
39 |
}
|
40 |
});
|
41 |
}
|
42 |
|
43 |
function checkGameStatus() {
|
44 |
-
fetch(`/
|
45 |
.then(response => response.json())
|
46 |
.then(data => {
|
47 |
-
if (data.
|
48 |
-
document.getElementById('
|
49 |
-
|
|
|
50 |
} else {
|
51 |
-
setTimeout(checkGameStatus, 2000);
|
52 |
}
|
53 |
});
|
54 |
}
|
55 |
|
56 |
-
function
|
57 |
-
|
58 |
}
|
59 |
|
|
|
|
|
60 |
function submitWord() {
|
61 |
const word = document.getElementById('wordInputField').value;
|
62 |
if (word.length !== 7) {
|
@@ -68,23 +55,43 @@ function submitWord() {
|
|
68 |
headers: {
|
69 |
'Content-Type': 'application/json',
|
70 |
},
|
71 |
-
body: JSON.stringify({ session_id: sessionId,
|
72 |
})
|
73 |
.then(response => response.json())
|
74 |
.then(data => {
|
75 |
if (data.status === 'waiting_for_opponent') {
|
76 |
document.getElementById('wordInput').style.display = 'none';
|
77 |
-
document.getElementById('
|
|
|
78 |
} else if (data.status === 'game_started') {
|
79 |
-
|
80 |
-
document.getElementById('gameArea').style.display = 'block';
|
81 |
-
document.getElementById('statusMessage').textContent = 'Game started!';
|
82 |
-
updateGameState();
|
83 |
}
|
84 |
});
|
85 |
}
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
function makeGuess() {
|
|
|
|
|
|
|
|
|
88 |
const guess = document.getElementById('guessInput').value;
|
89 |
if (guess.length !== 1) {
|
90 |
alert('Please enter a single letter');
|
@@ -95,12 +102,12 @@ function makeGuess() {
|
|
95 |
headers: {
|
96 |
'Content-Type': 'application/json',
|
97 |
},
|
98 |
-
body: JSON.stringify({ session_id: sessionId,
|
99 |
})
|
100 |
.then(response => response.json())
|
101 |
.then(data => {
|
102 |
if (data.status === 'game_over') {
|
103 |
-
alert(data.winner ===
|
104 |
}
|
105 |
updateGameState();
|
106 |
});
|
@@ -110,12 +117,11 @@ function updateGameState() {
|
|
110 |
fetch(`/game_state/${sessionId}`)
|
111 |
.then(response => response.json())
|
112 |
.then(data => {
|
113 |
-
document.getElementById('playerWord').textContent = data.words[
|
114 |
-
document.getElementById('opponentWord').textContent = data.words[
|
115 |
document.getElementById('guesses').textContent = 'Guessed letters: ' + data.guesses.join(', ');
|
116 |
-
|
117 |
document.getElementById('playerTurn').textContent = isMyTurn ? 'Your turn' : "Opponent's turn";
|
118 |
-
document.getElementById('guessButton').disabled = !isMyTurn;
|
119 |
updateHangmanSVG(data.incorrect_guesses);
|
120 |
});
|
121 |
setTimeout(updateGameState, 2000); // Poll every 2 seconds
|
@@ -127,4 +133,16 @@ function updateHangmanSVG(stage) {
|
|
127 |
.then(svgContent => {
|
128 |
document.getElementById('hangmanSvg').innerHTML = svgContent;
|
129 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
}
|
|
|
1 |
let sessionId = null;
|
2 |
+
let playerId = null;
|
3 |
+
let isMyTurn = false;
|
4 |
|
5 |
document.addEventListener('DOMContentLoaded', (event) => {
|
6 |
+
const playButton = document.getElementById('playButton');
|
7 |
+
if (playButton) playButton.addEventListener('click', startGame);
|
|
|
8 |
});
|
9 |
|
10 |
+
function startGame() {
|
11 |
fetch('/play', { method: 'POST' })
|
12 |
.then(response => response.json())
|
13 |
.then(data => {
|
14 |
sessionId = data.session_id;
|
15 |
+
playerId = data.player_id;
|
16 |
document.getElementById('sessionId').textContent = sessionId;
|
17 |
+
document.getElementById('playButton').style.display = 'none';
|
18 |
+
if (data.waiting) {
|
19 |
+
document.getElementById('waitingMessage').style.display = 'block';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
checkGameStatus();
|
21 |
+
} else {
|
22 |
+
startGamePlay();
|
23 |
}
|
24 |
});
|
25 |
}
|
26 |
|
27 |
function checkGameStatus() {
|
28 |
+
fetch(`/check_status/${sessionId}`)
|
29 |
.then(response => response.json())
|
30 |
.then(data => {
|
31 |
+
if (data.players === 2) {
|
32 |
+
document.getElementById('waitingMessage').style.display = 'none';
|
33 |
+
document.getElementById('gameStarting').style.display = 'block';
|
34 |
+
setTimeout(startGamePlay, 2000);
|
35 |
} else {
|
36 |
+
setTimeout(checkGameStatus, 2000);
|
37 |
}
|
38 |
});
|
39 |
}
|
40 |
|
41 |
+
function startGamePlay() {
|
42 |
+
window.location.href = `/game.html?session=${sessionId}&player=${playerId}`;
|
43 |
}
|
44 |
|
45 |
+
// The following functions will be used in game.html
|
46 |
+
|
47 |
function submitWord() {
|
48 |
const word = document.getElementById('wordInputField').value;
|
49 |
if (word.length !== 7) {
|
|
|
55 |
headers: {
|
56 |
'Content-Type': 'application/json',
|
57 |
},
|
58 |
+
body: JSON.stringify({ session_id: sessionId, player_id: playerId, word: word }),
|
59 |
})
|
60 |
.then(response => response.json())
|
61 |
.then(data => {
|
62 |
if (data.status === 'waiting_for_opponent') {
|
63 |
document.getElementById('wordInput').style.display = 'none';
|
64 |
+
document.getElementById('waitingMessage').style.display = 'block';
|
65 |
+
setTimeout(checkWordSubmissionStatus, 2000);
|
66 |
} else if (data.status === 'game_started') {
|
67 |
+
startHangmanGame();
|
|
|
|
|
|
|
68 |
}
|
69 |
});
|
70 |
}
|
71 |
|
72 |
+
function checkWordSubmissionStatus() {
|
73 |
+
fetch(`/game_state/${sessionId}`)
|
74 |
+
.then(response => response.json())
|
75 |
+
.then(data => {
|
76 |
+
if (Object.keys(data.words).length === 2) {
|
77 |
+
startHangmanGame();
|
78 |
+
} else {
|
79 |
+
setTimeout(checkWordSubmissionStatus, 2000);
|
80 |
+
}
|
81 |
+
});
|
82 |
+
}
|
83 |
+
|
84 |
+
function startHangmanGame() {
|
85 |
+
document.getElementById('waitingMessage').style.display = 'none';
|
86 |
+
document.getElementById('gameArea').style.display = 'block';
|
87 |
+
updateGameState();
|
88 |
+
}
|
89 |
+
|
90 |
function makeGuess() {
|
91 |
+
if (!isMyTurn) {
|
92 |
+
alert("It's not your turn!");
|
93 |
+
return;
|
94 |
+
}
|
95 |
const guess = document.getElementById('guessInput').value;
|
96 |
if (guess.length !== 1) {
|
97 |
alert('Please enter a single letter');
|
|
|
102 |
headers: {
|
103 |
'Content-Type': 'application/json',
|
104 |
},
|
105 |
+
body: JSON.stringify({ session_id: sessionId, player_id: playerId, guess: guess }),
|
106 |
})
|
107 |
.then(response => response.json())
|
108 |
.then(data => {
|
109 |
if (data.status === 'game_over') {
|
110 |
+
alert(data.winner === playerId ? 'You win!' : 'You lose!');
|
111 |
}
|
112 |
updateGameState();
|
113 |
});
|
|
|
117 |
fetch(`/game_state/${sessionId}`)
|
118 |
.then(response => response.json())
|
119 |
.then(data => {
|
120 |
+
document.getElementById('playerWord').textContent = data.words[playerId];
|
121 |
+
document.getElementById('opponentWord').textContent = data.words[data.players.find(p => p !== playerId)];
|
122 |
document.getElementById('guesses').textContent = 'Guessed letters: ' + data.guesses.join(', ');
|
123 |
+
isMyTurn = data.current_player === playerId;
|
124 |
document.getElementById('playerTurn').textContent = isMyTurn ? 'Your turn' : "Opponent's turn";
|
|
|
125 |
updateHangmanSVG(data.incorrect_guesses);
|
126 |
});
|
127 |
setTimeout(updateGameState, 2000); // Poll every 2 seconds
|
|
|
133 |
.then(svgContent => {
|
134 |
document.getElementById('hangmanSvg').innerHTML = svgContent;
|
135 |
});
|
136 |
+
}
|
137 |
+
|
138 |
+
// Initialize game when game.html loads
|
139 |
+
window.onload = function() {
|
140 |
+
const urlParams = new URLSearchParams(window.location.search);
|
141 |
+
sessionId = urlParams.get('session');
|
142 |
+
playerId = urlParams.get('player');
|
143 |
+
if (sessionId && playerId) {
|
144 |
+
document.getElementById('wordInput').style.display = 'block';
|
145 |
+
} else {
|
146 |
+
alert('Invalid game session');
|
147 |
+
}
|
148 |
}
|