Timer Setups

Jan 14, 2018
Timer Setups
  • Description:

    Timers can be handled through the Plugin class. When the interval elapses the timer will repeat itself and raise a callback (<timerName>Callback), with the TimedEvent as a parameter. You can run only one basic timer with the same name, however you can run more simultaneously of Parallel Timers. Use Parallel Timers only if you have to.

    Timer specific methods:

    Code (C#):
    CreateParallelTimer(string Name, int Interval, Dictionary<string, object> Args):TimedEvent // In JavaScript It's AdvancedTimers.CreateParallelTimer(..
    CreateTimer(string Name, int Interval, Dictionary<string, object> Args):TimedEvent
    CreateTimer(string Name, int Interval):TimedEvent
    GetParallelTimer(string Name):List<TimedEvent> // In JavaScript It's AdvancedTimers.GetParallelTimer(..
    GetTimer(string Name):TimedEvent
    KillParallelTimer(string Name):void // In JavaScript It's AdvancedTimers.KillParallelTimer(..
    KillTimer(string Name):void
    KillTimers():void



    ParallelTimer:


    Parallel Timers are having the same structure, as the normal Timers, the difference is that they are must have a Dictionary.

    Dictionary Represents a collection of keys and values.


    Code (Python):
    def On_PlayerConnected(self, Player):
            ConnectionData = Plugin.CreateDict()
            ConnectionData["PlayerID"] = Player.SteamID
            ConnectionData["Player"] = Player
            # 10 seconds
            Plugin.CreateParallelTimer("Connect", 10000, ConnectionData).Start()

    def ConnectCallback(self, ATimedEvent):
            ATimedEvent.Kill()
            data = ATimedEvent.Args
            playerID = data["PlayerID"]
            StoredPlayer = data["Player"] # Instead of finding you can use this, and check if StoredPlayer.IsOnline:
            Player = Server.FindPlayer(playerID)
            if Player is not None:
                    Player.Message("Hey, I think the timer found you!")
    Code (Javascript):
    function On_PlayerConnected(Player)
    {
        var ConnectionData = Plugin.CreateDict();
        ConnectionData["PlayerID"] = Player.SteamID;
        // 10 seconds
        Plugin.AdvancedTimers.CreateParallelTimer("Connect", 10000, ConnectionData).Start();
    }

    function ConnectCallback(ATimedEvent)
    {
        ATimedEvent.Kill();
        var data = ATimedEvent.Args;
        var playerID = data["PlayerID"];
        var Player = Server.FindPlayer(playerID);
        if (Player != null) {
            Player.Message("Hey, I think the timer found you!");
        }
    }
    Code (Lua):
    function On_PlayerConnected(Player)
        local ConnectionData = Plugin.CreateDict()
        ConnectionData["PlayerID"] = Player.SteamID
        -- 10 seconds
        Plugin:CreateParallelTimer("Connect", 10000, ConnectionData):Start()
    end

    function ConnectCallback(ATimedEvent)
        ATimedEvent.Kill()
        local data = ATimedEvent.Args
        local playerID = data["PlayerID"]
        local Player = Server:FindPlayer(playerID)
        if Player ~= nil then
            Player.Message("Hey, I think the timer found you!")
        end
    end
    RegularTimer:

    Regular Timers are being called without anykind of extra stuffs.

    Code (Python):
        def On_PluginInit(self):
            # Launching a timer (5 seconds)
            Plugin.CreateTimer("PingCheck", 5000).Start()

        def PingCheckCallback(self, ATimedEvent):
            ATimedEvent.Kill()
            for pl in Server.Players:
                if int(pl.Ping) >= 250:
                    pl.MessageFrom("[High Ping Kicker]", "[color#FF2222]Your Ping: " + str(pl.Ping) +
                                   " Maximum you can have: 250")
                    pl.Disconnect()
            Plugin.CreateTimer("PingCheck", 5000).Start()
    Code (Javascript):
    function On_PluginInit()
    {
        // Launching a timer (5 seconds)
        Plugin.AdvancedTimers.CreateTimer("PingCheck", 5000).Start();
    }

    function PingCheckCallback(ATimedEvent)
    {
        ATimedEvent.Kill();
        for (var pl in Server.Players)
        {
            if (pl.Ping >= 250)
            {
                pl.MessageFrom("[High Ping Kicker]", "[color#FF2222]Your Ping: "
                    + pl.Ping + " Maximum you can have: 250");
                pl.Disconnect();
            }
        }
        Plugin.AdvancedTimers.CreateTimer("PingCheck", 5000).Start();
    }
    Code (Lua):
    function On_PluginInit()
        -- Launching a timer (5 seconds)
        Plugin:CreateTimer("PingCheck", 5000):Start()
    end

    function PingCheckCallback(ATimedEvent)
        ATimedEvent.Kill()
        -- This might be wrong, sorry I'm not a lua fan
        for i, pl in ipairs(Server.Players) do
            if pl.Ping >= 250 then
                pl:MessageFrom("[High Ping Kicker]", "[color#FF2222]Your Ping: "
                    + pl.Ping + " Maximum you can have: 250")
                pl:Disconnect()
            end
        end
        Plugin:CreateTimer("PingCheck", 5000):Start()
    end

    C# Parallel Timers.

    Code (C#):
        public class PluginTimedEvent
        {

            private Dictionary<string, object> _args;
            private readonly System.Timers.Timer _timer;
            private long lastTick;
            private int _elapsedCount;

            internal delegate void TimedEventFireDelegate(PluginTimedEvent evt);

            internal event TimedEventFireDelegate OnFire;

            internal PluginTimedEvent(double interval)
            {
                this._timer = new System.Timers.Timer();
                this._timer.Interval = interval;
                this._timer.Elapsed += new ElapsedEventHandler(this._timer_Elapsed);
                this._elapsedCount = 0;
            }

            internal PluginTimedEvent(double interval, Dictionary<string, object> args)
                : this(interval)
            {
                this.Args = args;
            }

            private void _timer_Elapsed(object sender, ElapsedEventArgs e)
            {
                if (this.OnFire != null)
                {
                    this.OnFire(this);
                }

                this._elapsedCount += 1;
                this.lastTick = DateTime.UtcNow.Ticks;
            }

            internal void Start()
            {
                this._timer.Start();
                this.lastTick = DateTime.UtcNow.Ticks;
            }

            internal void Stop()
            {
                this._timer.Stop();
            }

            internal void Kill()
            {
                this._timer.Stop();
                this._timer.Dispose();
            }

            internal Dictionary<string, object> Args
            {
                get { return this._args; }
                set { this._args = value; }
            }

            internal double Interval
            {
                get { return this._timer.Interval; }
                set { this._timer.Interval = value; }
            }

            internal double TimeLeft
            {
                get { return (this.Interval - ((DateTime.UtcNow.Ticks - this.lastTick) / 0x2710L)); }
            }

            internal int ElapsedCount
            {
                get { return this._elapsedCount; }
            }
        }

    Code (C#):
    internal PluginTimedEvent CreateParallelTimer(int timeoutDelay, Dictionary<string, object> args)
    {
        PluginTimedEvent TimedEvent = new PluginTimedEvent (timeoutDelay);
        TimedEvent.Args = args;
        TimedEvent.OnFire += Callback;
        return TimedEvent;
    }

    internal void Callback(PluginTimedEvent e)
    {
        e.Kill();
        var data = e.Args;
        Fougerite.Player Player = (Fougerite.Player) data["Player"];
        //todo do something here
    }

    public void OnPlayerSpawned(Fougerite.Player Player, SpawnEvent se)
    {
        var dict = new Dictionary<string, object>();
        dict["Player"] = Player;
        CreateParallelTimer(5000, dict).Start();
    }

     
Pompeyo likes this.