serverplace / BACKEND_NOTES.md
3v324v23's picture
Update Server: Fix Vote Bug, Memory Leak, and Role Reveal
05d8628

Backend Architecture Update

Core Components

  1. GameManager (Source of Truth):
    • Manages Map<string, Room> with Automatic Cleanup (garbage collects empty rooms > 5 mins old).
    • Enforces Server-Side Timer for all phases (Night 15s, Day 30s, Voting 15s).
    • Handles Game Logic: Role assignment (Unique distribution), Night Actions (Kill/Save/Investigate), Voting Tally, and Win Conditions.
  2. Sanitizer (Anti-Cheat):
    • sanitizeRoomState(room, socketId) ensures clients never receive hidden data (e.g., other players' roles) unless allowed (e.g., Mafia teammates).
  3. Strict Types:
    • Updated types/index.ts to enforce GamePhase, Role, and Room structure.
    • Switched internal storage to Record for better serialization.

Key Logic Details

  • Night: Mafia Kill vs Doctor Save logic implemented. Detective gets instant private feedback.
  • Joker: Specific win condition on Day Vote (Win) vs Night Kill (Loss).
  • Disconnects: Handled gracefully. Host migrates if in Lobby. Active players marked offline but kept in game state until timeout/cleanup.

Integration

  • RoomManager.ts was removed (consolidated into GameManager).
  • socketHandler.ts updated to use try/catch blocks for all events to prevent server crashes.

Verification

  • npm run build passed successfully.