dungeon_game / static /scripts.js
broadfield-dev's picture
Update static/scripts.js
3059061 verified
raw
history blame
1.37 kB
const socket = io("https://broadfield-dev-dungeon-game.hf.space", {
transports: ['websocket'] // Force WebSocket transport
});
socket.on('connect', () => {
console.log('Connected to the server! Socket ID:', socket.id);
});
socket.on('connect_error', (error) => {
console.error('Connection error:', error);
});
socket.on('disconnect', (reason) => {
console.log('Disconnected from the server. Reason:', reason);
});
socket.on('update_game', (state) => {
console.log('Received game state:', state);
window.currentState = state;
if (allImagesLoaded) drawGame(state);
else console.log('Waiting for images to load...');
document.getElementById('health').textContent = state.health;
document.getElementById('attack').textContent = state.attack;
document.getElementById('inventory').textContent = state.inventory.join(', ');
document.getElementById('level').textContent = state.level;
document.getElementById('usePotion').disabled = !state.inventory.includes('potion');
if (state.health <= 0) alert('Game Over! Refresh to restart.');
});
socket.on('message', (msg) => {
console.log('Received message:', msg); // Log messages to ensure they’re working
const messagesDiv = document.getElementById('messages');
messagesDiv.innerHTML += `<p>${msg}</p>`;
messagesDiv.scrollTop = messagesDiv.scrollHeight;
});