-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.lua
202 lines (172 loc) · 5.84 KB
/
core.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
local addon, ns = ...
local gEnable = ns.cfg.enable
local fcfg = ns.cfg.filterConfig
local Bich = ns.Bich
local BichBar = ns.BichBar
-- some functions
local function printMobInfoToChat(mobInfo)
local name = Bich:getUnitName(mobInfo.unitid)
local msg = ""
for i, spellid in ipairs(mobInfo.spells) do
msg = msg .. GetSpellLink(spellid)
end
DEFAULT_CHAT_FRAME:AddMessage(name .. ":" .. msg, 255, 255, 255)
end
local function printSearchResultToChat(resultSet, limit)
limit = limit or #resultSet -- no limit by default
for i, mobInfo in ipairs(resultSet) do
if i <= limit then
printMobInfoToChat(mobInfo)
end
end
end
local function searchAndShow(cmd, param)
searchResult = Bich:search(cmd, param)
if #searchResult > 0 then
local randomChoosen = searchResult[ fastrandom(1,#searchResult) ]
BichBar:createBar(randomChoosen.spells)
printSearchResultToChat(searchResult)
--printSearchResultToChat({randomChoosen})
end
end
-- initial
local function initAddon()
Bich:checkVersion() -- check database version
ns.loaded = true
DEFAULT_CHAT_FRAME:AddMessage("|cff00f0f0Bich|r loaded", 255, 255, 255)
end
local function handlerAddonLoaded(name)
if name == "Bich" then
initAddon()
end
end
-- get the spells and create bar
local function unitCreateBar(flag)
local guid = UnitGUID(flag)
local unitid = Bich:getCreatureIdByGuid(guid)
local zone = Bich:getZone()
local spells = Bich:getFilteredSpellInfo(zone, unitid, "sort")
if next(spells) ~= nil then
local name = UnitName(flag)
Bich:setUnitName(unitid, name)
BichBar:createBar(spells)
-- for debug
-- printMobInfoToChat({zone=zone, unitid=unitid, spells=spells})
end
end
local function m_GetSpellLink(l)
if GetSpellLink(l) then
return GetSpellLink(l)
else
return l
end
end
local function m_legalString(str)
if type(str) == "string" then
return str
elseif type(str) == "number" then
return string.format("[%d]", str)
elseif type(str) == "nil" then
return "nil"
else
return type(str)
end
end
-- store spell information
local function handlerCombatLog(timeStamp, event, hideCaster, sourceGUID, sourceName, sourceFlags,
sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags, ...)
if not Bich:checkZone(fcfg.track) then
return
end
-- if select(1,...) ~= nil and not string.find(event, "^SWING") then -- "SWING_xxx" events dont have spell id.
-- local l = select(1,...)
-- DEFAULT_CHAT_FRAME:AddMessage(event..":"..m_legalString(sourceName)..":"..m_GetSpellLink(l), 255, 255, 255)
-- end
if event == "SPELL_HEAL" or event == "SPELL_CAST_START" or event == "SPELL_CAST_SUCCESS" or event == "SPELL_PERIODIC_DAMAGE" or event == "SPELL_MISSED" then
local spellid = select(1, ...)
local unitid = Bich:getCreatureIdByGuid(sourceGUID)
local zone, zoneName = Bich:getZone()
if unitid > 0 then
local newSpell = Bich:addSpellAndSetName(zone, zoneName, unitid, sourceName, spellid)
local tarGuid = UnitGUID("target")
if unitid == Bich:getCreatureIdByGuid(tarGuid) and newSpell then
unitCreateBar("target")
end
end
end
end
-- show spells stored when target changed
local function handlerTargetChanged()
BichBar:hideAllButtons()
if UnitGUID("target") == nil or not Bich:checkZone(fcfg.show) then --target is nil or not shown in this zone
return
end
unitCreateBar("target")
end
-- save BichDB after logout
local function handlerPlayerLogout()
for zone, _ in pairs(BichDB) do
if not Bich:checkZone(fcfg.save, zone) then
BichDB[zone] = nil
end
end
for zone, _ in pairs(BichAddition.zone) do
if not Bich:checkZone(fcfg.save, zone) then
BichAddition.zone[zone] = nil
end
end
end
--event handler
local frame = CreateFrame("Frame")
function frame:onEvent(event, ...)
if gEnable then
if ns.loaded then
if event == "COMBAT_LOG_EVENT_UNFILTERED" then
handlerCombatLog(...)
elseif event == "PLAYER_TARGET_CHANGED" or event == "ZONE_CHANGED_NEW_AREA" then
handlerTargetChanged()
elseif event == "PLAYER_LOGOUT" then
handlerPlayerLogout()
end
elseif event == "ADDON_LOADED" then
handlerAddonLoaded(...)
end
end
end
frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
frame:RegisterEvent("PLAYER_TARGET_CHANGED")
frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("PLAYER_LOGOUT")
frame:RegisterEvent("ZONE_CHANGED_NEW_AREA")
frame:SetScript("OnEvent", frame.onEvent)
-- slash commands
SLASH_BICH_RELOADER1 = "/rl"
SlashCmdList.BICH_RELOADER = function() ReloadUI() end
SLASH_BICH_CLEARDB1 = "/bichcleardb"
SLASH_BICH_CLEARDB2 = "/bclr"
SlashCmdList.BICH_CLEARDB = function()
_G["BichDB"] = {}
_G["BichAddition"].zone = {}
_G["BichAddition"].unit = {}
end
SLASH_BICH_SEARCHDB1 = "/bichsearchdb"
SLASH_BICH_SEARCHDB2 = "/bs"
SlashCmdList.BICH_SEARCHDB = function(msg, editbox)
local cmd, param = msg:match("^(%S*)%s*(.-)$")
cmd = string.lower(cmd)
if cmd == "num" or cmd == "number" then
param = tonumber(param)
elseif cmd == "mob" or cmd == "creature" or cmd == "unit" then
param = tonumber(param)
elseif cmd == "name" then
-- do nothing
else -- invalid cmd
param = nil
end
if param ~= nil then
searchAndShow(cmd, param)
else
DEFAULT_CHAT_FRAME:AddMessage("Wrong parameters!", 255, 255, 255)
DEFAULT_CHAT_FRAME:AddMessage("|cff00f0f0/bs|r num|unit|name param", 255, 255, 255)
end
end