-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloomAshenvaleTracker.lua
More file actions
executable file
·245 lines (203 loc) · 8.82 KB
/
Copy pathBloomAshenvaleTracker.lua
File metadata and controls
executable file
·245 lines (203 loc) · 8.82 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
local BloomAshenvaleTracker = ...
BloomAshenvaleTrackerSettings = BloomAshenvaleTrackerSettings or {}
if BloomAshenvaleTrackerSettings.accountForLayers == nil then
BloomAshenvaleTrackerSettings.accountForLayers = false
end
if BloomAshenvaleTrackerSettings.shareInGuild == nil then
BloomAshenvaleTrackerSettings.shareInGuild = true
end
if BloomAshenvaleTrackerSettings.shareInParty == nil then
BloomAshenvaleTrackerSettings.shareInParty = true
end
if BloomAshenvaleTrackerSettings.shareInRaid == nil then
BloomAshenvaleTrackerSettings.shareInRaid = true
end
local settings = BloomAshenvaleTrackerSettings
BloomAshenvaleTrackerLayerInfo = {}
local lastUpdateTime = 0
local UPDATE_INTERVAL = 5
local ASHENVALE_MAP_ID = 1440
local ALLIANCE_WIDGET_ID = 5360
local HORDE_WIDGET_ID = 5361
-- Create main frame for display
local mainFrame = CreateFrame("Frame", "BloomAshenvaleTrackerMainFrame", UIParent, "BackdropTemplate")
local contentText
-- Create a clear button
local clearButton = CreateFrame("Button", "BloomAshenvaleTrackerClearButton", mainFrame, "UIPanelButtonTemplate")
clearButton:SetSize(80, 22) -- Width, Height
clearButton:SetPoint("BOTTOM", mainFrame, "BOTTOM", 0, 10) -- Position it at the bottom of the mainFrame
clearButton:SetText("Clear")
clearButton:Hide() -- Initially hide the button
-- Function to update the visibility of the clear button
UpdateClearButtonVisibility = function()
if settings.accountForLayers and next(BloomAshenvaleTrackerLayerInfo) ~= nil then
clearButton:Show()
else
clearButton:Hide()
end
end
local function ClearEntries()
wipe(BloomAshenvaleTrackerLayerInfo) -- Clears the BloomAshenvaleTrackerLayerInfo table
UpdateFrame() -- Update the frame to reflect the cleared data
UpdateClearButtonVisibility() -- Update the visibility of the clear button
end
clearButton:SetScript("OnClick", ClearEntries)
-- Local Function Definitions
local function SetupMainFrame(frame)
frame:SetWidth(360)
frame:SetPoint("CENTER")
frame:SetBackdrop({
bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
tile = true,
tileSize = 32,
edgeSize = 32,
insets = { left = 8, right = 8, top = 8, bottom = 8 }
})
frame:SetMovable(true)
frame:EnableMouse(true)
frame:RegisterForDrag("LeftButton")
frame:SetScript("OnDragStart", frame.StartMoving)
frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
local titleText = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
titleText:SetPoint("CENTER", frame, "TOP", 0, -20)
titleText:SetText("Bloom Ashenvale Tracker")
contentText = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
contentText:SetPoint("CENTER", frame, "CENTER", 0, 0)
contentText:SetWidth(350)
contentText:SetJustifyH("CENTER")
contentText:SetJustifyV("CENTER")
contentText:SetText("")
end
function UpdateFrame()
local displayText, frameHeight = "", 60
local info = settings.accountForLayers and BloomAshenvaleTrackerLayerInfo or
{ default = BloomAshenvaleTrackerLayerInfo["default"] }
for layer, layerData in pairs(info) do
local timeString = date("%H:%M", layerData.lastUpdated)
if settings.accountForLayers then
displayText = displayText ..
string.format("Layer %s: Alliance: %s, Horde: %s (Updated: %s)\n",
tostring(layer), layerData.allianceProgress, layerData.hordeProgress, timeString)
else
displayText = string.format("Alliance: %s, Horde: %s (Updated: %s)\n",
layerData.allianceProgress, layerData.hordeProgress, timeString)
break -- Break after the first iteration as we only need one entry
end
frameHeight = frameHeight + 20
end
mainFrame:SetHeight(frameHeight)
contentText:SetText(displayText)
end
local function UpdateTimeDisplay()
if time() - lastUpdateTime >= UPDATE_INTERVAL then
lastUpdateTime = time()
UpdateFrame()
end
end
local function GetEventProgress()
local allianceProgress = C_UIWidgetManager.GetIconAndTextWidgetVisualizationInfo(ALLIANCE_WIDGET_ID).text
local hordeProgress = C_UIWidgetManager.GetIconAndTextWidgetVisualizationInfo(HORDE_WIDGET_ID).text
local layer = settings.accountForLayers and (_G["NWB_CurrentLayer"] or 0) or 0
return allianceProgress, hordeProgress, layer
end
local function SendProgress()
if C_Map.GetBestMapForUnit("player") == ASHENVALE_MAP_ID then
local allianceProgress, hordeProgress, layer = GetEventProgress()
local message = string.format("%s|%s|%s", allianceProgress, hordeProgress, layer)
if BloomAshenvaleTrackerSettings.shareInGuild and IsInGuild() then
C_ChatInfo.SendAddonMessage("BAT", message, "GUILD")
end
if BloomAshenvaleTrackerSettings.shareInParty and IsInGroup(LE_PARTY_CATEGORY_HOME) and not IsInRaid(LE_PARTY_CATEGORY_HOME) then
C_ChatInfo.SendAddonMessage("BAT", message, "PARTY")
end
if BloomAshenvaleTrackerSettings.shareInRaid and IsInRaid(LE_PARTY_CATEGORY_HOME) then
C_ChatInfo.SendAddonMessage("BAT", message, "RAID")
end
UpdateFrame()
end
end
local function HandleAddonMessage(self, event, ...)
if event == "ADDON_LOADED" then
local addonName = ...
if addonName == "BloomAshenvaleTracker" then
-- Initialize or update settings from SavedVariables
BloomAshenvaleTrackerSettings = BloomAshenvaleTrackerSettings or {}
settings = BloomAshenvaleTrackerSettings
-- Initialize the settings UI
InitializeSettingsUI()
-- Update Clear Button visibility based on the loaded settings
UpdateClearButtonVisibility()
BloomAshenvaleTrackerLayerInfo = BloomAshenvaleTrackerLayerInfo or {}
-- Unregister the ADDON_LOADED event
self:UnregisterEvent("ADDON_LOADED")
end
elseif event == "CHAT_MSG_ADDON" then
local prefix, message, sender = ...
if prefix == "BAT" then
local allianceProgress, hordeProgress, layer = strsplit("|", message)
local layerKey = settings.accountForLayers and layer or "default"
BloomAshenvaleTrackerLayerInfo[layerKey] = {
allianceProgress = allianceProgress,
hordeProgress = hordeProgress,
lastUpdated = time()
}
UpdateFrame()
end
end
end
local function RemoveDuplicateZeroLayerEntries()
local entriesToRemove = {}
-- Iterate over all entries
for layer, layerData in pairs(BloomAshenvaleTrackerLayerInfo) do
-- Skip the 'default' layer
if layer ~= "default" then
-- Compare with other layers
for otherLayer, otherLayerData in pairs(BloomAshenvaleTrackerLayerInfo) do
-- Check if it's not the same layer and not the 'default' layer
if layer ~= otherLayer and otherLayer ~= "default" then
-- Check if both layers have the same progress values
if layerData.allianceProgress == otherLayerData.allianceProgress and
layerData.hordeProgress == otherLayerData.hordeProgress then
-- Mark layer 0 for removal if it's one of the duplicates
if layer == "0" or otherLayer == "0" then
entriesToRemove[layer == "0" and layer or otherLayer] = true
end
end
end
end
end
end
-- Remove marked entries
for layer, _ in pairs(entriesToRemove) do
BloomAshenvaleTrackerLayerInfo[layer] = nil
end
-- Update the UI if necessary
UpdateFrame()
end
local function ToggleFrameAndUpdate()
if mainFrame:IsShown() then
mainFrame:Hide()
else
mainFrame:Show()
UpdateFrame()
end
end
-- Setup main frame
SetupMainFrame(mainFrame)
-- Register Addon
local addonRegisteredSuccessfully = C_ChatInfo.RegisterAddonMessagePrefix("BAT")
print(addonRegisteredSuccessfully and "BloomAshenvaleTracker started successfully!" or
"BloomAshenvaleTracker failed to start successfully!")
-- Event Handling
local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("ADDON_LOADED")
eventFrame:RegisterEvent("CHAT_MSG_ADDON")
eventFrame:SetScript("OnEvent", HandleAddonMessage)
-- Slash Command
SLASH_BLOOMASHENVALETRACKER1 = "/bat"
SlashCmdList["BLOOMASHENVALETRACKER"] = ToggleFrameAndUpdate
-- Timers
C_Timer.NewTicker(UPDATE_INTERVAL, UpdateTimeDisplay)
C_Timer.NewTicker(UPDATE_INTERVAL * 2, SendProgress)
C_Timer.NewTicker(UPDATE_INTERVAL * 3, RemoveDuplicateZeroLayerEntries)