update block broadcasting and chat system

This commit is contained in:
jwinterm
2025-05-26 08:47:53 -04:00
parent ab67728fb2
commit eb2bdaa825
3 changed files with 188 additions and 52 deletions
+35 -23
View File
@@ -98,29 +98,41 @@
console.log("UI available:", typeof UI !== 'undefined');
console.log("InputHandler available:", typeof InputHandler !== 'undefined');
// Initialize Game
if (typeof Game !== 'undefined') {
console.log("Initializing Game...");
Game.init();
} else {
console.error("Game object not found!");
}
// Initialize SocketHandlers (CRITICAL for receiving game_start events!)
if (typeof SocketHandlers !== 'undefined') {
console.log("Initializing SocketHandlers...");
SocketHandlers.init();
} else {
console.error("SocketHandlers object not found!");
}
// Initialize InputHandler
if (typeof InputHandler !== 'undefined') {
console.log("Initializing InputHandler...");
InputHandler.init();
} else {
console.error("InputHandler object not found!");
}
// Small delay to ensure all modules are fully loaded
setTimeout(function() {
// Initialize Game first
if (typeof Game !== 'undefined') {
console.log("Initializing Game...");
const gameInitResult = Game.init();
console.log("Game initialization result:", gameInitResult);
// Ensure welcome screen is shown after a brief delay
setTimeout(function() {
if (gameInitResult && typeof ScreenManager !== 'undefined' && ScreenManager.drawWelcomeScreen) {
console.log("Ensuring welcome screen is drawn...");
ScreenManager.drawWelcomeScreen();
}
}, 50);
} else {
console.error("Game object not found!");
}
// Initialize SocketHandlers (CRITICAL for receiving game_start events!)
if (typeof SocketHandlers !== 'undefined') {
console.log("Initializing SocketHandlers...");
SocketHandlers.init();
} else {
console.error("SocketHandlers object not found!");
}
// Initialize InputHandler
if (typeof InputHandler !== 'undefined') {
console.log("Initializing InputHandler...");
InputHandler.init();
} else {
console.error("InputHandler object not found!");
}
}, 100); // 100ms delay to ensure everything is loaded
});
</script>
</body>
+46 -1
View File
@@ -25,6 +25,8 @@ const SocketHandlers = {
socket.on('connect', this.onConnect);
socket.on('welcome', this.onWelcome);
socket.on('message', this.onMessage);
socket.on('status_update', this.onStatusUpdate);
socket.on('chat_broadcast', this.onChatBroadcast);
// Game state handlers
socket.on('waiting_status', this.onWaitingStatus);
@@ -33,7 +35,7 @@ const SocketHandlers = {
socket.on('game_over', this.onGameOver);
socket.on('queue_cancelled', this.onQueueCancelled);
// Block height handler
// Block height handler (broadcast to all clients)
socket.on('blockheight', this.onBlockHeight);
},
@@ -59,6 +61,27 @@ const SocketHandlers = {
UI.scrollChat();
},
onStatusUpdate: function(data) {
// Handle player-specific status updates
console.log("📨 STATUS UPDATE received:", data);
// Display status message with appropriate styling
const statusClass = data.type === 'error' ? 'error' : 'status';
$('#messages').append($(`<li class="${statusClass}">`).text(data.message));
UI.scrollChat();
},
onChatBroadcast: function(data) {
// Handle chat messages broadcast to all players
console.log("💬 CHAT BROADCAST received:", data);
const timestamp = new Date(data.timestamp || Date.now()).toLocaleTimeString();
const chatMsg = `[${timestamp}] ${data.username || 'Anonymous'}: ${data.message}`;
$('#messages').append($('<li class="chat">').text(chatMsg));
UI.scrollChat();
},
onWaitingStatus: function(data) {
console.log("🕒 WAITING STATUS received:", data);
@@ -181,6 +204,28 @@ const SocketHandlers = {
if (Game) {
Game._gameActive = false;
}
// Auto-return to title screen after 30 seconds
console.log("⏰ Setting 30-second timer to return to title screen...");
setTimeout(() => {
console.log("⏰ 30 seconds elapsed - returning to title screen");
$('#messages').append($('<li style="color:#888;">').text("Returning to title screen..."));
UI.scrollChat();
// Return to welcome screen
if (Game && Game._drawWelcomeScreen) {
Game._drawWelcomeScreen();
}
// Reset game state
if (typeof GameState !== 'undefined') {
GameState.reset();
}
// Focus chat for new commands
$('#chatInput').focus();
UI.updateFocusIndicator();
}, 30000); // 30 seconds
}
UI.scrollChat();