extends Node class_name MessageProtocol ## 消息协议定义 ## 定义客户端和服务器之间的消息类型和格式 # 消息类型常量 enum MessageType { AUTH_REQUEST, # 身份验证请求 AUTH_RESPONSE, # 身份验证响应 CHARACTER_CREATE, # 创建角色 CHARACTER_MOVE, # 角色移动 CHARACTER_STATE, # 角色状态更新 DIALOGUE_SEND, # 发送对话 WORLD_STATE, # 世界状态同步 PING, # 心跳包 PONG, # 心跳响应 ERROR # 错误消息 } # 消息类型字符串映射 const MESSAGE_TYPE_STRINGS = { MessageType.AUTH_REQUEST: "auth_request", MessageType.AUTH_RESPONSE: "auth_response", MessageType.CHARACTER_CREATE: "character_create", MessageType.CHARACTER_MOVE: "character_move", MessageType.CHARACTER_STATE: "character_state", MessageType.DIALOGUE_SEND: "dialogue_send", MessageType.WORLD_STATE: "world_state", MessageType.PING: "ping", MessageType.PONG: "pong", MessageType.ERROR: "error" } # 反向映射:字符串到枚举 static var STRING_TO_MESSAGE_TYPE = {} static func _ensure_initialized(): """确保反向映射已初始化""" if STRING_TO_MESSAGE_TYPE.is_empty(): for type in MESSAGE_TYPE_STRINGS: STRING_TO_MESSAGE_TYPE[MESSAGE_TYPE_STRINGS[type]] = type ## 创建消息 static func create_message(type: MessageType, data: Dictionary = {}) -> Dictionary: """ 创建标准格式的消息 @param type: 消息类型 @param data: 消息数据 @return: 格式化的消息字典 """ return { "type": MESSAGE_TYPE_STRINGS[type], "data": data, "timestamp": Time.get_unix_time_from_system() * 1000 } ## 序列化消息为 JSON static func serialize(message: Dictionary) -> String: """ 将消息字典序列化为 JSON 字符串 @param message: 消息字典 @return: JSON 字符串 """ return JSON.stringify(message) ## 反序列化 JSON 为消息 static func deserialize(json_string: String) -> Dictionary: """ 将 JSON 字符串反序列化为消息字典 @param json_string: JSON 字符串 @return: 消息字典,如果解析失败返回空字典 """ var json = JSON.new() var parse_result = json.parse(json_string) if parse_result == OK: return json.data else: push_error("Failed to parse JSON: " + json_string) return {} ## 验证消息格式 static func validate_message(message: Dictionary) -> bool: """ 验证消息是否符合协议格式 @param message: 消息字典 @return: 是否有效 """ if not message.has("type"): return false if not message.has("data"): return false if not message.has("timestamp"): return false # 确保初始化并验证类型是否有效 _ensure_initialized() if not STRING_TO_MESSAGE_TYPE.has(message["type"]): return false return true ## 获取消息类型枚举 static func get_message_type(message: Dictionary) -> MessageType: """ 从消息中获取类型枚举 @param message: 消息字典 @return: 消息类型枚举 """ _ensure_initialized() if message.has("type") and STRING_TO_MESSAGE_TYPE.has(message["type"]): return STRING_TO_MESSAGE_TYPE[message["type"]] return MessageType.ERROR ## 创建身份验证请求 static func create_auth_request(username: String) -> Dictionary: """创建身份验证请求消息""" return create_message(MessageType.AUTH_REQUEST, { "username": username }) ## 创建角色创建请求 static func create_character_create(character_name: String, personalization_data: Dictionary = {}) -> Dictionary: """创建角色创建请求消息""" var data = { "name": character_name } # 如果有个性化数据,添加到消息中 if not personalization_data.is_empty(): data["personalization"] = personalization_data return create_message(MessageType.CHARACTER_CREATE, data) ## 创建角色移动消息 static func create_character_move(character_id: String, position: Vector2, direction: Vector2) -> Dictionary: """创建角色移动消息""" return create_message(MessageType.CHARACTER_MOVE, { "character_id": character_id, "position": { "x": position.x, "y": position.y }, "direction": { "x": direction.x, "y": direction.y } }) ## 创建对话消息 static func create_dialogue_send(sender_id: String, receiver_id: String, message_text: String) -> Dictionary: """创建对话消息""" return create_message(MessageType.DIALOGUE_SEND, { "sender_id": sender_id, "receiver_id": receiver_id, "message": message_text }) ## 创建心跳包 static func create_ping() -> Dictionary: """创建心跳包""" return create_message(MessageType.PING, {}) ## 创建错误消息 static func create_error(error_message: String, error_code: int = 0) -> Dictionary: """创建错误消息""" return create_message(MessageType.ERROR, { "message": error_message, "code": error_code })