using System;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
namespace ET.Server
{
///
/// 加入房间
///
[MessageHandler(SceneType.Game)]
public class C2G_JoinRoomHandler : AMRpcHandler
{
protected override async ETTask Run(Session session, C2G_JoinRoom request, G2C_JoinRoom response, Action reply)
{
Player player = session.GetComponent().GetMyPlayer();
if (player == null)
{
response.Error = ErrorCode.ERR_OperationError;
response.Message = "玩家不存在或离线...";
reply();
return;
}
// 判断参数
if (request.RoomId <= 0)
{
response.Error = ErrorCode.ERR_ParameterError;
response.Message = "参数错误,房间号有误...";
reply();
return;
}
// 判断玩家是否在其他房间
if (player.RoomId > 0 && request.RoomId != player.RoomId)
{
response.Error = ErrorCode.ERR_OperationError;
response.Message = "玩家已在房间,不可加入其他房间...";
reply();
return;
}
Scene scene = session.DomainScene();
// 请求的房间是否能找到
Room room = scene.GetComponent().Get(request.RoomId);
if (room == null)
{
response.Error = ErrorCode.ERR_OperationError;
response.Message = "请求的房间不存在或已解散...";
reply();
return;
}
// todo 玩家是否可以观战,如果可以 下面的判断屏蔽
// 房间人数是否已满
if (room.PlayerIsMax())
{
response.Error = ErrorCode.ERR_OperationError;
response.Message = "房间人数已满,不可进入...";
reply();
return;
}
// 房间状态是否可进入
HGHuangHuangComponent hgHuangHuangComponent = room.GetComponent();
if (hgHuangHuangComponent != null && hgHuangHuangComponent.State != 0)
{
response.Error = ErrorCode.ERR_OperationError;
response.Message = "房间已开始,不可进入...";
reply();
return;
}
// 是否已经在房间
if (!room.GetAllPlayers().ContainsKey(player.Id))
{
// 加入玩家
room.Add(player);
// 绑定玩家房间
player.RoomId = room.RoomId;
}
// 广播玩家加入房间
foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null))
{
MessageHelper.SendToClient(p, new G2C_JoinRoomPush(){info = ProtoHelper.RoomToProto(room, p, null)});
}
reply();
await ETTask.CompletedTask;
}
}
}