-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepTracker.lua
188 lines (175 loc) · 5.74 KB
/
RepTracker.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
local f = CreateFrame("Frame", nil, UIParent)
local events = {}
local start_x = 25
local start_y = -100
-- Return a color to a StandingId
-- @param standingId
-- @returns red green blue values between 0 and 255
local function get_standing_color(standingId)
-- Ids according to https://wow.gamepedia.com/StandingId
if standingId == 0 then -- 0 - Unknown
return 0, 0, 0
elseif standingId == 1 then -- 1 - Hated
return 127, 0, 0
elseif standingId == 2 then -- 2 - Hostile
return 255, 0, 0
elseif standingId == 3 then -- 3 - Unfriendly
return 255, 127, 0
elseif standingId == 4 then -- 4 - Neutral
return 127, 127, 127
elseif standingId == 5 then -- 5 - Friendly
return 127, 255, 0
elseif standingId == 6 then -- 6 - Honored
return 0, 255, 0
elseif standingId == 7 then -- 7 - Revered
return 0, 127, 0
elseif standingId == 8 then -- 8 - Exalted
return 127, 0, 255
end
return 0, 0, 0
end
-- Check if the given faction should be shown
-- @param faction name
-- @returns true if the faction should be shown, false otherwise
local function show_faction(name)
return RepTracker_Factions[string.lower(name)]
end
-- Manages all bars.
local BarManager = {
ref_pos = "TOPLEFT",
next_x = start_x,
next_y = start_y,
bars={},
cur_bar = 0,
-- Initializes all bars (empty)
-- @param width width of a bar
-- @param height height of a bar
init = function(self, width, height)
for i=0, 20 do
bar = CreateFrame("StatusBar", nil, UIParent)
bar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
bar:SetPoint(self.ref_pos, self.next_x, self.next_y)
bar:SetSize(width, height)
bar.label = bar:CreateFontString(nil, "OVERLAY")
bar.label:SetFont("Fonts\\FRIZQT__.TTF", 11, "OUTLINE")
bar.label:SetAllPoints(true)
bar.label:SetJustifyH("LEFT") -- label is left aligned
bar.label:SetJustifyV("TOP") -- label is aligned to top
RegisterStateDriver(bar, "visibility", "hide")
self.bars[i] = bar
self.next_y = self.next_y - height -- set next_y to next bar slot
end
end,
-- Update bars according to shown factions.
-- @param str name of faction
-- @param min Minimum value of StatusBar
-- @param max Maximum value of StatusBar
-- @param standingId
update = function(self, str, min, max, current, standingId)
if show_faction(str) ~= true then return end
i = self.cur_bar
if standingId == 8 then
-- Show full bar on Exalted (21000/21000).
current = 21000
max = 21000
end
self.bars[i]:SetMinMaxValues(min, max)
self.bars[i]:SetValue(current)
self.bars[i]:SetStatusBarColor(get_standing_color(standingId))
self.bars[i].label:SetText(str..": "..current.." / "..max)
if show_faction(str) and RepTracker_GeneralSettings.enabled then
RegisterStateDriver(self.bars[i], "visibility", "[group]hide;show")
else
RegisterStateDriver(self.bars[i], "visibility", "hide")
end
-- Increment current bar...
self.cur_bar = i + 1
end,
reset = function(self)
self.cur_bar = 0
-- Hide all bars.
for k,v in pairs(self.bars) do
RegisterStateDriver(v, "visibility", "hide")
end
end
}
--- Handler for ADDON_LOADED event (on login or UI reload)
function events:ADDON_LOADED(name)
if string.lower(name) ~= "reptracker" then return end
if RepTracker_GeneralSettings then
RepTracker_GeneralSettings = RepTracker_GeneralSettings
else
RepTracker_GeneralSettings = {
enabled = true,
bar_width = 200,
bar_height = 10,
}
end
if RepTracker_Factions then
RepTracker_Factions = RepTracker_Factions
else
RepTracker_Factions = {}
end
-- Init bar manager
BarManager:init(RepTracker_GeneralSettings.bar_width,
RepTracker_GeneralSettings.bar_height)
end
--- Handler for PLAYER_LOGOUT event (logout)
function events:PLAYER_LOGOUT(name)
if string.lower(name) ~= "reptracker" then return end
RepTracker_GeneralSettings = RepTracker_GeneralSettings
RepTracker_Factions = RepTracker_Factions
end
-- Handler for UPDATE_FACTION event
function events:UPDATE_FACTION(...)
BarManager:reset()
for idx = 1, GetNumFactions() do
repeat
name, _, standingId, bottomValue, topValue, earnedValue, _,
_, isHeader, _, _, _, _ = GetFactionInfo(idx);
-- Skip headers
if isHeader == true then do break end end
-- Adjust to current standing.
topValue = topValue - bottomValue
earnedValue = earnedValue - bottomValue
-- Finally add this faction to a bar.
BarManager:update(name, 0, topValue, earnedValue, standingId)
until true
end
end
f:SetScript("OnEvent", function(self, event, ...)
events[event](self, ...)
end);
for k, v in pairs(events) do
f:RegisterEvent(k)
end
--- Handle console commands
local function CommandHandler(msg, editbox)
local _, _, cmd, args = string.find(msg, "%s?(%w+)%s?(.*)")
if cmd == "add" and args ~= "" then
print("adding " .. args)
RepTracker_Factions[string.lower(args)] = true
events:UPDATE_FACTION()
elseif cmd == "remove" and args ~= "" then
print("removing " .. args)
RepTracker_Factions[string.lower(args)] = false
events:UPDATE_FACTION()
elseif cmd == "enable" then
RepTracker_GeneralSettings.enabled = true
elseif cmd == "disable" then
RepTracker_GeneralSettings.enabled = false
elseif cmd == "clear" then
RepTracker_Factions = {}
elseif cmd == "show" then
for k, v in pairs(RepTracker_Factions) do print(k, v) end
elseif cmd == "update" then
events:UPDATE_FACTION() -- Force update
else
-- TODO: Improve help message.
print("Syntax: /reptracker (add|remove|clear|show|update) [factionName]");
end
end
SLASH_REPTRACKER1 = '/reptracker'
SlashCmdList["REPTRACKER"] = CommandHandler
-- Show the bars
f:Show()