Пример #1
0
        private void CheckServerLoadProc(DateTime now, ServerLoadContext context)
        {
            context.AlivedServerCount    = 0;
            context.AlivedGameFuBenCount = 0;
            context.ServerLoadAvg        = 0;
            context.IdelActiveServerQueue.Clear();

            foreach (var srv in Persistence.KuaFuServerIdGameConfigDict.Values)
            {
                if (srv.GameType == (int)GameTypes.HuanYingSiYuan)
                {
                    HuanYingSiYuanAgent huanYingSiYuanAgent;
                    if (ServerId2KuaFuClientAgent.TryGetValue(srv.ServerId, out huanYingSiYuanAgent))
                    {
                        int state = 0;
                        int load  = huanYingSiYuanAgent.GetAliveGameFuBenCount();
                        if (huanYingSiYuanAgent.IsAlive)
                        {
                            context.AlivedServerCount++;
                            context.AlivedGameFuBenCount += load;
                            state = 1;

                            if (load < srv.Capacity)
                            {
                                context.IdelActiveServerQueue.AddLast(srv);
                            }
                        }

                        Persistence.UpdateServerLoadState(huanYingSiYuanAgent.ClientInfo.ServerId, load, state);
                    }
                }
            }

            context.CalcServerLoadAvg();
        }
Пример #2
0
        private bool AssignGameFuben(KuaFuRoleData kuaFuRoleData, ServerLoadContext context, long waitSecs1, long waitSecs2, DateTime now)
        {
            int                     roleCount               = 0;
            DateTime                stateEndTime            = now.AddSeconds(EnterGameSecs);
            HuanYingSiYuanAgent     huanYingSiYuanAgent     = null;
            HuanYingSiYuanFuBenData huanYingSiYuanFuBenData = null;
            IKuaFuClient            kuaFuClient             = null;

            KuaFuFuBenRoleData kuaFuFuBenRoleData = new KuaFuFuBenRoleData()
            {
                ServerId  = kuaFuRoleData.ServerId,
                RoleId    = kuaFuRoleData.RoleId,
                ZhanDouLi = HuanYingSiYuanGameData.GetZhanDouLi(kuaFuRoleData.GameData),
            };

            List <KuaFuRoleData> updateRoleDataList = new List <KuaFuRoleData>();

            try
            {
                //先检测是否有需要补充人的队伍
                foreach (var fuBenData in ShotOfRolesFuBenDataDict.Values)
                {
                    //分组编号要相等
                    if (fuBenData.CanRemove())
                    {
                        RemoveGameFuBen(fuBenData);
                    }
                    else if (fuBenData.CanEnter(kuaFuRoleData.GroupIndex, waitSecs1, waitSecs2))
                    {
                        if (ServerId2KuaFuClientAgent.TryGetValue(fuBenData.ServerId, out huanYingSiYuanAgent) && huanYingSiYuanAgent.IsAlive)
                        {
                            roleCount = fuBenData.AddKuaFuFuBenRoleData(kuaFuFuBenRoleData, GameFuBenRoleCountChangedHandler);
                            if (roleCount > 0)
                            {
                                huanYingSiYuanFuBenData = fuBenData;
                                break;
                            }
                        }
                    }
                }

                if (null == huanYingSiYuanFuBenData)
                {
                    //按负载状态在一个跨服活动服务器上分配一个新的游戏副本,并加入
                    LinkedListNode <KuaFuServerGameConfig> node = context.IdelActiveServerQueue.First;
                    int count = context.IdelActiveServerQueue.Count;
                    for (int i = 0; i < count && node != null; i++)
                    {
                        KuaFuServerGameConfig srv = node.Value;
                        LinkedListNode <KuaFuServerGameConfig> next = node.Next;

                        if (ServerId2KuaFuClientAgent.TryGetValue(srv.ServerId, out huanYingSiYuanAgent) && huanYingSiYuanAgent.IsAlive)
                        {
                            int serverLoad = huanYingSiYuanAgent.GetAliveGameFuBenCount();
                            if (serverLoad < srv.Capacity && serverLoad <= context.ServerLoadAvg)
                            {
                                try
                                {
                                    huanYingSiYuanFuBenData = Persistence.CreateHysyGameFuBen(huanYingSiYuanAgent, kuaFuRoleData.GroupIndex, 1);
                                    if (huanYingSiYuanFuBenData != null)
                                    {
                                        AddGameFuBen(huanYingSiYuanFuBenData, huanYingSiYuanAgent);
                                        roleCount = huanYingSiYuanFuBenData.AddKuaFuFuBenRoleData(kuaFuFuBenRoleData, GameFuBenRoleCountChangedHandler);
                                        if (roleCount > 0)
                                        {
                                            context.AlivedGameFuBenCount++;
                                            context.CalcServerLoadAvg();
                                            break;
                                        }
                                    }
                                }
                                catch (System.Exception ex)
                                {
                                    huanYingSiYuanAgent.MaxActiveTicks = 0;
                                }
                            }
                            else
                            {
                                context.IdelActiveServerQueue.Remove(node);
                                if (serverLoad < srv.Capacity)
                                {
                                    context.IdelActiveServerQueue.AddLast(node);
                                }
                            }
                        }

                        node = next;
                    }
                }

                if (huanYingSiYuanFuBenData != null && roleCount > 0)
                {
                    if (roleCount == 1)
                    {
                        huanYingSiYuanFuBenData.EndTime = now; //第一个人进入时,重置副本创建时间
                    }

                    if (huanYingSiYuanFuBenData.State == GameFuBenState.Wait)
                    {
                        if (roleCount == Consts.HuanYingSiYuanRoleCountTotal)
                        {
                            List <KuaFuFuBenRoleData> roleList = huanYingSiYuanFuBenData.SortFuBenRoleList();
                            foreach (var role in roleList)
                            {
                                KuaFuRoleData kuaFuRoleDataTemp;
                                KuaFuRoleKey  key = KuaFuRoleKey.Get(role.ServerId, role.RoleId);
                                if (RoleIdKuaFuRoleDataDict.TryGetValue(key, out kuaFuRoleDataTemp))
                                {
                                    kuaFuRoleDataTemp.UpdateStateTime(huanYingSiYuanFuBenData.GameId, KuaFuRoleStates.NotifyEnterGame, stateEndTime.Ticks);
                                }
                            }

                            huanYingSiYuanFuBenData.State = GameFuBenState.Start;
                            NotifyFuBenRoleEnterGame(huanYingSiYuanFuBenData);
                        }
                        else
                        {
                            kuaFuRoleData.UpdateStateTime(huanYingSiYuanFuBenData.GameId, KuaFuRoleStates.SignUpWaiting, kuaFuRoleData.StateEndTicks);
                            NotifyFuBenRoleCount(huanYingSiYuanFuBenData);
                        }
                    }
                    else if (huanYingSiYuanFuBenData.State == GameFuBenState.Start)
                    {
                        kuaFuRoleData.UpdateStateTime(huanYingSiYuanFuBenData.GameId, KuaFuRoleStates.NotifyEnterGame, stateEndTime.Ticks);
                        NotifyFuBenRoleEnterGame(huanYingSiYuanFuBenData);
                    }
                }
                else
                {
                    //如果分配失败,则返回false,本轮不在尝试分配
                    return(false);
                }

                return(true);
            }
            catch (System.Exception ex)
            {
                LogManager.WriteExceptionUseCache(ex.ToString());
            }

            return(false);
        }
