serverplace / src /socket /socketHandler.ts
3v324v23's picture
Update Server: Fix Vote Bug, Memory Leak, and Role Reveal
05d8628
import { Server } from 'socket.io';
import { ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData } from '../types';
import GameManager from '../managers/GameManager';
import { sanitizeRoomState } from '../utils/sanitizer';
export const setupSocketHandlers = (io: Server<ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData>) => {
// Initialize IO in GameManager
GameManager.setIo(io);
io.on('connection', (socket) => {
console.log(`User connected: ${socket.id}`);
// --- LOBBY ---
socket.on('create_room', ({ name, isPublic, gameType, hostUser, settings }, callback) => {
try {
if (!name || !hostUser || !hostUser.userId) {
socket.emit('error_message', 'Invalid room data.');
return;
}
const room = GameManager.createRoom(name, isPublic, gameType, hostUser, socket.id, settings);
socket.join(room.id);
socket.data.roomId = room.id;
socket.data.userId = hostUser.userId;
if (typeof callback === 'function') callback(room.id);
if (isPublic) {
io.emit('room_list_update', GameManager.getPublicRooms());
}
} catch (e) {
console.error("Error creating room:", e);
socket.emit('error_message', 'Failed to create room.');
}
});
socket.on('join_room', ({ roomId, user }) => {
try {
if (!roomId || !user || !user.userId) {
socket.emit('error_message', 'Invalid join request.');
return;
}
const room = GameManager.joinRoom(roomId, user, socket.id);
if (room) {
socket.join(roomId);
socket.data.roomId = roomId;
socket.data.userId = user.userId;
socket.emit('room_state_update', sanitizeRoomState(room, socket.id));
const clients = io.sockets.adapter.rooms.get(roomId);
if (clients) {
for (const clientId of clients) {
const clientSocket = io.sockets.sockets.get(clientId);
if (clientSocket) {
clientSocket.emit('room_state_update', sanitizeRoomState(room, clientId));
}
}
}
if (room.isPublic) {
io.emit('room_list_update', GameManager.getPublicRooms());
}
} else {
socket.emit('error_message', 'Room not found, full, or game in progress.');
}
} catch (e) {
console.error("Error joining room:", e);
socket.emit('error_message', 'Failed to join room.');
}
});
socket.on('get_rooms', () => {
socket.emit('room_list_update', GameManager.getPublicRooms());
});
// --- GAME ACTIONS ---
socket.on('start_game', (roomId) => {
try {
if (!roomId) return;
GameManager.startGame(roomId, socket.id);
io.emit('room_list_update', GameManager.getPublicRooms());
} catch (e) {
console.error("Error starting game:", e);
}
});
socket.on('reset_room', (roomId) => {
if (!roomId) return;
GameManager.resetRoom(roomId, socket.id);
});
socket.on('kick_player', ({ roomId, targetId }) => {
try {
if (!roomId || !targetId) return;
GameManager.kickPlayer(roomId, socket.id, targetId);
} catch (e) {
console.error("Error kicking player:", e);
}
});
socket.on('update_settings', ({ roomId, settings }) => {
try {
if (!roomId || !settings) return;
GameManager.updateSettings(roomId, socket.id, settings);
} catch (e) {
console.error("Error updating settings:", e);
}
});
socket.on('add_bot', (roomId: string) => {
try {
if (!roomId) return;
GameManager.addBot(roomId, socket.id);
} catch (e) {
console.error("Error adding bot:", e);
}
});
socket.on('night_action', (data) => {
try {
if (!data || !data.roomId || !data.action || !data.targetId) return;
GameManager.submitNightAction(data.roomId, socket.id, data.action, data.targetId);
} catch (e) {
console.error("Error submitting night action:", e);
}
});
socket.on('day_action', (data) => {
try {
if (!data || !data.roomId || !data.action) return;
GameManager.handleDayAction(data.roomId, socket.id, data.action, data.targetId, data.targetA, data.targetB);
} catch (e) {
console.error("Error day action:", e);
}
});
socket.on('vote', (data) => {
try {
if (!data || !data.roomId || !data.targetId) return;
GameManager.submitVote(data.roomId, socket.id, data.targetId);
} catch (e) {
console.error("Error vote:", e);
}
});
socket.on('send_message', ({ roomId, text }) => {
try {
GameManager.handleChat(roomId, socket.id, text);
} catch (e) {
console.error("Error sending message:", e);
}
});
// --- DISCONNECT ---
socket.on('disconnect', () => {
try {
GameManager.playerDisconnect(socket.id);
io.emit('room_list_update', GameManager.getPublicRooms());
} catch (e) {
console.error("Error disconnect:", e);
}
});
});
};