/// <summary> /// 刷新队伍信息并且广播给队伍内玩家 /// </summary> private void OnUpdateMatchTeam(ulong teamId) { S2C_UpdateMatchTeamInfo message = null; MatchTeam matchTeam = null; if (!m_teamDic.TryGetValue(teamId, out matchTeam)) { return; } //队伍状态检测 if (matchTeam.State != MatchTeam.MatchTeamState.OPEN) { goto Result; } message = new S2C_UpdateMatchTeamInfo(); message.TeamInfo = new MatchTeamInfo(); message.TeamInfo.MatchTeamId = teamId; message.MatchTeamId = teamId; foreach (var item in matchTeam.GetMembers()) { message.TeamInfo.PlayerIds.Add(item); } Result: if (matchTeam != null && message != null) { PostLocalMessageToCtx(new SystemSendNetMessage { Message = message }, matchTeam?.GetMembers()); } }
/// <summary> /// 队伍进入匹配队列 /// 保证是队长发起 保证队伍人数大于0人 /// </summary> public void OnJoinMatchQueue(ulong teamId, string playerId, int barrierId) { S2CM_JoinMatchQueueComplete message = new S2CM_JoinMatchQueueComplete(); message.State = S2CM_JoinMatchQueueComplete.Types.State.Fail; //1 验证 队伍是否存在 MatchTeam matchTeam = null; GameMatchPlayerContextQueue gameMatchPlayerContextQueue = null; if (!m_teamDic.TryGetValue(teamId, out matchTeam)) { goto Result; } //2 验证队长是否合法 if (matchTeam.GetCaptainId() != playerId) { goto Result; } //3 验证team状态是否满足 if (matchTeam.State != MatchTeam.MatchTeamState.OPEN) { goto Result; } //4 验证是否有对应的关卡匹配队列 if (!m_gameMatchPlayerCtxQueDic.TryGetValue(barrierId, out gameMatchPlayerContextQueue)) { goto Result; } //5 向匹配队列加入该队伍 方法内修改matchTeam的状态 gameMatchPlayerContextQueue.OnJoinMatchQueue(matchTeam); message.State = S2CM_JoinMatchQueueComplete.Types.State.Ok; message.BarrierId = barrierId; message.MatchTeamId = teamId; Result: if (message.State == S2CM_JoinMatchQueueComplete.Types.State.Ok) { PostLocalMessageToCtx(new SystemSendNetMessage { Message = message }, matchTeam.GetMembers()); } else { PostLocalMessageToCtx(new SystemSendNetMessage { Message = message }, playerId); } }
/// <summary> /// 队伍离开匹配队列 /// 任何队伍中的玩家都能发起队伍离开匹配队列 /// </summary> public void OnExitMatchQueue(ulong teamId, string playerId) { //1 验证 队伍是否存在 MatchTeam matchTeam = null; if (!m_teamDic.TryGetValue(teamId, out matchTeam)) { return; } //2 验证队伍是否包含该玩家 if (!matchTeam.IsContain(playerId)) { return; } //3 检查队伍状态是否合法 if (matchTeam.State != MatchTeam.MatchTeamState.Matching) { return; } //4 获取匹配队列 var matchQueue = FindMatchTeamQueue(matchTeam); if (matchQueue == null) { return; } //5 从匹配队列中删除 matchQueue.OnExitMatchQueue(matchTeam); //6 向队伍内人广播退出匹配队列 if (matchTeam.State == MatchTeam.MatchTeamState.OPEN) { PostLocalMessageToCtx(new SystemSendNetMessage { Message = new S2CM_ExitMatchQueue { LaunchPlayerId = playerId, MatchTeamId = teamId, State = S2CM_ExitMatchQueue.Types.State.Client } }, matchTeam?.GetMembers()); } }
/// <summary> /// 玩家加入队伍 /// </summary> /// <param name="teamId"></param> /// <param name="playerId"></param> private void OnJoinMatchTeam(UInt64 teamId, string playerId) { MatchTeam matchTeam = null; S2CM_JoinMatchTeamComplete message = new S2CM_JoinMatchTeamComplete(); message.State = S2CM_JoinMatchTeamComplete.Types.State.Complete; message.LaunchPlayerId = playerId; ulong realTeamId = 0; if ((realTeamId = FindMatchTeamById(playerId)) != default) { Log.Error("加入队伍失败,因为玩家已经在队伍中了"); message.State = S2CM_JoinMatchTeamComplete.Types.State.HaveTeam; message.MatchTeamId = realTeamId; goto Result; } if (!m_teamDic.TryGetValue(teamId, out matchTeam)) { message.State = S2CM_JoinMatchTeamComplete.Types.State.SystemError; goto Result; } //队伍状态检测 if (matchTeam.State != MatchTeam.MatchTeamState.OPEN) { message.State = S2CM_JoinMatchTeamComplete.Types.State.SystemError; goto Result; } //检测队伍容量 if (!matchTeam.IsFull()) { message.State = S2CM_JoinMatchTeamComplete.Types.State.SystemError; goto Result; } matchTeam.Add(playerId);//向队伍内添加玩家 message.LaunchPlayerId = playerId; message.MatchTeamId = matchTeam.Id; Result: if (matchTeam == null) { PostLocalMessageToCtx(new SystemSendNetMessage { Message = message }, playerId); } else { PostLocalMessageToCtx(new SystemSendNetMessage { Message = message }, matchTeam?.GetMembers()); if (matchTeam.CurrentCount <= 0)//如果这时候队伍的人数为空 则执行清除工作 并设置队伍的状态为关闭 { //可以执行清除任务了 if (m_teamDic.TryRemove(matchTeam.Id, out matchTeam)) { matchTeam.State = MatchTeam.MatchTeamState.CLOSE; } } } }
/// <summary> /// 玩家离开队伍 /// </summary> private void OnExitMatchTeam(UInt64 teamId, string playerId) { //如果离开队伍后 房间人数为0 那么就执行清除任务 并设置房间的状态为Close S2CM_ExitMatchTeamComplete message = new S2CM_ExitMatchTeamComplete { State = S2CM_ExitMatchTeamComplete.Types.State.Ok }; message.LaunchPlayerId = playerId; message.MatchTeamId = teamId; MatchTeam matchTeam = null; ulong realTeamId = FindMatchTeamById(playerId); //首先不信任客户端 在服务器内找到玩家 if (realTeamId == default) //找不到说明是个野消息 { message.State = S2CM_ExitMatchTeamComplete.Types.State.Fail; goto Result; } else { if (realTeamId == teamId) //如果队伍一样 说明是真实消息 { if (!m_teamDic.TryGetValue(teamId, out matchTeam)) //没有队伍 { message.State = S2CM_ExitMatchTeamComplete.Types.State.Fail; goto Result; } } else//否则 就是虚假消息,目前决策是这个用户是个脏用户,也需要被清除,直接从找到的真是队伍中删除 { if (!m_teamDic.TryGetValue(realTeamId, out matchTeam))//没有队伍 { message.State = S2CM_ExitMatchTeamComplete.Types.State.Fail; goto Result; } } } if (matchTeam.State == MatchTeam.MatchTeamState.CLOSE) //队伍不开放暂时不能退出,脏的也不让退 { message.State = S2CM_ExitMatchTeamComplete.Types.State.Fail; Log.Info("由于队伍已经处于关闭所以退出失败 = " + playerId); goto Result; } OnExitMatchQueue(matchTeam.Id, playerId); if (!matchTeam.Remove(playerId))//从队伍中移除 { message.State = S2CM_ExitMatchTeamComplete.Types.State.Fail; goto Result; } message.LaunchPlayerId = playerId; message.MatchTeamId = matchTeam.Id; //TODO:还要向战斗系统发送玩家退出的请求 Result: PostLocalMessageToCtx(new SystemSendNetMessage { Message = message }, playerId); //向发起者发送 if (matchTeam != null) { PostLocalMessageToCtx(new SystemSendNetMessage { Message = message }, matchTeam.GetMembers()); if (matchTeam.CurrentCount <= 0)//如果这时候队伍的人数为空 则执行清除工作 并设置队伍的状态为关闭 { //可以执行清除任务了 if (m_teamDic.TryRemove(matchTeam.Id, out matchTeam)) { matchTeam.State = MatchTeam.MatchTeamState.CLOSE; } } } }