#nullable disable using Godot; using Godot.Collections; using ChineseChess; public partial class ChessGame : Node2D { ChessBoard board; Global global; ConfirmationDialog dialog; private bool isSession = false; ChessCore Game; ChessCore.TurnsSideType sideSelf; ChessCore.TurnsSideType sideOpposite; // Called when the node enters the scene tree for the first time. public override void _Ready() { // Init.Call(); global = GetNode("/root/Global"); board = GetNode("Chessboard"); isSession = global.RPClient.IsOnline(); GetNode("Control/VBoxContainer/MarginContainer3/HFlowContainer/LineEdit") .Text = global.GlobalData["player_color"].AsString(); LineEdit turnSideEdit = GetNode("Control/VBoxContainer/MarginContainer3/HFlowContainer/LineEdit2"); turnSideEdit.Text = "red"; GD.PrintErr("ChessGame ", global.RPClient.GetUserId(), ":",global.GlobalData["player_color"]); dialog = new ConfirmationDialog { DialogAutowrap = true, MinSize = new Vector2I(400, 200), Position = new Vector2I(200, 400), }; AddChild(dialog); if (global.GlobalData["player_color"].AsString() == "red") { sideSelf = ChessCore.TurnsSideType.Red; sideOpposite = ChessCore.TurnsSideType.Black; } else { sideSelf = ChessCore.TurnsSideType.Black; sideOpposite = ChessCore.TurnsSideType.Red; } // if (isSession) { // Game = new(ChessCore.Mode.MultiMode, sideSelf); // } else { // Game = new(ChessCore.Mode.SingleMode, sideSelf); // } Game = new(isSession ? ChessCore.Mode.MultiMode : ChessCore.Mode.SingleMode, sideSelf, board); Game.Init(); board.OnStepsChanged += (sender, e) => { turnSideEdit.Text = Game.GetTurnsType() == ChessCore.TurnsSideType.Red ? "red" : "black"; }; board.OnPosClicked += (sender, pos) => { int posX = (int)pos.X; int posY = (int)pos.Y; Vector.Vector2I vector = new(posX, posY); if (isSession) { Game.OnPosClicked(vector, ChessCore.PlayerSideType.Self); var res = global.RPClient.SendSessionToAll(global.sessionId, new Dictionary { {"type", "mouseClicked"}, {"X", pos.X}, {"Y", pos.Y}, {"id", global.RPClient.GetUserId()} }); } else { Game.OnPosClicked(vector); } }; if (isSession) { GD.Print("ws is connected"); global.RPClient.OnPRCSessionExit += (cmd, code) => { GoHome(); }; global.RPClient.OnPRCSessionRecv += (msg) => { SessionMsgHandle(msg["msg"].AsGodotDictionary()); }; } } // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { } private void SessionMsgHandle(Dictionary msg) { GD.PrintErr($"session msg: {msg}"); switch (msg["type"].AsString()) { case "over": if (global.RPClient.GetUserId() == msg["id"].AsString()) { break; } dialog.Title = "Opponent Finished"; dialog.DialogText = "Turn On You\n"; dialog.Visible = true; break; case "mouseClicked": if (msg["id"].ToString() == global.RPClient.GetUserId()) { break; } Vector2 mouseClicked = new(GD.StrToVar(msg["X"].ToString()).AsInt32(), GD.StrToVar(msg["Y"].ToString()).AsInt32()); Vector.Vector2I vector = new((int)mouseClicked.X, (int)mouseClicked.Y); Game.OnPosClicked(vector, ChessCore.PlayerSideType.Opponent); break; case "undo": Game.Undo(); break; case "reInit": Game.ReInit(); break; } } private void BtnOver() { GD.Print($"BtnOver {isSession}"); if (isSession == false) { return; } global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{ {"type", "over"}, {"id", global.RPClient.GetUserId()}, }); } public void GoHome() { if (global.RPClient.IsOnline()) { global.RPClient.ExitServer(); } global.GotoScene("res://Main.tscn"); } public void Undo() { GD.Print($"Undo {isSession}"); if (isSession) { global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{ {"type", "undo"}, {"id", global.RPClient.GetUserId()}, }); } else { if (Game.board.Steps == 0) board.Clear(); Game.Undo(); } } public void ReInit() { GD.PrintErr($"ReInit {isSession}"); if (isSession) { global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{ {"type", "reInit"}, }); } else { Game.ReInit(); board.Clear(); } } }