-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.lua
executable file
·231 lines (200 loc) · 7.61 KB
/
functions.lua
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
local ADDON_NAME, ns = ...
local L = ns.L
ns.data.warbandFormatted = "|cff01e2ff" .. L.WarbandWide .. "|r"
local achievements = ns.data.achievements
local achievementsCount = #achievements
local CT = C_Timer
local CQL = C_QuestLog
---
-- Local Functions
---
-- Set default values for options which are not yet set.
-- @param {string} option
-- @param {any} default
local function RegisterDefaultOption(option, default)
if HKT_options[ns.prefix .. option] == nil then
if HKT_options[option] ~= nil then
HKT_options[ns.prefix .. option] = HKT_options[option]
HKT_options[option] = nil
else
HKT_options[ns.prefix .. option] = default
end
end
end
local function FormatNumber(number)
local thousandsSeparator = ns:OptionValue("thousandsSeparator") == 2 and "." or ","
local formatted = tostring(number)
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", "%1" .. thousandsSeparator .. "%2")
if k == 0 then
break
end
end
return formatted
end
local function HighestAchievementIndex()
if ns.data.highestAchievementIndex ~= nil then
return ns.data.highestAchievementIndex
end
local index
for total, _ in pairs(achievements) do
if index == nil or index < total then
index = total
end
end
ns.data.highestAchievementIndex = tonumber(index)
return tonumber(index)
end
local function HighestAchievementID()
if ns.data.highestAchievementID ~= nil then
return ns.data.highestAchievementID
end
local id = tonumber(achievements[HighestAchievementIndex()])
ns.data.highestAchievementID = id
return id
end
local function CurrentAchievementIndex(warbandHKs)
if ns.data.currentAchievementIndex ~= nil and warbandHKs < ns.data.currentAchievementIndex then
return ns.data.currentAchievementIndex
end
local max = HighestAchievementIndex()
if warbandHKs >= max then
return max
end
local index
for total, _ in pairs(achievements) do
if warbandHKs < total and total < (index or max) then
index = tonumber(total)
end
end
ns.data.currentAchievementIndex = index
return index
end
local function CurrentAchievementID(warbandHKs)
if ns.data.currentAchievementID ~= nil and ns.data.currentAchievementIndex ~= nil and warbandHKs < ns.data.currentAchievementIndex then
return ns.data.currentAchievementID
end
local id = tonumber(achievements[CurrentAchievementIndex(warbandHKs)])
ns.data.currentAchievementID = id
return id
end
local function AchievementLink(warbandHKs)
return "|cffffffaa|Hachievement:" .. CurrentAchievementID(warbandHKs) .. ":" .. ns.data.characterID .. ":0:0:0:0:0:0:0:0|h[" .. L.HKs:format(CurrentAchievementIndex(warbandHKs)) .. "]|h|r"
end
local function ShouldTrackCharacterSpecific(warbandHKs)
return ns:OptionValue("characterSpecific") or HighestAchievementIndex() < warbandHKs
end
local function DisplayDivision()
return ns.data.divisions[ns:OptionValue("displayDivision")]
end
local function GetChangeIndex(old, new)
local minLength = math.min(#old, #new)
for i = 1, minLength do
if old:sub(i, i) ~= "," and old:sub(i, i) ~= new:sub(i, i) and new:sub(i, i) ~= "," then
return i
end
end
if #old ~= #new then
return minLength + 1
end
return nil
end
local function FormatChange(old, new)
local index = GetChangeIndex(old, new)
if not index then
return new
end
local left = new:sub(1, index - 1)
local right = new:sub(index)
return left .. "|cff44ff44" .. right .. "|r"
end
local function CharacterHKs()
local value = GetStatistic(ns.data.statistic)
return value and tonumber(value) or 0
end
local function WarbandHKs()
local _, _, _, _, _, _, _, _, quantityString = GetAchievementCriteriaInfo(HighestAchievementID(), 1)
return tonumber(quantityString:match("%d+"))
end
local function PrintStats(trackingType, key, honorableKills, forced)
print(L.HKs:format(trackingType) .. ": " .. (forced and FormatNumber(honorableKills) or FormatChange(FormatNumber(HKT_data[key] or "0"), FormatNumber(honorableKills))))
end
local function EqualDivision(characterHKs, warbandHKs, characterSpecific)
local displayDivision = DisplayDivision()
if displayDivision == 0 then
return false
elseif displayDivision == 1 then
return true
end
local x = math.fmod(characterSpecific and characterHKs or warbandHKs, displayDivision)
return x == 0
end
local displayLocked = false
local function DisplayStats(characterHKs, warbandHKs, characterSpecific, forced)
if forced or not displayLocked then
displayLocked = true
-- Print stats based on character-specific parameter
local trackingType = characterSpecific and ns.data.characterNameFormatted or ns.data.warbandFormatted
local key = characterSpecific and "honorableKillsCharacter" or "honorableKills"
local honorableKills = characterSpecific and characterHKs or warbandHKs
PrintStats(trackingType, key, honorableKills, forced)
-- Print stats based on opposite of character-specific parameter
if forced then
trackingType = characterSpecific and ns.data.warbandFormatted or ns.data.characterNameFormatted
key = characterSpecific and "honorableKills" or "honorableKillsCharacter"
honorableKills = characterSpecific and warbandHKs or characterHKs
PrintStats(trackingType, key, honorableKills, forced)
end
local remaining = CurrentAchievementIndex(warbandHKs) - warbandHKs
if ns:OptionValue("trackAchievements") and warbandHKs < HighestAchievementIndex() then
print(AchievementLink(warbandHKs) .. " " .. L.Remaining:format(FormatNumber(remaining or "0")))
end
HKT_data.remaining = remaining
C_Timer.After(1, function()
displayLocked = false
end)
end
end
---
-- Namespaced Functions
---
--- Set some data about the player
function ns:SetPlayerState()
ns.data.characterID = UnitGUID("player")
ns.data.characterName = UnitName("player") .. "-" .. GetNormalizedRealmName("player")
local _, className, _ = UnitClass("player")
ns.data.className = className
ns.data.characterNameFormatted = "|cff" .. ns.data.classColors[ns.data.className:lower()] .. ns.data.characterName .. "|r"
end
--- Returns an option from the options table
function ns:OptionValue(option)
return HKT_options[ns.prefix .. option]
end
--- Sets default options if they are not already set
function ns:SetDefaultOptions()
HKT_data = HKT_data or {}
HKT_options = HKT_options or {}
for option, default in pairs(ns.data.defaults) do
RegisterDefaultOption(option, default)
end
end
--- Prints a formatted message to the chat
-- @param {string} message
function ns:PrettyPrint(message)
DEFAULT_CHAT_FRAME:AddMessage("|cff" .. ns.color .. ns.name .. "|r " .. message)
end
--- Opens the Addon settings menu and plays a sound
function ns:OpenSettings()
PlaySound(SOUNDKIT.IG_MAINMENU_OPEN)
Settings.OpenToCategory(ns.Settings:GetID())
end
function ns:Alert(forced)
local characterHKs = CharacterHKs()
local warbandHKs = WarbandHKs()
local characterSpecific = ShouldTrackCharacterSpecific(warbandHKs)
if forced or (EqualDivision(characterHKs, warbandHKs, characterSpecific) and HKT_data.honorableKillsCharacter < characterHKs) then
DisplayStats(characterHKs, warbandHKs, characterSpecific, forced)
end
HKT_data.honorableKills = warbandHKs
HKT_data.honorableKillsCharacter = characterHKs
end