/// <summary> /// 创建一个匹配队伍,由玩家发起的本地消息 /// 创建的队伍并不直接放入匹配队列,而是通过匹配系统进行管理 /// 每个MatchTeam 有若干个状态进行控制 /// </summary> private void OnCreateMatchTeam(string playerId) { S2C_CreateMatchTeamComplete.Types.State state = S2C_CreateMatchTeamComplete.Types.State.Complete; ulong teamId = default; if (m_teamDic.Values.Count > m_gameMatchTeamConfig.MaxCount) { Log.Error("服务器队伍容量过载"); state = S2C_CreateMatchTeamComplete.Types.State.SystemError; goto Result; } if ((teamId = FindMatchTeamById(playerId)) != default) { Log.Info("创建队伍失败,因为玩家已经在队伍中了"); state = S2C_CreateMatchTeamComplete.Types.State.HaveTeam; goto Result; } //创建队伍,返回队伍id UInt64 id = m_roomIdFactory.AllocateSessionId(); MatchTeam matchTeam = new MatchTeam(id, m_gameMatchTeamConfig.TeamCapacity);//生成一个房间,并通知对应的玩家队伍生成好了 teamId = matchTeam.Id; //加入队伍字典中 m_teamDic.TryAdd(id, matchTeam); matchTeam.Add(playerId); Result: //向玩家发送 创建并且加入到队伍的消息 PostLocalMessageToCtx(new SystemSendNetMessage { Message = new S2C_CreateMatchTeamComplete { State = state, MatchTeamId = teamId }, PlayerId = playerId }, playerId); Log.Info($"创建房间Id = {teamId} 执行完毕 state = " + state.ToString()); }
/// <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; } } } }