Sergidev commited on
Commit
07bb9cd
·
verified ·
1 Parent(s): c6626c3

Update static/js/game.js

Browse files
Files changed (1) hide show
  1. 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("DOMContentLoaded", (event) => {
6
- const playButton = document.getElementById("playButton");
7
- const joinButton = document.getElementById("joinButton");
8
- const submitWordButton = document.getElementById("submitWordButton");
9
- const guessButton = document.getElementById("guessButton");
10
 
11
- if (playButton) playButton.addEventListener("click", startGame);
12
- if (joinButton) joinButton.addEventListener("click", joinGame);
13
- if (submitWordButton) submitWordButton.addEventListener("click", submitWord);
14
- if (guessButton) guessButton.addEventListener("click", makeGuess);
15
  });
16
 
17
  function startGame() {
18
- fetch("/play", { method: "POST" })
19
- .then((response) => response.json())
20
- .then((data) => {
21
- sessionId = data.session_id;
22
- playerId = data.player_id;
23
- document.getElementById("sessionId").textContent = sessionId;
24
- document.getElementById("sessionInfo").style.display = "block";
25
- document.getElementById("playButton").style.display = "none";
26
- document.getElementById("wordInput").style.display = "block";
27
- });
 
 
 
 
28
  }
29
 
30
- function joinGame() {
31
- const joinSessionId = document.getElementById("joinSessionId").value;
32
- fetch(`/join/${joinSessionId}`, { method: "POST" })
33
- .then((response) => response.json())
34
- .then((data) => {
35
- if (data.error) {
36
- alert(data.error);
37
- } else {
38
- sessionId = joinSessionId;
39
- playerId = data.player_id;
40
- document.getElementById("joinGame").style.display = "none";
41
- document.getElementById("wordInput").style.display = "block";
42
- }
43
- });
44
  }
45
 
46
- function submitWord() {
47
- const word = document.getElementById("wordInputField").value;
48
- if (word.length !== 7) {
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 startGamePlay() {
75
- document.getElementById("waitingMessage").style.display = "none";
76
- document.getElementById("gameArea").style.display = "block";
77
- updateGameState();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  }
79
 
80
  function makeGuess() {
81
- if (!isMyTurn) {
82
- alert("It's not your turn!");
83
- return;
84
- }
85
- const guess = document.getElementById("guessInput").value;
86
- if (guess.length !== 1) {
87
- alert("Please enter a single letter");
88
- return;
89
- }
90
- fetch("/guess", {
91
- method: "POST",
92
- headers: {
93
- "Content-Type": "application/json",
94
- },
95
- body: JSON.stringify({
96
- session_id: sessionId,
97
- player_id: playerId,
98
- guess: guess,
99
- }),
100
- })
101
- .then((response) => response.json())
102
- .then((data) => {
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
- fetch(`/game_state/${sessionId}`)
112
- .then((response) => response.json())
113
- .then((data) => {
114
- document.getElementById("playerWord").textContent = data.words[playerId];
115
- document.getElementById("opponentWord").textContent =
116
- data.words[data.players.find((p) => p !== playerId)];
117
- document.getElementById("guesses").textContent =
118
- "Guessed letters: " + data.guesses.join(", ");
119
- isMyTurn = data.current_player === playerId;
120
- document.getElementById("playerTurn").textContent = isMyTurn
121
- ? "Your turn"
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
- fetch(`/static/images/hangman-stage-${stage + 1}.svg`)
130
- .then((response) => response.text())
131
- .then((svgContent) => {
132
- document.getElementById("hangmanSvg").innerHTML = svgContent;
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
+ }