Update static/js/game.js
Browse files- static/js/game.js +86 -75
static/js/game.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
// Initialize Socket.IO connection
|
2 |
const socket = io({
|
3 |
reconnection: true,
|
4 |
reconnectionAttempts: 5,
|
@@ -17,14 +17,11 @@ class Game {
|
|
17 |
recordingTime: 30,
|
18 |
listeningTime: 60,
|
19 |
votingTime: 60,
|
20 |
-
recordings: new Map(),
|
21 |
-
votes: new Map(),
|
22 |
-
impostor: null,
|
23 |
maxPlayers: 5,
|
24 |
minPlayers: 3
|
25 |
};
|
26 |
|
27 |
-
// Wait for DOM to be
|
28 |
if (document.readyState === 'loading') {
|
29 |
document.addEventListener('DOMContentLoaded', () => this.initialize());
|
30 |
} else {
|
@@ -33,47 +30,19 @@ class Game {
|
|
33 |
}
|
34 |
|
35 |
initialize() {
|
36 |
-
console.log('Initializing game...');
|
37 |
this.initializeEventListeners();
|
38 |
this.bindUIElements();
|
39 |
this.initializeSocketHandlers();
|
40 |
this.showPage('landing');
|
41 |
}
|
42 |
|
43 |
-
initializeEventListeners() {
|
44 |
-
// Handle play button click
|
45 |
-
const playButton = document.getElementById('play-button');
|
46 |
-
if (playButton) {
|
47 |
-
playButton.addEventListener('click', () => {
|
48 |
-
console.log('Play button clicked');
|
49 |
-
this.handlePlayButton();
|
50 |
-
});
|
51 |
-
}
|
52 |
-
|
53 |
-
// Add player button handler
|
54 |
-
const addPlayerButton = document.getElementById('add-player-button');
|
55 |
-
if (addPlayerButton) {
|
56 |
-
addPlayerButton.addEventListener('click', () => {
|
57 |
-
console.log('Add player button clicked');
|
58 |
-
this.addPlayer();
|
59 |
-
});
|
60 |
-
}
|
61 |
-
|
62 |
-
// Start game button handler
|
63 |
-
const startButton = document.getElementById('start-button');
|
64 |
-
if (startButton) {
|
65 |
-
startButton.addEventListener('click', () => this.startGame());
|
66 |
-
}
|
67 |
-
}
|
68 |
-
|
69 |
bindUIElements() {
|
70 |
-
// Cache frequently used elements
|
71 |
this.ui = {
|
72 |
gameContainer: document.getElementById('game-container'),
|
73 |
pages: {
|
74 |
landing: document.getElementById('landing-page'),
|
75 |
setup: document.getElementById('setup-page'),
|
76 |
-
game: document.getElementById('game-page'),
|
77 |
recording: document.getElementById('recording-page'),
|
78 |
listening: document.getElementById('listening-page'),
|
79 |
voting: document.getElementById('voting-page'),
|
@@ -82,13 +51,29 @@ class Game {
|
|
82 |
buttons: {
|
83 |
play: document.getElementById('play-button'),
|
84 |
start: document.getElementById('start-button'),
|
85 |
-
addPlayer: document.getElementById('add-player-button')
|
86 |
-
record: document.getElementById('record-button')
|
87 |
},
|
88 |
playerList: document.getElementById('player-list')
|
89 |
};
|
90 |
}
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
initializeSocketHandlers() {
|
93 |
socket.on('connect', () => {
|
94 |
console.log('Connected to server');
|
@@ -98,17 +83,16 @@ class Game {
|
|
98 |
this.handleGameCreated(data);
|
99 |
});
|
100 |
|
101 |
-
socket.on('
|
102 |
-
this.
|
103 |
});
|
104 |
}
|
105 |
|
106 |
async handlePlayButton() {
|
107 |
-
console.log('
|
108 |
try {
|
109 |
await this.createGame();
|
110 |
this.showPage('setup');
|
111 |
-
// Add default players after transitioning to setup page
|
112 |
this.addDefaultPlayers();
|
113 |
} catch (error) {
|
114 |
console.error('Error starting game:', error);
|
@@ -117,34 +101,33 @@ class Game {
|
|
117 |
}
|
118 |
|
119 |
addDefaultPlayers() {
|
|
|
|
|
120 |
// Add two default players
|
121 |
-
this.addPlayer(
|
122 |
-
this.addPlayer(
|
123 |
this.updatePlayerControls();
|
124 |
}
|
125 |
|
126 |
-
addPlayer(
|
127 |
if (this.state.players.length >= this.state.maxPlayers) {
|
128 |
this.handleError('Maximum player limit reached');
|
129 |
return;
|
130 |
}
|
131 |
|
132 |
-
const
|
133 |
-
if (!playerName) return;
|
134 |
-
|
135 |
const newPlayer = {
|
136 |
-
id:
|
137 |
-
name:
|
138 |
};
|
139 |
|
140 |
this.state.players.push(newPlayer);
|
141 |
this.updatePlayerList();
|
142 |
this.updatePlayerControls();
|
143 |
|
144 |
-
// Emit player joined event
|
145 |
socket.emit('join_game', {
|
146 |
gameId: this.state.gameId,
|
147 |
-
playerName:
|
148 |
});
|
149 |
}
|
150 |
|
@@ -152,34 +135,37 @@ class Game {
|
|
152 |
if (!this.ui.playerList) return;
|
153 |
|
154 |
this.ui.playerList.innerHTML = '';
|
|
|
|
|
|
|
|
|
|
|
155 |
this.state.players.forEach(player => {
|
156 |
const playerElement = document.createElement('div');
|
157 |
-
playerElement.className = 'player-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
playerElement.appendChild(playerName);
|
171 |
-
this.ui.playerList.appendChild(playerElement);
|
172 |
});
|
|
|
|
|
173 |
}
|
174 |
|
175 |
updatePlayerControls() {
|
176 |
-
// Update add player button state
|
177 |
if (this.ui.buttons.addPlayer) {
|
178 |
this.ui.buttons.addPlayer.disabled =
|
179 |
this.state.players.length >= this.state.maxPlayers;
|
180 |
}
|
181 |
|
182 |
-
// Update start game button state
|
183 |
if (this.ui.buttons.start) {
|
184 |
this.ui.buttons.start.disabled =
|
185 |
this.state.players.length < this.state.minPlayers;
|
@@ -200,17 +186,47 @@ class Game {
|
|
200 |
this.state.gameId = data.gameId;
|
201 |
}
|
202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
showPage(pageName) {
|
204 |
console.log('Showing page:', pageName);
|
205 |
|
206 |
-
// Hide all pages
|
207 |
Object.values(this.ui.pages).forEach(page => {
|
208 |
if (page) {
|
209 |
page.classList.remove('active');
|
210 |
}
|
211 |
});
|
212 |
|
213 |
-
// Show requested page
|
214 |
const pageToShow = this.ui.pages[pageName];
|
215 |
if (pageToShow) {
|
216 |
pageToShow.classList.add('active');
|
@@ -220,20 +236,15 @@ class Game {
|
|
220 |
|
221 |
handleError(message) {
|
222 |
console.error(message);
|
223 |
-
// Create error message element
|
224 |
const errorElement = document.createElement('div');
|
225 |
errorElement.className = 'error-message';
|
226 |
errorElement.textContent = message;
|
227 |
-
|
228 |
-
// Add to container and remove after delay
|
229 |
this.ui.gameContainer.appendChild(errorElement);
|
230 |
setTimeout(() => errorElement.remove(), 3000);
|
231 |
}
|
232 |
-
|
233 |
-
// Rest of the Game class implementation remains the same...
|
234 |
}
|
235 |
|
236 |
-
// Initialize the game
|
237 |
document.addEventListener('DOMContentLoaded', () => {
|
238 |
window.game = new Game();
|
239 |
});
|
|
|
1 |
+
// Initialize Socket.IO connection
|
2 |
const socket = io({
|
3 |
reconnection: true,
|
4 |
reconnectionAttempts: 5,
|
|
|
17 |
recordingTime: 30,
|
18 |
listeningTime: 60,
|
19 |
votingTime: 60,
|
|
|
|
|
|
|
20 |
maxPlayers: 5,
|
21 |
minPlayers: 3
|
22 |
};
|
23 |
|
24 |
+
// Wait for DOM to be loaded
|
25 |
if (document.readyState === 'loading') {
|
26 |
document.addEventListener('DOMContentLoaded', () => this.initialize());
|
27 |
} else {
|
|
|
30 |
}
|
31 |
|
32 |
initialize() {
|
33 |
+
console.log('Initializing game...');
|
34 |
this.initializeEventListeners();
|
35 |
this.bindUIElements();
|
36 |
this.initializeSocketHandlers();
|
37 |
this.showPage('landing');
|
38 |
}
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
bindUIElements() {
|
|
|
41 |
this.ui = {
|
42 |
gameContainer: document.getElementById('game-container'),
|
43 |
pages: {
|
44 |
landing: document.getElementById('landing-page'),
|
45 |
setup: document.getElementById('setup-page'),
|
|
|
46 |
recording: document.getElementById('recording-page'),
|
47 |
listening: document.getElementById('listening-page'),
|
48 |
voting: document.getElementById('voting-page'),
|
|
|
51 |
buttons: {
|
52 |
play: document.getElementById('play-button'),
|
53 |
start: document.getElementById('start-button'),
|
54 |
+
addPlayer: document.getElementById('add-player-button')
|
|
|
55 |
},
|
56 |
playerList: document.getElementById('player-list')
|
57 |
};
|
58 |
}
|
59 |
|
60 |
+
initializeEventListeners() {
|
61 |
+
// Play button handler
|
62 |
+
if (this.ui?.buttons.play) {
|
63 |
+
this.ui.buttons.play.addEventListener('click', () => this.handlePlayButton());
|
64 |
+
}
|
65 |
+
|
66 |
+
// Add player button handler
|
67 |
+
if (this.ui?.buttons.addPlayer) {
|
68 |
+
this.ui.buttons.addPlayer.addEventListener('click', () => this.addPlayer());
|
69 |
+
}
|
70 |
+
|
71 |
+
// Start game button handler
|
72 |
+
if (this.ui?.buttons.start) {
|
73 |
+
this.ui.buttons.start.addEventListener('click', () => this.startGame());
|
74 |
+
}
|
75 |
+
}
|
76 |
+
|
77 |
initializeSocketHandlers() {
|
78 |
socket.on('connect', () => {
|
79 |
console.log('Connected to server');
|
|
|
83 |
this.handleGameCreated(data);
|
84 |
});
|
85 |
|
86 |
+
socket.on('round_start', (data) => {
|
87 |
+
this.handleRoundStart(data);
|
88 |
});
|
89 |
}
|
90 |
|
91 |
async handlePlayButton() {
|
92 |
+
console.log('Play button clicked');
|
93 |
try {
|
94 |
await this.createGame();
|
95 |
this.showPage('setup');
|
|
|
96 |
this.addDefaultPlayers();
|
97 |
} catch (error) {
|
98 |
console.error('Error starting game:', error);
|
|
|
101 |
}
|
102 |
|
103 |
addDefaultPlayers() {
|
104 |
+
// Clear existing players
|
105 |
+
this.state.players = [];
|
106 |
// Add two default players
|
107 |
+
this.addPlayer();
|
108 |
+
this.addPlayer();
|
109 |
this.updatePlayerControls();
|
110 |
}
|
111 |
|
112 |
+
addPlayer() {
|
113 |
if (this.state.players.length >= this.state.maxPlayers) {
|
114 |
this.handleError('Maximum player limit reached');
|
115 |
return;
|
116 |
}
|
117 |
|
118 |
+
const playerNumber = this.state.players.length + 1;
|
|
|
|
|
119 |
const newPlayer = {
|
120 |
+
id: playerNumber,
|
121 |
+
name: `Player ${playerNumber}`
|
122 |
};
|
123 |
|
124 |
this.state.players.push(newPlayer);
|
125 |
this.updatePlayerList();
|
126 |
this.updatePlayerControls();
|
127 |
|
|
|
128 |
socket.emit('join_game', {
|
129 |
gameId: this.state.gameId,
|
130 |
+
playerName: newPlayer.name
|
131 |
});
|
132 |
}
|
133 |
|
|
|
135 |
if (!this.ui.playerList) return;
|
136 |
|
137 |
this.ui.playerList.innerHTML = '';
|
138 |
+
|
139 |
+
// Create player grid container
|
140 |
+
const gridContainer = document.createElement('div');
|
141 |
+
gridContainer.className = 'player-grid';
|
142 |
+
|
143 |
this.state.players.forEach(player => {
|
144 |
const playerElement = document.createElement('div');
|
145 |
+
playerElement.className = 'player-card';
|
146 |
+
|
147 |
+
const circle = document.createElement('div');
|
148 |
+
circle.className = 'player-circle';
|
149 |
+
circle.textContent = player.id;
|
150 |
+
|
151 |
+
const name = document.createElement('div');
|
152 |
+
name.className = 'player-name';
|
153 |
+
name.textContent = player.name;
|
154 |
+
|
155 |
+
playerElement.appendChild(circle);
|
156 |
+
playerElement.appendChild(name);
|
157 |
+
gridContainer.appendChild(playerElement);
|
|
|
|
|
158 |
});
|
159 |
+
|
160 |
+
this.ui.playerList.appendChild(gridContainer);
|
161 |
}
|
162 |
|
163 |
updatePlayerControls() {
|
|
|
164 |
if (this.ui.buttons.addPlayer) {
|
165 |
this.ui.buttons.addPlayer.disabled =
|
166 |
this.state.players.length >= this.state.maxPlayers;
|
167 |
}
|
168 |
|
|
|
169 |
if (this.ui.buttons.start) {
|
170 |
this.ui.buttons.start.disabled =
|
171 |
this.state.players.length < this.state.minPlayers;
|
|
|
186 |
this.state.gameId = data.gameId;
|
187 |
}
|
188 |
|
189 |
+
async startGame() {
|
190 |
+
console.log('Starting game...');
|
191 |
+
try {
|
192 |
+
const response = await fetch('/api/start_game', {
|
193 |
+
method: 'POST',
|
194 |
+
headers: {
|
195 |
+
'Content-Type': 'application/json'
|
196 |
+
},
|
197 |
+
body: JSON.stringify({
|
198 |
+
gameId: this.state.gameId
|
199 |
+
})
|
200 |
+
});
|
201 |
+
|
202 |
+
const data = await response.json();
|
203 |
+
if (data.status === 'success') {
|
204 |
+
this.handleRoundStart(data);
|
205 |
+
} else {
|
206 |
+
throw new Error(data.error || 'Failed to start game');
|
207 |
+
}
|
208 |
+
} catch (error) {
|
209 |
+
console.error('Error starting game:', error);
|
210 |
+
this.handleError('Failed to start game');
|
211 |
+
}
|
212 |
+
}
|
213 |
+
|
214 |
+
handleRoundStart(data) {
|
215 |
+
console.log('Round starting:', data);
|
216 |
+
this.state.currentQuestion = data.question;
|
217 |
+
this.state.currentPhase = 'recording';
|
218 |
+
this.showPage('recording');
|
219 |
+
}
|
220 |
+
|
221 |
showPage(pageName) {
|
222 |
console.log('Showing page:', pageName);
|
223 |
|
|
|
224 |
Object.values(this.ui.pages).forEach(page => {
|
225 |
if (page) {
|
226 |
page.classList.remove('active');
|
227 |
}
|
228 |
});
|
229 |
|
|
|
230 |
const pageToShow = this.ui.pages[pageName];
|
231 |
if (pageToShow) {
|
232 |
pageToShow.classList.add('active');
|
|
|
236 |
|
237 |
handleError(message) {
|
238 |
console.error(message);
|
|
|
239 |
const errorElement = document.createElement('div');
|
240 |
errorElement.className = 'error-message';
|
241 |
errorElement.textContent = message;
|
|
|
|
|
242 |
this.ui.gameContainer.appendChild(errorElement);
|
243 |
setTimeout(() => errorElement.remove(), 3000);
|
244 |
}
|
|
|
|
|
245 |
}
|
246 |
|
247 |
+
// Initialize the game
|
248 |
document.addEventListener('DOMContentLoaded', () => {
|
249 |
window.game = new Game();
|
250 |
});
|