/** * Network protocol for client-server communication * Matches the Python protocol implementation */ const MessageType = { // Client -> Server JOIN: 'JOIN', MOVE: 'MOVE', START_GAME: 'START_GAME', LEAVE: 'LEAVE', // Server -> Client WELCOME: 'WELCOME', STATE_UPDATE: 'STATE_UPDATE', PLAYER_JOINED: 'PLAYER_JOINED', PLAYER_LEFT: 'PLAYER_LEFT', GAME_STARTED: 'GAME_STARTED', GAME_OVER: 'GAME_OVER', ERROR: 'ERROR' }; class Message { constructor(type, data = {}) { this.type = type; this.data = data; } toJSON() { return JSON.stringify({ type: this.type, data: this.data }); } static fromJSON(jsonStr) { const obj = JSON.parse(jsonStr); return new Message(obj.type, obj.data || {}); } } // Helper functions for creating messages function createJoinMessage(playerName) { return new Message(MessageType.JOIN, { player_name: playerName }); } function createMoveMessage(direction) { return new Message(MessageType.MOVE, { direction: direction }); } function createStartGameMessage() { return new Message(MessageType.START_GAME); } function createLeaveMessage() { return new Message(MessageType.LEAVE); } // Direction constants (matching Python) const Direction = { UP: [0, -1], DOWN: [0, 1], LEFT: [-1, 0], RIGHT: [1, 0] };