From 0882e526e8e96727b5f2d5c7e5de2210430e293c Mon Sep 17 00:00:00 2001 From: milimoe <110188673+milimoe@users.noreply.github.com> Date: Wed, 25 Oct 2023 00:12:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=B9=E9=85=8D=E6=88=BF=E9=97=B4=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 匹配房间 第一部分 * 修复开始匹配后按钮可用状态异常问题 * 优化匹配结果弹窗 * 优化匹配结束的UI变化 * 匹配成功后进入房间 * 需要再次检查匹配状态 --------- Co-authored-by: yeziuku --- .gitignore | 3 +- FunGame.Desktop/Controller/MainController.cs | 25 ++ .../Controller/RunTimeController.cs | 28 ++ FunGame.Desktop/UI/Main/Main.Designer.cs | 2 + FunGame.Desktop/UI/Main/Main.cs | 249 +++++++++--------- 5 files changed, 187 insertions(+), 120 deletions(-) diff --git a/.gitignore b/.gitignore index d6c2ed7..76c3ee8 100644 --- a/.gitignore +++ b/.gitignore @@ -363,5 +363,6 @@ MigrationBackup/ FodyWeavers.xsd # FunGame Implement +/FunGame.Implement/*.cs FunGame.Implement/Implement/*.cs -FunGame.Implement/Implement/Implement/*.cs \ No newline at end of file +FunGame.Implement/Implement/Implement/*.cs diff --git a/FunGame.Desktop/Controller/MainController.cs b/FunGame.Desktop/Controller/MainController.cs index 80c93ad..0ea552a 100644 --- a/FunGame.Desktop/Controller/MainController.cs +++ b/FunGame.Desktop/Controller/MainController.cs @@ -15,6 +15,7 @@ namespace Milimoe.FunGame.Desktop.Controller private readonly Session Usercfg = RunTime.Session; private readonly DataRequest ChatRequest; private readonly DataRequest CreateRoomRequest; + private readonly DataRequest MatchRoomRequest; private readonly DataRequest GetRoomPlayerCountRequest; private readonly DataRequest UpdateRoomRequest; private readonly DataRequest IntoRoomRequest; @@ -25,6 +26,7 @@ namespace Milimoe.FunGame.Desktop.Controller Main = main; ChatRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_Chat); CreateRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_CreateRoom); + MatchRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_MatchRoom); GetRoomPlayerCountRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Room_GetRoomPlayerCount); UpdateRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_UpdateRoom); IntoRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_IntoRoom); @@ -183,6 +185,29 @@ namespace Milimoe.FunGame.Desktop.Controller return room; } + + public async Task MatchRoomAsync(string RoomType, bool isCancel = false) + { + bool result = false; + + try + { + MatchRoomRequest.AddRequestData("roomtype", RoomType); + MatchRoomRequest.AddRequestData("matcher", Usercfg.LoginUser); + MatchRoomRequest.AddRequestData("iscancel", isCancel); + await MatchRoomRequest.SendRequestAsync(); + if (MatchRoomRequest.Result == RequestResult.Success) + { + result = MatchRoomRequest.GetResult("result"); + } + } + catch (Exception e) + { + Main.GetMessage(e.GetErrorInfo(), TimeType.None); + } + + return result; + } public async Task ChatAsync(string msg) { diff --git a/FunGame.Desktop/Controller/RunTimeController.cs b/FunGame.Desktop/Controller/RunTimeController.cs index a493f57..888f353 100644 --- a/FunGame.Desktop/Controller/RunTimeController.cs +++ b/FunGame.Desktop/Controller/RunTimeController.cs @@ -147,6 +147,16 @@ namespace Milimoe.FunGame.Desktop.Controller Close(); } + protected override void SocketHandler_System(SocketObject ServerMessage) + { + // 收到系统消息,直接发送弹窗 + string msg = ""; + ShowMessageType type = ShowMessageType.General; + if (ServerMessage.Parameters.Length > 0) msg = ServerMessage.GetParam(0) ?? ""; + if (ServerMessage.Parameters.Length > 1) type = ServerMessage.GetParam(1); + Main.ShowMessage(type, msg, "系统消息", 60); + } + protected override void SocketHandler_HeartBeat(SocketObject ServerMessage) { // 收到心跳包时更新与服务器的连接延迟 @@ -184,5 +194,23 @@ namespace Milimoe.FunGame.Desktop.Controller if (ServerMessage.Length > 0) room = ServerMessage.GetParam(0) ?? General.HallInstance; if (room.Roomid != "-1" && room.Roomid == Usercfg.InRoom.Roomid) Main.UpdateUI(MainInvokeType.UpdateRoomMaster, room); } + + protected override void SocketHandler_MatchRoom(SocketObject ServerMessage) + { + // 匹配成功,询问是否加入房间 + Config.FunGame_isMatching = false; + Room room = General.HallInstance; + if (ServerMessage.Length > 0) room = ServerMessage.GetParam(0) ?? General.HallInstance; + if (room.Roomid == "-1") + { + Main.ShowMessage(ShowMessageType.General, "暂时无法找到符合条件的房间。", "匹配房间"); + Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Success, room); + Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Cancel); + } + else if (Main.ShowMessage(ShowMessageType.YesNo, "已找到符合条件的房间,是否加入?", "匹配房间", 10) == MessageResult.Yes) + { + Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Success, room); + } + } } } diff --git a/FunGame.Desktop/UI/Main/Main.Designer.cs b/FunGame.Desktop/UI/Main/Main.Designer.cs index 45a3cf9..d4aaf3c 100644 --- a/FunGame.Desktop/UI/Main/Main.Designer.cs +++ b/FunGame.Desktop/UI/Main/Main.Designer.cs @@ -555,6 +555,8 @@ namespace Milimoe.FunGame.Desktop.UI StopMatch.UseVisualStyleBackColor = true; StopMatch.Visible = false; StopMatch.Click += StopMatch_Click; + StopMatch.MouseHover += StopMatch_MouseHover; + StopMatch.MouseLeave += StopMatch_MouseLeave; // // Main // diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index 755ce49..2f90ef9 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -26,15 +26,10 @@ namespace Milimoe.FunGame.Desktop.UI /** * 变量 */ - private Task? MatchFunGame = null; // 匹配线程(即将删除) private MainController? MainController = null; private readonly Core.Model.RoomList Rooms = RunTime.RoomList; private readonly Core.Model.Session Usercfg = RunTime.Session; - - /** - * 委托【即将删除】 - */ - Action? StartMatch_Action = null; + private int _MatchSeconds = 0; // 记录匹配房间的秒数 public Main() { @@ -239,6 +234,13 @@ namespace Milimoe.FunGame.Desktop.UI } break; + case MainInvokeType.MatchRoom: + if (objs != null && objs.Length > 0) + { + StartMatch_Method((StartMatchState)objs[0], objs[1..]); + } + break; + default: break; } @@ -409,6 +411,7 @@ namespace Milimoe.FunGame.Desktop.UI SetRoomid(Usercfg.InRoom); QuitRoom.Visible = false; StartMatch.Visible = true; + StopMatch.Visible = false; RoomSetting.Visible = false; CreateRoom.Visible = true; NowRoomID.Visible = false; @@ -422,6 +425,7 @@ namespace Milimoe.FunGame.Desktop.UI { // 显示:退出房间、房间设置、当前房间号、复制房间号 // 隐藏:停止匹配、创建房间 + StartMatch.Visible = false; StopMatch.Visible = false; QuitRoom.Visible = true; CreateRoom.Visible = false; @@ -458,12 +462,16 @@ namespace Milimoe.FunGame.Desktop.UI CheckTeam.Enabled = isLogon; CheckHasPass.Enabled = isLogon; StartMatch.Enabled = isLogon; - CreateRoom.Enabled = isLogon; - RoomBox.Enabled = isLogon; AccountSetting.Enabled = isLogon; Stock.Enabled = isLogon; Store.Enabled = isLogon; - RefreshRoomList.Enabled = isLogon; + if (!Config.FunGame_isMatching) + { + // 匹配中时不修改部分按钮状态 + RoomBox.Enabled = isLogon; + CreateRoom.Enabled = isLogon; + RefreshRoomList.Enabled = isLogon; + } } /// @@ -539,110 +547,113 @@ namespace Milimoe.FunGame.Desktop.UI /// /// 主要参数:触发方法的哪一个分支 /// 可传多个参数 - private void StartMatch_Method(int i, object[]? objs = null) + private void StartMatch_Method(StartMatchState status, params object[] objs) { - switch (i) + switch (status) { - case (int)StartMatchState.Matching: + case StartMatchState.Matching: // 开始匹配 - Config.FunGame_isMatching = true; - int loop = 0; - string roomid = Convert.ToString(new Random().Next(1, 10000)); - // 匹配中 匹配成功返回房间号 - Task.Factory.StartNew(() => + if (MainController != null) { - // 创建新线程,防止主界面阻塞 - Thread.Sleep(3000); - while (loop < 10000 && Config.FunGame_isMatching) + TaskUtility.NewTask(async () => { - loop++; - if (loop == Convert.ToInt32(roomid)) + Config.FunGame_isMatching = true; + if (await MainController.MatchRoomAsync(Config.FunGame_GameMode)) { - // 创建委托,操作主界面 - StartMatch_Action = (int i, object[]? objs) => + // 开始匹配 + while (Config.FunGame_isMatching) { - StartMatch_Method(i, objs); - }; - if (InvokeRequired) - { - Invoke(StartMatch_Action, (int)StartMatchState.Success, new object[] { roomid }); + if (_MatchSeconds < 60) + { + await Task.Delay(1000); + _MatchSeconds++; + SetMatchSecondsText(); + continue; + } + // 达到60秒时 + if (Config.FunGame_isMatching && await MainController.MatchRoomAsync(Config.FunGame_GameMode, true)) + { + // 取消匹配 + UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Success, General.HallInstance); + UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Cancel); + break; + } } - else - { - StartMatch_Action((int)StartMatchState.Success, new object[] { roomid }); - } - break; } - } - }); - break; - case (int)StartMatchState.Success: - Config.FunGame_isMatching = false; - // 匹配成功返回房间号 - roomid = "-1"; - if (objs != null) roomid = (string)objs[0]; - if (!roomid.Equals(-1)) - { - WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 匹配成功"); - WritelnGameInfo(">> 房间号: " + roomid); - SetRoomid(GetRoom(roomid)); + else Config.FunGame_isMatching = false; + }); } else { WritelnGameInfo("ERROR:匹配失败!"); - break; } - // 设置按钮可见性 - InRoom(); - // 创建委托,操作主界面 - StartMatch_Action = (i, objs) => - { - StartMatch_Method(i, objs); - }; - if (InvokeRequired) - { - Invoke(StartMatch_Action, (int)StartMatchState.Enable, new object[] { true }); - } - else - { - StartMatch_Action((int)StartMatchState.Enable, new object[] { true }); - } - MatchFunGame = null; break; - case (int)StartMatchState.Enable: + case StartMatchState.Success: + // 匹配成功返回房间号 + TaskUtility.NewTask(async () => + { + Room room = General.HallInstance; + if (objs != null) room = (Room)objs[0]; + if (room.Roomid != "-1") + { + GetMessage("匹配成功 -> 房间号: " + room.Roomid); + if (MainController != null) await MainController.UpdateRoomAsync(); + if (Rooms.IsExist(room.Roomid)) + { + Room target = Rooms[room.Roomid]; + await InvokeController_IntoRoom(target); + } + else + { + GetMessage("加入房间失败!原因:房间号不存在或已被解散。"); + } + } + else + { + GetMessage("匹配失败!暂时无法找到符合条件的房间。"); + } + // 更新按钮图标和文字 + UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Enable, true); + StopMatch.Visible = false; + StartMatch.Visible = true; + }); + break; + case StartMatchState.Enable: // 设置匹配过程中的各种按钮是否可用 - bool isPause = false; - if (objs != null) isPause = (bool)objs[0]; - CheckMix.Enabled = isPause; - CheckTeam.Enabled = isPause; - CheckHasPass.Enabled = isPause; - CreateRoom.Enabled = isPause; - RoomBox.Enabled = isPause; - Login.Enabled = isPause; + bool isEnabel = false; + if (objs != null) isEnabel = (bool)objs[0]; + CheckMix.Enabled = isEnabel; + CheckTeam.Enabled = isEnabel; + CheckHasPass.Enabled = isEnabel; + CreateRoom.Enabled = isEnabel; + RoomBox.Enabled = isEnabel; + RefreshRoomList.Enabled = isEnabel; + Logout.Enabled = isEnabel; break; - case (int)StartMatchState.Cancel: + case StartMatchState.Cancel: + Config.FunGame_isMatching = false; WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 终止匹配"); WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已终止匹配。"); - Config.FunGame_isMatching = false; - StartMatch_Action = (i, objs) => - { - StartMatch_Method(i, objs); - }; - if (InvokeRequired) - { - Invoke(StartMatch_Action, (int)StartMatchState.Enable, new object[] { true }); - } - else - { - StartMatch_Action((int)StartMatchState.Enable, new object[] { true }); - } - MatchFunGame = null; + UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Enable, true); StopMatch.Visible = false; StartMatch.Visible = true; break; } } + /// + /// 更新当前匹配时间 + /// + private void SetMatchSecondsText() + { + if (_MatchSeconds <= 0) StopMatch.Text = "停止匹配"; + if (_MatchSeconds < 60) + { + StopMatch.Text = _MatchSeconds + " 秒"; + } + else StopMatch.Text = "1 分 " + (_MatchSeconds - 60) + " 秒"; + } + /// /// 登录账号,显示登出按钮 /// @@ -685,18 +696,13 @@ namespace Milimoe.FunGame.Desktop.UI /// private void StopMatch_Click() { - StartMatch_Action = (i, objs) => + TaskUtility.NewTask(async () => { - StartMatch_Method(i, objs); - }; - if (InvokeRequired) - { - Invoke(StartMatch_Action, (int)StartMatchState.Cancel, new object[] { true }); - } - else - { - StartMatch_Action((int)StartMatchState.Cancel, new object[] { true }); - } + if (MainController != null && await MainController.MatchRoomAsync(Config.FunGame_GameMode, true)) + { + UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Cancel); + } + }); } /// @@ -881,6 +887,25 @@ namespace Milimoe.FunGame.Desktop.UI }); } + /// + /// 鼠标离开停止匹配按钮时,恢复按钮文本为匹配时长 + /// + /// + /// + /// + private void StopMatch_MouseLeave(object? sender, EventArgs e) => SetMatchSecondsText(); + + /// + /// 鼠标轻触停止匹配按钮时,将文本从匹配时长转为停止匹配 + /// + /// + /// + /// + private void StopMatch_MouseHover(object? sender, EventArgs e) + { + StopMatch.Text = "停止匹配"; + } + #endregion #region 事件 @@ -903,6 +928,8 @@ namespace Milimoe.FunGame.Desktop.UI private void StartMatch_Click(object sender, EventArgs e) { // 开始匹配 + _MatchSeconds = 0; + SetMatchSecondsText(); WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 开始匹配"); WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 开始匹配"); WriteGameInfo(">> 匹配参数:"); @@ -911,25 +938,9 @@ namespace Milimoe.FunGame.Desktop.UI StartMatch.Visible = false; StopMatch.Visible = true; // 暂停其他按钮 - StartMatch_Method((int)StartMatchState.Enable, new object[] { false }); - // 创建委托,开始匹配 - StartMatch_Action = (i, objs) => - { - StartMatch_Method(i, objs); - }; - // 创建新线程匹配 - MatchFunGame = Task.Factory.StartNew(() => - { - - if (InvokeRequired) - { - Invoke(StartMatch_Action, (int)StartMatchState.Matching, null); - } - else - { - StartMatch_Action((int)StartMatchState.Matching, null); - } - }); + UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Enable, false); + // 开始匹配 + UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Matching); } /// @@ -1407,7 +1418,7 @@ namespace Milimoe.FunGame.Desktop.UI } break; case Constant.FunGame_Disconnect: - if (Config.FunGame_isConnected && MainController != null) + if (Config.FunGame_isConnected && !Config.FunGame_isMatching && MainController != null) { // 先退出登录再断开连接 bool SuccessLogOut = false;