Spaces:
Sleeping
Sleeping
| import { Room, Player } from '../types'; | |
| export function sanitizeRoomState(room: Room, receiverSocketId: string): Partial<Room> { | |
| const receiver = room.players.find(p => p.id === receiverSocketId); | |
| const isReceiverMafia = receiver?.role === 'MAFIA'; | |
| const isGameOver = room.phase === 'GAME_OVER'; | |
| // Create a shallow copy of the room to avoid mutating the original | |
| // We only need to clone deeply what we intend to modify (players) | |
| const sanitized: Partial<Room> = { ...room }; | |
| // Sanitize Players | |
| sanitized.players = room.players.map(p => { | |
| // Create a shallow copy of the player to avoid mutation | |
| const pCopy = { ...p }; | |
| const isSelf = p.id === receiverSocketId; | |
| // Determine if role should be visible | |
| // 1. Always see self | |
| // 2. See others if game is OVER | |
| // 3. See other MAFIA if you are MAFIA | |
| const showRole = isSelf || isGameOver || (isReceiverMafia && p.role === 'MAFIA'); | |
| if (!showRole) { | |
| delete pCopy.role; | |
| } | |
| // Hide action targets during Night, unless it's your own | |
| if (room.phase === 'NIGHT') { | |
| // Only show my own target | |
| if (!isSelf) { | |
| delete pCopy.actionTarget; | |
| } | |
| } | |
| return pCopy; | |
| }); | |
| // Hide internal state | |
| if (!isGameOver) { | |
| // Hide specific night actions logic map | |
| delete sanitized.nightActions; | |
| } | |
| return sanitized; | |
| } |