ZZY e16f76e11f refactor(重构): 重构了事件驱动的代码体系,使用全新命名和版本,以及测试套件的初试
- 移除了.csproj文件
- 更新了.gitignore,添加了.editorconfig
- 重构了IBoard和IPiece接口,引入了新的事件处理机制
- 优化了CCBoard、CCPiece等类的实现,使用新的事件驱动模型
- 删除了冗余代码,提高了代码的可读性和可维护性
2024-11-24 15:42:30 +08:00

142 lines
4.3 KiB
C#

#nullable disable
using Godot;
using Godot.Collections;
public partial class GameLobby : Control {
Global global = null;
Timer timer = null;
ItemList lists = null;
ConfirmationDialog dialog = null;
string URL;
string userName;
bool isAcceptedPart = false;
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
global = GetNode<Global>("/root/Global");
lists = GetNode<ItemList>("MarginContainer/VBoxContainer/MarginContainer3/ItemList");
dialog = GetNode<ConfirmationDialog>("Dialogs/ConfirmationDialog");
URL = global.GlobalConfigDict["server_url"].AsString();
userName = global.GlobalConfigDict["user_name"].AsString();
dialog.MinSize = new Vector2I(400, 200);
dialog.DialogAutowrap = true;
dialog.Canceled += () => {
if (dialog.Title == "Session Created")
global.RPClient.SessionAckCreate(dialog.GetMeta("sessionId").ToString(), false);
};
dialog.Confirmed += () => {
if (dialog.Title == "Session Created") {
// FIXME
isAcceptedPart = true;
global.RPClient.SessionAckCreate(dialog.GetMeta("sessionId").ToString(), true);
}
};
global.RPClient.OnOpen += (string eventName, object[] args) => {
global.RPClient.UserInit(userName, "godot chessboard", () => {
global.RPClient.UserRename(userName);
return global.RPClient.RegionAdd("server");
});
};
// global.RPClient.OnOpen += (eventName, args) => {
// global.RPClient.UserInit(userName, "none", () => {
// GD.PrintErr($"User Init Success");
// return true;
// });
// // global.RPClient.UserRename(userName);
// };
global.RPClient.RegSessionAckCreateCallback((sessionId, res, reqUserId, reqUserName) => {
if (reqUserId != null) {
dialog.Title = "Session Created";
dialog.SetMeta("reqUserName", reqUserName);
dialog.SetMeta("reqUserId", reqUserId);
dialog.SetMeta("sessionId", sessionId);
// dialog.GetLabel==>Text = $"{sessdata["reqUserName"]}";
dialog.DialogText = $"username: {reqUserName}\n" +
$"reqUserId: {reqUserId}\n";
dialog.Visible = true;
} else {
if (res) {
// TODO FIXME
global.sessionId = sessionId;
if (!isAcceptedPart) {
global.GlobalData["player_color"] = "red";
} else {
global.GlobalData["player_color"] = "black";
}
// GD.PrintErr("sessionId: ", reqUserId, "color: ", global.GlobalData["player_color"].AsString());
global.GotoScene("res://Scenes/ChessGame.tscn");
} else {
dialog.Title = "Failed";
dialog.DialogText = $"session create failed";
dialog.Visible = true;
}
}
return true;
});
timer = new Timer();
AddChild(timer);
ColorRect colorRect = GetNode<ColorRect>("MarginContainer/VBoxContainer/MarginContainer2/MarginContainer2/ColorRect");
timer.Connect("timeout", Callable.From(() => {
if (global.RPClient.GetIsConnected()) {
colorRect.Color = Colors.Green;
} else {
colorRect.Color = Colors.Red;
}
FlushData();
}));
timer.Start(1);
Connect();
}
public void Connect() {
GD.Print("Connect");
if (global.RPClient.GetIsConnected()) {
return;
}
// global.RPClient.ExitServer();
global.RPClient.ConnectToUrlEx(URL);
global.SetProcess(true);
}
private void OnItemSelected(int index) {
Dictionary item = lists.GetItemMetadata(index).AsGodotDictionary();
GD.Print($"Item {index} selected, {item}");
string[] strings = { item["id"].ToString() };
global.RPClient.SessionCreate(strings);
}
private void FlushData() {
global.RPClient.RegionInspect("server", (data) => {
// GD.Print(data);
lists?.Clear();
foreach (Dictionary<string, string> user in data) {
string userId = user["id"].ToString();
string userName = user["name"].ToString();
if (userId == global.RPClient.GetUserId()) {
lists.SetItemDisabled(
lists.AddItem($"Name: {userName}"),
true);
continue;
}
var idx = lists.AddItem($"Name: {userName}");
lists.SetItemMetadata(idx, user);
lists.SetItemTooltip(idx, $"User ID: {userId}");
}
return true;
});
}
private void OnBack() {
global.RPClient.ExitServer();
global.GotoScene("res://Main.tscn");
}
}