예제 #1
0
        private void AddGameFuBen(HuanYingSiYuanFuBenData huanYingSiYuanFuBenData, HuanYingSiYuanAgent huanYingSiYuanAgent)
        {
            HuanYingSiYuanFuBenDataDict[huanYingSiYuanFuBenData.GameId] = huanYingSiYuanFuBenData;

            //增加服务器负载计数
            huanYingSiYuanAgent.AddGameFuBen(huanYingSiYuanFuBenData.GameId);
        }
예제 #2
0
        public int RoleChangeState(int serverId, int roleId, int state)
        {
            HuanYingSiYuanAgent huanYingSiYuanAgent = null;

            //查找对应的代理对象
            KuaFuRoleData kuaFuRoleData;

            if (!ServerId2KuaFuClientAgent.TryGetValue(serverId, out huanYingSiYuanAgent))
            {
                return(StdErrorCode.Error_Server_Internal_Error);
            }

            //添加或更新角色信息和状态
            KuaFuRoleKey key = KuaFuRoleKey.Get(serverId, roleId);

            if (!RoleIdKuaFuRoleDataDict.TryGetValue(key, out kuaFuRoleData))
            {
                return(StdErrorCode.Error_Server_Internal_Error);
            }

            //更新到新的状态
            int oldGameId = 0;

            lock (kuaFuRoleData)
            {
                if (state == (int)KuaFuRoleStates.None)
                {
                    oldGameId            = kuaFuRoleData.GameId;
                    kuaFuRoleData.GameId = 0;
                }

                kuaFuRoleData.Age++;
                kuaFuRoleData.State = (KuaFuRoleStates)state;
            }

            //if (kuaFuRoleData.State != KuaFuRoleStates.SignUp)
            //{
            //    KuaFuRoleData kuaFuRoleDataTemp;
            //    WaitingKuaFuRoleDataDict.TryRemove(key, out kuaFuRoleDataTemp);
            //}

            if (oldGameId > 0)
            {
                RemoveRoleFromFuBen(oldGameId, roleId);
            }

            return(state);
        }
예제 #3
0
        public int GameFuBenRoleChangeState(int serverId, int roleId, int gameId, int state)
        {
            HuanYingSiYuanFuBenData huanYingSiYuanFuBenData;

            if (HuanYingSiYuanFuBenDataDict.TryGetValue(gameId, out huanYingSiYuanFuBenData))
            {
                lock (huanYingSiYuanFuBenData)
                {
                    KuaFuFuBenRoleData kuaFuFuBenRoleData;
                    if (huanYingSiYuanFuBenData.RoleDict.TryGetValue(roleId, out kuaFuFuBenRoleData))
                    {
                        if ((KuaFuRoleStates)state == KuaFuRoleStates.Offline || (KuaFuRoleStates)state == KuaFuRoleStates.None)
                        {
                            RemoveRoleFromFuBen(gameId, roleId);
                        }
                    }
                }
            }

            KuaFuRoleData kuaFuRoleData;

            //添加或更新角色信息和状态
            if (!RoleIdKuaFuRoleDataDict.TryGetValue(KuaFuRoleKey.Get(serverId, roleId), out kuaFuRoleData))
            {
                return(StdErrorCode.Error_Not_Exist);
            }

            HuanYingSiYuanAgent huanYingSiYuanAgent = null;

            //查找对应的代理对象
            if (ServerId2KuaFuClientAgent.TryGetValue(serverId, out huanYingSiYuanAgent))
            {
                //更新到新的状态
                if (kuaFuRoleData.GameId == gameId)
                {
                    ChangeRoleState(huanYingSiYuanAgent, kuaFuRoleData, (KuaFuRoleStates)state);
                }
            }

            return(state);
        }
