Sergidev commited on
Commit
0e1ddb7
·
verified ·
1 Parent(s): 53da5e9

Update static/js/game.js

Browse files
Files changed (1) hide show
  1. static/js/game.js +40 -32
static/js/game.js CHANGED
@@ -1,48 +1,59 @@
1
  let sessionId = null;
2
- let playerIndex = 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
- playerIndex = data.player_index;
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
 
@@ -57,27 +68,23 @@ function submitWord() {
57
  headers: {
58
  'Content-Type': 'application/json',
59
  },
60
- body: JSON.stringify({ session_id: sessionId, player_index: playerIndex, 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');
@@ -88,26 +95,27 @@ function makeGuess() {
88
  headers: {
89
  'Content-Type': 'application/json',
90
  },
91
- body: JSON.stringify({ session_id: sessionId, player_index: playerIndex, guess: guess }),
92
  })
93
  .then(response => response.json())
94
  .then(data => {
95
  if (data.status === 'game_over') {
96
- alert(data.winner == playerIndex ? 'You win!' : 'You lose!');
97
  }
98
  updateGameState();
99
  });
100
  }
101
 
102
  function updateGameState() {
103
- fetch(`/game_state/${sessionId}/${playerIndex}`)
104
  .then(response => response.json())
105
  .then(data => {
106
- document.getElementById('playerWord').textContent = data.player_word;
107
- document.getElementById('opponentWord').textContent = data.opponent_word;
108
  document.getElementById('guesses').textContent = 'Guessed letters: ' + data.guesses.join(', ');
109
- isMyTurn = data.current_player_index == playerIndex;
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
 
1
  let sessionId = null;
2
+ let player = null;
 
3
 
4
  document.addEventListener('DOMContentLoaded', (event) => {
5
+ document.getElementById('playButton').addEventListener('click', createGame);
6
+ document.getElementById('joinButton').addEventListener('click', joinGame);
7
+ document.getElementById('submitWordButton').addEventListener('click', submitWord);
 
 
 
 
8
  });
9
 
10
+ function createGame() {
11
  fetch('/play', { method: 'POST' })
12
  .then(response => response.json())
13
  .then(data => {
14
  sessionId = data.session_id;
15
+ player = 'player1';
16
  document.getElementById('sessionId').textContent = sessionId;
17
+ document.getElementById('startOptions').style.display = 'none';
18
+ document.getElementById('gameInfo').style.display = 'block';
19
+ document.getElementById('statusMessage').textContent = 'Waiting for player 2...';
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(`/status/${sessionId}`)
45
  .then(response => response.json())
46
  .then(data => {
47
  if (data.status === 'ready') {
48
+ document.getElementById('statusMessage').textContent = 'Game is starting...';
49
+ startGame();
50
  } else {
51
  setTimeout(checkGameStatus, 2000); // Check again in 2 seconds
52
  }
53
  });
54
  }
55
 
56
+ function startGame() {
 
57
  document.getElementById('wordInput').style.display = 'block';
58
  }
59
 
 
68
  headers: {
69
  'Content-Type': 'application/json',
70
  },
71
+ body: JSON.stringify({ session_id: sessionId, player: player, word: word }),
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('statusMessage').textContent = 'Waiting for opponent to submit their word...';
 
78
  } else if (data.status === 'game_started') {
79
  document.getElementById('wordInput').style.display = 'none';
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
  headers: {
96
  'Content-Type': 'application/json',
97
  },
98
+ body: JSON.stringify({ session_id: sessionId, player: player, guess: guess }),
99
  })
100
  .then(response => response.json())
101
  .then(data => {
102
  if (data.status === 'game_over') {
103
+ alert(data.winner === player ? 'You win!' : 'You lose!');
104
  }
105
  updateGameState();
106
  });
107
  }
108
 
109
  function updateGameState() {
110
+ fetch(`/game_state/${sessionId}`)
111
  .then(response => response.json())
112
  .then(data => {
113
+ document.getElementById('playerWord').textContent = data.words[player];
114
+ document.getElementById('opponentWord').textContent = data.words[player === 'player1' ? 'player2' : 'player1'];
115
  document.getElementById('guesses').textContent = 'Guessed letters: ' + data.guesses.join(', ');
116
+ const isMyTurn = data.current_player === player;
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