Foereaper

Eluna Event Enforcer

Apr 21st, 2018
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.16 KB | None | 0 0
  1. local EventEnforcer = {
  2.     Register = {
  3.         -- {event, day, time, startstate, description}
  4.         --    Day is from 1 to 7, where Monday is 1 and Sunday is 7
  5.         --    Time is either seconds if day is 0 or format hour.minutes if day is specified
  6.         --    startstate is true to start the event, false to stop
  7.         {15, 7, 14.00, true, "Stranglethorn Fishing Pools - Start"},
  8.         {15, 7, 16.00, false, "Stranglethorn Fishing Pools - Stop"},
  9.         {62, 7, 14.00, true, "Stranglethorn Fishing Turn In - Start"},
  10.         {62, 7, 16.00, false, "Stranglethorn Fishing Turn In - Stop"},
  11.     }
  12. }
  13.  
  14. local function SeparateTime(t)
  15.     local h = math.floor(t)
  16.     local m = ((t*1000)-(h*1000))/10
  17.     return h, m;
  18. end
  19.  
  20. local function GetTimeDiff(weekday, h, m, s)
  21.     local d = os.date("*t")
  22.  
  23.     d.sec = s or 0
  24.     d.min = m
  25.     d.hour = h
  26.  
  27.     local ddiff = weekday-d.wday+1
  28.     d.day = d.day+ddiff
  29.     local now = os.date("*t")
  30.     if (ddiff < 0) then
  31.         -- Take into consideration that it is tuesday and we want monday
  32.         d.day = d.day+7
  33.     elseif (ddiff == 0 and d.hour*60*60+d.min*60+d.sec < now.hour*60*60+now.min*60+now.sec) then
  34.         -- Take into consideration that it is the same date, but its already past the wanted time
  35.         d.day = d.day+7
  36.     end
  37.  
  38.     -- get final times
  39.     local e = os.time(d)
  40.     local diff = e-os.time() -- this is the time in seconds until the wanted date is achieved
  41.  
  42.     return diff;
  43. end
  44.  
  45. function EventEnforcer.HandleEvent(evt, d, t, state)
  46.     if state and not IsGameEventActive(evt) then
  47.         StartGameEvent(evt)
  48.     elseif not state and IsGameEventActive(evt) then
  49.         StopGameEvent(evt)
  50.     end
  51.    
  52.     if(d > 0) then
  53.         local regtime = GetTimeDiff(d, SeparateTime(t))
  54.         CreateLuaEvent(function() EventEnforcer.HandleEvent(evt, d, t, state) end, regtime*1000, 1)
  55.     end
  56. end
  57.  
  58. function EventEnforcer.OnLoad()
  59.     for i, v in ipairs(EventEnforcer.Register) do
  60.         local evt, d, t, state = table.unpack(EventEnforcer.Register[i])
  61.         if d == 0 then
  62.             CreateLuaEvent(function() EventEnforcer.HandleEvent(evt, d, t, state) end, t*1000, 0)
  63.         else
  64.             local regtime = GetTimeDiff(d, SeparateTime(t))
  65.             CreateLuaEvent(function() EventEnforcer.HandleEvent(evt, d, t, state) end, regtime*1000, 1)
  66.         end
  67.     end
  68. end
  69.  
  70. EventEnforcer.OnLoad()
Advertisement
Add Comment
Please, Sign In to add comment