예제 #4
0
        private int ChangeRoleState(HuanYingSiYuanAgent huanYingSiYuanAgent, KuaFuRoleData kuaFuRoleData, KuaFuRoleStates state)
        {
            int result = -1;

            try
            {
                IKuaFuClient  kuaFuClient = null;
                KuaFuRoleData roleData    = null;
                int           oldGameId   = 0;
                lock (kuaFuRoleData)
                {
                    kuaFuRoleData.Age++;
                    kuaFuRoleData.State = state;
                    if (state == KuaFuRoleStates.None && kuaFuRoleData.GameId > 0)
                    {
                        oldGameId = kuaFuRoleData.GameId;
                    }

                    kuaFuClient = huanYingSiYuanAgent.KuaFuClient;
                    roleData    = (KuaFuRoleData)kuaFuRoleData;
                }

                if (oldGameId > 0)
                {
                    RemoveRoleFromFuBen(oldGameId, kuaFuRoleData.RoleId);
                }

                if (null != kuaFuClient && null != roleData)
                {
                    result = huanYingSiYuanAgent.NotifyChangeState(kuaFuRoleData);
                }
            }
            catch (System.Exception ex)
            {
                return(-1);
            }

            return(result);
        }
예제 #5
0
        /// <summary>
        /// 创建一个幻影寺院副本
        /// </summary>
        /// <param name="huanYingSiYuanAgent"></param>
        /// <param name="groupIndex"></param>
        /// <param name="roleNum"></param>
        /// <returns></returns>
        public HuanYingSiYuanFuBenData CreateHysyGameFuBen(HuanYingSiYuanAgent huanYingSiYuanAgent, int groupIndex, int roleNum)
        {
            int fuBenSeqId = huanYingSiYuanAgent.PopFuBenSeqId();

            if (fuBenSeqId <= 0)
            {
                return(null);
            }

            int gameId;
            int serverId = huanYingSiYuanAgent.ClientInfo.ServerId;

            try
            {
                gameId = (int)DbHelperMySQL.GetSingle(string.Format("select CreateHysyGameFuBen({0},{1},{2})", serverId, fuBenSeqId, roleNum));
                //id = (int)DbHelperMySQL.GetSingle(string.Format("select CreateHysyGameFuBen('{0}',{1},{2},{3})", DateTime.Now.ToString("yyMMdd"), serverId, fuBenSeqId, roleNum));
            }
            catch (System.Exception ex)
            {
                LogManager.WriteExceptionUseCache(ex.ToString());
                gameId = -1;
            }

            HuanYingSiYuanFuBenData huanYingSiYuanFuBenData = null;

            if (gameId > 0)
            {
                huanYingSiYuanFuBenData = new HuanYingSiYuanFuBenData()
                {
                    ServerId   = serverId,
                    GroupIndex = groupIndex,
                    SequenceId = fuBenSeqId,
                    GameId     = gameId,
                    EndTime    = Global.NowTime.AddMinutes(Consts.HuanYingSiYuanGameFuBenMaxExistMinutes),
                };
            }

            return(huanYingSiYuanFuBenData);
        }
예제 #6
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);
        }
