Thread Tools Display Modes
Prev Previous Post   Next Post Next
03-04-24, 10:24 PM   #1
Hubb777
A Flamescale Wyrmkin
 
Hubb777's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2024
Posts: 114
Timer with 2 variables

Lua Code:
  1. local addonName, addon = ...
  2. local Backdrop = {
  3.     bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
  4. }
  5.  
  6. local frame_x = 100    
  7. local frame_y = -250    
  8. local f = CreateFrame("Button", "ZAMTimer777", UIParent, "BackdropTemplate")
  9. f:SetWidth(255)                                          
  10. f:SetHeight(30)
  11. f:SetBackdrop(Backdrop)
  12. f.text = f:CreateFontString(nil,"OVERLAY","GameTooltipText")
  13. f.text:SetTextHeight(15)
  14. f.text:SetPoint("CENTER")
  15. f:SetClampedToScreen(true)
  16. f:SetPoint("CENTER",UIParent,"CENTER",frame_x,frame_y)
  17. f:EnableMouse(true)
  18. f:SetMovable(true)
  19. f:RegisterForDrag("LeftButton")
  20. f:RegisterForClicks("AnyUp")
  21. f:Show()
  22. f:RegisterEvent("PLAYER_ENTERING_WORLD")
  23. f:SetScript("OnDragStart",function(this)
  24.     this:StartMoving()
  25. end)
  26. f:SetScript("OnDragStop",function(this)  
  27.     this:StopMovingOrSizing()
  28.     frame_x,frame_y = this:GetRIGHT()
  29.     frame_x = frame_x - GetScreenWidth() / 2
  30.     frame_y = frame_y - GetScreenHeight() / 2
  31.     this:ClearAllPoints()
  32.     this:SetPoint("CENTER",UIParent,"CENTER",frame_x,frame_y)
  33. end)
  34. -- first %s is replaced by the color. The second is replaced by the time. |r resets the color back to default
  35. local Localizations = {
  36.     enUS = {
  37.         Waiting = "|c1C7BCEFFEvent:\nbefore the start: %s%s|r",
  38.         Running = "|cFF35BE21Event:\n%s%s until completion|r",
  39.     },
  40. }
  41.  
  42. local locale = GetLocale()
  43. local L = Localizations[locale] or Localizations.enUS -- Default to enUS if locale doesn't exist in the table
  44.  
  45. ------------------------------------------------------------------------------------------------------
  46. -- These might be converted to Saved Variables so each character can determine
  47. -- wether or not to play a sound, the alert times and colors and sound to play.
  48. -- If so then most of the code below will have to move into an event handler for
  49. -- the PLAYER_LOGIN or PLAYER_ENTERING_WORLD event.
  50. local useColor = true
  51. local useSound = true
  52. local alert1 = 600 -- Alarm 1 set to 10 minutes before event
  53. local alert1Color = "|cffffff00" -- Yellow
  54. local alert2 = 300 -- Alarm 2 set to 5 minutes before event
  55. local alert2Color = "|cffff0000" -- Red
  56. local soundKit = 32585 -- Alarm sound
  57. ------------------------------------------------------------------------------------------------------
  58.  
  59. local function printTime(timetotrun, inevent)
  60.     local hideSeconds = timetotrun >= 120
  61.     local msg = L.Waiting
  62.     local msgColor = "|cffffffff"
  63.     if inevent then
  64.         msg = L.Running
  65.     else
  66.         if useColor and timetotrun <= alert2 then
  67.             msgColor = alert2Color
  68.         elseif timetotrun <= alert1 then
  69.             if useSound and not ZAMTimer777.Alerted then
  70.                 ZAMTimer777.Alerted = true
  71.                 PlaySound(soundKit, "Master")
  72.             end
  73.             if useColor then
  74.                 msgColor = alert1Color
  75.             end
  76.         end
  77.     end
  78.     f.text:SetText(format(msg, msgColor, SecondsToTime(timetotrun, hideSeconds)))
  79. end
  80.  
  81. regionEventStartTime = {
  82.     [1] = { -- eu
  83.         starttime = 1709615040,
  84.         eventDuration = 240,
  85.         eventIntervalInSeconds = 3600,
  86.         enable = true,
  87.         datablock = {}
  88.     },
  89. }
  90.  
  91. local inEvent, timeToRun
  92. local eventTime = regionEventStartTime[1].eventDuration -- Time the event runs in seconds(15 mins)
  93. local waitTime = regionEventStartTime[1].eventIntervalInSeconds -- Time between events in seconds (90 mins)
  94. local startTime = regionEventStartTime[1].starttime -- Start time from the table
  95. local serverTime = GetServerTime()
  96. local timeToEvent = (startTime - serverTime) % waitTime -- Remaining time before next event starts
  97.  
  98. if timeToEvent > (waitTime - eventTime) then -- Is there between 1:15 and 1:30 to go? If so, we're in the event
  99.     inEvent = true
  100.     timeToRun = eventTime - (waitTime - timeToEvent)
  101. else                    -- Otherwise, set the ticker timer to time to next event
  102.     inEvent = false
  103.     timeToRun = timeToEvent
  104. end
  105. local ticker = C_Timer.NewTicker(1, function()
  106.     if timeToRun > 0 then
  107.         timeToRun = timeToRun - 1
  108.         printTime(timeToRun, inEvent)
  109.         return
  110.     end
  111.     ZAMTimer777.Alerted = false
  112.     if inEvent then -- The event just finished
  113.         inEvent = false
  114.         timeToRun = waitTime - eventTime -- Reset ticker timer to 90 minutes wait time minus 15 mins event time
  115.     else  -- Waiting for the next event just expired
  116.         inEvent = true
  117.         timeToRun = eventTime -- And the event is running
  118.     end
  119.     printTime(timeToRun, inEvent)
  120. end)
  121. printTime(timeToRun, inEvent)

1) Data. There is an event A and an event B.
2) Event A data is entered into the code (Start at 1709615040, interval 3600 and event time 240)
3) Event B (Start at 1709629200, interval 21600 (6 hours) and event time 7200 (2 hours))

Task:
1) The timer should fire every hour (this is in the code and it works).
2) The timer should increase the interval between events when event B occurs, for the duration of event B (by 2 hours).

For example:
08:04 - event A has started - the timer shows 1 hour until the next event.
09:04 - event A has started - the timer shows 1 hour until the next event.
10:04 - event A has started - the timer shows 3 hours until the next event. Since at 11:00 there will be event B.
11:04 - event A has not started - the timer shows 2 hours until the next event. Since at 11:00 there was event B.
12:04 - event A has not started - the timer shows 1 hour until the next event. Since at 11:00 there was event B.
13:04 - event A has started - the timer shows 1 hour until the next event.

Last edited by Hubb777 : 03-04-24 at 10:36 PM.
  Reply With Quote
 

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Timer with 2 variables


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off