/Tukui/modules/datatext/time.lua

http://github.com/Asphyxia/Tukui · Lua · 206 lines · 179 code · 23 blank · 4 comment · 72 complexity · f9d50d224ad8f710360ca5b0be91d80d MD5 · raw file

  1. --------------------------------------------------------------------
  2. -- TIME
  3. --------------------------------------------------------------------
  4. local T, C, L = unpack(select(2, ...)) -- Import Functions/Constants, Config, Locales
  5. if not C["datatext"].wowtime or C["datatext"].wowtime == 0 then return end
  6. local europeDisplayFormat = string.join("", "%02d", T.datacolor,":|r%02d")
  7. local ukDisplayFormat = string.join("", "", "%d", T.datacolor, ":|r%02d", T.datacolor, " %s|r")
  8. local timerLongFormat = "%d:%02d:%02d"
  9. local timerShortFormat = "%d:%02d"
  10. local lockoutInfoFormat = "%s |cffaaaaaa(%s%s, %s/%s)"
  11. local formatBattleGroundInfo = "%s: "
  12. local lockoutColorExtended, lockoutColorNormal = { r=0.3,g=1,b=0.3 }, { r=1,g=1,b=1 }
  13. local difficultyInfo = { "N", "N", "H", "H" }
  14. local curHr, curMin, curAmPm
  15. local Stat = CreateFrame("Frame")
  16. Stat:EnableMouse(true)
  17. Stat:SetFrameStrata("MEDIUM")
  18. Stat:SetFrameLevel(3)
  19. local Text = Stat:CreateFontString(nil, "OVERLAY")
  20. Text:SetFont(C.media.pixelfont, C["datatext"].fontsize, "MONOCHROMEOUTLINE")
  21. T.PP(C["datatext"].wowtime, Text)
  22. local APM = { TIMEMANAGER_PM, TIMEMANAGER_AM }
  23. local function CalculateTimeValues(tt)
  24. if tt == nil then tt = false end
  25. local Hr, Min, AmPm
  26. if tt == true then
  27. if C["datatext"].localtime == true then
  28. Hr, Min = GetGameTime()
  29. if C["datatext"].time24 == true then
  30. return Hr, Min, -1
  31. else
  32. if Hr>=12 then
  33. if Hr>12 then Hr = Hr - 12 end
  34. AmPm = 1
  35. else
  36. if Hr == 0 then Hr = 12 end
  37. AmPm = 2
  38. end
  39. return Hr, Min, AmPm
  40. end
  41. else
  42. local Hr24 = tonumber(date("%H"))
  43. Hr = tonumber(date("%I"))
  44. Min = tonumber(date("%M"))
  45. if C["datatext"].time24 == true then
  46. return Hr24, Min, -1
  47. else
  48. if Hr24>=12 then AmPm = 1 else AmPm = 2 end
  49. return Hr, Min, AmPm
  50. end
  51. end
  52. else
  53. if C["datatext"].localtime == true then
  54. local Hr24 = tonumber(date("%H"))
  55. Hr = tonumber(date("%I"))
  56. Min = tonumber(date("%M"))
  57. if C["datatext"].time24 == true then
  58. return Hr24, Min, -1
  59. else
  60. if Hr24>=12 then AmPm = 1 else AmPm = 2 end
  61. return Hr, Min, AmPm
  62. end
  63. else
  64. Hr, Min = GetGameTime()
  65. if C["datatext"].time24 == true then
  66. return Hr, Min, -1
  67. else
  68. if Hr>=12 then
  69. if Hr>12 then Hr = Hr - 12 end
  70. AmPm = 1
  71. else
  72. if Hr == 0 then Hr = 12 end
  73. AmPm = 2
  74. end
  75. return Hr, Min, AmPm
  76. end
  77. end
  78. end
  79. end
  80. local function CalculateTimeLeft(time)
  81. local hour = floor(time / 3600)
  82. local min = floor(time / 60 - (hour*60))
  83. local sec = time - (hour * 3600) - (min * 60)
  84. return hour, min, sec
  85. end
  86. local function formatResetTime(sec,table)
  87. local table = table or {}
  88. local d,h,m,s = ChatFrame_TimeBreakDown(floor(sec))
  89. local string = gsub(gsub(format(" %dd %dh %dm "..((d==0 and h==0) and "%ds" or ""),d,h,m,s)," 0[dhms]"," "),"%s+"," ")
  90. local string = strtrim(gsub(string, "([dhms])", {d=table.days or L.datatext_time_day,h=table.hours or L.datatext_time_hour,m=table.minutes or L.datatext_time_min,s=table.seconds or L.datatext_time_sec})," ")
  91. return strmatch(string,"^%s*$") and "0"..(table.seconds or L"s") or string
  92. end
  93. local int = 1
  94. local function Update(self, t)
  95. int = int - t
  96. if int > 0 then return end
  97. local Hr, Min, AmPm = CalculateTimeValues()
  98. if GameTimeFrame.flashInvite then
  99. T.Flash(Text, 0.4)
  100. else
  101. T.StopFlash(Text)
  102. end
  103. -- no update quick exit
  104. if (Hr == curHr and Min == curMin and AmPm == curAmPm) then
  105. int = 2
  106. return
  107. end
  108. curHr = Hr
  109. curMin = Min
  110. curAmPm = AmPm
  111. if AmPm == -1 then
  112. Text:SetFormattedText(europeDisplayFormat, Hr, Min)
  113. else
  114. Text:SetFormattedText(ukDisplayFormat, Hr, Min, APM[AmPm])
  115. end
  116. self:SetAllPoints(Text)
  117. int = 2
  118. end
  119. Stat:SetScript("OnEnter", function(self)
  120. OnLoad = function(self) RequestRaidInfo() end
  121. local anchor, panel, xoff, yoff = T.DataTextTooltipAnchor(Text)
  122. GameTooltip:SetOwner(panel, anchor, xoff, yoff)
  123. GameTooltip:ClearLines()
  124. local localizedName, isActive, canQueue, startTime, canEnter
  125. for i = 1, GetNumWorldPVPAreas() do
  126. _, localizedName, isActive, canQueue, startTime, canEnter = GetWorldPVPAreaInfo(i)
  127. if canEnter then
  128. if isActive then
  129. startTime = WINTERGRASP_IN_PROGRESS
  130. elseif startTime == nil then
  131. startTime = QUEUE_TIME_UNAVAILABLE
  132. else
  133. local hour, min, sec = CalculateTimeLeft(startTime)
  134. if hour > 0 then
  135. startTime = string.format(timerLongFormat, hour, min, sec)
  136. else
  137. startTime = string.format(timerShortFormat, min, sec)
  138. end
  139. end
  140. GameTooltip:AddDoubleLine(format(formatBattleGroundInfo, localizedName), startTime)
  141. end
  142. end
  143. local timeText
  144. local Hr, Min, AmPm = CalculateTimeValues(true)
  145. if C["datatext"].localtime == true then
  146. timeText = TIMEMANAGER_TOOLTIP_REALMTIME
  147. else
  148. timeText = TIMEMANAGER_TOOLTIP_LOCALTIME
  149. end
  150. if AmPm == -1 then
  151. GameTooltip:AddDoubleLine(timeText, string.format(europeDisplayFormat, Hr, Min))
  152. else
  153. GameTooltip:AddDoubleLine(timeText, string.format(ukDisplayFormat, Hr, Min, APM[AmPm]))
  154. end
  155. local oneraid, lockoutColor
  156. for i = 1, GetNumSavedInstances() do
  157. local name, _, reset, difficulty, locked, extended, _, isRaid, maxPlayers, _, numEncounters, encounterProgress = GetSavedInstanceInfo(i)
  158. if isRaid and (locked or extended) then
  159. local tr,tg,tb,diff
  160. if not oneraid then
  161. GameTooltip:AddLine(" ")
  162. GameTooltip:AddLine(L.datatext_savedraid)
  163. oneraid = true
  164. end
  165. if extended then lockoutColor = lockoutColorExtended else lockoutColor = lockoutColorNormal end
  166. GameTooltip:AddDoubleLine(format(lockoutInfoFormat, name, maxPlayers, difficultyInfo[difficulty],encounterProgress,numEncounters), formatResetTime(reset), 1,1,1, lockoutColor.r,lockoutColor.g,lockoutColor.b)
  167. end
  168. end
  169. GameTooltip:Show()
  170. end)
  171. Stat:SetScript("OnLeave", function() GameTooltip:Hide() end)
  172. Stat:RegisterEvent("CALENDAR_UPDATE_PENDING_INVITES")
  173. Stat:RegisterEvent("PLAYER_ENTERING_WORLD")
  174. Stat:SetScript("OnUpdate", Update)
  175. Stat:RegisterEvent("UPDATE_INSTANCE_INFO")
  176. Stat:SetScript("OnMouseDown", function(self, btn)
  177. if btn == 'RightButton' then
  178. ToggleTimeManager()
  179. else
  180. GameTimeFrame:Click()
  181. end
  182. end)
  183. Update(Stat, 10)