View Single Post
03-19-24, 11:04 PM   #13
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,892
The PrintTime function is not actually being used in this version so I removed it. Because we replaced the ticker, the calculating and display is all done in the OnUpdate function.

Added some test times for the alarm/colours at 10 and 3 seconds for use with the event test times.

Lua Code:
  1. local addonName, addon = ...
  2. ------------------------------------------------------------------------------------------------------
  3. -- The Data (and Settings?) Section
  4.  
  5. local L, MyRegion
  6. local RegionTimes = {
  7.     [1] = {
  8.         startTime = 1679572800,
  9.         totalDuration = 14400, -- complete session time 4 hours repeating
  10.         sub_sessionDuration = 3600, -- 1 hour
  11.         waitTime = 3300, -- 55 minutes
  12.         eventtime = 300, -- 5 minutes implied but..
  13.         [1] = { -- sub-sessions
  14.             name = "A",
  15.         },
  16.         [2] = {
  17.             name = "B",
  18.         },
  19.         [3] = {
  20.             name = "C",
  21.         },
  22.         [4] = {
  23.             name = "D",
  24.         },
  25.     },
  26. }
  27.  
  28. --[[ TEST TIMES ONLY: over 4 minutes instead of 4 hours ]]--
  29.  
  30. --[[
  31. RegionTimes[1].totalDuration = 240 -- 4 minutes
  32. RegionTimes[1].sub_sessionDuration = 60 -- 1 minute
  33. RegionTimes[1].waitTime = 55 -- seconds
  34. RegionTimes[1].eventtime = 5 -- seconds
  35. ]]--
  36.  
  37. --[[ END TEST TIMES ]]--
  38.  
  39. local Localizations = {
  40.     enUS = {
  41.         Waiting = "%s before event %s starts",
  42.         Running = "Event: |cFF35BE21%s|r\n%s remaining",
  43.         A = "Name of event A",
  44.         B = "Name of event B",
  45.         C = "Name of event B",
  46.         D = "Name of event B",
  47.     },
  48.      deDE = {
  49.         Waiting = "%s verbleibende Zeit bis zur Veranstaltung %s",
  50.         Running = "Ereignis: |cFF35BE21%s|r\n%s Kommen",
  51.         A = "Freiflächen",
  52.         B = "Grüne",
  53.         C = "Rote",
  54.         D = "Weiß",
  55.     },
  56. }
  57.  
  58. -- These might be converted to Saved Variables so each character can determine
  59. -- wether or not to play a sound, the alert times and colors and sound to play.
  60. -- If so then most of the code below will have to move into an event handler for
  61. -- the PLAYER_LOGIN or PLAYER_ENTERING_WORLD event.
  62. local useColor = true
  63. local useSound = true
  64. local alert1 = 300 -- Alarm 1 set to 5 minutes before event
  65. local alert1Color = "|cffffff00" -- Yellow
  66. local alert2 = 30 -- Alarm 2 set to 30 seconds before event
  67. local alert2Color = "|cffff0000" -- Red
  68. local soundKit = 32585 -- Alarm sound
  69.  
  70. --[[ TEST TIMES ONLY: over 10/3 seconds ]]--
  71.  
  72. --[[
  73. local alert1 = 10 -- Alarm 1 set to 5 minutes before event
  74. local alert2 = 3 -- Alarm 2 set to 30 seconds before event
  75. ]]--
  76.  
  77. --[[ END TEST TIMES ]]--
  78.  
  79. ------------------------------------------------------------------------------------------------------
  80. --- The Calculation (work) Section
  81.  
  82. local function OnUpdate(self, elapsed)
  83.     self.Elapsed = self.Elapsed - elapsed
  84.     if self.Elapsed > 0 then -- Only check once per second
  85.         return
  86.     end
  87.     self.Elapsed = 1 -- reset the timeout (we've counted down 1 second)
  88.     local serverTime = GetServerTime()
  89.     local remainingTime = (MyRegion.startTime - serverTime) % MyRegion.totalDuration
  90.     local base = math.ceil(remainingTime / MyRegion.sub_sessionDuration)
  91.     local hourRemaining = MyRegion.sub_sessionDuration - ((base * MyRegion.sub_sessionDuration) - remainingTime)
  92.     local id = 4 - (base - 1)
  93.     if id == 5 then
  94.         id = 1
  95.     end
  96.     local msg
  97.     if hourRemaining > MyRegion.waitTime then
  98.         self.Alarm = false
  99.         msg = format(L.Running, L[MyRegion[id].name], SecondsToTime(hourRemaining - MyRegion.waitTime, false))
  100.     else
  101.         id = id == 4 and 1 or id + 1
  102.         local cm = L.Waiting
  103.         if useColor and hourRemaining <= alert2 then
  104.             cm = alert2Color .. L.Waiting
  105.         elseif hourRemaining <= alert1 then
  106.             if useColor then
  107.                 cm = alert1Color .. L.Waiting
  108.             end
  109.             if useSound and not self.Alarm then
  110.                 self.Alarm = true
  111.                 PlaySound(soundKit, "Master")
  112.             end
  113.         end
  114.         msg = format(cm, SecondsToTime(hourRemaining, false), L[MyRegion[id].name])
  115.     end
  116.     self.Text:SetText(msg)
  117.     self:SetSize(self.Text:GetWidth() + 10, self.Text:GetHeight() + 10)
  118. end
  119.  
  120. ------------------------------------------------------------------------------------------------------
  121. -- The Visual (Frame) Section
  122.  
  123. local Backdrop = {
  124.     bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
  125. }
  126.  
  127. local f = CreateFrame("Button", "ZAMTimer_4_Events", UIParent, "BackdropTemplate")
  128. f:SetWidth(255)                                          
  129. f:SetHeight(30)
  130. f:SetPoint("CENTER")
  131. f:SetBackdrop(Backdrop)
  132. f:SetClampedToScreen(true)
  133. f:EnableMouse(true)
  134. f:SetMovable(true)
  135. f:SetUserPlaced(true)
  136. f:RegisterForDrag("LeftButton")
  137. f:RegisterForClicks("AnyUp")
  138. f.Text = f:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  139. f.Text:SetPoint("CENTER")
  140. f.Elapsed = 0 -- Set starting timeout (0 second)
  141. f:SetScript("OnDragStart",function(self)
  142.     self:StartMoving()
  143. end)
  144. f:SetScript("OnDragStop",function(self)  
  145.     self:StopMovingOrSizing()
  146. end)
  147.  
  148. ------------------------------------------------------------------------------------------------------
  149. -- The "Getting Started" Section:
  150. -- Get the players locale at login and start the OnUpdate timer (to do the work).
  151.  
  152. f:RegisterEvent("PLAYER_LOGIN")
  153. f:SetScript("OnEvent", function(self)
  154.     local locale = GetLocale()
  155.     L = Localizations[locale] or Localizations.enUS -- Default to enUS if locale doesn't exist in the table
  156.     MyRegion = RegionTimes[GetCurrentRegion()] or RegionTimes[1] -- Default to region 1 (US) if it doesn't exist in the table
  157.     f:SetScript("OnUpdate", OnUpdate)
  158. end)
  159.  
  160. ------------------------------------------------------------------------------------------------------
  161. -- Slash Command
  162.  
  163. SLASH_ZAM4TIMER1 = "/z4" -- toggle hiding/showing the ZAMTimer_4_Events frame using just /z4
  164. SlashCmdList.ZAM4TIMER = function(msg)
  165.     ZAMTimer_4_Events.Elapsed = 0 -- set the "clock" to re-calculate when shown.
  166.     ZAMTimer_4_Events:SetShown(not ZAMTimer_4_Events:IsShown()) -- hide/show the frame
  167. end
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 03-20-24 at 11:56 AM.
  Reply With Quote