/
Frenemy.lua
153 lines (116 loc) · 4.45 KB
/
Frenemy.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
-- ----------------------------------------------------------------------------
-- AddOn Namespace
-- ----------------------------------------------------------------------------
local AddOnFolderName = ... ---@type string
local private = select(2, ...) ---@class PrivateNamespace
local DataObject = private.DataObject
local MapHandler = private.MapHandler
local Preferences = private.Preferences
local TooltipHandler = private.TooltipHandler
---@class Frenemy: AceAddon, AceBucket-3.0, AceConsole-3.0, AceEvent-3.0, AceTimer-3.0
local Frenemy =
LibStub("AceAddon-3.0"):NewAddon(AddOnFolderName, "AceBucket-3.0", "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0")
local HereBeDragons = LibStub("HereBeDragons-2.0")
-- ----------------------------------------------------------------------------
-- Events.
-- ----------------------------------------------------------------------------
---@param _ string Callback name. Unused, but is passed from HereBeDragons so it must be handled in the parameter list.
---@param mapID number
function Frenemy:HandleZoneChange(_, mapID)
local needDisplayUpdate = MapHandler.Data.MapID ~= mapID
MapHandler.Data.MapID = mapID
if not MapHandler.Data.MapID or MapHandler.Data.MapID <= 0 then
MapHandler.Data.MapName = UNKNOWN
return
end
MapHandler.Data.MapName = HereBeDragons:GetLocalizedMap(mapID) or UNKNOWN
local pvpType, _, factionName = GetZonePVPInfo()
if pvpType == "hostile" or pvpType == "friendly" then
pvpType = factionName
elseif not pvpType or pvpType == "" then
pvpType = "normal"
end
local zonePVPStatus = MapHandler:GetZonePVPStatus(pvpType)
private.DB.ZoneData[MapHandler.Data.MapID] = zonePVPStatus
MapHandler:SetRGBColor(MapHandler.Data.MapID, zonePVPStatus)
if needDisplayUpdate then
self:UpdateData()
end
end
-- ----------------------------------------------------------------------------
-- Framework
-- ----------------------------------------------------------------------------
local function RequestUpdates()
C_FriendList.ShowFriends()
if IsInGuild() then
C_GuildInfo.GuildRoster()
end
end
local RequestUpdateInterval = 30
function Frenemy:OnEnable()
self:RegisterBucketEvent({
"BN_FRIEND_INFO_CHANGED",
"FRIENDLIST_UPDATE",
"GUILD_RANKS_UPDATE",
"GUILD_ROSTER_UPDATE",
}, 1, self.UpdateData)
HereBeDragons.RegisterCallback(self, "PlayerZoneChanged", "HandleZoneChange")
self:ScheduleRepeatingTimer(RequestUpdates, RequestUpdateInterval)
RequestUpdates()
end
function Frenemy:OnInitialize()
local DB = Preferences:InitializeDatabase()
private.DB = DB
local LDBIcon = LibStub("LibDBIcon-1.0")
if LDBIcon then
LDBIcon:Register(AddOnFolderName, DataObject, DB.DataObject.MinimapIcon)
end
Preferences:SetupOptions()
self:RegisterChatCommand("frenemy", "ChatCommand")
for zoneID, zonePVPStatus in pairs(DB.ZoneData) do
MapHandler:SetRGBColor(zoneID, zonePVPStatus)
end
end
do
local UpdateDisplayThrottleIntervalSeconds = 5
local lastUpdateTime = time()
function Frenemy:UpdateData()
private.UpdateStatistics()
DataObject:UpdateDisplay()
if TooltipHandler.Tooltip.Main and TooltipHandler.Tooltip.Main:IsShown() then
local now = time()
if now > lastUpdateTime + UpdateDisplayThrottleIntervalSeconds then
lastUpdateTime = now
TooltipHandler:Render(DataObject)
end
end
end
end
do
local SUBCOMMAND_FUNCS = {
--@debug@
DEBUG = function()
local debugger = private.GetDebugger()
if debugger:Lines() == 0 then
debugger:AddLine("Nothing to report.")
debugger:Display()
debugger:Clear()
return
end
debugger:Display()
end,
--@end-debug@
}
---@param input string
function Frenemy:ChatCommand(input)
local subcommand, arguments = self:GetArgs(input, 2)
if subcommand then
local func = SUBCOMMAND_FUNCS[subcommand:upper()]
if func then
func(arguments or "")
end
return
end
Preferences:ToggleOptionsVisibility()
end
end -- do-block