Exemplo n.º 1
0
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            timer.Stop();
            try {
                var b = tas.GetBattle();
                if (!autoHost.ComFix(TasSayEventArgs.Default, "silent"))
                {
                    return;
                }

                var  factions = new List <IFaction>(server.GetFactions(account));
                var  sides    = tas.GetBattle().Mod.Sides.ToList();
                bool teamsOk  = true;
                foreach (var user in b.Users.Where(x => !x.IsSpectator && x.SyncStatus != SyncStatuses.Unknown))
                {
                    var info = server.GetPlayerInfo(account, user.name);
                    if (info == null)
                    {
                        tas.ForceSpectator(user.name);
                        tas.Say(TasClient.SayPlace.User, user.name, string.Format("This is online campaign server - http://{0}/ - you must register first /to play here. \n To register say: !register side newpassword (optional planetnname) \n Example: !register core secretpw \n Or: !register arm mynewpassword Alpha Centauri", host), false);
                    }
                    else
                    {
                        int    hisFaction      = factions.IndexOf(factions.Find((f) => f.Name == info.FactionName));
                        string springSideName  = sides.SingleOrDefault(s => s.ToUpper() == info.FactionName.ToUpper());
                        int    springSideIndex = sides.IndexOf(springSideName);

                        // he is in wrong team
                        if (user.AllyNumber != hisFaction)
                        {
                            tas.ForceAlly(user.name, hisFaction);
                            teamsOk = false;
                        }
                        else if (user.Side != springSideIndex)
                        {
                            tas.ForceSide(user.name, springSideIndex);
                        }
                    }
                }
                if (!teamsOk)
                {
                    return;                           // dont proceed to balancing if teams are bing changed
                }
                var grouping = b.Users.Where(u => !u.IsSpectator && u.SyncStatus != SyncStatuses.Unknown).GroupBy(u => u.AllyNumber).OrderBy(g => g.Count()).ToList();
                if (grouping.Count() == 2)
                {
                    if (grouping[1].Count() > 2 * grouping[0].Count())
                    {
                        var newest = b.Users.Where(u => u.AllyNumber == grouping[1].Key && !u.IsSpectator).OrderByDescending(u => u.JoinTime).First();
                        tas.ForceSpectator(newest.name);
                        autoHost.SayBattle(string.Format("There are too many players on that side {0}, sorry you joined last.", newest.name));
                    }
                }
            } catch (Exception ex) {
                autoHost.SayBattle("Problem with PlanetWars:" + ex);
            } finally {
                timer.Start();
            }
        }