Exemple #1
0
        /// <summary>
        /// 匹配算法 操作m_matchTeamQue
        /// 这个Update由MatchSystem进行调用
        /// </summary>
        public void MatchUpdate()
        {
            //TODO:首先要检测是否能够进行匹配
            if (m_matchingQue.Count < 1)
            {
                return;
            }


            //这里执行匹配算法
            //1 关卡容量maxCount
            //2 队伍人数
            //3 贪心算法
            // 注意 Queue 除了on开头的方法以外 其他的方法都不修改队伍的状态

            // 初始化
            List <MatchBucket>  matchBuckets  = new List <MatchBucket>();
            List <MatchTeamArg> matchTeamArgs = new List <MatchTeamArg>();

            foreach (var item in m_matchingQue)
            {
                MatchTeamArg matchTeamArg = new MatchTeamArg {
                    MatchTeam = item, index = -1
                };
                matchTeamArgs.Add(matchTeamArg);
            }

            //装桶
            for (int i = 0; i < matchTeamArgs.Count; i++)
            {
                MatchTeamArg matchTeamArg = matchTeamArgs[i];
                if (matchTeamArg.index != -1)
                {
                    continue;
                }
                int j = 0;
                for (; j < matchBuckets.Count; j++)
                {
                    MatchBucket matchBucket = matchBuckets[j];
                    int         count       = matchBucket.CurrentVolume;
                    if ((matchBucket.Capacity - matchBucket.CurrentVolume) >= matchTeamArg.MatchTeam.CurrentCount)//如果容量足够 则添加
                    {
                        matchBucket.matchTeams.Add(matchTeamArg.MatchTeam);
                        matchTeamArg.index = j;//设置当前所在桶编号
                    }
                }
                if (matchTeamArg.index < 0)//需要新的桶
                {
                    var matchBucket = MatchBucketPool.Instance.Fetch(m_maxMemberCount);
                    matchBucket.matchTeams.Add(matchTeamArg.MatchTeam);
                    matchTeamArg.index = j;
                    matchBuckets.Add(matchBucket);

                    if (j != matchBuckets.Count - 1)
                    {
                        Log.Error("索引出现问题");
                        return;
                    }
                }
            }

            // 把桶遍历
            for (int i = 0; i < matchBuckets.Count; i++)
            {
                var matchBucket = matchBuckets[i];
                if (matchBucket.CurrentVolume == matchBucket.Capacity)//桶的容量满了
                {
                    Log.Info($"第 {i} 桶满足条件 向GameServer发送开启战斗请求");
                    //向matchSystem发送这几个队伍的id

                    MatchQueueCompleteSingleMessage matchQueueCompleteSingleMessage = new MatchQueueCompleteSingleMessage();


                    // 将matchteam从匹配列表中删除
                    foreach (var item in matchBucket.matchTeams)
                    {
                        matchQueueCompleteSingleMessage.teamIds.Add(item.Id);
                        matchQueueCompleteSingleMessage.barrierId = m_id;
                        m_matchingQue.Remove(item);//从匹配队列中清除
                    }
                    //向MatchSystem发送队伍匹配完成消息
                    m_matchSystemClient.PostLocalMessage(matchQueueCompleteSingleMessage);
                }
                else
                {
                    //不符合 这里可以再进行一次桶合并策略,目前不做 进一步的算法优化
                }
            }
            // 释放所有的桶
            for (int i = 0; i < matchBuckets.Count; i++)
            {
                var matchBucket = matchBuckets[i];
                matchBucket.Dispose();
            }
        }
        /// <summary>
        /// 以下要处理收到的消息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public override Task OnMessage(ILocalMessage msg)
        {
            switch (msg.MessageId)
            {
            case GameServerConstDefine.MatchSystemCreateMatchTeam:
                CreateMatchTeamMessage createMatchTeamMessage = msg as CreateMatchTeamMessage;
                OnCreateMatchTeam(createMatchTeamMessage.playerId);
                break;

            case GameServerConstDefine.MatchSystemJoinMatchTeam:
                JoinMatchTeamMessage joinMatchTeamMessage = msg as JoinMatchTeamMessage;
                OnJoinMatchTeam(joinMatchTeamMessage.teamId, joinMatchTeamMessage.playerId);
                break;

            case GameServerConstDefine.MatchSystemExitMatchTeam:
                ExitMatchTeamMessage exitMatchTeamMessage = msg as ExitMatchTeamMessage;
                OnExitMatchTeam(exitMatchTeamMessage.teamId, exitMatchTeamMessage.playerId);
                break;

            case GameServerConstDefine.MatchSystemJoinMatchQueue:
                JoinMatchQueueMessage joinMatchQueueMessage = msg as JoinMatchQueueMessage;
                OnJoinMatchQueue(joinMatchQueueMessage.teamId, joinMatchQueueMessage.playerId, joinMatchQueueMessage.barrierId);
                break;

            case GameServerConstDefine.MatchSystemExitMatchQueue:
                ExitMatchQueueMessage exitMatchQueueMessage = msg as ExitMatchQueueMessage;
                OnExitMatchQueue(exitMatchQueueMessage.teamId, exitMatchQueueMessage.playerId);
                break;

            case GameServerConstDefine.MatchQueueCompleteSingle:    //来自 MatcingSystemQueue 的消息,通知system匹配完成
                MatchQueueCompleteSingleMessage matchQueueCompleteSingleMessage = msg as MatchQueueCompleteSingleMessage;
                OnCompleteMatching(matchQueueCompleteSingleMessage.teamIds, matchQueueCompleteSingleMessage.barrierId);
                break;

            case GameServerConstDefine.MatchSysteamMatchTeamUpdateInfo:
                MatchTeamUpdateInfoMessage matchTeamUpdateInfoMessage = msg as MatchTeamUpdateInfoMessage;
                OnUpdateMatchTeam(matchTeamUpdateInfoMessage.teamId);
                break;

            case GameServerConstDefine.MatchSystemPlayerShutdown:
                ToMatchPlayerShutdownMessage toMatchPlayerShutdownMessage = msg as ToMatchPlayerShutdownMessage;
                OnShutdownPlayer(toMatchPlayerShutdownMessage);
                break;

            case GameServerConstDefine.UpdateOnlinePlayerList:    //更新客户端在线玩家状态
                UpdateOnlinePlayerMessage updateOnlinePlayerMessage = msg as UpdateOnlinePlayerMessage;
                OnUpdateOnlinePlayerRpc(updateOnlinePlayerMessage);
                break;

            case GameServerConstDefine.ReleaseBattleToMatchTeam:
                ReleaseBattleToMatchTeamMessage releaseBattleToMatchTeamMessage = msg as ReleaseBattleToMatchTeamMessage;
                OnReleaseBattle(releaseBattleToMatchTeamMessage);
                break;

            default:
                break;
            }


            return(base.OnMessage(msg));
        }