SphereCommunity
Real Life Timer - Printable Version

+- SphereCommunity (https://forum.spherecommunity.net)
+-- Forum: Sphere X (/Forum-Sphere-X)
+--- Forum: Script Submissions (/Forum-Script-Submissions--61)
+--- Thread: Real Life Timer (/Thread-Real-Life-Timer)



Real Life Timer - ShiryuX - 04-04-2020 05:08 AM

Hi,
I didn't find an exact title for this, but anyway, this is a timer that is synced with your real life clock, it fires once per hour and you can configure specific functions for each day at a specific hour. Pretty simple actually. The clock will start automatically when the server is launched, you don't have to do anything, if anything happens (it gets deleted or something), a check is made at every world save. And last, if it gets out of sync, at each @Timer it will get synced again for the next hour. Every event is logged.

Special thanks for the guys (Soulless and Xun) in Discord who gave me the idea to use rTime functions.
Which I didn't know it existed Lol

NOTES:
It will only work on o'clock timers. You can fire it at 16.00 but not at 16.15.
Functions are fired on SERV, not on the object or anything else.
Days start a 0 = Sunday, 1 = Monday and so on...
Defname is provided with examples.
YOU NEED TO CONFIGURE YOUR TIME ZONE CORRECTLY!


Example defname:
Code:
[DEFNAME dWorldClockFunctions]
WorldClock_0_20        "f_AutomatedPVP" // Will fire on sunday at 20hs
WorldClock_5_16        "f_AutomatedCTF" // Will fire on friday at 16hs

Item for timer:
Code:
[ITEMDEF i_WorldClock]
ID=i_gravestone_4
TYPE=t_normal

ON=@Create
attr = attr_move_never|attr_decay

ON=@Timer
serv.log @WorldClock: Timer triggered at <serv.rtime.format %T>
local.ToCall = _<serv.rtime.format %w>_<serv.rtime.format %H>
if !<isempty <def.WorldClock<local.ToCall>>>
    serv.<def.WorldClock<local.ToCall>>
    serv.log @WorldClock: Called function <def.WorldClock<local.ToCall>>.
endif
local.WaitToNext = <eval 60 - <serv.rtime.format %M>>
timer = <eval (<local.WaitToNext> * 60) - <serv.rtime.format %S>>
serv.log @WorldClock: Next timer in <eval <timer>/60>:<eval <timer>%60>
return 1


Look for f_onserver_save function in your default script pack.
Or add it if you don't have one.
Add the following code:
Code:
if (<uid.<var0.WorldClock>> == 0)
    serv.log @WorldClock: TIMER WAS REMOVED! Created new timer.
    newitem i_WorldClock
    new.p = 11,11
    local.WaitToNext = <eval 60 - <serv.rtime.format %M>>
    new.timer = <eval (<local.WaitToNext> * 60) - <serv.rtime.format %S>>
    new.sector.flags = 01
    var.WorldClock = <new.uid>
    serv.log @WorldClock: Synced at <serv.rtime.format %T> will trigger in <eval <new.timer> / 60>:<eval <new.timer> % 60> minutes.
endif


Look for f_onserver_start function. Same as before.
Add the following code:
Code:
if ((<var0.WorldClock> == 0) || (<uid.<var0.WorldClock>> == 0))
    serv.log @WorldClock: Created new timer.
    newitem i_WorldClock
    new.p = 11,11
    local.WaitToNext = <eval 60 - <serv.rtime.format %M>>
    new.timer = <eval (<local.WaitToNext> * 60) - <serv.rtime.format %S>>
    new.sector.flags = 01
    var.WorldClock = <new.uid>
    serv.log @WorldClock: Synced at <serv.rtime.format %T> will trigger in <eval <new.timer> / 60>:<eval <new.timer> % 60> minutes.
else
    local.WaitToNext = <eval 60 - <serv.rtime.format %M>>
    uid.<var.WorldClock>.timer = <eval (<local.WaitToNext> * 60) - <serv.rtime.format %S>>
    serv.log @WorldClock: Synced at <serv.rtime.format %T> will trigger in <eval <uid.<var.WorldClock>.timer> /60>:<eval <uid.<var.WorldClock>.timer> % 60> minutes.
endif