Sergidev commited on
Commit
53da5e9
·
verified ·
1 Parent(s): 70a9ad8

Update static/js/game.js

Browse files
Files changed (1) hide show
  1. static/js/game.js +12 -44
static/js/game.js CHANGED
@@ -1,5 +1,5 @@
1
  let sessionId = null;
2
- let playerId = null;
3
  let isMyTurn = false;
4
 
5
  document.addEventListener('DOMContentLoaded', (event) => {
@@ -17,12 +17,12 @@ function startGame() {
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
- startWordInput();
26
  } else {
27
  checkGameStatus();
28
  }
@@ -34,48 +34,16 @@ function checkGameStatus() {
34
  .then(response => response.json())
35
  .then(data => {
36
  if (data.status === 'ready') {
37
- startWordInput();
38
  } else {
39
  setTimeout(checkGameStatus, 2000); // Check again in 2 seconds
40
  }
41
  });
42
  }
43
 
44
- function startWordInput() {
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
- startGamePlay();
70
- }
71
- });
72
- }
73
-
74
  function startGamePlay() {
75
  document.getElementById('waitingMessage').style.display = 'none';
76
- document.getElementById('wordInput').style.display = 'none';
77
- document.getElementById('gameArea').style.display = 'block';
78
- updateGameState();
79
  }
80
 
81
  function submitWord() {
@@ -89,7 +57,7 @@ function submitWord() {
89
  headers: {
90
  'Content-Type': 'application/json',
91
  },
92
- body: JSON.stringify({ session_id: sessionId, player_id: playerId, word: word }),
93
  })
94
  .then(response => response.json())
95
  .then(data => {
@@ -120,25 +88,25 @@ function makeGuess() {
120
  headers: {
121
  'Content-Type': 'application/json',
122
  },
123
- body: JSON.stringify({ session_id: sessionId, player_id: playerId, guess: guess }),
124
  })
125
  .then(response => response.json())
126
  .then(data => {
127
  if (data.status === 'game_over') {
128
- alert(data.winner === playerId ? 'You win!' : 'You lose!');
129
  }
130
  updateGameState();
131
  });
132
  }
133
 
134
  function updateGameState() {
135
- fetch(`/game_state/${sessionId}`)
136
  .then(response => response.json())
137
  .then(data => {
138
- document.getElementById('playerWord').textContent = data.words[playerId];
139
- document.getElementById('opponentWord').textContent = data.words[data.players.find(p => p !== playerId)];
140
  document.getElementById('guesses').textContent = 'Guessed letters: ' + data.guesses.join(', ');
141
- isMyTurn = data.current_player === playerId;
142
  document.getElementById('playerTurn').textContent = isMyTurn ? 'Your turn' : "Opponent's turn";
143
  updateHangmanSVG(data.incorrect_guesses);
144
  });
 
1
  let sessionId = null;
2
+ let playerIndex = null;
3
  let isMyTurn = false;
4
 
5
  document.addEventListener('DOMContentLoaded', (event) => {
 
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
  }
 
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() {
 
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 => {
 
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
  });