Thread Tools Display Modes
04-27-24, 04:14 AM   #1
Hubb777
A Flamescale Wyrmkin
 
Hubb777's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2024
Posts: 113
Timer with two regions (America and Europe)

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 = -200
  8. f = CreateFrame("Button", "ZAMROTimer", UIParent, "BackdropTemplate")
  9. f:SetWidth(185)
  10. f:SetHeight(30)
  11. f:SetBackdrop(Backdrop)
  12. f.text = f:CreateFontString(nil,"OVERLAY","GameTooltipText")
  13. f.text:SetPoint("CENTER")
  14. f:SetClampedToScreen(true)
  15. f:SetPoint("CENTER",UIParent,"CENTER",frame_x,frame_y)
  16. f:EnableMouse(true)
  17. f:SetMovable(true)
  18. f:RegisterForDrag("LeftButton")
  19. f:RegisterForClicks("AnyUp")
  20. f:Show()
  21. f:RegisterEvent("PLAYER_ENTERING_WORLD")
  22. f:SetScript("OnDragStart",function(this)
  23.     this:StartMoving()
  24. end)
  25. f:SetScript("OnDragStop",function(this)
  26.     this:StopMovingOrSizing()
  27.     frame_x,frame_y = this:GetCenter()
  28.     frame_x = frame_x - GetScreenWidth() / 2
  29.     frame_y = frame_y - GetScreenHeight() / 2
  30.     this:ClearAllPoints()
  31.     this:SetPoint("CENTER",UIParent,"CENTER",frame_x,frame_y)
  32. end)
  33.  
  34. local Localizations = {
  35.     enUS = {
  36.         Waiting = "|c1C7BCEFFCommunity Feast:\nbefore the start: %s%s|r",
  37.         Running = "|cFF35BE21Community Feast:\n%s%s until completion|r",
  38.     },
  39.     deDE = {
  40.         Waiting = "|c1C7BCEFFGemeinschaftliches Festmahl:\nvor dem Anfang: %s%s|r",
  41.         Running = "|cFF35BE21Gemeinschaftliches Festmahl:\n%s%s bis zur Fertigstellung|r",
  42.     },
  43. }
  44.  
  45. local locale = GetLocale()
  46. local L = Localizations[locale] or Localizations.enUS
  47.  
  48. local useColor = true
  49. local useSound = true
  50. local alert1 = 600
  51. local alert1Color = "|cffffff00"
  52. local alert2 = 300
  53. local alert2Color = "|cffff0000"
  54. local soundKit = 32585
  55.  
  56. local function printTime(timetotrun, inevent)
  57.     local hideSeconds = timetotrun >= 120
  58.     local msg = L.Waiting
  59.     local msgColor = "|cffffffff"
  60.     if inevent then
  61.         msg = L.Running
  62.     else
  63.         if useColor and timetotrun <= alert2 then
  64.             msgColor = alert2Color
  65.         elseif timetotrun <= alert1 then
  66.             if useSound and not ZAMROTimer.Alerted then
  67.                 ZAMROTimer.Alerted = true
  68.                 PlaySound(soundKit, "Master")
  69.             end
  70.             if useColor then
  71.                 msgColor = alert1Color
  72.             end
  73.         end
  74.     end
  75.     f.text:SetText(format(msg, msgColor, SecondsToTime(timetotrun, hideSeconds)))
  76. end
  77.  
  78. local regionEventStartTime = {
  79.     [1] = { -- eu
  80.         starttime = 1670331660,
  81.         eventDuration = 900,
  82.         eventIntervalInSeconds = 5400,
  83.         enable = true,
  84.         datablock = {}
  85.     },
  86.     [2] = { -- america
  87.         starttime = 1670338860,
  88.         eventDuration = 900,
  89.         eventIntervalInSeconds = 5400,
  90.         enable = true,
  91.         datablock = {}
  92.     }
  93. }
  94.  
  95. local inEvent, timeToRun
  96. local eventTime = regionEventStartTime[1].eventDuration -- Default to EU region for calculations
  97. local waitTime = regionEventStartTime[1].eventIntervalInSeconds
  98. local startTime = regionEventStartTime[1].starttime
  99. local serverTime = GetServerTime()
  100. local timeToEvent = (startTime - serverTime) % waitTime
  101.  
  102. if timeToEvent > (waitTime - eventTime) then
  103.     inEvent = true
  104.     timeToRun = eventTime - (waitTime - timeToEvent)
  105. else
  106.     inEvent = false
  107.     timeToRun = timeToEvent
  108. end
  109.  
  110. -- Check if in the Americas region, then adjust time calculations
  111. if GetCVar("portal") == "US" then
  112.     eventTime = regionEventStartTime[2].eventDuration
  113.     waitTime = regionEventStartTime[2].eventIntervalInSeconds
  114.     startTime = regionEventStartTime[2].starttime
  115.     timeToEvent = (startTime - serverTime) % waitTime
  116.  
  117.     if timeToEvent > (waitTime - eventTime) then
  118.         inEvent = true
  119.         timeToRun = eventTime - (waitTime - timeToEvent)
  120.     else
  121.         inEvent = false
  122.         timeToRun = timeToEvent
  123.     end
  124. end
  125.  
  126. local ticker = C_Timer.NewTicker(1, function()
  127.     if timeToRun > 0 then
  128.         timeToRun = timeToRun - 1
  129.         printTime(timeToRun, inEvent)
  130.         return
  131.     end
  132.     ZAMROTimer.Alerted = false
  133.     if inEvent then
  134.         inEvent = false
  135.         timeToRun = waitTime - eventTime
  136.     else
  137.         inEvent = true
  138.         timeToRun = eventTime
  139.     end
  140.     printTime(timeToRun, inEvent)
  141. end)
  142. printTime(timeToRun, inEvent)


