From 21e44909346f5eaea95d85c85df855b22b9fc373 Mon Sep 17 00:00:00 2001 From: lishuo Date: Wed, 12 Jul 2023 10:39:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=96=AD?= =?UTF-8?q?=E5=BC=80=E6=97=B6=E6=9B=B4=E6=96=B0=E6=88=BF=E9=97=B4=E7=8A=B6?= =?UTF-8?q?=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.config | 6 ++-- ViewModels/MainWindowViewModel.cs | 57 ++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/App.config b/App.config index 9f64160..92f0d49 100644 --- a/App.config +++ b/App.config @@ -1,11 +1,11 @@  - + - - + + diff --git a/ViewModels/MainWindowViewModel.cs b/ViewModels/MainWindowViewModel.cs index 67a76ec..51906b1 100644 --- a/ViewModels/MainWindowViewModel.cs +++ b/ViewModels/MainWindowViewModel.cs @@ -282,6 +282,7 @@ namespace OnlineUserPool.ViewModels case 4: BroadCastGameRoomList(); break; } } + private void SendGameRoomMessage(RoomModel gameRoom) { if (gameRoom == null) @@ -311,6 +312,7 @@ namespace OnlineUserPool.ViewModels client.Request = ""; } } + private void BroadCastGameRoomList() { //广播 @@ -332,6 +334,7 @@ namespace OnlineUserPool.ViewModels item.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, item.IPEndPoint); } } + //查询房间列表 private void HandleQueryGameRoomList(ReceiveModel msg) { @@ -383,6 +386,7 @@ namespace OnlineUserPool.ViewModels bool isZip = needSend.Encoding == "gzip"; needSend.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, needSend.IPEndPoint); } + //创建房间 private void HandleCreateGameRoom(ReceiveModel msg) { @@ -430,6 +434,7 @@ namespace OnlineUserPool.ViewModels RoomList.Add(room); RoomMaxId = room.RoomId; } + //先移除当前用户在其他房间的信息(考虑服务器重启的时候没有响应客户端退出命令) private void RemoveOtherRoomInfo(int userId, int roomId) { @@ -446,6 +451,7 @@ namespace OnlineUserPool.ViewModels SendGameRoomMessage(preRoom); } } + //处理当前用户加入房间 private void HandleJoinGameRoom(ReceiveModel msg) { @@ -485,6 +491,7 @@ namespace OnlineUserPool.ViewModels //发送当前房间的信息给当前房间内的人 SendGameRoomMessage(room); } + //处理当前用户准备状态 private void HandleGameRoomReadyStatus(ReceiveModel msg) { @@ -502,6 +509,7 @@ namespace OnlineUserPool.ViewModels //发送信息给当前房间内的人 SendGameRoomMessage(room); } + //处理当前用户准备状态 private void HandleGameRoomStart(ReceiveModel msg) { @@ -520,6 +528,7 @@ namespace OnlineUserPool.ViewModels //发送信息给当前房间内的人 SendGameRoomMessage(room); } + //处理房间踢人的操作 private void HandleGameRoomKick(ReceiveModel msg) { @@ -651,7 +660,7 @@ namespace OnlineUserPool.ViewModels { if (item.Expire) { - Clients.Remove(item); + RemoveClient(item); } }); }); @@ -700,7 +709,7 @@ namespace OnlineUserPool.ViewModels var info = Clients.FirstOrDefault(n => n.MemberId == msgs[i].MemberId); if (info != null) { - Clients.Remove(info); + RemoveClient(info); } } } @@ -711,7 +720,7 @@ namespace OnlineUserPool.ViewModels { if (item.Expire) { - Clients.Remove(item); + RemoveClient(item); } }); }); @@ -900,7 +909,7 @@ namespace OnlineUserPool.ViewModels var client = Clients.FirstOrDefault(f => f.IPEndPoint.ToString() == point.ToString()); if (client != null) { - Clients.Remove(client); + RemoveClient(client); GameRoomDisConnectHandler(client); } } @@ -910,8 +919,46 @@ namespace OnlineUserPool.ViewModels } }); } - } + + private void RemoveClient(HostModel client) + { + BeforeRemoved(client); + Clients.Remove(client); + } + private void BeforeRemoved(HostModel client) + { + if(client.RoomId == 0)return; + var room = RoomList.ToList().FirstOrDefault(c => c.RoomId == client.RoomId); + if(room == null)return; + + var needRemove = room.List.FirstOrDefault(c => c.UserId == client.MemberId); + if(needRemove == null)return; + + //从房间中移除,并考虑是否需要删除房间和移交房主 + room.List.Remove(needRemove); + if (room.List.Count == 0) + { + RoomList.Remove(room); + } + else + { + var host = room.List.FirstOrDefault(); + if (host != null) + { + host.IsOwner = true; + } + } + + //更新房间状态 + var isComplete = room.List.All(c => c.Saved != false); + if (room.Status > 0) + { + room.Status = isComplete ? 2 : 1; + } + } + } + public class Data1 { private string _txt = "";