forked from cybertk/ConcentrationRecharge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.lua
More file actions
216 lines (177 loc) · 5.17 KB
/
Util.lua
File metadata and controls
216 lines (177 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
local _, namespace = ...
local Util = {
debug = false,
}
namespace.Util = Util
namespace.debug = function(...)
Util:Debug(...)
end
function Util:Debug(...)
if self.debug ~= true then
return
end
if type(select(1, ...)) == "function" then
-- Delayed print
print(RED_FONT_COLOR:WrapTextInColorCode("Debug:"), select(1, ...)())
else
print(RED_FONT_COLOR:WrapTextInColorCode("Debug:"), ...)
end
end
function Util:DebugQuest(questID)
if self.debug ~= true or questID == nil or questID == 0 then
return
end
local s = format(
"(%d)[%s]: IsOn=%s Completed=%s TimeLeft=%s Objectives:",
questID,
QuestUtils_GetQuestName(questID),
C_QuestLog.IsOnQuest(questID) == true and "YES" or "NO",
C_QuestLog.IsQuestFlaggedCompleted(questID) == true and "YES" or "NO",
C_TaskQuest.GetQuestTimeLeftSeconds(questID) or "unknown"
)
for i, objective in ipairs(C_QuestLog.GetQuestObjectives(questID) or {}) do
if objective then
s = s .. format("[%d] = ", i) .. objective.text
end
end
print(RED_FONT_COLOR:WrapTextInColorCode("DebugQuest:"), s)
end
function Util:Filter(t, pattern, inplace, asList)
asList = asList or true
if inplace then
for i = #t, 1, -1 do
if pattern(t[i]) == false then
table.remove(t, i)
end
end
if not asList then
for k, v in pairs(t) do
if pattern(v) == false then
t[k] = nil
end
end
end
return t
end
local newTable = {}
local indicesProcessed = {}
for i, v in ipairs(t) do
if pattern(v) == true then
table.insert(newTable, v)
indicesProcessed[i] = true
end
end
if not asList then
for k, v in pairs(t) do
if indicesProcessed[k] == nil and pattern(v) == true then
newTable[k] = v
end
end
end
return newTable
end
function Util:GetCalendarActiveEvents(calendarType)
local now = C_DateAndTime.GetCurrentCalendarTime()
local events = {}
calendarType = calendarType or "HOLIDAY"
for i = 1, C_Calendar.GetNumDayEvents(0, now.monthDay) do
local event = C_Calendar.GetDayEvent(0, now.monthDay, i)
if
event.calendarType == calendarType
and C_DateAndTime.CompareCalendarTime(event.startTime, now) >= 0
and C_DateAndTime.CompareCalendarTime(event.endTime, now) < 0
then
events[event.eventID] = event
end
end
return events
end
function Util:GetTimestampFromCalendarTime(calendarTime)
return time({
year = calendarTime.year,
month = calendarTime.month,
day = calendarTime.monthDay,
hour = calendarTime.hour,
min = calendarTime.minute,
sec = 0,
})
end
-- Return whether the current character has learned the given profession
---@param skillLineID number The profession SkillLine IDs, see https://warcraft.wiki.gg/wiki/TradeSkillLineID
---@param useCache? boolean Use the cache by default
---@return boolean
function Util:IsProfessionLearned(skillLineID, useCache)
useCache = useCache or true
if self.professions == nil or not useCache then
local professions = {}
local tabIndices = { GetProfessions() }
for i = 1, 5 do
if tabIndices[i] ~= nil then
local name, icon, skillLevel, maxSkillLevel, numAbilities, spelloffset, skillLine, _ = GetProfessionInfo(tabIndices[i])
professions[skillLine] = { id = skillLine, icon = icon }
end
end
self.professions = professions
end
return self.professions[skillLineID] ~= nil
end
-- Return the icon of given profession
---@param skillLineID number
---@return number The icon ID
function Util:GetProfessionIcon(skillLineID)
local professionSpells = {
[171] = 423321, -- Alchemy
[794] = 278910, -- Archaeology
[164] = 423332, -- Blacksmithing
[185] = 2550, -- Cooking
[333] = 423334, -- Enchanting
[202] = 423335, -- Engineering
[356] = 131474, -- Fishing
[182] = 441327, -- Herbalism
[773] = 423338, -- Inscription
[755] = 423339, -- Jewelcrafting
[165] = 423340, -- Leatherworking
[186] = 423341, -- Mining
[393] = 423342, -- Skinning
[197] = 423343, -- Tailoring
}
return select(1, C_Spell.GetSpellTexture(professionSpells[skillLineID]))
end
function Util:DungeonToQuest(dungeonID)
return dungeonID + 9000000
end
function Util.FormatTimeDuration(seconds, useAbbreviation)
local minutes = seconds / 60
local hours = minutes / 60
local days = hours / 24
useAbbreviation = useAbbreviation or false
local unitD, unitH, unitM = useAbbreviation and "d" or " Days", useAbbreviation and "h" or " Hours", useAbbreviation and "m" or " Minutes"
if hours < 1 then
-- Round up to 1 min
return format("%d%s", minutes >= 1 and minutes or 1, unitM)
end
if days < 1 then
return format("%d%s", hours % 24, unitH)
end
return format("%d%s %d%s", days, unitD, hours % 24, unitH)
end
function Util.FormatItem(item)
local s = CreateSimpleTextureMarkup(item.texture or 0, 13, 13) -- There is hidden item, i.e. spark drops
if item.quality then
s = s .. ITEM_QUALITY_COLORS[item.quality].color:WrapTextInColorCode(format(" [%s]", item.name))
else
s = s .. " " .. item.name
end
local quantity = item.quantity or item.amount or 0
if quantity > 1 then
s = s .. format(" x%d", quantity)
end
return WHITE_FONT_COLOR:WrapTextInColorCode(s)
end
function Util.WrapTextInClassColor(classFile, ...)
local color = C_ClassColor.GetClassColor(classFile)
if color then
return color:WrapTextInColorCode(...)
end
return ...
end