ZZY 327c2df94d refactor(chess): 重构象棋程序基础结构,使用Nullable重构核心代码
- 更新了多个文件的代码结构和类型定义,提高了代码的健壮性和可维护性
- 优化了事件处理、棋子管理和移动逻辑,为后续功能扩展打下坚实基础
- 修复了一些潜在的空指针异常问题,提高了程序的稳定性
- 修复部分bug
2024-11-22 23:51:32 +08:00

47 lines
1.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Godot;
using Godot.Collections;
namespace RPPackage {
public static class RPHelper
{
public static Dictionary SerializeRPMessage(RPMessage message)
{
return message.ToDictionary();
}
public static RPMessage? DeserializeRPMessage(Dictionary data)
{
if (!data.ContainsKey("type") || !data.ContainsKey("cmd")) {
return null;
}
return new RPMessage {
Type = (string)data["type"],
Cmd = (string)data["cmd"],
Code = data.ContainsKey("code") ? (string)data["code"] : null,
Uid = data.ContainsKey("uid") ? (string)data["uid"] : null,
Token = data.ContainsKey("token") ? (string)data["token"] : null,
Data = data.ContainsKey("data") ? data["data"].AsGodotDictionary() : null,
};
}
// 假设你有WebSocket通信的实现这里仅展示如何使用上述方法
public static void SendRPMessage(this EventDrivenWebSocket ws, RPMessage message)
{
ws.SendJsonEx(RPHelper.SerializeRPMessage(message));
}
public static RPMessage? HandleIncomingMessage(string jsonMessage)
{
var dataDict = Json.ParseString(jsonMessage).AsGodotDictionary();
if (dataDict != null)
{
return RPHelper.DeserializeRPMessage(dataDict);
}
else
{
GD.PrintErr("Failed to parse incoming message.");
return null;
}
}
}
}