View Single Post
02-24-24, 10:18 PM   #1
Hubb777
A Flamescale Wyrmkin
 
Hubb777's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2024
Posts: 113
2 addons use one timer code

Posted in a separate topic

I'm planning to create 2 addons that will use the same code

Lua Code:
  1. local addonName, addon = ...
  2. local Backdrop = {
  3.     bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
  4. }
  5.  
  6. local frame_x = 0    
  7. local frame_y = -250    
  8. 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 = "|c1C7BCEFFResearchers Under Fire:\nbefore the start: %s%s|r",
  38.         Running = "|cFF35BE21Researchers Under Fire:\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 = 1708756200,
  84.         eventDuration = 1500,
  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)



Lua Code:
  1. local frame_x = 0    
  2. local frame_y = -250    
  3. f = CreateFrame("Button", "ZAMTimer777", UIParent, "BackdropTemplate")
I changed 200 to 250 and ZAMTimer to ZAMTimer777 in one of them
I even changed all the variables ZAMTimer to ZAMTimer777
But the addon flashes periodically and displays data from another addon
  Reply With Quote