View Single Post
03-17-24, 10:34 PM   #6
Hubb777
A Flamescale Wyrmkin
 
Hubb777's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2024
Posts: 113
Originally Posted by Fizzlemizz View Post
It's all variations on a theme (and math). Knowing the remainder of a session (session totalDuration of 1, 2, 4, 6 or 12 hours). This is based on the start time (startTime - serverTime) % totalDuration.

Then figuring out what to do with that remainder based on the breakdown of each sub-session eg. 4 sub-sessions of exactly 5 and 55 (like below) or x sub-sessions of "things" that add up to totalDuration.

Test times are over 4 minutes instead of 4 hours.

Lua Code:
  1. local addonName, addon = ...
  2. local L, MyRegion
  3. local RegionTimes = {
  4.     [1] = {
  5.         startTime = 1679572800,
  6.         totalDuration = 14400, -- complete session time 4 hours repeating
  7.         sub_sessionDuration = 3600, -- 1 hour
  8.         waitTime = 3300, -- 55 minutes
  9.         eventtime = 300, -- 5 minutes implied but..
  10.         [1] = { -- sub-sessions
  11.             name = "A",
  12.         },
  13.         [2] = {
  14.             name = "B",
  15.         },
  16.         [3] = {
  17.             name = "C",
  18.         },
  19.         [4] = {
  20.             name = "D",
  21.         },
  22.     },
  23. }
  24.  
  25. --[[ TEST TIMES ONLY: over 4 minutes instead of 4 hours ]]--
  26.  
  27. --[[
  28. RegionTimes[1].totalDuration = 240 -- 4 minutes
  29. RegionTimes[1].sub_sessionDuration = 60 -- 1 minute
  30. RegionTimes[1].waitTime = 55 -- seconds
  31. RegionTimes[1].eventtime = 5 -- seconds
  32. ]]--
  33.  
  34. --[[ END TEST TIMES ]]--
  35.  
  36. local Localizations = {
  37.     enUS = {
  38.         Waiting = "%s before event %s starts",
  39.         Running = "Event: |cFF35BE21%s|r\n%s remaining",
  40.     },
  41. }
  42.  
  43. local function OnUpdate(self, elapsed)
  44.     self.Elapsed = self.Elapsed - elapsed
  45.     if self.Elapsed > 0 then -- Only check once per second
  46.         return
  47.     end
  48.     self.Elapsed = 1 -- reset the timeout (we've counted down 1 second)
  49.     local serverTime = GetServerTime()
  50.     local remainingTime = (MyRegion.startTime - serverTime) % MyRegion.totalDuration
  51.     local base = math.ceil(remainingTime / MyRegion.sub_sessionDuration)
  52.     local hourRemaining = MyRegion.sub_sessionDuration - ((base * MyRegion.sub_sessionDuration) - remainingTime)
  53.     local id = 4 - (base - 1)
  54.     if id == 5 then
  55.         id = 1
  56.     end
  57.     local msg
  58.     if hourRemaining > MyRegion.waitTime then
  59.         msg = format(L.Running, MyRegion[id].name, SecondsToTime(hourRemaining - MyRegion.waitTime, false))
  60.     else
  61.         id = id == 4 and 1 or id + 1
  62.         msg = format(L.Waiting, SecondsToTime(hourRemaining, false), MyRegion[id].name)
  63.     end
  64.     self.Text:SetText(msg)
  65.     self:SetSize(self.Text:GetWidth() + 10, self.Text:GetHeight() + 10)
  66. end
  67.  
  68. local Backdrop = {
  69.     bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
  70. }
  71.  
  72. local f = CreateFrame("Button", "ZAMTimer_4_Events", UIParent, "BackdropTemplate")
  73. f:SetWidth(255)                                          
  74. f:SetHeight(30)
  75. f:SetPoint("CENTER")
  76. f:SetBackdrop(Backdrop)
  77. f:SetClampedToScreen(true)
  78. f:EnableMouse(true)
  79. f:SetMovable(true)
  80. f:SetUserPlaced(true)
  81. f:RegisterForDrag("LeftButton")
  82. f:RegisterForClicks("AnyUp")
  83. f.Text = f:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  84. f.Text:SetPoint("CENTER")
  85. f.Elapsed = 0 -- Set starting timeout (0 second)
  86. f:SetScript("OnDragStart",function(self)
  87.     self:StartMoving()
  88. end)
  89. f:SetScript("OnDragStop",function(self)  
  90.     self:StopMovingOrSizing()
  91. end)
  92.  
  93. f:RegisterEvent("PLAYER_LOGIN")
  94. f:SetScript("OnEvent", function(self)
  95.     local locale = GetLocale()
  96.     L = Localizations[locale] or Localizations.enUS -- Default to enUS if locale doesn't exist in the table
  97.     MyRegion = RegionTimes[GetCurrentRegion()] or RegionTimes[1] -- Default to region 1 (US) if it doesn't exist in the table
  98.     f:SetScript("OnUpdate", OnUpdate)
  99. end)
  100.  
  101. SLASH_ZAM4TIMER1 = "/z4" -- toggle hiding/showing the ZAMTimer_4_Events frame using just /z4
  102. SlashCmdList.ZAM4TIMER = function(msg)
  103.     ZAMTimer_4_Events.Elapsed = 0 -- set the "clock" to re-calculate when shown.
  104.     ZAMTimer_4_Events:SetShown(not ZAMTimer_4_Events:IsShown()) -- hide/show the frame
  105. end
Hello. Yes it works. Thank you very much. I understood how slash commands work, thanks for that too.

If I want to remove one event that lasts 1 hour or add one event that lasts 1 hour, I will need to change this number 14400, right? And add or remove a letter.

Lua Code:
  1. totalDuration = 14400, -- complete session time 4 hours repeating

How to add localization to other languages for lines A, B, C, D, etc.

Lua Code:
  1. local RegionTimes = {
  2.     [1] = {
  3.         startTime = 1679572800,
  4.         totalDuration = 14400, -- complete session time 4 hours repeating
  5.         sub_sessionDuration = 3600, -- 1 hour
  6.         waitTime = 3300, -- 55 minutes
  7.         eventtime = 300, -- 5 minutes implied but..
  8.         [1] = { -- sub-sessions
  9.             name = "A",
  10.         },
  11.         [2] = {
  12.             name = "B",
  13.         },
  14.         [3] = {
  15.             name = "C",
  16.         },
  17.         [4] = {
  18.             name = "D",
  19.         },
  20.     },
  21. }

and how to return a sound/color alert to the code
Lua Code:
  1. -- These might be converted to Saved Variables so each character can determine
  2. -- wether or not to play a sound, the alert times and colors and sound to play.
  3. -- If so then most of the code below will have to move into an event handler for
  4. -- the PLAYER_LOGIN or PLAYER_ENTERING_WORLD event.
  5. local useColor = true
  6. local useSound = true
  7. local alert1 = 600 -- Alarm 1 set to 10 minutes before event
  8. local alert1Color = "|cffffff00" -- Yellow
  9. local alert2 = 300 -- Alarm 2 set to 5 minutes before event
  10. local alert2Color = "|cffff0000" -- Red
  11. local soundKit = 32585 -- Alarm sound
  12. ------------------------------------------------------------------------------------------------------
  13.  
  14. local function printTime(timetotrun, inevent)
  15.     local hideSeconds = timetotrun >= 120
  16.     local msg = L.Waiting
  17.     local msgColor = "|cffffffff"
  18.     if inevent then
  19.         msg = L.Running
  20.     else
  21.         if useColor and timetotrun <= alert2 then
  22.             msgColor = alert2Color
  23.         elseif timetotrun <= alert1 then
  24.             if useSound and not ZAMTimer777.Alerted then
  25.                 ZAMTimer777.Alerted = true
  26.                 PlaySound(soundKit, "Master")
  27.             end
  28.             if useColor then
  29.                 msgColor = alert1Color
  30.             end
  31.         end
  32.     end
  33.     f.text:SetText(format(msg, msgColor, SecondsToTime(timetotrun, hideSeconds)))
  34. end
  35. ZAMTimer777.Alerted = false

Last edited by Hubb777 : 03-19-24 at 09:45 PM.
  Reply With Quote