View Single Post
03-17-24, 10:23 AM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,894
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
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 03-17-24 at 01:31 PM.
  Reply With Quote