View Single Post
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: 128
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