示例#1
0
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            lock (timer) {
                timer.Stop();
                try {
                    if (from > 0 && !spring.IsRunning)
                    {
                        Battle b = tas.GetBattle();
                        if (b != null)
                        {
                            int plrCnt = b.CountPlayers();
                            if (plrCnt >= from && plrCnt <= to)
                            {
                                string notReady;
                                bool   isReady = ah.AllReadyAndSynced(out notReady);
                                if (plrCnt % 2 == 0)
                                {
                                    int allyno;
                                    if (!ah.BalancedTeams(out allyno))
                                    {
                                        ah.ComFix(TasSayEventArgs.Default, new string[] {});
                                        ah.ComFixColors(TasSayEventArgs.Default, new string[] {});
                                        ah.BalanceTeams(2, false);
                                    }
                                }
                                if (isReady)
                                {
                                    Thread.Sleep(1000);
                                    if (!spring.IsRunning)
                                    {
                                        ah.ComStart(TasSayEventArgs.Default, new string[] {});
                                    }
                                }
                                else
                                {
                                    DateTime now = DateTime.Now;
                                    if (!waitReady)
                                    {
                                        waitReady         = true;
                                        waitForReadySince = now;
                                    }
                                    if (plrCnt > from && plrCnt % 2 == 1 && b.IsLocked)
                                    {
                                        ah.ComAutoLock(TasSayEventArgs.Default, new string[] { (plrCnt + 1).ToString() });
                                    }

                                    if (now.Subtract(lastRing).TotalSeconds > RingEvery)
                                    {
                                        lastRing = now;
                                        ah.ComRing(TasSayEventArgs.Default, new string[] {});
                                    }

                                    if (plrCnt > from && now.Subtract(waitForReadySince).TotalSeconds > SpecForceAfter)
                                    {
                                        ah.ComForceSpectator(TasSayEventArgs.Default, new string[] { notReady });
                                    }

                                    if (now.Subtract(waitForReadySince).TotalSeconds > SpecAfkAfter)
                                    {
                                        ah.ComForceSpectatorAfk(TasSayEventArgs.Default, new string[] {});
                                    }
                                    if (now.Subtract(waitForReadySince).TotalSeconds > KickAfter)
                                    {
                                        ah.ComKick(TasSayEventArgs.Default, new string[] { notReady });
                                    }
                                }
                            }
                            else
                            {
                                if (b.IsLocked && plrCnt < from)
                                {
                                    ah.ComAutoLock(TasSayEventArgs.Default, new string[] { to.ToString() });
                                }
                                else if (plrCnt > to)
                                {
                                    string notready;
                                    if (!ah.AllReadyAndSynced(out notready))
                                    {
                                        ah.ComForceSpectator(TasSayEventArgs.Default, new string[] { notready });
                                    }
                                }
                                waitReady = false;
                            }
                        }
                    }
                    else
                    {
                        lastRing  = DateTime.Now;
                        waitReady = false;
                    }
                } finally {
                    timer.Start();
                }
            }
        }
示例#2
0
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            lock (timer) {
                timer.Stop();
                try {
                    if (from > 0 && !spring.IsRunning)
                    {
                        var b = tas.GetBattle();
                        if (b != null)
                        {
                            int plrCnt = b.CountPlayers();
                            if (plrCnt >= from)
                            {
                                List <string> notReady;

                                if (!ah.AllUniqueTeams(out notReady))
                                {
                                    ah.ComFix(TasSayEventArgs.Default, new string[] {});
                                    return;
                                }

                                bool isReady = ah.AllReadyAndSynced(out notReady);
                                if (plrCnt % 2 == 0)
                                {
                                    int allyno;
                                    if (!ah.BalancedTeams(out allyno))
                                    {
                                        //teams are not balanced but even number - fix colors and balance
                                        ah.ComFixColors(TasSayEventArgs.Default, new string[] {});
                                        ah.BalanceTeams(2, false);
                                    }
                                }
                                if (isReady)
                                {
                                    Thread.Sleep(1000);
                                    if (!spring.IsRunning)
                                    {
                                        ah.ComStart(TasSayEventArgs.Default, new string[] {});
                                    }
                                }
                                else
                                {
                                    var now = DateTime.Now;

                                    // we have enough people, but its odd number and server locked = unlock for more
                                    if (plrCnt > from && plrCnt % 2 == 1 && b.IsLocked)
                                    {
                                        ah.ComAutoLock(TasSayEventArgs.Default, new[] { (plrCnt + 1).ToString() });
                                    }


                                    if (plrCnt % 2 == 0)
                                    {
                                        // even number of players

                                        if (now.Subtract(lastRing).TotalSeconds > RingEvery)
                                        {
                                            // we ring them
                                            lastRing = now;
                                            ah.ComRing(TasSayEventArgs.Default, new string[] {});
                                        }

                                        var    worstTime = DateTime.MaxValue;
                                        String worstName = "";
                                        foreach (var s in notReady)
                                        {
                                            // find longest offending player
                                            if (!problemSince.ContainsKey(s))
                                            {
                                                problemSince[s] = DateTime.Now;
                                            }
                                            if (problemSince[s] < worstTime)
                                            {
                                                worstTime = problemSince[s];
                                                worstName = s;
                                            }
                                        }
                                        foreach (var s in new List <string>(problemSince.Keys))
                                        {
                                            // delete not offending plaeyrs
                                            if (!notReady.Contains(s))
                                            {
                                                problemSince.Remove(s);
                                            }
                                        }


                                        if (now.Subtract(worstTime).TotalSeconds > SpecForceAfter)
                                        {
                                            ah.ComForceSpectator(TasSayEventArgs.Default, new[] { worstName });                                                                                              // spec longest offending person
                                        }
                                        if (now.Subtract(worstTime).TotalSeconds > KickAfter)
                                        {
                                            ah.ComKick(TasSayEventArgs.Default, new[] { worstName });                                                                                         // kick longest offending
                                        }
                                    }
                                    else
                                    {
                                        // teams are not even delet offender list
                                        problemSince.Clear();
                                    }
                                }
                            }
                            else
                            {
                                // not enough players, make sure we unlock and clear offenders
                                if (b.IsLocked && plrCnt < from)
                                {
                                    ah.ComAutoLock(TasSayEventArgs.Default, new[] { to.ToString() });
                                }
                                problemSince.Clear();
                            }
                        }
                    }
                    else
                    {
                        // spring running, reset timer and delete offenders
                        lastRing = DateTime.Now;
                        problemSince.Clear();
                    }
                } finally {
                    timer.Start();
                }
            }
        }