Spaces:
Sleeping
Sleeping
File size: 5,376 Bytes
05d8628 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | 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);
}
});
});
};
|