예제 #7
0
        /// <summary>
        /// 初始化跨服客户端回调对象
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="age"></param>
        /// <param name="version"></param>
        /// <returns>clientId</returns>
        public int InitializeClient(IKuaFuClient callback, KuaFuClientContext clientInfo)
        {
            int  clientId  = -1;
            bool firstInit = false;

            try
            {
                if (clientInfo.GameType == (int)GameTypes.HuanYingSiYuan && clientInfo.ServerId != 0)
                {
                    //如果是第一次初始化,需要清空原始的
                    if (clientInfo.ClientId == 0)
                    {
                        firstInit = true;
                    }

                    Lazy <HuanYingSiYuanAgent> lazy = new Lazy <HuanYingSiYuanAgent>(() =>
                    {
                        clientInfo.ClientId = Persistence.GetUniqueClientId(clientInfo.ServerId);
                        return(new HuanYingSiYuanAgent(clientInfo, callback));
                    });

                    HuanYingSiYuanAgent huanYingSiYuanAgent = ServerId2KuaFuClientAgent.GetOrAdd(clientInfo.ServerId, (x) => { return(lazy.Value); });
                    if (clientInfo.ClientId != huanYingSiYuanAgent.ClientInfo.ClientId)
                    {
                        if (clientInfo.ClientId > 0)
                        {
                            if (huanYingSiYuanAgent.IsClientAlive())
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("InitializeClient服务器ID重复,禁止连接.ServerId:{0},ClientId:{1}", clientInfo.ServerId, clientInfo.ClientId));
                                return(StdErrorCode.Error_Duplicate_Key_ServerId);
                            }
                        }

                        clientId = huanYingSiYuanAgent.ClientInfo.ClientId;
                    }
                    else
                    {
                        clientId = clientInfo.ClientId;
                    }

                    if (firstInit)
                    {
                        huanYingSiYuanAgent.Reset();
                        List <int> list = huanYingSiYuanAgent.GetAliveGameFuBenList();
                        if (null != list && list.Count > 0)
                        {
                            foreach (var id in list)
                            {
                                HuanYingSiYuanFuBenData fuBenData;
                                if (HuanYingSiYuanFuBenDataDict.TryGetValue(id, out fuBenData))
                                {
                                    RemoveGameFuBen(fuBenData, huanYingSiYuanAgent);
                                }
                                else
                                {
                                    huanYingSiYuanAgent.RemoveGameFuBen(id);
                                }
                            }
                        }
                    }
                }
                else
                {
                    LogManager.WriteLog(LogTypes.Error, string.Format("InitializeClient时GameType错误,禁止连接.ServerId:{0},GameType:{1}", clientInfo.ServerId, clientInfo.GameType));
                    return(StdErrorCode.Error_Invalid_GameType);
                }
            }
            catch (System.Exception ex)
            {
                LogManager.WriteExceptionUseCache(string.Format("InitializeClient服务器ID重复,禁止连接.ServerId:{0},ClientId:{1}", clientInfo.ServerId, clientInfo.ClientId));
                return(StdErrorCode.Error_Server_Internal_Error);
            }

            return(clientId);
        }
예제 #8
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);
                    }
                }
            }
        }
예제 #9
0
        private void RemoveGameFuBen(HuanYingSiYuanFuBenData huanYingSiYuanFuBenData, HuanYingSiYuanAgent huanYingSiYuanAgent = null)
        {
            int gameId = huanYingSiYuanFuBenData.GameId;
            HuanYingSiYuanFuBenData huanYingSiYuanFuBenDataTemp;

            ShotOfRolesFuBenDataDict.TryRemove(gameId, out huanYingSiYuanFuBenDataTemp);
            if (HuanYingSiYuanFuBenDataDict.TryRemove(gameId, out huanYingSiYuanFuBenDataTemp))
            {
                huanYingSiYuanFuBenDataTemp.State = GameFuBenState.End;

                //减少服务器负载计数
                if (null == huanYingSiYuanAgent)
                {
                    if (!ServerId2KuaFuClientAgent.TryGetValue(huanYingSiYuanFuBenDataTemp.ServerId, out huanYingSiYuanAgent))
                    {
                        huanYingSiYuanAgent = null;
                    }
                }

                if (null != huanYingSiYuanAgent)
                {
                    huanYingSiYuanAgent.RemoveGameFuBen(huanYingSiYuanFuBenDataTemp.GameId);
                }
            }

            lock (huanYingSiYuanFuBenData)
            {
                foreach (var fuBenRoleData in huanYingSiYuanFuBenData.RoleDict.Values)
                {
                    KuaFuRoleData kuaFuRoleData;
                    if (RoleIdKuaFuRoleDataDict.TryGetValue(KuaFuRoleKey.Get(fuBenRoleData.ServerId, fuBenRoleData.RoleId), out kuaFuRoleData))
                    {
                        if (kuaFuRoleData.GameId == gameId)
                        {
                            kuaFuRoleData.State = KuaFuRoleStates.None;
                        }
                    }
                }
            }
        }