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); }
private bool AssignGameFuben(KuaFuRoleData kuaFuRoleData, long waitSecs1, long waitSecs2, DateTime now) { int roleCount = 0; DateTime stateEndTime = now.AddSeconds((double)this.EnterGameSecs); HuanYingSiYuanFuBenData selectFuben = null; KuaFuFuBenRoleData kuaFuFuBenRoleData = new KuaFuFuBenRoleData { ServerId = kuaFuRoleData.ServerId, RoleId = kuaFuRoleData.RoleId, ZhanDouLi = kuaFuRoleData.GameData.ZhanDouLi }; try { foreach (HuanYingSiYuanFuBenData tmpFuben in this.ShotOfRolesFuBenDataDict.Values) { if (tmpFuben.CanRemove()) { this.RemoveGameFuBen(tmpFuben); } else if (tmpFuben.CanEnter(kuaFuRoleData.GroupIndex, waitSecs1, waitSecs2)) { if (ClientAgentManager.Instance().IsAgentAlive(tmpFuben.ServerId)) { roleCount = tmpFuben.AddKuaFuFuBenRoleData(kuaFuFuBenRoleData, new GameFuBenRoleCountChanged(this.GameFuBenRoleCountChangedHandler)); if (roleCount > 0) { selectFuben = tmpFuben; break; } } } } if (null == selectFuben) { int gameId = this.Persistence.GetNextGameId(); int kfSrvId = 0; if (ClientAgentManager.Instance().AssginKfFuben(this.GameType, (long)gameId, 1, out kfSrvId)) { selectFuben = new HuanYingSiYuanFuBenData(); selectFuben.ServerId = kfSrvId; selectFuben.GameId = gameId; selectFuben.GroupIndex = kuaFuRoleData.GroupIndex; selectFuben.EndTime = Global.NowTime.AddMinutes(15.0); this.AddGameFuBen(selectFuben); roleCount = selectFuben.AddKuaFuFuBenRoleData(kuaFuFuBenRoleData, new GameFuBenRoleCountChanged(this.GameFuBenRoleCountChangedHandler)); this.Persistence.LogCreateHysyFuben(gameId, kfSrvId, 0, 1); } } if (selectFuben != null && roleCount > 0) { if (roleCount == 1) { selectFuben.EndTime = now; } if (selectFuben.State == GameFuBenState.Wait) { if (roleCount == Consts.HuanYingSiYuanRoleCountTotal) { List <KuaFuFuBenRoleData> roleList = selectFuben.SortFuBenRoleList(); foreach (KuaFuFuBenRoleData role in roleList) { KuaFuRoleKey key = KuaFuRoleKey.Get(role.ServerId, role.RoleId); KuaFuRoleData kuaFuRoleDataTemp; if (this.RoleIdKuaFuRoleDataDict.TryGetValue(key, out kuaFuRoleDataTemp)) { kuaFuRoleDataTemp.UpdateStateTime(selectFuben.GameId, KuaFuRoleStates.NotifyEnterGame, stateEndTime.Ticks); } } selectFuben.State = GameFuBenState.Start; this.NotifyFuBenRoleEnterGame(selectFuben); } else { kuaFuRoleData.UpdateStateTime(selectFuben.GameId, KuaFuRoleStates.SignUpWaiting, kuaFuRoleData.StateEndTicks); this.NotifyFuBenRoleCount(selectFuben); } } else if (selectFuben.State == GameFuBenState.Start) { kuaFuRoleData.UpdateStateTime(selectFuben.GameId, KuaFuRoleStates.NotifyEnterGame, stateEndTime.Ticks); this.NotifyFuBenRoleEnterGame(selectFuben); } return(true); } return(false); } catch (Exception ex) { LogManager.WriteExceptionUseCache(ex.ToString()); } return(false); }
private bool AssignGameFuben(KuaFuRoleData kuaFuRoleData, long waitSecs1, long waitSecs2, DateTime now) { int roleCount = 0; DateTime stateEndTime = now.AddSeconds(EnterGameSecs); HuanYingSiYuanFuBenData selectFuben = null; KuaFuFuBenRoleData kuaFuFuBenRoleData = new KuaFuFuBenRoleData() { ServerId = kuaFuRoleData.ServerId, RoleId = kuaFuRoleData.RoleId, ZhanDouLi = HuanYingSiYuanGameData.GetZhanDouLi(kuaFuRoleData.GameData), }; try { //先检测是否有需要补充人的队伍 foreach (var tmpFuben in ShotOfRolesFuBenDataDict.Values) { //分组编号要相等 if (tmpFuben.CanRemove()) { RemoveGameFuBen(tmpFuben); } else if (tmpFuben.CanEnter(kuaFuRoleData.GroupIndex, waitSecs1, waitSecs2)) { if (ClientAgentManager.Instance().IsAgentAlive(tmpFuben.ServerId)) { roleCount = tmpFuben.AddKuaFuFuBenRoleData(kuaFuFuBenRoleData, GameFuBenRoleCountChangedHandler); if (roleCount > 0) { selectFuben = tmpFuben; break; } } } } if (null == selectFuben) { int gameId = Persistence.GetNextGameId(); int kfSrvId = 0; if (ClientAgentManager.Instance().AssginKfFuben(GameType, gameId, 1, out kfSrvId)) { selectFuben = new HuanYingSiYuanFuBenData(); selectFuben.ServerId = kfSrvId; selectFuben.GameId = gameId; selectFuben.GroupIndex = kuaFuRoleData.GroupIndex; selectFuben.EndTime = Global.NowTime.AddMinutes(Consts.HuanYingSiYuanGameFuBenMaxExistMinutes); AddGameFuBen(selectFuben); roleCount = selectFuben.AddKuaFuFuBenRoleData(kuaFuFuBenRoleData, GameFuBenRoleCountChangedHandler); Persistence.LogCreateHysyFuben(gameId, kfSrvId, 0, 1); } } if (selectFuben != null && roleCount > 0) { if (roleCount == 1) { selectFuben.EndTime = now; //第一个人进入时,重置副本创建时间 } if (selectFuben.State == GameFuBenState.Wait) { if (roleCount == Consts.HuanYingSiYuanRoleCountTotal) { List <KuaFuFuBenRoleData> roleList = selectFuben.SortFuBenRoleList(); foreach (var role in roleList) { KuaFuRoleData kuaFuRoleDataTemp; KuaFuRoleKey key = KuaFuRoleKey.Get(role.ServerId, role.RoleId); if (RoleIdKuaFuRoleDataDict.TryGetValue(key, out kuaFuRoleDataTemp)) { kuaFuRoleDataTemp.UpdateStateTime(selectFuben.GameId, KuaFuRoleStates.NotifyEnterGame, stateEndTime.Ticks); } } selectFuben.State = GameFuBenState.Start; NotifyFuBenRoleEnterGame(selectFuben); } else { kuaFuRoleData.UpdateStateTime(selectFuben.GameId, KuaFuRoleStates.SignUpWaiting, kuaFuRoleData.StateEndTicks); NotifyFuBenRoleCount(selectFuben); } } else if (selectFuben.State == GameFuBenState.Start) { kuaFuRoleData.UpdateStateTime(selectFuben.GameId, KuaFuRoleStates.NotifyEnterGame, stateEndTime.Ticks); NotifyFuBenRoleEnterGame(selectFuben); } } else { //如果分配失败,则返回false,本轮不在尝试分配 return(false); } return(true); } catch (System.Exception ex) { LogManager.WriteExceptionUseCache(ex.ToString()); } return(false); }