using Godot; using Godot.Collections; public partial class ChessGame : Node2D { ChessBoard board; Global global; ConfirmationDialog dialog; private bool isSession = false; private Player playerSelf; private Player playerOpponent; // 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"); playerSelf = new Player(board.board, Player.PlayerType.Human); playerOpponent = new Player(board.board, Player.PlayerType.Human); board.OnMouseClicked += (sender, clickPosition) => { Vector2 clickBoardPos = (PosTrans.transArrToPix.AffineInverse() * clickPosition).Round(); playerSelf.HandleBoardPosClick(clickBoardPos); }; dialog = new ConfirmationDialog { DialogAutowrap = true, MinSize = new Vector2I(400, 200), Position = new Vector2I(200, 400), }; AddChild(dialog); if (!global.RPClient.GetIsConnected()) { return; } isSession = true; GD.Print("ws is connected"); global.RPClient.OnPRCSessionExit += (cmd, code) => { GoHome(); }; board.OnMouseClicked += (sender, clickPosition) => { Vector2 clickBoardPos = (PosTrans.transArrToPix.AffineInverse() * clickPosition).Round(); var res = global.RPClient.SendSessionToAll(global.sessionId, new Dictionary { {"type", "mouseClicked"}, {"X", clickBoardPos.X}, {"Y", clickBoardPos.Y}, }); // GD.Print($"chessMoveFunc Callback {fromPos} -> {newPos} {res}"); }; 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": Vector2 mouseClicked = new(GD.StrToVar(msg["X"].ToString()).AsInt32(), GD.StrToVar(msg["Y"].ToString()).AsInt32()); playerOpponent.HandleBoardPosClick(mouseClicked); break; case "undo": _Undo(); break; case "reInit": _ReInit(); break; } } private void _Undo() { playerSelf.Undo(); } private void _ReInit() { playerSelf.ReInit(); board.InitChessBoard(); } 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"}, }); } else { _Undo(); } } public void ReInit() { GD.PrintErr($"ReInit {isSession}"); if (isSession) { global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{ {"type", "reInit"}, }); } else { _ReInit(); } } }