Пример #3
0
        private void CheckRoleTimerProc(DateTime now, ServerLoadContext context)
        {
            context.SignUpRoleCount    = 0;
            context.StartGameRoleCount = 0;
            bool assgionGameFuBen   = true;
            long maxRemoveRoleTicks = now.AddHours(-2).Ticks;
            long waitTicks1         = now.AddSeconds(-Persistence.SignUpWaitSecs1).Ticks;
            long waitTicks2         = now.AddSeconds(-Persistence.SignUpWaitSecs2).Ticks;

            foreach (var kuaFuRoleData in RoleIdKuaFuRoleDataDict.Values)
            {
                int oldGameId = 0;
                lock (kuaFuRoleData)
                {
                    //清理超时角色
                    if (kuaFuRoleData.State == KuaFuRoleStates.None || kuaFuRoleData.State > KuaFuRoleStates.StartGame)
                    {
                        if (kuaFuRoleData.StateEndTicks < maxRemoveRoleTicks)
                        {
                            KuaFuRoleData kuaFuRoleDataTemp;
                            RoleIdKuaFuRoleDataDict.TryRemove(KuaFuRoleKey.Get(kuaFuRoleData.ServerId, kuaFuRoleData.RoleId), out kuaFuRoleDataTemp);
                            continue;
                        }
                    }
                    else if (kuaFuRoleData.State == KuaFuRoleStates.NotifyEnterGame || kuaFuRoleData.State == KuaFuRoleStates.EnterGame)
                    {
                        if (kuaFuRoleData.StateEndTicks < now.Ticks)
                        {
                            kuaFuRoleData.Age++;
                            kuaFuRoleData.State = KuaFuRoleStates.None;
                            oldGameId           = kuaFuRoleData.GameId;
                        }
                    }
                }

                if (kuaFuRoleData.State == KuaFuRoleStates.SignUp)
                {
                    context.SignUpRoleCount++;
                    if (assgionGameFuBen)
                    {
                        assgionGameFuBen = AssignGameFuben(kuaFuRoleData, context, waitTicks1, waitTicks2, now);
                    }
                }
                else if (kuaFuRoleData.State == KuaFuRoleStates.SignUpWaiting)
                {
                    context.SignUpRoleCount++;
                }
                else if (kuaFuRoleData.State == KuaFuRoleStates.StartGame)
                {
                    context.StartGameRoleCount++;
                }

                if (oldGameId > 0)
                {
                    RemoveRoleFromFuBen(oldGameId, kuaFuRoleData.RoleId);
                    HuanYingSiYuanAgent huanYingSiYuanAgent = GetKuaFuAgent(kuaFuRoleData.ServerId);
                    if (null != huanYingSiYuanAgent)
                    {
                        huanYingSiYuanAgent.NotifyChangeState(kuaFuRoleData);
                    }
                }
            }
        }