Start() public static method

Starts this timer with the specified duration, and end message.
startedBy is null. duration is less than ChatTimer.MinDuration.
public static Start ( System.TimeSpan duration, [ message, [ startedBy ) : ChatTimer
duration System.TimeSpan Amount of time the timer should run before completion. Should not be less than ChatTimer.MinDuration.
message [ Message to display when timer reaches zero. May be null.
startedBy [ Name of player who started timer. May not be null.
return ChatTimer
Example #1
0
        static void TimerHandler([NotNull] Player player, [NotNull] CommandReader cmd)
        {
            string param = cmd.Next();

            // List timers
            if (param == null)
            {
                ChatTimer[] list = ChatTimer.TimerList.OrderBy(timer => timer.TimeLeft).ToArray();
                if (list.Length == 0)
                {
                    player.Message("No timers running.");
                }
                else
                {
                    player.Message("There are {0} timers running:", list.Length);
                    foreach (ChatTimer timer in list)
                    {
                        player.Message("  #{0} \"{1}&S\" (started by {2}, {3} left)",
                                       timer.Id,
                                       timer.Message,
                                       timer.StartedBy,
                                       timer.TimeLeft.ToMiniString());
                    }
                }
                return;
            }

            // Abort a timer
            if (param.Equals("abort", StringComparison.OrdinalIgnoreCase))
            {
                int timerId;
                if (cmd.NextInt(out timerId))
                {
                    ChatTimer timer = ChatTimer.FindTimerById(timerId);
                    if (timer == null || !timer.IsRunning)
                    {
                        player.Message("Given timer (#{0}) does not exist.", timerId);
                    }
                    else
                    {
                        timer.Abort();
                        string abortMsg = String.Format("&Y(Timer) {0}&Y aborted a timer with {1} left: {2}",
                                                        player.ClassyName,
                                                        timer.TimeLeft.ToMiniString(),
                                                        timer.Message);
                        Chat.SendSay(player, abortMsg);
                    }
                }
                else
                {
                    CdTimer.PrintUsage(player);
                }
                return;
            }

            // Start a timer
            if (player.Info.IsMuted)
            {
                player.MessageMuted();
                return;
            }
            if (player.DetectChatSpam())
            {
                return;
            }
            TimeSpan duration;

            if (!param.TryParseMiniTimeSpan(out duration))
            {
                CdTimer.PrintUsage(player);
                return;
            }
            if (duration > DateTimeUtil.MaxTimeSpan)
            {
                player.MessageMaxTimeSpan();
                return;
            }
            if (duration < ChatTimer.MinDuration)
            {
                player.Message("Timer: Must be at least 1 second.");
                return;
            }

            string sayMessage;
            string message = cmd.NextAll();

            if (String.IsNullOrWhiteSpace(message))
            {
                sayMessage = String.Format("&Y(Timer) {0}&Y started a {1} timer",
                                           player.ClassyName,
                                           duration.ToMiniString());
            }
            else
            {
                sayMessage = String.Format("&Y(Timer) {0}&Y started a {1} timer: {2}",
                                           player.ClassyName,
                                           duration.ToMiniString(),
                                           message);
            }
            Chat.SendSay(player, sayMessage);
            ChatTimer.Start(duration, message, player.Name);
        }
Example #2
0
        internal static void LoadAll()
        {
            try {
                if (!Directory.Exists("./Timers"))
                {
                    return;
                }

                string[] files = Directory.GetFiles("./Timers");
                foreach (string file in files)
                {
                    if (Path.GetExtension("./Timers/" + file) != ".txt")
                    {
                        continue;
                    }
                    string[] data = File.ReadAllLines(file);

                    DateTime   start   = default(DateTime);
                    DateTime   end     = default(DateTime);
                    PlayerInfo creator = null;
                    string     message = null;

                    foreach (string line in data)
                    {
                        if (line.Contains("StartDate: "))
                        {
                            string date = line.Remove(0, "StartDate: ".Length);
                            DateTime.TryParse(date, out start);
                        }
                        else if (line.Contains("EndDate: "))
                        {
                            string date = line.Remove(0, "EndDate: ".Length);
                            DateTime.TryParse(date, out end);
                        }
                        else if (line.Contains("CreatedBy: "))
                        {
                            string creatorName = line.Remove(0, "CreatedBy: ".Length);
                            creator = PlayerDB.FindPlayerInfoExact(creatorName);
                        }
                        else if (line.Contains("Message: "))
                        {
                            message = line.Remove(0, "Creator: ".Length);
                        }
                    }

                    if (creator == null)
                    {
                        creator = Player.Console.Info;
                    }
                    if (start.Ticks == 0 || end.Ticks == 0 || message == null)
                    {
                        Player.Console.Message("Error starting a Timer: {0}, {1}, {2}, {3}", start, end, creator.Name, message);
                        continue;
                    }

                    if (end < DateTime.UtcNow)
                    {
                        Player.Console.Message("Timer Expired: {0}, {1}, {2}, {3} Time Now: {4}", start, end, creator.Name, message, DateTime.UtcNow);
                        File.Delete(file);
                        continue;
                    }

                    ChatTimer.Start((end - DateTime.UtcNow), message, creator.Name);
                }

                if (files.Length > 0)
                {
                    Player.Console.Message("All Timers Loaded. ({0})", files.Length);
                }
                else
                {
                    Player.Console.Message("No Timers Were Loaded.");
                }
            } catch (Exception ex) {
                Player.Console.Message("Timer Loader Has Crashed: {0}", ex);
            }
        }