Hello. I added support for a second region to the timer (added America), initially there was only Europe. But I can't test. Did I do the right thing? I play on European servers and don't understand how I can test this code. I wanted to test it on the PTR server (it seems to be in America), but it still displays European time.
  Reply With Quote
04-27-24, 09:11 AM   #2
Hubb777
A Flamescale Wyrmkin
 
Hubb777's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2024
Posts: 113
Lua Code:
  1. local addonName, addon = ...
  2.  
  3. local Backdrop = {
  4.     bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
  5. }
  6.  
  7. local frame_x = 0
  8. local frame_y = -200
  9.  
  10. local f = CreateFrame("Button", "ZAMROTimer", UIParent, "BackdropTemplate")
  11. f:SetWidth(185)
  12. f:SetHeight(30)
  13. f:SetBackdrop(Backdrop)
  14. f.text = f:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  15. f.text:SetPoint("CENTER")
  16. f:SetClampedToScreen(true)
  17. f:SetPoint("CENTER", UIParent, "CENTER", frame_x, frame_y)
  18. f:EnableMouse(true)
  19. f:SetMovable(true)
  20. f:RegisterForDrag("LeftButton")
  21. f:RegisterForClicks("AnyUp")
  22. f:Show()
  23. f:RegisterEvent("PLAYER_ENTERING_WORLD")
  24. f:SetScript("OnDragStart", function(this)
  25.     this:StartMoving()
  26. end)
  27. f:SetScript("OnDragStop", function(this)
  28.     this:StopMovingOrSizing()
  29.     frame_x, frame_y = this:GetCenter()
  30.     frame_x = frame_x - GetScreenWidth() / 2
  31.     frame_y = frame_y - GetScreenHeight() / 2
  32.     this:ClearAllPoints()
  33.     this:SetPoint("CENTER", UIParent, "CENTER", frame_x, frame_y)
  34. end)
  35.  
  36. local Localizations = {
  37.     enUS = {
  38.         Waiting = "|c1C7BCEFFCommunity Feast:\nbefore the start: %s%s|r",
  39.         Running = "|cFF35BE21Community Feast:\n%s%s until completion|r",
  40.     },
  41.     deDE = {
  42.         Waiting = "|c1C7BCEFFGemeinschaftliches Festmahl:\nvor dem Anfang: %s%s|r",
  43.         Running = "|cFF35BE21Gemeinschaftliches Festmahl:\n%s%s bis zur Fertigstellung|r",
  44.     },
  45. }
  46.  
  47. local locale = GetLocale()
  48. local L = Localizations[locale] or Localizations.enUS
  49.  
  50. local useColor = true
  51. local useSound = true
  52. local alert1 = 600
  53. local alert1Color = "|cffffff00"
  54. local alert2 = 300
  55. local alert2Color = "|cffff0000"
  56. local soundKit = 32585
  57.  
  58. local function printTime(timetotrun, inevent)
  59.     local hideSeconds = timetotrun >= 120
  60.     local msg = L.Waiting
  61.     local msgColor = "|cffffffff"
  62.     if inevent then
  63.         msg = L.Running
  64.     else
  65.         if useColor and timetotrun <= alert2 then
  66.             msgColor = alert2Color
  67.         elseif timetotrun <= alert1 then
  68.             if useSound and not ZAMROTimer.Alerted then
  69.                 ZAMROTimer.Alerted = true
  70.                 PlaySound(soundKit, "Master")
  71.             end
  72.             if useColor then
  73.                 msgColor = alert1Color
  74.             end
  75.         end
  76.     end
  77.     f.text:SetText(format(msg, msgColor, SecondsToTime(timetotrun, hideSeconds)))
  78. end
  79.  
  80. local regionEventStartTime = {
  81.     [1] = { -- eu
  82.         starttime = 1670331660,
  83.         eventDuration = 900,
  84.         eventIntervalInSeconds = 5400,
  85.         enable = true,
  86.         datablock = {}
  87.     },
  88.     [2] = { -- na
  89.         starttime = 1670338860,
  90.         eventDuration = 900,
  91.         eventIntervalInSeconds = 5400,
  92.         enable = true,
  93.         datablock = {}
  94.     }
  95. }
  96.  
  97. local startTime, eventTime, waitTime
  98.  
  99. if GetCVar("portal") == "EU" then
  100.     startTime = regionEventStartTime[1].starttime
  101.     eventTime = regionEventStartTime[1].eventDuration
  102.     waitTime = regionEventStartTime[1].eventIntervalInSeconds
  103. else
  104.     startTime = regionEventStartTime[2].starttime
  105.     eventTime = regionEventStartTime[2].eventDuration
  106.     waitTime = regionEventStartTime[2].eventIntervalInSeconds
  107. end
  108.  
  109. local serverTime = GetServerTime()
  110. local timeToEvent = (startTime - serverTime) % waitTime
  111.  
  112. local inEvent, timeToRun
  113.  
  114. if timeToEvent > (waitTime - eventTime) then
  115.     inEvent = true
  116.     timeToRun = eventTime - (waitTime - timeToEvent)
  117. else
  118.     inEvent = false
  119.     timeToRun = timeToEvent
  120. end
  121.  
  122. local ticker = C_Timer.NewTicker(1, function()
  123.     if timeToRun > 0 then
  124.         timeToRun = timeToRun - 1
  125.         printTime(timeToRun, inEvent)
  126.         return
  127.     end
  128.     ZAMROTimer.Alerted = false
  129.     if inEvent then
  130.         inEvent = false
  131.         timeToRun = waitTime - eventTime
  132.     else
  133.         inEvent = true
  134.         timeToRun = eventTime
  135.     end
  136.     printTime(timeToRun, inEvent)
  137. end)
  138.  
  139. printTime(timeToRun, inEvent)

I solved this problem myself like this. There might have been another solution, but that was the only option I could come up with.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Timer with two regions (America and Europe)


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