Update static/js/game.js
Browse files- static/js/game.js +108 -17
static/js/game.js
CHANGED
@@ -11,7 +11,6 @@ class Game {
|
|
11 |
this.state = {
|
12 |
gameId: null,
|
13 |
currentPhase: 'landing',
|
14 |
-
playerId: null,
|
15 |
players: [],
|
16 |
currentQuestion: null,
|
17 |
isRecording: false,
|
@@ -20,7 +19,9 @@ class Game {
|
|
20 |
votingTime: 60,
|
21 |
recordings: new Map(),
|
22 |
votes: new Map(),
|
23 |
-
impostor: null
|
|
|
|
|
24 |
};
|
25 |
|
26 |
// Wait for DOM to be fully loaded before initializing
|
@@ -32,14 +33,10 @@ class Game {
|
|
32 |
}
|
33 |
|
34 |
initialize() {
|
35 |
-
|
36 |
this.initializeEventListeners();
|
37 |
this.bindUIElements();
|
38 |
-
|
39 |
-
// Initialize Socket.IO event handlers
|
40 |
this.initializeSocketHandlers();
|
41 |
-
|
42 |
-
// Show the landing page
|
43 |
this.showPage('landing');
|
44 |
}
|
45 |
|
@@ -48,14 +45,25 @@ class Game {
|
|
48 |
const playButton = document.getElementById('play-button');
|
49 |
if (playButton) {
|
50 |
playButton.addEventListener('click', () => {
|
51 |
-
console.log('Play button clicked');
|
52 |
this.handlePlayButton();
|
53 |
});
|
54 |
}
|
55 |
|
56 |
-
//
|
57 |
-
document.getElementById('
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
}
|
60 |
|
61 |
bindUIElements() {
|
@@ -76,7 +84,8 @@ class Game {
|
|
76 |
start: document.getElementById('start-button'),
|
77 |
addPlayer: document.getElementById('add-player-button'),
|
78 |
record: document.getElementById('record-button')
|
79 |
-
}
|
|
|
80 |
};
|
81 |
}
|
82 |
|
@@ -95,18 +104,88 @@ class Game {
|
|
95 |
}
|
96 |
|
97 |
async handlePlayButton() {
|
98 |
-
console.log('Handling play button click');
|
99 |
try {
|
100 |
-
// Create new game session
|
101 |
await this.createGame();
|
102 |
-
// Show the setup page
|
103 |
this.showPage('setup');
|
|
|
|
|
104 |
} catch (error) {
|
105 |
console.error('Error starting game:', error);
|
106 |
this.handleError('Failed to start game');
|
107 |
}
|
108 |
}
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
async createGame() {
|
111 |
try {
|
112 |
socket.emit('create_game');
|
@@ -117,12 +196,12 @@ class Game {
|
|
117 |
}
|
118 |
|
119 |
handleGameCreated(data) {
|
120 |
-
console.log('Game created:', data);
|
121 |
this.state.gameId = data.gameId;
|
122 |
}
|
123 |
|
124 |
showPage(pageName) {
|
125 |
-
console.log('Showing page:', pageName);
|
126 |
|
127 |
// Hide all pages
|
128 |
Object.values(this.ui.pages).forEach(page => {
|
@@ -139,6 +218,18 @@ class Game {
|
|
139 |
}
|
140 |
}
|
141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
// Rest of the Game class implementation remains the same...
|
143 |
}
|
144 |
|
|
|
11 |
this.state = {
|
12 |
gameId: null,
|
13 |
currentPhase: 'landing',
|
|
|
14 |
players: [],
|
15 |
currentQuestion: null,
|
16 |
isRecording: false,
|
|
|
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 fully loaded before initializing
|
|
|
33 |
}
|
34 |
|
35 |
initialize() {
|
36 |
+
console.log('Initializing game...'); // Debug log
|
37 |
this.initializeEventListeners();
|
38 |
this.bindUIElements();
|
|
|
|
|
39 |
this.initializeSocketHandlers();
|
|
|
|
|
40 |
this.showPage('landing');
|
41 |
}
|
42 |
|
|
|
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() {
|
|
|
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 |
|
|
|
104 |
}
|
105 |
|
106 |
async handlePlayButton() {
|
107 |
+
console.log('Handling play button click');
|
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);
|
115 |
this.handleError('Failed to start game');
|
116 |
}
|
117 |
}
|
118 |
|
119 |
+
addDefaultPlayers() {
|
120 |
+
// Add two default players
|
121 |
+
this.addPlayer('Player 1');
|
122 |
+
this.addPlayer('Player 2');
|
123 |
+
this.updatePlayerControls();
|
124 |
+
}
|
125 |
+
|
126 |
+
addPlayer(defaultName = null) {
|
127 |
+
if (this.state.players.length >= this.state.maxPlayers) {
|
128 |
+
this.handleError('Maximum player limit reached');
|
129 |
+
return;
|
130 |
+
}
|
131 |
+
|
132 |
+
const playerName = defaultName || prompt('Enter player name:');
|
133 |
+
if (!playerName) return;
|
134 |
+
|
135 |
+
const newPlayer = {
|
136 |
+
id: this.state.players.length + 1,
|
137 |
+
name: playerName
|
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: playerName
|
148 |
+
});
|
149 |
+
}
|
150 |
+
|
151 |
+
updatePlayerList() {
|
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-avatar';
|
158 |
+
|
159 |
+
// Create avatar circle with player number
|
160 |
+
const avatarCircle = document.createElement('div');
|
161 |
+
avatarCircle.className = 'avatar-circle';
|
162 |
+
avatarCircle.textContent = player.id;
|
163 |
+
|
164 |
+
// Create player name display
|
165 |
+
const playerName = document.createElement('div');
|
166 |
+
playerName.className = 'player-name';
|
167 |
+
playerName.textContent = player.name;
|
168 |
+
|
169 |
+
playerElement.appendChild(avatarCircle);
|
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;
|
186 |
+
}
|
187 |
+
}
|
188 |
+
|
189 |
async createGame() {
|
190 |
try {
|
191 |
socket.emit('create_game');
|
|
|
196 |
}
|
197 |
|
198 |
handleGameCreated(data) {
|
199 |
+
console.log('Game created:', data);
|
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 => {
|
|
|
218 |
}
|
219 |
}
|
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 |
|