Spaces:
Paused
Paused
Update static/js/game.js
Browse files- static/js/game.js +29 -26
static/js/game.js
CHANGED
@@ -4,7 +4,12 @@ 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() {
|
@@ -14,12 +19,12 @@ function startGame() {
|
|
14 |
sessionId = data.session_id;
|
15 |
playerId = data.player_id;
|
16 |
document.getElementById('sessionId').textContent = sessionId;
|
17 |
-
|
18 |
if (data.waiting) {
|
19 |
-
|
20 |
checkGameStatus();
|
21 |
} else {
|
22 |
-
|
23 |
}
|
24 |
});
|
25 |
}
|
@@ -29,21 +34,18 @@ function checkGameStatus() {
|
|
29 |
.then(response => response.json())
|
30 |
.then(data => {
|
31 |
if (data.players === 2) {
|
32 |
-
|
33 |
-
|
34 |
-
setTimeout(
|
|
|
|
|
|
|
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) {
|
@@ -59,9 +61,9 @@ function submitWord() {
|
|
59 |
})
|
60 |
.then(response => response.json())
|
61 |
.then(data => {
|
|
|
62 |
if (data.status === 'waiting_for_opponent') {
|
63 |
-
|
64 |
-
document.getElementById('waitingMessage').style.display = 'block';
|
65 |
setTimeout(checkWordSubmissionStatus, 2000);
|
66 |
} else if (data.status === 'game_started') {
|
67 |
startHangmanGame();
|
@@ -82,8 +84,8 @@ function checkWordSubmissionStatus() {
|
|
82 |
}
|
83 |
|
84 |
function startHangmanGame() {
|
85 |
-
|
86 |
-
|
87 |
updateGameState();
|
88 |
}
|
89 |
|
@@ -108,6 +110,8 @@ function makeGuess() {
|
|
108 |
.then(data => {
|
109 |
if (data.status === 'game_over') {
|
110 |
alert(data.winner === playerId ? 'You win!' : 'You lose!');
|
|
|
|
|
111 |
}
|
112 |
updateGameState();
|
113 |
});
|
@@ -135,14 +139,13 @@ function updateHangmanSVG(stage) {
|
|
135 |
});
|
136 |
}
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
}
|
148 |
}
|
|
|
4 |
|
5 |
document.addEventListener('DOMContentLoaded', (event) => {
|
6 |
const playButton = document.getElementById('playButton');
|
7 |
+
const submitWordButton = document.getElementById('submitWordButton');
|
8 |
+
const guessButton = document.getElementById('guessButton');
|
9 |
+
|
10 |
if (playButton) playButton.addEventListener('click', startGame);
|
11 |
+
if (submitWordButton) submitWordButton.addEventListener('click', submitWord);
|
12 |
+
if (guessButton) guessButton.addEventListener('click', makeGuess);
|
13 |
});
|
14 |
|
15 |
function startGame() {
|
|
|
19 |
sessionId = data.session_id;
|
20 |
playerId = data.player_id;
|
21 |
document.getElementById('sessionId').textContent = sessionId;
|
22 |
+
hideAllSections();
|
23 |
if (data.waiting) {
|
24 |
+
showSection('waitingMessage');
|
25 |
checkGameStatus();
|
26 |
} else {
|
27 |
+
showSection('wordInput');
|
28 |
}
|
29 |
});
|
30 |
}
|
|
|
34 |
.then(response => response.json())
|
35 |
.then(data => {
|
36 |
if (data.players === 2) {
|
37 |
+
hideAllSections();
|
38 |
+
showSection('gameStarting');
|
39 |
+
setTimeout(() => {
|
40 |
+
hideAllSections();
|
41 |
+
showSection('wordInput');
|
42 |
+
}, 2000);
|
43 |
} else {
|
44 |
setTimeout(checkGameStatus, 2000);
|
45 |
}
|
46 |
});
|
47 |
}
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
function submitWord() {
|
50 |
const word = document.getElementById('wordInputField').value;
|
51 |
if (word.length !== 7) {
|
|
|
61 |
})
|
62 |
.then(response => response.json())
|
63 |
.then(data => {
|
64 |
+
hideAllSections();
|
65 |
if (data.status === 'waiting_for_opponent') {
|
66 |
+
showSection('waitingForWord');
|
|
|
67 |
setTimeout(checkWordSubmissionStatus, 2000);
|
68 |
} else if (data.status === 'game_started') {
|
69 |
startHangmanGame();
|
|
|
84 |
}
|
85 |
|
86 |
function startHangmanGame() {
|
87 |
+
hideAllSections();
|
88 |
+
showSection('gameArea');
|
89 |
updateGameState();
|
90 |
}
|
91 |
|
|
|
110 |
.then(data => {
|
111 |
if (data.status === 'game_over') {
|
112 |
alert(data.winner === playerId ? 'You win!' : 'You lose!');
|
113 |
+
hideAllSections();
|
114 |
+
showSection('initialScreen');
|
115 |
}
|
116 |
updateGameState();
|
117 |
});
|
|
|
139 |
});
|
140 |
}
|
141 |
|
142 |
+
function hideAllSections() {
|
143 |
+
const sections = ['initialScreen', 'waitingMessage', 'gameStarting', 'wordInput', 'waitingForWord', 'gameArea'];
|
144 |
+
sections.forEach(section => {
|
145 |
+
document.getElementById(section).style.display = 'none';
|
146 |
+
});
|
147 |
+
}
|
148 |
+
|
149 |
+
function showSection(sectionId) {
|
150 |
+
document.getElementById(sectionId).style.display = 'block';
|
|
|
151 |
}
|