public void ApplyBalance(BalanceTeamsResult balance) { if (balance.DeleteBots) { Bots.Clear(); } if (balance.Bots?.Count > 0) { Bots = balance.Bots; } if (balance.Players?.Count > 0) { Players = balance.Players; } }
/// <summary> /// The function that actually moves players arounds /// </summary> /// <param name="teamCount"></param> /// <param name="mode"></param> /// <param name="b"></param> /// <param name="unmovablePlayers"></param> /// <returns></returns> BalanceTeamsResult LegacyBalance(int teamCount, BalanceMode mode, LobbyHostingContext b, params List<Account>[] unmovablePlayers) { var ret = new BalanceTeamsResult(); if (b.IsMatchMakerGame) mode = BalanceMode.Party; // override, for matchmaker mode is always party try { ret.CanStart = true; ret.Players = b.Players.ToList(); var db = new ZkDataContext(); var nonSpecList = b.Players.Where(y => !y.IsSpectator).Select(y => (int?)y.LobbyID).ToList(); var accs = db.Accounts.Where(x => nonSpecList.Contains(x.AccountID)).ToList(); if (accs.Count < 1) { ret.CanStart = false; return ret; } if (teamCount < 1) teamCount = 1; if (teamCount > accs.Count) teamCount = accs.Count; if (teamCount == 1) { foreach (var p in ret.Players) p.AllyID = 0; return ret; } maxTeamSize = (int)Math.Ceiling(accs.Count / (double)teamCount); teams.Clear(); for (var i = 0; i < teamCount; i++) { var team = new BalanceTeam(); teams.Add(team); if (unmovablePlayers != null && unmovablePlayers.Length > i) { var unmovables = unmovablePlayers[i]; team.AddItem(new BalanceItem(b.IsMatchMakerGame, unmovablePlayers[i].ToArray()) { CanBeMoved = false }); accs.RemoveAll(x => unmovables.Any(y => y.AccountID == x.AccountID)); } } balanceItems = new List<BalanceItem>(); if (mode == BalanceMode.Party) { var clanGroups = accs.GroupBy(x => b.Players.First(p => p.Name == x.Name).PartyID ?? x.AccountID).ToList(); if (teamCount > clanGroups.Count() || clanGroups.Any(x => x.Count() > maxTeamSize)) mode = BalanceMode.Normal; else balanceItems.AddRange(clanGroups.Select(x => new BalanceItem(b.IsMatchMakerGame, x.ToArray()))); } if (mode == BalanceMode.ClanWise) { var clanGroups = accs.GroupBy(x => b.Players.First(p => p.Name == x.Name).PartyID ?? x.ClanID ?? x.AccountID).ToList(); if (teamCount > clanGroups.Count() || clanGroups.Any(x => x.Count() > maxTeamSize)) mode = BalanceMode.Normal; else balanceItems.AddRange(clanGroups.Select(x => new BalanceItem(b.IsMatchMakerGame, x.ToArray()))); } if (mode == BalanceMode.FactionWise) { balanceItems.Clear(); var factionGroups = accs.GroupBy(x => x.FactionID ?? x.AccountID).ToList(); balanceItems.AddRange(factionGroups.Select(x => new BalanceItem(b.IsMatchMakerGame, x.ToArray()))); } if (mode == BalanceMode.Normal) { balanceItems.Clear(); balanceItems.AddRange(accs.Select(x => new BalanceItem(b.IsMatchMakerGame, x))); } var sw = new Stopwatch(); sw.Start(); RecursiveBalance(0); sw.Stop(); if (bestTeams == null) { var fallback = new Balancer().LegacyBalance(teamCount, BalanceMode.ClanWise, b, null); fallback.Message += "\nWarning: STANDARD TEAM BALANCE USED, PlanetWars not possible with those teams, too many from one faction"; return fallback; } var minSize = bestTeams.Min(x => x.Count); var maxSize = bestTeams.Max(x => x.Count); var sizesWrong = maxSize / (double)minSize > MaxTeamSizeDifferenceRatio; // cbalance failed, rebalance using normal if (mode == BalanceMode.ClanWise && (bestTeams == null || GetTeamsDifference(bestTeams) > MaxCbalanceDifference || sizesWrong)) return new Balancer().LegacyBalance(teamCount, BalanceMode.Normal, b, unmovablePlayers); // cbalance failed, rebalance using normal if (sizesWrong && mode == BalanceMode.FactionWise) { var fallback = new Balancer().LegacyBalance(teamCount, BalanceMode.ClanWise, b, null); fallback.Message += "\nWarning: STANDARD TEAM BALANCE USED, PlanetWars not possible with those teams, too many from one faction"; return fallback; // fallback standard balance if PW balance fails /*ret.CanStart = false; ret.Message = string.Format("Failed to balance - too many people from same faction"); return ret;*/ } if (unmovablePlayers != null && unmovablePlayers.Length > 0) { var minElo = bestTeams.Min(x => x.AvgElo); var maxElo = bestTeams.Max(x => x.AvgElo); if (maxElo - minElo > GlobalConst.MaxPwEloDifference) { var fallback = new Balancer().LegacyBalance(teamCount, BalanceMode.ClanWise, b, null); fallback.Message += "\nWarning: STANDARD TEAM BALANCE USED, PlanetWars not possible with those teams, too many from one faction"; return fallback; // fallback standard balance if PW balance fails /* ret.CanStart = false; ret.Message = string.Format("Team difference is too big - win chance {0}% - spectate some or wait for more people", Utils.GetWinChancePercent(maxElo - minElo)); return ret;*/ } } if (bestTeams == null) { ret.CanStart = false; ret.Message = string.Format( "Failed to balance {0} - too many people from same clan or faction (in teams game you can try !random and !forcestart)"); return ret; } if (unmovablePlayers == null || unmovablePlayers.Length == 0) bestTeams = bestTeams.Shuffle(); // permute when not unmovable players present var text = "( "; var lastTeamElo = 0.0; var allyNum = 0; foreach (var team in bestTeams) { if (allyNum > 0) text += " : "; text += string.Format("{0}", (allyNum + 1)); if (allyNum > 0) text += string.Format("={0}%)", Utils.GetWinChancePercent(lastTeamElo - team.AvgElo)); lastTeamElo = team.AvgElo; foreach (var u in team.Items.SelectMany(x => x.LobbyId)) ret.Players.Single(x => x.LobbyID == u).AllyID = allyNum; allyNum++; } text += ")"; ret.Message = string.Format( "{0} players balanced {2} to {1} teams {3}. {4} combinations checked, spent {5}ms of CPU time", bestTeams.Sum(x => x.Count), teamCount, mode, text, iterationsChecked, sw.ElapsedMilliseconds); } catch (Exception ex) { Trace.TraceError(ex.ToString()); ret.Message = ex.ToString(); ret.CanStart = false; } return ret; }
/// <summary> /// Makes <see cref="Springie" /> print a message if two or more people have the same IP /// </summary> /// <param name="context"></param> /// <param name="res">The <see cref="BalanceTeamsResult" /> to write the message to</param> static void VerifySpecCheaters(LobbyHostingContext context, BalanceTeamsResult res) { try { // find specs with same IP as some player and kick them using (var db = new ZkDataContext()) { var ids = context.Players.Select(y => (int?)y.LobbyID).ToList(); var ipByLobbyID = db.Accounts.Where(x => ids.Contains(x.AccountID)) .ToDictionary(x => x.AccountID, x => x.AccountIPs.OrderByDescending(y => y.LastLogin).Select(y => y.IP).FirstOrDefault()); // lobbyid -> ip mapping foreach (var grp in context.Players.GroupBy(x => ipByLobbyID[x.LobbyID]).Where(x => x.Count() > 1)) res.Message += $"\nThese people are in same location: {string.Join(", ", grp.Select(x => x.Name))}"; } } catch (Exception ex) { Trace.TraceError("Error checking speccheaters: {0}", ex); } }
BalanceTeamsResult PlanetwarsBalance(LobbyHostingContext context) { var res = new BalanceTeamsResult(); res.CanStart = true; res.DeleteBots = true; using (var db = new ZkDataContext()) { res.Message = ""; var planet = db.Galaxies.Single(x => x.IsDefault).Planets.First(x => x.Resource.InternalName == context.Map); // todo hackvar info = PlanetWarsMatchMaker.GetBattleInfo(context.AutohostName); PlanetWarsMatchMaker.AttackOption info = null; if (info == null) { res.Message = "Start battle using matchmaker"; res.CanStart = false; return res; } /* foreach (var matchUser in info.Attackers) AddPwPlayer(context, matchUser, res, 0); foreach (var matchUser in info.Defenders) AddPwPlayer(context, matchUser, res, 1); foreach (var p in context.Players.Where(x => !res.Players.Any(y => y.Name == x.Name))) { p.IsSpectator = true; res.Players.Add(p); } // bots game var cnt = 0; if (planet.PlanetStructures.Any(x => !string.IsNullOrEmpty(x.StructureType.EffectBots))) { foreach (var b in planet.PlanetStructures.Select(x => x.StructureType).Where(x => !string.IsNullOrEmpty(x.EffectBots))) res.Bots.Add(new BotTeam { AllyID = 2, BotAI = b.EffectBots, BotName = "Aliens" + cnt++ }); res.Message += "This planet is infested by aliens, fight for your survival"; return res; } */ return res; } }
/// <summary> /// Calls <see cref="LegacyBalance" /> with the appropriate parameters depending on game settings and conditions /// </summary> /// <param name="isGameStart"> /// If true and <see cref="AutohostMode" /> is none, do nothing (i.e. don't autobalance custom /// rooms at start) /// </param> /// <param name="allyCount"></param> /// <param name="clanWise"></param> /// <param name="config"></param> /// <remarks>Also removes bots from team games, and tells people to add bots to a chicken game if absent</remarks> static BalanceTeamsResult PerformBalance( LobbyHostingContext context, bool isGameStart, int? allyCount, bool? clanWise) { var res = new BalanceTeamsResult(); var mode = context.Mode; using (var db = new ZkDataContext()) { switch (mode) { case AutohostMode.None: { if (!isGameStart) res = new Balancer().LegacyBalance(allyCount ?? 2, clanWise == true ? BalanceMode.ClanWise : BalanceMode.Normal, context); } break; case AutohostMode.Teams: case AutohostMode.Game1v1: { res = new Balancer().LegacyBalance(allyCount ?? 2, clanWise == false ? BalanceMode.Normal : BalanceMode.ClanWise, context); res.DeleteBots = true; } break; case AutohostMode.GameChickens: { res.Players = context.Players.ToList(); res.Bots = context.Bots.ToList(); foreach (var p in res.Players) p.AllyID = 0; foreach (var b in res.Bots) b.AllyID = 1; // add chickens via modoptions hackish thingie string chickBot = null; if (context.ModOptions?.TryGetValue("chickenailevel", out chickBot) == true && !string.IsNullOrEmpty(chickBot) && chickBot != "none") { res.Bots.Add(new BotTeam() { AllyID = 1, BotName = "default_Chicken", BotAI = chickBot }); } if (!res.Bots.Any() && res.Players.Count > 0) { //res.Message = "Add some bot (computer player) as your enemy. Use button on bottom left. Chicken or CAI is recommended."; var map = db.Resources.FirstOrDefault(x => x.InternalName == context.Map); if (map?.MapIsChickens == true) res.Bots.Add(new BotTeam() { AllyID = 1, BotName = "default_Chicken", BotAI = "Chicken: Normal", }); else res.Bots.Add(new BotTeam() { AllyID = 1, BotName = "cai", BotAI = "CAI", }); res.Message = "Adding computer AI player for you"; } } break; case AutohostMode.GameFFA: { res.DeleteBots = true; var map = db.Resources.Single(x => x.InternalName == context.Map); if (map.MapFFAMaxTeams != null) { res = new Balancer().LegacyBalance( allyCount ?? map.MapFFAMaxTeams.Value, clanWise == false ? BalanceMode.Normal : BalanceMode.ClanWise, context); } else { res = new Balancer().LegacyBalance( allyCount ?? map.MapFFAMaxTeams ?? 8, clanWise == false ? BalanceMode.Normal : BalanceMode.ClanWise, context); } return res; } case AutohostMode.Planetwars: return new Balancer().PlanetwarsBalance(context); } return res; } }
static void VerifySpecCheaters(BattleContext context, BalanceTeamsResult res) { try { // find specs with same IP as some player and kick them using (var db = new ZkDataContext()) { var ids = context.Players.Select(y => (int?)y.LobbyID).ToList(); var ipByLobbyID = db.Accounts.Where(x => ids.Contains(x.AccountID)) .ToDictionary(x => x.AccountID, x => x.AccountIPs.OrderByDescending(y => y.LastLogin).Select(y=>y.IP).FirstOrDefault()); // lobbyid -> ip mapping var mode = context.GetMode(); // kick same ip specs for starred and non chickens /* if (mode != AutohostMode.None && mode != AutohostMode.GameChickens) { foreach (var p in context.Players.Where(x => x.IsSpectator)) { var ip = ipByLobbyID[p.LobbyID]; if (context.Players.Any(x => !x.IsSpectator && ipByLobbyID[x.LobbyID] == ip)) Global.Nightwatch.Tas.AdminKickFromLobby(p.Name, "Spectators from same location as players are not allowed here!"); } }*/ foreach (var grp in context.Players.GroupBy(x => ipByLobbyID[x.LobbyID]).Where(x => x.Count() > 1)) res.Message += string.Format("\nThese people are in same location: {0}", string.Join(", ", grp.Select(x => x.Name))); } } catch (Exception ex) { Trace.TraceError("Error checking speccheaters: {0}", ex); } }
static void AddPwPlayer(BattleContext context, string matchUser, BalanceTeamsResult res, int allyID) { PlayerTeam player = context.Players.FirstOrDefault(x => x.Name == matchUser); if (player == null) { player = new PlayerTeam() { Name = matchUser }; User us; if (Global.Nightwatch.Tas.GetExistingUser(matchUser, out us)) player.LobbyID = us.AccountID; else { var db = new ZkDataContext(); var acc = Account.AccountByName(db, matchUser); if (acc != null) player.LobbyID = acc.AccountID; } } res.Players.Add(new PlayerTeam { AllyID = allyID , IsSpectator = false, Name = player.Name, LobbyID = player.LobbyID, TeamID = player.TeamID }); }
static BalanceTeamsResult PerformBalance(BattleContext context, bool isGameStart, int? allyCount, bool? clanWise, AutohostConfig config, int playerCount) { var res = new BalanceTeamsResult(); var mode = context.GetMode(); using (var db = new ZkDataContext()) { if (!CheckPlayersMinimumConditions(context, db, config, ref res.Message)) { res.CanStart = false; return res; } switch (mode) { case AutohostMode.None: { if (!isGameStart) res = new Balancer().LegacyBalance(allyCount ?? 2, clanWise == true ? BalanceMode.ClanWise : BalanceMode.Normal, context); } break; case AutohostMode.Generic: { if (allyCount == null && res.Bots != null && res.Bots.Any()) { res.Players = context.Players.ToList(); res.Bots = context.Bots.Where(x => x.Owner != context.AutohostName).ToList(); foreach (var p in res.Players) p.AllyID = 0; foreach (var b in res.Bots) b.AllyID = 1; } else { var map = db.Resources.Single(x => x.InternalName == context.Map); res = new Balancer().LegacyBalance(allyCount ?? map.MapFFAMaxTeams ?? 2, clanWise == false ? BalanceMode.Normal : BalanceMode.ClanWise, context); res.DeleteBots = mode == AutohostMode.Teams; } return res; } case AutohostMode.Teams: { var map = db.Resources.Single(x => x.InternalName == context.Map); res = new Balancer().LegacyBalance(allyCount ?? map.MapFFAMaxTeams ?? 2, clanWise == false ? BalanceMode.Normal : BalanceMode.ClanWise, context); res.DeleteBots = mode == AutohostMode.Teams; return res; } case AutohostMode.Game1v1: { res = new Balancer().LegacyBalance(allyCount ?? 2, clanWise == false ? BalanceMode.Normal : BalanceMode.ClanWise, context); res.DeleteBots = true; } break; case AutohostMode.GameChickens: { res.Players = context.Players.ToList(); res.Bots = context.Bots.Where(x => x.Owner != context.AutohostName).ToList(); foreach (var p in res.Players) p.AllyID = 0; foreach (var b in res.Bots) b.AllyID = 1; if (!res.Bots.Any() && res.Players.Count > 0) { res.Message = "Add some bot (computer player) as your enemy. Use button on bottom left. Chicken or CAI is recommended."; res.CanStart = false; /*else { res.Bots.Add(new BotTeam() { AllyID = 1, TeamID = 16, BotName = "default_Chicken", BotAI = "Chicken: Normal", }); res.Message = "Adding a normal chickens bot for you"; }*/ } } break; case AutohostMode.GameFFA: { var map = db.Resources.Single(x => x.InternalName == context.Map); if (map.MapFFAMaxTeams != null) res = new Balancer().LegacyBalance(allyCount ?? map.MapFFAMaxTeams.Value, clanWise == false ? BalanceMode.Normal : BalanceMode.ClanWise, context); else res = new Balancer().LegacyBalance(allyCount ?? map.MapFFAMaxTeams ?? 8, clanWise == false ? BalanceMode.Normal : BalanceMode.ClanWise, context); return res; } case AutohostMode.Planetwars: return new Balancer().PlanetwarsBalance(context); } return res; } }
public void ApplyBalanceResults(BalanceTeamsResult balance) { if (!string.IsNullOrEmpty(balance.Message)) SayBattle(balance.Message, false); if (balance.Players != null && balance.Players.Count > 0) { foreach (var user in tas.MyBattle.Users.Values.Where(x => !x.IsSpectator && !balance.Players.Any(y => y.Name == x.Name))) tas.ForceSpectator(user.Name); // spec those that werent in response foreach (var user in balance.Players.Where(x => x.IsSpectator)) tas.ForceSpectator(user.Name); bool comsharing = false; bool coopOptExists = tas.MyBattle.ModOptions.Any(x => x.Key.ToLower() == "coop"); if (coopOptExists) { KeyValuePair<string, string> comsharing_modoption = tas.MyBattle.ModOptions.FirstOrDefault(x => x.Key.ToLower() == "coop"); if (comsharing_modoption.Value != "0" && comsharing_modoption.Value != "false") comsharing = true; } foreach (var user in balance.Players.Where(x => !x.IsSpectator)) { tas.ForceTeam(user.Name, comsharing ? user.AllyID : user.TeamID); tas.ForceAlly(user.Name, user.AllyID); } } if (balance.DeleteBots) foreach (var b in tas.MyBattle.Bots.Keys) tas.RemoveBot(b); if (balance.Bots != null && balance.Bots.Count > 0) { foreach (var b in tas.MyBattle.Bots.Values.Where(x => !balance.Bots.Any(y => y.BotName == x.Name && y.Owner == x.owner))) tas.RemoveBot(b.Name); foreach (var b in balance.Bots) { var existing = tas.MyBattle.Bots.Values.FirstOrDefault(x => x.owner == b.Owner && x.Name == b.BotName); if (existing != null) { tas.UpdateBot(existing.Name, b.BotAI, b.AllyID, b.TeamID); } else { tas.AddBot(b.BotName.Replace(" ", "_"), b.BotAI, b.AllyID, b.TeamID); } } } }
static void AddPwPlayer(LobbyHostingContext context, string matchUser, BalanceTeamsResult res, int allyID) { var player = context.Players.FirstOrDefault(x => x.Name == matchUser); if (player == null) { player = new PlayerTeam { Name = matchUser }; ConnectedUser us; // todo hack if (Global.Server.ConnectedUsers.TryGetValue(matchUser, out us)) player.LobbyID = us.User.AccountID; // else { var db = new ZkDataContext(); var acc = Account.AccountByName(db, matchUser); if (acc != null) player.LobbyID = acc.AccountID; } } res.Players.Add( new PlayerTeam { AllyID = allyID, IsSpectator = false, Name = player.Name, LobbyID = player.LobbyID }); }
public void ApplyBalance(BalanceTeamsResult balance) { if (balance.DeleteBots) Bots.Clear(); if (balance.Bots?.Count > 0) Bots = balance.Bots; if (balance.Players?.Count > 0) Players = balance.Players; }