Advertisement
Foereaper

Eluna Broadcaster

Apr 10th, 2018
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. local Broadcast = {
  2.     Register = {
  3.         -- {"String", day, time}
  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.         {"Announce message here", 0, 1800},
  7.     }
  8. }
  9.  
  10. local function SeparateTime(t)
  11.     local h = math.floor(t)
  12.     local m = ((t*1000)-(h*1000))/10
  13.     return h, m;
  14. end
  15.  
  16. local function GetTimeDiff(weekday, h, m, s)
  17.     local d = os.date("*t")
  18.  
  19.     d.sec = s or 0
  20.     d.min = m
  21.     d.hour = h
  22.  
  23.     local ddiff = weekday-d.wday+1
  24.     d.day = d.day+ddiff
  25.     local now = os.date("*t")
  26.     if (ddiff < 0) then
  27.         -- Take into consideration that it is tuesday and we want monday
  28.         d.day = d.day+7
  29.     elseif (ddiff == 0 and d.hour*60*60+d.min*60+d.sec < now.hour*60*60+now.min*60+now.sec) then
  30.         -- Take into consideration that it is the same date, but its already past the wanted time
  31.         d.day = d.day+7
  32.     end
  33.  
  34.     -- get final times
  35.     local e = os.time(d)
  36.     local diff = e-os.time() -- this is the time in seconds until the wanted date is achieved
  37.  
  38.     return diff;
  39. end
  40.  
  41. function Broadcast.SendAndReset(msg, d, t)
  42.     SendWorldMessage(msg)
  43.     if(d > 0) then
  44.         local regtime = GetTimeDiff(d, SeparateTime(t))
  45.         CreateLuaEvent(function() Broadcast.SendAndReset(msg, d, t) end, regtime*1000, 1)
  46.     end
  47. end
  48.  
  49. function Broadcast.OnLoad()
  50.     for i, v in ipairs(Broadcast.Register) do
  51.         local msg, d, t = table.unpack(Broadcast.Register[i])
  52.         if d == 0 then
  53.             CreateLuaEvent(function() Broadcast.SendAndReset(msg, d, t) end, t*1000, 0)
  54.         else
  55.             local regtime = GetTimeDiff(d, SeparateTime(t))
  56.             CreateLuaEvent(function() Broadcast.SendAndReset(msg, d, t) end, regtime*1000, 1)
  57.         end
  58.     end
  59. end
  60.  
  61. Broadcast.OnLoad()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement