Thread: Timer problems
View Single Post
03-30-24, 02:31 AM   #1
Hubb777
A Flamescale Wyrmkin
 
Hubb777's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2024
Posts: 122
Timer problems

Lua Code:
  1. local addonName, addon = ...
  2. local L, MyRegion
  3. local RegionTimes = {
  4.     [1] = {
  5.         startTime = 1710482400,
  6.         totalDuration = 18000,
  7.         A = {
  8.             duration = 7200,
  9.             interval = 3600,
  10.             eventtime = 600,
  11.         },
  12.         B = {
  13.             duration = 10200,
  14.         },
  15.     },
  16.     [2] = {
  17.         startTime = 1670331660,
  18.         A = {
  19.             duration = 900,
  20.             interval = 5400,
  21.         },
  22.     },
  23. }
  24.  
  25. local Localizations = {
  26.     enUS = {
  27.         Waiting = "|cFFDEB887Trial of the Elements:%s\nbefore the start: %s%s|r",
  28.         Running = "|cFF35BE21Trial of the Elements:%s\n%s%s until completion|r",
  29.         Timer2Interval = "Every hour", -- Added English text for timer 2 interval
  30.         Timer2EventTime = "10 minutes", -- Added English text for timer 2 event time
  31.     },
  32.     deDE = {
  33.         Waiting = "|cFFDEB887Prüfung der Elemente:%s\nvor dem Anfang: %s%s|r",
  34.         Running = "|cFF35BE21Prüfung der Elemente:%s\n%s%s bis zur Fertigstellung|r",
  35.         Timer2Interval = "|c1C7BCEFFTolles Fest durch:|r", -- Added German text for timer 2 interval
  36.         Timer2EventTime = "|cFF35BE21Das große Fest steht vor der Tür:|r", -- Added German text for timer 2 event time
  37.     },
  38. }
  39.  
  40. ------------------------------------------------------------------------------------------------------
  41. -- These might be converted to Saved Variables so each character can determine
  42. -- wether or not to play a sound, the alert times and colors and sound to play.
  43. -- If so then most of the code below will have to move into an event handler for
  44. -- the PLAYER_LOGIN or PLAYER_ENTERING_WORLD event.
  45. local defaults = {
  46.     useColor = true,
  47.     useSound = true,
  48.     alert1 = 600, -- Alarm 1 set to 10 minutes before event
  49.     alert1Color = "|cffffff00", -- Yellow
  50.     alert2 = 300, -- Alarm 2 set to 5 minutes before event
  51.     alert2Color = "|cffff0000", -- Red
  52.     soundKit = 32585, -- Alarm sound
  53. }
  54.  
  55. ------------------------------------------------------------------------------------------------------
  56. local function CalcTime(starttime, servertime, duration, interval)
  57.     local timeToEvent = (starttime - servertime) % interval
  58.     local inEvent, timeToRun
  59.     if timeToEvent > (interval - duration) then
  60.         inEvent = true
  61.         timeToRun = duration - (interval - timeToEvent)
  62.     else
  63.         inEvent = false
  64.         timeToRun = timeToEvent
  65.     end
  66.     return inEvent, timeToRun
  67. end
  68.  
  69. local function printTime(self)
  70.     local serverTime = GetServerTime()
  71.     local remainingTime1 = (MyRegion.startTime - serverTime) % MyRegion.totalDuration
  72.     local remainingTime2 = 0
  73.     local longWait = ""
  74.     local inEvent1, inEvent2
  75.     if remainingTime1 > RegionTimes[1].B.duration then
  76.         inEvent1, remainingTime1 = CalcTime(MyRegion.startTime, serverTime, MyRegion.A.eventtime, MyRegion.A.interval)
  77.     else
  78.         longWait = "|cffffff00|r"
  79.     end
  80.     if MyRegion.A.interval > 0 then
  81.         local timer2Remaining = (MyRegion.startTime - serverTime) % MyRegion.A.interval
  82.         remainingTime2 = timer2Remaining
  83.         if timer2Remaining > (MyRegion.A.interval - MyRegion.A.eventtime) then
  84.             inEvent2 = true
  85.             remainingTime2 = MyRegion.A.eventtime - (MyRegion.A.interval - timer2Remaining)
  86.         else
  87.             inEvent2 = false
  88.             remainingTime2 = MyRegion.A.interval - timer2Remaining
  89.         end
  90.     end
  91.     local hideSeconds = remainingTime1 >= 120
  92.     local msg = L.Waiting
  93.     local msgColor = "|cffffffff"
  94.     if inEvent1 then
  95.         msg = L.Running
  96.     else
  97.         if defaults.useColor and remainingTime1 <= defaults.alert2 then
  98.             msgColor = defaults.alert2Color
  99.         elseif remainingTime1 <= defaults.alert1 then
  100.             if defaults.useSound and not self.Alerted then
  101.                 self.Alerted = true
  102.                 PlaySound(defaults.soundKit, "Master")
  103.             end
  104.             if defaults.useColor then
  105.                 msgColor = defaults.alert1Color
  106.             end
  107.         end
  108.     end
  109.    
  110.        -- Display timer 1
  111.     local timer1Msg = format("%s", SecondsToTime(remainingTime1, false))
  112.  
  113.     -- Display timer 2 information
  114.     local timer2Msg = ""
  115.     if MyRegion.A.interval > 0 then
  116.         timer2Msg = format("\n\n%s\n%s", L.Timer2Interval, SecondsToTime(remainingTime2, false))
  117.     end
  118.     if MyRegion.A.eventtime > 0 and inEvent2 then
  119.         timer2Msg = format("%s\n\n%s\n%s", timer2Msg, L.Timer2EventTime, SecondsToTime(remainingTime2, false))
  120.     end
  121.  
  122.     self.Text:SetText(format(msg, longWait, msgColor, timer1Msg) .. timer2Msg)
  123. end
  124.  
  125. ------------------------------------------------------------------------------------------------------
  126. local Backdrop = {
  127.     bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
  128. }
  129.  
  130. local frame_x = 100    
  131. local frame_y = -250    
  132. local f = CreateFrame("Button", "ZAMTimer777", UIParent, "BackdropTemplate")
  133. f:SetWidth(185)                                          
  134. f:SetHeight(30)
  135. f:SetPoint("CENTER")
  136. f:SetBackdrop(Backdrop)
  137. f:SetClampedToScreen(true)
  138. f:EnableMouse(true)
  139. f:SetMovable(true)
  140. f:SetUserPlaced(true)
  141. f:RegisterForDrag("LeftButton")
  142. f:RegisterForClicks("AnyUp")
  143. f.Text = f:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  144. f.Text:SetPoint("CENTER")
  145. f.Elapsed = 0 -- Set starting timeout (0 second)
  146. f:SetScript("OnDragStart",function(self)
  147.     self:StartMoving()
  148. end)
  149. f:SetScript("OnDragStop",function(self)  
  150.     self:StopMovingOrSizing()
  151. end)
  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.     self:SetScript("OnUpdate", function(self, elapsed)
  158.     self.Elapsed = self.Elapsed - elapsed
  159.     if self.Elapsed > 0 then -- Only check once per second
  160.         return
  161.     end
  162.     self.Elapsed = 1 -- reset the timeout (we've counted down 1 second)
  163.     printTime(self)
  164.     end)
  165. end)
  166.  
  167. SLASH_ZAM1 = "/zam"
  168. SlashCmdList["ZAM"] = function(msg)
  169.     if strupper(strtrim(msg)) == "HD" then -- toggle the shown state of the button if the type /hubb btn
  170.         ZAMTimer777:SetShown(not ZAMTimer777:IsShown()) -- show the button
  171.         return
  172.     end
  173.     updateData()
  174.     updateList()
  175.     ZAMTimer777:Show()
  176. end

Hello. I tried to add a second timer (Timer2Interval) to the current timer 1 (Trial of the Elements).

Errors:
Timer 2 counts incorrectly.
When the "duration" condition occurs, 3 timers are displayed.
There is no sound signal from the second timer.

Last edited by Hubb777 : 03-30-24 at 02:51 AM.
  Reply With Quote