diff --git a/FunGame.Desktop/Controller/MainController.cs b/FunGame.Desktop/Controller/MainController.cs index d4b34bf..6576456 100644 --- a/FunGame.Desktop/Controller/MainController.cs +++ b/FunGame.Desktop/Controller/MainController.cs @@ -216,18 +216,19 @@ namespace Milimoe.FunGame.Desktop.Controller } } - public async Task CreateRoomAsync(RoomType RoomType, string GameModule, string GameMap, bool IsRank, string Password = "") + public async Task CreateRoomAsync(RoomType roomType, string gameModuleServer, string gameMap, bool isRank, int maxUsers, string password = "") { Room room = General.HallInstance; try { - CreateRoomRequest.AddRequestData("roomtype", RoomType); - CreateRoomRequest.AddRequestData("gamemodule", GameModule); - CreateRoomRequest.AddRequestData("gamemap", GameMap); + CreateRoomRequest.AddRequestData("roomtype", roomType); + CreateRoomRequest.AddRequestData("gamemoduleserver", gameModuleServer); + CreateRoomRequest.AddRequestData("gamemap", gameMap); CreateRoomRequest.AddRequestData("master", Usercfg.LoginUser); - CreateRoomRequest.AddRequestData("password", Password); - CreateRoomRequest.AddRequestData("isrank", IsRank); + CreateRoomRequest.AddRequestData("password", password); + CreateRoomRequest.AddRequestData("isrank", isRank); + CreateRoomRequest.AddRequestData("maxusers", maxUsers); await CreateRoomRequest.SendRequestAsync(); if (CreateRoomRequest.Result == RequestResult.Success) { @@ -242,13 +243,13 @@ namespace Milimoe.FunGame.Desktop.Controller return room; } - public async Task MatchRoomAsync(RoomType RoomType, bool isCancel = false) + public async Task MatchRoomAsync(RoomType roomType, bool isCancel = false) { bool result = false; try { - MatchRoomRequest.AddRequestData("roomtype", RoomType); + MatchRoomRequest.AddRequestData("roomtype", roomType); MatchRoomRequest.AddRequestData("matcher", Usercfg.LoginUser); MatchRoomRequest.AddRequestData("iscancel", isCancel); await MatchRoomRequest.SendRequestAsync(); diff --git a/FunGame.Desktop/Controller/RunTimeController.cs b/FunGame.Desktop/Controller/RunTimeController.cs index d4733f6..02a4494 100644 --- a/FunGame.Desktop/Controller/RunTimeController.cs +++ b/FunGame.Desktop/Controller/RunTimeController.cs @@ -276,9 +276,9 @@ namespace Milimoe.FunGame.Desktop.Controller protected override void SocketHandler_Gaming(SocketObject ServerMessage) { GamingType gamingtype = GamingType.None; - Hashtable data = []; + Dictionary data = []; if (ServerMessage.Length > 0) gamingtype = ServerMessage.GetParam(0); - if (ServerMessage.Length > 1) data = ServerMessage.GetParam(1) ?? data; + if (ServerMessage.Length > 1) data = ServerMessage.GetParam>(1) ?? data; RunTime.Gaming?.GamingHandler(gamingtype, data); } } diff --git a/FunGame.Desktop/Library/Component/GeneralForm.cs b/FunGame.Desktop/Library/Component/GeneralForm.cs index d84f63e..227bd4e 100644 --- a/FunGame.Desktop/Library/Component/GeneralForm.cs +++ b/FunGame.Desktop/Library/Component/GeneralForm.cs @@ -105,6 +105,16 @@ namespace Milimoe.FunGame.Desktop.Library.Component return input; } + /// + /// 委托更新UI + /// + /// + public virtual void InvokeUpdateUI(Action action) + { + if (InvokeRequired) Invoke(action); + else action(); + } + /// /// 绑定事件,子类需要重写 /// @@ -197,16 +207,5 @@ namespace Milimoe.FunGame.Desktop.Library.Component { BindEvent(); } - - /// - /// 委托更新UI - /// - /// - protected virtual void InvokeUpdateUI(Action action) - { - if (InvokeRequired) Invoke(action); - else action(); - } - } } diff --git a/FunGame.Desktop/Library/Component/ShowMessage.cs b/FunGame.Desktop/Library/Component/ShowMessage.cs index ee9096c..3f6235e 100644 --- a/FunGame.Desktop/Library/Component/ShowMessage.cs +++ b/FunGame.Desktop/Library/Component/ShowMessage.cs @@ -244,7 +244,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component ChangeSecond(msg, autoclose); } MessageResult = AutoResult; - Close(); + InvokeUpdateUI(Dispose); } } @@ -273,7 +273,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component /// private void ChangeSecond(string msg, int s) { - MsgText.Text = msg + "\n[ " + s + " 秒后自动关闭 ]"; + InvokeUpdateUI(() => MsgText.Text = msg + "\n[ " + s + " 秒后自动关闭 ]"); } private void LeftButton_Click(object sender, EventArgs e) diff --git a/FunGame.Desktop/Library/Other/Constant.cs b/FunGame.Desktop/Library/Other/Constant.cs index 9faa6e6..3fac3f9 100644 --- a/FunGame.Desktop/Library/Other/Constant.cs +++ b/FunGame.Desktop/Library/Other/Constant.cs @@ -122,7 +122,7 @@ namespace Milimoe.FunGame.Desktop.Library } public static object[] SupportedGameMap(GameModule module) { - IEnumerable list = module.GameModuleDepend.Maps.Where(map => module.GameModuleDepend.Maps.Contains(map)).Distinct(); + IEnumerable list = module.GameModuleDepend.Maps.Where(module.GameModuleDepend.Maps.Contains).Select(m => m.Name).Distinct(); if (list.Any()) return AllComboItem.Union(list).ToArray(); return ["- 缺少地图 -"]; } diff --git a/FunGame.Desktop/UI/Login/ForgetPassword.cs b/FunGame.Desktop/UI/Login/ForgetPassword.cs index 2b33058..1c1e699 100644 --- a/FunGame.Desktop/UI/Login/ForgetPassword.cs +++ b/FunGame.Desktop/UI/Login/ForgetPassword.cs @@ -22,12 +22,17 @@ namespace Milimoe.FunGame.Desktop.UI { if (RunTime.Socket != null) { - string username = UsernameText.Text.Trim(); - string email = EmailText.Text.Trim(); + string username = ""; + string email = ""; + InvokeUpdateUI(() => + { + username = UsernameText.Text.Trim(); + email = EmailText.Text.Trim(); + }); if (username == "" || email == "") { ShowMessage(ShowMessageType.Error, "账号或邮箱不能为空!"); - UsernameText.Focus(); + InvokeUpdateUI(() => UsernameText.Focus()); return; } @@ -111,7 +116,7 @@ namespace Milimoe.FunGame.Desktop.UI } if (success) { - Dispose(); + InvokeUpdateUI(Dispose); } } } diff --git a/FunGame.Desktop/UI/Login/Login.cs b/FunGame.Desktop/UI/Login/Login.cs index 93c42d5..7d9083d 100644 --- a/FunGame.Desktop/UI/Login/Login.cs +++ b/FunGame.Desktop/UI/Login/Login.cs @@ -73,7 +73,7 @@ namespace Milimoe.FunGame.Desktop.UI result = await Login_Handler(); }).OnCompleted(() => { - if (result) Dispose(); + if (result) InvokeUpdateUI(Dispose); else GoToLogin.Enabled = true; }); } diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index 2833275..bf02e8e 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -403,9 +403,7 @@ namespace Milimoe.FunGame.Desktop.UI /// private void WritelnGameInfo() { - GameInfo.AppendText("\n"); - GameInfo.SelectionStart = GameInfo.Text.Length - 1; - GameInfo.ScrollToCaret(); + WritelnGameInfo("\r\n"); } /// @@ -416,9 +414,7 @@ namespace Milimoe.FunGame.Desktop.UI { if (msg.Trim() != "") { - GameInfo.AppendText(msg + "\n"); - GameInfo.SelectionStart = GameInfo.Text.Length - 1; - GameInfo.ScrollToCaret(); + WriteGameInfo(msg + "\r\n"); } } @@ -430,9 +426,12 @@ namespace Milimoe.FunGame.Desktop.UI { if (msg.Trim() != "") { - GameInfo.AppendText(msg); - GameInfo.SelectionStart = GameInfo.Text.Length - 1; - GameInfo.ScrollToCaret(); + InvokeUpdateUI(() => + { + GameInfo.AppendText(msg); + GameInfo.SelectionStart = GameInfo.Text.Length - 1; + GameInfo.ScrollToCaret(); + }); } } @@ -490,16 +489,16 @@ namespace Milimoe.FunGame.Desktop.UI TaskUtility.NewTask(async () => { int PlayerCount = users.Count; - for (int i = 10; i > 0; i--) + for (int i = 5; i > 0; i--) { WritelnGameInfo("房间 [ " + room.Roomid + " (" + PlayerCount + "人" + RoomSet.GetTypeString(room.RoomType) + ") ] 的游戏将在" + i + "秒后开始..."); await Task.Delay(1000); } WritelnGameInfo("房间 [ " + room.Roomid + " (" + PlayerCount + "人" + RoomSet.GetTypeString(room.RoomType) + ") ] 的游戏正式开始!"); - if (RunTime.GameModuleLoader?.Modules.ContainsKey(room.GameModule) ?? false) + if (RunTime.GameModuleLoader?.Modules.TryGetValue(room.GameModule, out GameModule? module) ?? false && module != null) { - RunTime.Gaming = Core.Model.Gaming.StartGame(RunTime.GameModuleLoader[room.GameModule], room, RunTime.Session.LoginUser, users, RunTime.GameModuleLoader); - Visible = false; // 隐藏主界面 + RunTime.Gaming = Core.Model.Gaming.StartGame(module, room, RunTime.Session.LoginUser, users, RunTime.GameModuleLoader); + Visible = !module.HideMain; // 隐藏主界面 } else { @@ -526,8 +525,8 @@ namespace Milimoe.FunGame.Desktop.UI { // 恢复界面 Visible = true; - _InGame = false; SetButtonEnabled(true, ClientState.InRoom); + _InGame = false; OnSucceedEndGameEvent(this, e); RunTime.PluginLoader?.OnSucceedEndGameEvent(this, e); } @@ -630,12 +629,12 @@ namespace Milimoe.FunGame.Desktop.UI /// /// 通过双击房间列表的房间号加入房间 /// - /// - private async Task JoinRoom(int selectedindex) + /// + private async Task JoinRoom(int selectedIndex) { - if (selectedindex != -1 && RunTime.RoomList.Count > selectedindex) + if (selectedIndex != -1 && RunTime.RoomList.Count > selectedIndex) { - string roomid = RunTime.RoomList.ListRoom[selectedindex]?.Roomid ?? ""; + string roomid = RunTime.RoomList.ListRoom[selectedIndex]?.Roomid ?? ""; return await JoinRoom_Handler(roomid); } return false; @@ -655,7 +654,8 @@ namespace Milimoe.FunGame.Desktop.UI { if (Usercfg.InRoom.Roomid == "-1") { - if (await MainController.GetRoomPlayerCountAsync(roomid) < 8) + GameModule? gameModule = RunTime.GameModuleLoader?[RunTime.RoomList[roomid].GameModule]; + if (await MainController.GetRoomPlayerCountAsync(roomid) < gameModule?.MaxUsers) { if (ShowMessage(ShowMessageType.YesNo, "已找到房间 -> [ " + roomid + " ]\n是否加入?", "已找到房间") == MessageResult.Yes) { @@ -890,23 +890,27 @@ namespace Milimoe.FunGame.Desktop.UI /// /// 创建房间的处理方法 /// - /// - /// + /// + /// + /// + /// + /// /// - private async Task CreateRoom_Handler(RoomType RoomType, string GameModule, string GameMap, bool IsRank, string Password = "") + private async Task CreateRoom_Handler(RoomType roomType, string gameModule, string gameMap, bool isRank, string password = "") { if (Usercfg.InRoom.Roomid != "-1") { ShowMessage(ShowMessageType.Warning, "已在房间中,无法创建房间。"); return; } - GameModule? mode = RunTime.GameModuleLoader?.Modules.Values.FirstOrDefault() ?? default; - if (mode is null) + GameModule? module = RunTime.GameModuleLoader?[gameModule]; + if (module is null) { ShowMessage(ShowMessageType.Error, ">> 缺少" + Config.FunGame_RoomType + "所需的模组,无法创建房间。"); return; } - Room room = await InvokeController_CreateRoom(RoomType, GameModule, GameMap, IsRank, Password); + string gameModuleServer = module.AssociatedServerModuleName; + Room room = await InvokeController_CreateRoom(roomType, gameModuleServer, gameMap, isRank, module.MaxUsers, password); if (MainController is not null && room.Roomid != "-1") { await MainController.UpdateRoomAsync(); @@ -1071,12 +1075,12 @@ namespace Milimoe.FunGame.Desktop.UI string modmap = ComboGameMap.SelectedItem?.ToString() ?? all; if (RunTime.GameModuleLoader is null || (modname != all && !RunTime.GameModuleLoader.Modules.ContainsKey(modname))) { - ShowMessage(ShowMessageType.Error, ">> 模组未正确加载,无法创建房间。"); + ShowMessage(ShowMessageType.Error, "模组未正确加载,无法创建房间。"); return; } if (RunTime.GameModuleLoader is null || (modmap != all && !RunTime.GameModuleLoader.Maps.ContainsKey(modmap))) { - ShowMessage(ShowMessageType.Error, ">> 地图未正确加载,无法创建房间。"); + ShowMessage(ShowMessageType.Error, "地图未正确加载,无法创建房间。"); return; } // 开始匹配 @@ -1123,12 +1127,12 @@ namespace Milimoe.FunGame.Desktop.UI } if (RunTime.GameModuleLoader is null || (modname != all && !RunTime.GameModuleLoader.Modules.ContainsKey(modname))) { - ShowMessage(ShowMessageType.Error, ">> 模组未正确加载,无法创建房间。"); + ShowMessage(ShowMessageType.Error, "模组未正确加载,无法创建房间。"); return; } if (RunTime.GameModuleLoader is null || (modmap != all && !RunTime.GameModuleLoader.Maps.ContainsKey(modmap))) { - ShowMessage(ShowMessageType.Error, ">> 地图未正确加载,无法创建房间。"); + ShowMessage(ShowMessageType.Error, "地图未正确加载,无法创建房间。"); return; } if (CheckHasPass.Checked) @@ -1159,7 +1163,7 @@ namespace Milimoe.FunGame.Desktop.UI { WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 离开房间"); WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已离开房间 -> [ " + roomid + " ]"); - InMain(); + InvokeUpdateUI(InMain); _ = MainController?.UpdateRoomAsync(); result = true; } @@ -1276,7 +1280,7 @@ namespace Milimoe.FunGame.Desktop.UI SetRoomTypeString(); ComboGameMap.Items.Clear(); ComboGameMap.Items.AddRange(Constant.SupportedGameMap(mod)); - ComboGameMap.SelectedIndex = 0; + ComboGameMap.SelectedItem = mod.DefaultMap; } } else @@ -1295,7 +1299,8 @@ namespace Milimoe.FunGame.Desktop.UI { if (RoomList.SelectedItem != null) { - TaskUtility.NewTask(async () => await JoinRoom(RoomList.SelectedIndex)); + int selected = RoomList.SelectedIndex; + TaskUtility.NewTask(async () => await JoinRoom(selected)); } } @@ -1533,8 +1538,11 @@ namespace Milimoe.FunGame.Desktop.UI /// private void SucceedIntoRoomEvent(object sender, RoomEventArgs e) { - SetRoomid(e.Room); - InRoom(); + InvokeUpdateUI(() => + { + SetRoomid(e.Room); + InRoom(); + }); } /// @@ -1625,13 +1633,20 @@ namespace Milimoe.FunGame.Desktop.UI { if (Usercfg.InRoom.Roomid != "-1") { - TaskUtility.NewTask(async () => + if (Usercfg.LoginUser.Id == Usercfg.InRoom.RoomMaster.Id) { - if (await MainController.SetReadyAsync(Usercfg.InRoom.Roomid)) + WritelnGameInfo(">> 房主无法使用此命令。"); + } + else + { + TaskUtility.NewTask(async () => { - await InvokeController_SendTalk(" [ " + Usercfg.LoginUser.Username + " ] 已准备。"); - } - }); + if (await MainController.SetReadyAsync(Usercfg.InRoom.Roomid)) + { + await InvokeController_SendTalk(" [ " + Usercfg.LoginUser.Username + " ] 已准备。"); + } + }); + } } else WritelnGameInfo(">> 不在房间中无法使用此命令。"); } @@ -1644,13 +1659,20 @@ namespace Milimoe.FunGame.Desktop.UI { if (Usercfg.InRoom.Roomid != "-1") { - TaskUtility.NewTask(async () => + if (Usercfg.LoginUser.Id == Usercfg.InRoom.RoomMaster.Id) { - if (await MainController.CancelReadyAsync(Usercfg.InRoom.Roomid)) + WritelnGameInfo(">> 房主无法使用此命令。"); + } + else + { + TaskUtility.NewTask(async () => { - await InvokeController_SendTalk(" [ " + Usercfg.LoginUser.Username + " ] 已取消准备。"); - } - }); + if (await MainController.CancelReadyAsync(Usercfg.InRoom.Roomid)) + { + await InvokeController_SendTalk(" [ " + Usercfg.LoginUser.Username + " ] 已取消准备。"); + } + }); + } } else WritelnGameInfo(">> 不在房间中无法使用此命令。"); } @@ -1967,9 +1989,9 @@ namespace Milimoe.FunGame.Desktop.UI /// /// /// - public async Task InvokeController_CreateRoom(RoomType RoomType, string GameModule, string GameMap, bool IsRank, string Password = "") + public async Task InvokeController_CreateRoom(RoomType roomType, string gameModuleServer, string gameMap, bool isRank, int maxUsers,string password = "") { - RoomEventArgs EventArgs = new(RoomType, Password); + RoomEventArgs EventArgs = new(roomType, password); Room room = General.HallInstance; try @@ -1978,7 +2000,7 @@ namespace Milimoe.FunGame.Desktop.UI RunTime.PluginLoader?.OnBeforeCreateRoomEvent(this, EventArgs); if (EventArgs.Cancel) return room; - room = MainController is null ? room : await MainController.CreateRoomAsync(RoomType, GameModule, GameMap, IsRank, Password); + room = MainController is null ? room : await MainController.CreateRoomAsync(roomType, gameModuleServer, gameMap, isRank, maxUsers, password); if (room.Roomid != "-1") { @@ -2029,7 +2051,7 @@ namespace Milimoe.FunGame.Desktop.UI OnSucceedQuitRoomEvent(this, EventArgs); RunTime.PluginLoader?.OnSucceedQuitRoomEvent(this, EventArgs); // 禁用和激活按钮,并切换预设快捷消息 - SetButtonEnabled(true, ClientState.Online); + InvokeUpdateUI(() => SetButtonEnabled(true, ClientState.Online)); } else { @@ -2047,7 +2069,7 @@ namespace Milimoe.FunGame.Desktop.UI OnAfterQuitRoomEvent(this, EventArgs); RunTime.PluginLoader?.OnAfterQuitRoomEvent(this, EventArgs); // 禁用和激活按钮,并切换预设快捷消息 - SetButtonEnabled(true, ClientState.Online); + InvokeUpdateUI(() => SetButtonEnabled(true, ClientState.Online)); } return result; diff --git a/FunGame.Desktop/UI/Register/Register.cs b/FunGame.Desktop/UI/Register/Register.cs index 2b09791..8c734fb 100644 --- a/FunGame.Desktop/UI/Register/Register.cs +++ b/FunGame.Desktop/UI/Register/Register.cs @@ -32,10 +32,17 @@ namespace Milimoe.FunGame.Desktop.UI { try { - string username = UsernameText.Text.Trim(); - string password = PasswordText.Text.Trim(); - string checkpassword = CheckPasswordText.Text.Trim(); - string email = EmailText.Text.Trim(); + string username = ""; + string password = ""; + string checkpassword = ""; + string email = ""; + InvokeUpdateUI(() => + { + username = UsernameText.Text.Trim(); + password = PasswordText.Text.Trim(); + checkpassword = CheckPasswordText.Text.Trim(); + email = EmailText.Text.Trim(); + }); if (username != "") { if (NetworkUtility.IsUserName(username)) @@ -46,21 +53,21 @@ namespace Milimoe.FunGame.Desktop.UI if (password != checkpassword) { ShowMessage(ShowMessageType.Error, "两个密码不相同,请重新输入!"); - CheckPasswordText.Focus(); + InvokeUpdateUI(() => CheckPasswordText.Focus()); return false; } } else { ShowMessage(ShowMessageType.Error, "账号名长度不符合要求:3~12个字符数(一个中文2个字符)"); - UsernameText.Focus(); + InvokeUpdateUI(() => UsernameText.Focus()); return false; } } else { ShowMessage(ShowMessageType.Error, "账号名不符合要求:不能包含特殊字符"); - UsernameText.Focus(); + InvokeUpdateUI(() => UsernameText.Focus()); return false; } } @@ -70,26 +77,26 @@ namespace Milimoe.FunGame.Desktop.UI if (length < 6 || length > 15) // 字节范围 6~15 { ShowMessage(ShowMessageType.Error, "密码长度不符合要求:6~15个字符数"); - PasswordText.Focus(); + InvokeUpdateUI(() => PasswordText.Focus()); return false; } } if (username == "" || password == "" || checkpassword == "") { ShowMessage(ShowMessageType.Error, "请将账号和密码填写完整!"); - UsernameText.Focus(); + InvokeUpdateUI(() => UsernameText.Focus()); return false; } if (email == "") { ShowMessage(ShowMessageType.Error, "邮箱不能为空!"); - EmailText.Focus(); + InvokeUpdateUI(() => EmailText.Focus()); return false; } if (!NetworkUtility.IsEmail(email)) { ShowMessage(ShowMessageType.Error, "这不是一个邮箱地址!"); - EmailText.Focus(); + InvokeUpdateUI(() => EmailText.Focus()); return false; } return await RegController.RegAsync(username, password, email); @@ -116,7 +123,10 @@ namespace Milimoe.FunGame.Desktop.UI string username = ((RegisterEventArgs)e).Username; string password = ((RegisterEventArgs)e).Password; TaskUtility.NewTask(async () => await LoginController.LoginAccountAsync(username, password, encrypt: false)); - RunTime.Login?.Close(); + if (RunTime.Login != null) + { + RunTime.Login.InvokeUpdateUI(RunTime.Login.Close); + } } private void RegButton_Click(object sender, EventArgs e) @@ -124,8 +134,8 @@ namespace Milimoe.FunGame.Desktop.UI RegButton.Enabled = false; TaskUtility.NewTask(async () => { - if (!await Reg_Handler()) RegButton.Enabled = true; - else Close(); + if (!await Reg_Handler()) InvokeUpdateUI(() => RegButton.Enabled = true); + else InvokeUpdateUI(Close); }); }