Spaces:
Paused
Paused
Update static/js/game.js
Browse files- static/js/game.js +98 -110
static/js/game.js
CHANGED
@@ -2,133 +2,121 @@ let sessionId = null;
|
|
2 |
let playerId = null;
|
3 |
let isMyTurn = false;
|
4 |
|
5 |
-
document.addEventListener(
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
const guessButton = document.getElementById("guessButton");
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
if (guessButton) guessButton.addEventListener("click", makeGuess);
|
15 |
});
|
16 |
|
17 |
function startGame() {
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
}
|
29 |
|
30 |
-
function
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
document.getElementById("joinGame").style.display = "none";
|
41 |
-
document.getElementById("wordInput").style.display = "block";
|
42 |
-
}
|
43 |
-
});
|
44 |
}
|
45 |
|
46 |
-
function
|
47 |
-
|
48 |
-
|
49 |
-
alert("Word must be 7 letters long");
|
50 |
-
return;
|
51 |
-
}
|
52 |
-
fetch("/submit_word", {
|
53 |
-
method: "POST",
|
54 |
-
headers: {
|
55 |
-
"Content-Type": "application/json",
|
56 |
-
},
|
57 |
-
body: JSON.stringify({
|
58 |
-
session_id: sessionId,
|
59 |
-
player_id: playerId,
|
60 |
-
word: word,
|
61 |
-
}),
|
62 |
-
})
|
63 |
-
.then((response) => response.json())
|
64 |
-
.then((data) => {
|
65 |
-
if (data.status === "waiting_for_opponent") {
|
66 |
-
document.getElementById("wordInput").style.display = "none";
|
67 |
-
document.getElementById("waitingMessage").style.display = "block";
|
68 |
-
} else if (data.status === "game_started") {
|
69 |
-
startGamePlay();
|
70 |
-
}
|
71 |
-
});
|
72 |
}
|
73 |
|
74 |
-
function
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
}
|
79 |
|
80 |
function makeGuess() {
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
if (data.status === "game_over") {
|
104 |
-
alert(data.winner === playerId ? "You win!" : "You lose!");
|
105 |
-
}
|
106 |
-
updateGameState();
|
107 |
});
|
108 |
}
|
109 |
|
110 |
function updateGameState() {
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
: "Opponent's turn";
|
123 |
-
updateHangmanSVG(data.incorrect_guesses);
|
124 |
-
});
|
125 |
-
setTimeout(updateGameState, 2000); // Poll every 2 seconds
|
126 |
}
|
127 |
|
128 |
function updateHangmanSVG(stage) {
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
}
|
|
|
2 |
let playerId = null;
|
3 |
let isMyTurn = false;
|
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() {
|
16 |
+
fetch('/play', { method: 'POST' })
|
17 |
+
.then(response => response.json())
|
18 |
+
.then(data => {
|
19 |
+
sessionId = data.session_id;
|
20 |
+
playerId = data.player_id;
|
21 |
+
document.getElementById('sessionId').textContent = sessionId;
|
22 |
+
document.getElementById('sessionInfo').style.display = 'block';
|
23 |
+
document.getElementById('playButton').style.display = 'none';
|
24 |
+
if (data.status === 'ready') {
|
25 |
+
startGamePlay();
|
26 |
+
} else {
|
27 |
+
checkGameStatus();
|
28 |
+
}
|
29 |
+
});
|
30 |
}
|
31 |
|
32 |
+
function checkGameStatus() {
|
33 |
+
fetch(`/check_status/${sessionId}`)
|
34 |
+
.then(response => response.json())
|
35 |
+
.then(data => {
|
36 |
+
if (data.status === 'ready') {
|
37 |
+
startGamePlay();
|
38 |
+
} else {
|
39 |
+
setTimeout(checkGameStatus, 2000); // Check again in 2 seconds
|
40 |
+
}
|
41 |
+
});
|
|
|
|
|
|
|
|
|
42 |
}
|
43 |
|
44 |
+
function startGamePlay() {
|
45 |
+
document.getElementById('waitingMessage').style.display = 'none';
|
46 |
+
document.getElementById('wordInput').style.display = 'block';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
}
|
48 |
|
49 |
+
function submitWord() {
|
50 |
+
const word = document.getElementById('wordInputField').value;
|
51 |
+
if (word.length !== 7) {
|
52 |
+
alert('Word must be 7 letters long');
|
53 |
+
return;
|
54 |
+
}
|
55 |
+
fetch('/submit_word', {
|
56 |
+
method: 'POST',
|
57 |
+
headers: {
|
58 |
+
'Content-Type': 'application/json',
|
59 |
+
},
|
60 |
+
body: JSON.stringify({ session_id: sessionId, player_id: playerId, word: word }),
|
61 |
+
})
|
62 |
+
.then(response => response.json())
|
63 |
+
.then(data => {
|
64 |
+
if (data.status === 'waiting_for_opponent') {
|
65 |
+
document.getElementById('wordInput').style.display = 'none';
|
66 |
+
document.getElementById('waitingMessage').style.display = 'block';
|
67 |
+
document.getElementById('waitingMessage').textContent = 'Waiting for opponent to submit their word...';
|
68 |
+
} else if (data.status === 'game_started') {
|
69 |
+
document.getElementById('wordInput').style.display = 'none';
|
70 |
+
document.getElementById('gameArea').style.display = 'block';
|
71 |
+
updateGameState();
|
72 |
+
}
|
73 |
+
});
|
74 |
}
|
75 |
|
76 |
function makeGuess() {
|
77 |
+
if (!isMyTurn) {
|
78 |
+
alert("It's not your turn!");
|
79 |
+
return;
|
80 |
+
}
|
81 |
+
const guess = document.getElementById('guessInput').value;
|
82 |
+
if (guess.length !== 1) {
|
83 |
+
alert('Please enter a single letter');
|
84 |
+
return;
|
85 |
+
}
|
86 |
+
fetch('/guess', {
|
87 |
+
method: 'POST',
|
88 |
+
headers: {
|
89 |
+
'Content-Type': 'application/json',
|
90 |
+
},
|
91 |
+
body: JSON.stringify({ session_id: sessionId, player_id: playerId, guess: guess }),
|
92 |
+
})
|
93 |
+
.then(response => response.json())
|
94 |
+
.then(data => {
|
95 |
+
if (data.status === 'game_over') {
|
96 |
+
alert(data.winner === playerId ? 'You win!' : 'You lose!');
|
97 |
+
}
|
98 |
+
updateGameState();
|
|
|
|
|
|
|
|
|
99 |
});
|
100 |
}
|
101 |
|
102 |
function updateGameState() {
|
103 |
+
fetch(`/game_state/${sessionId}`)
|
104 |
+
.then(response => response.json())
|
105 |
+
.then(data => {
|
106 |
+
document.getElementById('playerWord').textContent = data.words[playerId];
|
107 |
+
document.getElementById('opponentWord').textContent = data.words[data.players.find(p => p !== playerId)];
|
108 |
+
document.getElementById('guesses').textContent = 'Guessed letters: ' + data.guesses.join(', ');
|
109 |
+
isMyTurn = data.current_player === playerId;
|
110 |
+
document.getElementById('playerTurn').textContent = isMyTurn ? 'Your turn' : "Opponent's turn";
|
111 |
+
updateHangmanSVG(data.incorrect_guesses);
|
112 |
+
});
|
113 |
+
setTimeout(updateGameState, 2000); // Poll every 2 seconds
|
|
|
|
|
|
|
|
|
114 |
}
|
115 |
|
116 |
function updateHangmanSVG(stage) {
|
117 |
+
fetch(`/static/images/hangman-stage-${stage + 1}.svg`)
|
118 |
+
.then(response => response.text())
|
119 |
+
.then(svgContent => {
|
120 |
+
document.getElementById('hangmanSvg').innerHTML = svgContent;
|
121 |
+
});
|
122 |
+
}
|