feat:客户端断开时更新房间状态

This commit is contained in:
lishuo 2023-07-12 10:39:06 +08:00
parent b6ea5934c5
commit 21e4490934
2 changed files with 55 additions and 8 deletions

View File

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Host" value="http://pf.juze.pro/"/>
<add key="Host" value="https://wx.powerfun.com.cn/"/>
<add key="Top" value="0"/>
<add key="ShowVirtualUser" value="true"/>
<add key="Port" value="21000"/>
<add key="TcpPort" value="21001"/>
<add key="Port" value="11000"/>
<add key="TcpPort" value="11001"/>
<!-- DEV -->
<add key="DevHost" value="http://pf.juze.pro/"/>
<add key="DevTop" value="0"/>

View File

@ -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 = "";