forked from delflein/MobInfo2-Classic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMI2_Slash.lua
More file actions
565 lines (502 loc) · 18.5 KB
/
MI2_Slash.lua
File metadata and controls
565 lines (502 loc) · 18.5 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
--
-- MI2_Slash.lua
--
-- Handle all slash commands and the actions performed by slash commands.
-- All option dialog settings use slash commands for performing their
-- actions.
--
-- Note: version history now located in ReadMe.txt
--
local MI2_DeleteMode = ""
-- defaults for all MobInfo config options
local MI2_TTDefaults = {
ShowHealth=1, ShowMana=0, ShowXp=1, ShowNo2lev=1, ShowKills=0, ShowLoots=1, ShowTotal=1,
ShowCoin=0, ShowIV=0, ShowEmpty=0, ShowLowHpAction=1, ShowCloth=1, ShowDamage=1,
ShowDps=1, ShowLocation=1, ShowQuality=1, ShowResists=1, ShowImmuns=1, ShowItems=1,
ShowClothSkin=1, ShowIGrey=0, ShowIWhite=1, ShowIGreen=1, ShowIBlue=1, ShowIPurple=1 }
-----------------------------------------------------------------------------
-- MI2_SlashAction_Default()
--
-- Default tooltip content settings
--
function MI2_SlashAction_Default()
local idx, def
for idx,def in pairs(MI2_TTDefaults) do
MobInfoConfig[idx] = def
end
end -- MI2_SlashAction_Default
-----------------------------------------------------------------------------
-- MI2_SlashAction_AllOn()
--
-- Show all tooltip content
--
function MI2_SlashAction_AllOn()
local idx,def
for idx,def in pairs(MI2_TTDefaults) do
MobInfoConfig[idx] = 1
end
end -- MI2_SlashAction_AllOn
-----------------------------------------------------------------------------
-- MI2_SlashAction_AllOff()
--
-- Show no extra info in tooltip
--
function MI2_SlashAction_AllOff()
local idx,def
for idx,def in pairs(MI2_TTDefaults) do
MobInfoConfig[idx] = 0
end
end -- MI2_SlashAction_AllOff
-----------------------------------------------------------------------------
-- MI2_RegisterWithAddonManagers()
--
-- Register MobInfo with the KHAOS AddOn manager. This is a very simple
-- registration that merely creates a button to open the MobInfo options
-- dialog.
--
-- Register MobInfo with the myAddons manager.
--
-- Register with the EARTH AddOn manager.
--
function MI2_RegisterWithAddonManagers(self)
-- register with myAddons manager
if ( myAddOnsFrame_Register ) then
local mobInfo2Details = {
name = "MobInfo",
version = miVersionNo,
author = "Skeeve & Dizzarian",
website = "http://www.dizzarian.com/forums/viewforum.php?f=16",
category = MYADDONS_CATEGORY_OTHERS,
optionsframe = "MI2_OptionsFrame"
}
myAddOnsFrame_Register( mobInfo2Details )
end
-- register with EARTH manager (mainly for Cosmos support)
if EarthFeature_AddButton then
EarthFeature_AddButton(
{
id = "MobInfo",
name = "MobInfo",
subtext = "v"..miVersionNo,
tooltip = MI_DESCRIPTION,
icon = "Interface\\CharacterFrame\\TemporaryPortrait-Monster",
callback = function(state) MI2_SlashParse("", self) end
}
)
if not MobInfoConfig.ShowMMButton then MobInfoConfig.ShowMMButton = 0 end
-- register with KHAOS (only if EARTH not found)
elseif Khaos then
Khaos.registerOptionSet(
"tooltip",
{
id = "MobInfoOptionSet",
text = "MobInfo",
helptext = MI_DESCRIPTION,
difficulty = 1,
callback = function(state) end,
default = true,
options = {
{
id = "MobInfoOptionsHeader",
type = K_HEADER,
difficulty = 1,
text = MI_TXT_WELCOME,
helptext = MI_DESCRIPTION
},
{
id = "MobInfoOptionsButton",
type = K_BUTTON,
difficulty = 1,
text = "MobInfo",
helptext = "",
callback = function(state) MI2_SlashParse("", self) end,
feedback = function(state) end,
setup = { buttonText = MI_TXT_OPEN }
}
}
}
)
if not MobInfoConfig.ShowMMButton then MobInfoConfig.ShowMMButton = 0 end
end
end -- MI2_RegisterWithAddonManagers()
-----------------------------------------------------------------------------
-- MI2_SlashAction_ClearTarget()
--
-- Clear MobInfo and MobHealth data for current target.
--
function MI2_SlashAction_ClearTarget()
local index = MI2_Target.mobIndex
if index then
MI2_MobHealth_ClearTargetData()
MI2_DeleteMobData( index )
MI2_Target = {}
MI2_OnTargetChanged()
MI2_DbOptionsFrameOnShow()
chattext( "data for target "..MI_Green..index..MI_White.." has been deleted" )
end
end -- MI2_SlashAction_ClearTarget()
-----------------------------------------------------------------------------
-- MI2_Slash_ClearDbConfirmed()
--
-- Clear database handler : clear specific database if reqzested and
-- confirmed by user.
--
local function MI2_Slash_ClearDbConfirmed()
if MI2_DeleteMode == "MobDb" then
local curZoneName = MI2_ZoneTable[MI2_CurZone]
MI2_DeleteAllMobData()
MI2_ZoneTable[MI2_CurZone] = curZoneName
MobInfoConfig.ImportSignature = ""
elseif MI2_DeleteMode == "HealthDb" then
MI2_MobHealth_Reset()
elseif MI2_DeleteMode == "PlayerDb" then
MobHealthPlayerDB = {}
end
chattext( "database deleted: "..MI2_DeleteMode )
MI2_Target = {}
MI2_OnTargetChanged()
MI2_DbOptionsFrameOnShow()
collectgarbage( "collect" )
end -- MI2_Slash_ClearDbConfirmed()
-----------------------------------------------------------------------------
-- MI2_SlashAction_ClearHealthDb()
--
-- Clear entire contents of MobInfo and MobHealth databases.
-- Ask for confirmation before performing the clear operation.
--
function MI2_SlashAction_ClearHealthDb(self)
StaticPopupDialogs["MOBINFO_CONFIRMATION"].text = MI_TXT_CLR_ALL_CONFIRM.."'"..MI2_OPTIONS[self:GetName()].help.."' ?"
StaticPopupDialogs["MOBINFO_CONFIRMATION"].OnAccept = MI2_Slash_ClearDbConfirmed
MI2_DeleteMode = "HealthDb"
local dialog = StaticPopup_Show( "MOBINFO_CONFIRMATION", "")
end -- MI2_SlashAction_ClearHealthDb()
-----------------------------------------------------------------------------
-- MI2_SlashAction_ClearPlayerDb()
--
-- Clear entire contents of MobInfo and MobHealth databases.
-- Ask for confirmation before performing the clear operation.
--
function MI2_SlashAction_ClearPlayerDb(self)
StaticPopupDialogs["MOBINFO_CONFIRMATION"].text = MI_TXT_CLR_ALL_CONFIRM.."'"..MI2_OPTIONS[self:GetName()].help.."' ?"
StaticPopupDialogs["MOBINFO_CONFIRMATION"].OnAccept = MI2_Slash_ClearDbConfirmed
MI2_DeleteMode = "PlayerDb"
local dialog = StaticPopup_Show( "MOBINFO_CONFIRMATION", "")
end -- MI2_SlashAction_ClearPlayerDb()
-----------------------------------------------------------------------------
-- MI2_SlashAction_ClearMobDb()
--
-- Clear entire contents of MobInfo and MobHealth databases.
-- Ask for confirmation before performing the clear operation.
--
function MI2_SlashAction_ClearMobDb(self)
StaticPopupDialogs["MOBINFO_CONFIRMATION"].text = MI_TXT_CLR_ALL_CONFIRM.."'"..MI2_OPTIONS[self:GetName()].help.."' ?"
StaticPopupDialogs["MOBINFO_CONFIRMATION"].OnAccept = MI2_Slash_ClearDbConfirmed
MI2_DeleteMode = "MobDb"
local dialog = StaticPopup_Show( "MOBINFO_CONFIRMATION", "")
end -- MI2_SlashAction_ClearMobDb()
-----------------------------------------------------------------------------
-- MI2_Slash_TrimDownConfirmed()
--
-- Trim down the contents of the mob info database by removing all data
-- that is not set as being recorded. This function is called when the
-- user confirms the delete confirmation.
--
local function MI2_Slash_TrimDownConfirmed()
-- loop through database and check each record
-- remove all fields within the record where recording of the field is disabled
for idx, mobInfo in pairs(MobInfoDB) do
if MobInfoConfig.SaveBasicInfo == 0 then
mobInfo.bi = nil
mobInfo.ml = nil
end
if MobInfoConfig.SaveQualityData == 0 then
mobInfo.qi = nil
end
if MobInfoConfig.SaveItems == 0 then
mobInfo.il = nil
end
if MobInfoConfig.SaveResist == 0 then
mobInfo.re = nil
end
if MobInfoConfig.SaveCharData == 0 then
MI2_RemoveCharData( mobInfo )
end
end
if MobInfoConfig.SaveItems == 0 then
MI2_ItemNameTable = {}
end
-- char table can be deleted when not saving char specific data
if MobInfoConfig.SaveCharData == 0 then
MI2_CharTable = { charCount = 0 }
end
-- force a cleanup after trimming down
MI2_ClearMobCache()
MI2_CheckAndCleanDatabases()
collectgarbage( "collect" )
MI2_DbOptionsFrameOnShow()
end -- MI2_Slash_TrimDownConfirmed()
-----------------------------------------------------------------------------
-- MI2_SlashAction_TrimDownMobData()
--
-- Trim down the contents of the mob info database by removing all data
-- that is not set as being recorded. Ask for a confirmation before
-- actually deleting anything.
--
function MI2_SlashAction_TrimDownMobData()
StaticPopupDialogs["MOBINFO_CONFIRMATION"].text = MI_TXT_TRIM_DOWN_CONFIRM
StaticPopupDialogs["MOBINFO_CONFIRMATION"].OnAccept = MI2_Slash_TrimDownConfirmed
local dialog = StaticPopup_Show( "MOBINFO_CONFIRMATION", "")
end -- MI2_SlashAction_TrimDownMobData()
-----------------------------------------------------------------------------
-- MI2_UpdateMob()
--
-- Update a specific existing mob by adding to it the given new Mob data.
--
local function MI2_UpdateMob( mobIndex, newMobInfo )
local existingMobInfo = MobInfoDB[mobIndex]
local existingMobData, newMobData = {}, {}
MI2_GetMobDataFromMobInfo( existingMobInfo, existingMobData )
MI2_GetMobDataFromMobInfo( newMobInfo, newMobData )
MI2_AddTwoMobs( existingMobData, newMobData )
MI2_StoreAllMobData( existingMobData, nil, nil, MI2_PlayerName, mobIndex )
end -- MI2_UpdateMob()
-----------------------------------------------------------------------------
-- MI2_AdaptImportLocation()
--
-- Adapt the location info of an imported Mob. This is only necessary for
-- Mobs in instances, because instances are not available in the WoW
-- zone tables.
--
local function MI2_AdaptImportLocation( mobInfo, importZoneTable )
-- decode mob location information
local mobData = {}
MI2_DecodeMobLocation( mobInfo, mobData )
local loc = mobData.location
if not loc or not loc.z then return end
-- find correct zone name
local zone = mobData.location.z
local zoneName = importZoneTable[zone]
-- update the mobs location info
if zoneName and zoneName ~= "" then
MI2_SetNewZone( zoneName )
importZoneTable[MI2_CurZone] = zoneName
mobInfo.ml = (loc.x1 or "").."/"..(loc.y1 or "").."/"..(loc.x2 or "").."/"..(loc.y2 or "").."//"..MI2_CurZone
else
mobInfo.ml = nil
end
end -- MI2_AdaptImportLocation()
-----------------------------------------------------------------------------
-- MI2_SlashAction_ImportMobData()
--
-- Import externally supplied MobInfo database into own database.
--
function MI2_SlashAction_ImportMobData()
local newMobs, updatedMobs, newHealth, newItems = 0, 0, 0, 0
local mobIndex, mobInfo, healthInfo
local oldCurZone = MI2_CurZone
chattext( " starting external database import ...." )
-- import loot items into main loot item database
for itemId, itemInfo in pairs(MI2_ItemNameTable_Import) do
if not MI2_ItemNameTable[itemId] then
MI2_ItemNameTable[itemId] = itemInfo
newItems = newItems + 1
end
end
-- import health data into main Mob health database
for mobIndex, newHealthData in pairs(MobHealthDB_Import) do
local oldHealthData = MobHealthDB[mobIndex]
if not oldHealthData then
MobHealthDB[mobIndex] = newHealthData
newHealth = newHealth + 1
else
local _, oldPct = MI2_HpDecode( oldHealthData )
local _, newPct = MI2_HpDecode( newHealthData )
if newPct and oldPct and newPct > 90 and newPct > oldPct then
MobHealthDB[mobIndex] = newHealthData
newHealth = newHealth + 1
end
end
end
-- swap name/id in zone name table
if MI2_ZoneTable_Import.cnt then
local zoneName, zoneId
local newTable = {}
MI2_ZoneTable_Import.cnt = nil
for zoneName, zoneId in pairs(MI2_ZoneTable_Import) do
newTable[zoneId] = zoneName
end
MI2_ZoneTable_Import = newTable
end
-- import Mobs into main Mob database
for mobIndex, mobInfo in pairs(MobInfoDB_Import) do
MI2_RemoveCharData( mobInfo )
MI2_AdaptImportLocation( mobInfo, MI2_ZoneTable_Import )
if MobInfoDB[mobIndex] then
updatedMobs = updatedMobs + 1
if MobInfoConfig.ImportOnlyNew == 0 then
-- import Mob that already exists
MI2_UpdateMob( mobIndex, mobInfo )
end
else
-- import unknown Mob
MobInfoDB[mobIndex] = mobInfo
newMobs = newMobs + 1
end
end
-- update item cross reference table after import
if MobInfoConfig.ImportOnlyNew == 0 then
MI2_BuildXRefItemTable()
end
-- restore current zone ID after import
MI2_CurZone = oldCurZone
chattext( " imported "..newMobs.." new Mobs" )
chattext( " imported "..newHealth.." new health values" )
chattext( " imported "..newItems.." new loot items" )
if MobInfoConfig.ImportOnlyNew == 0 then
chattext( " updated data for "..updatedMobs.." existing Mobs" )
else
chattext( " did NOT update data for "..updatedMobs.." existing Mobs" )
end
-- update database options frame
MobInfoConfig.ImportSignature = MI2_Import_Signature
MI2_DbOptionsFrameOnShow()
end -- MI2_SlashAction_ImportMobData()
-----------------------------------------------------------------------------
-- MI2_SlashAction_DeleteSearch()
--
-- Delete all Mobs in the search result list from the MobInfo database.
-- This function will ask for confirmation before deleting.
--
function MI2_SlashAction_DeleteSearch()
local confirmationText = string.format( MI_TXT_DEL_SEARCH_CONFIRM, MI2_NumMobsFound )
StaticPopupDialogs["MOBINFO_CONFIRMATION"].text = confirmationText
StaticPopupDialogs["MOBINFO_CONFIRMATION"].OnAccept = MI2_DeleteSearchResultMobs
local dialog = StaticPopup_Show( "MOBINFO_CONFIRMATION", "")
end -- MI2_SlashAction_DeleteSearch()
-----------------------------------------------------------------------------
-- MI2_SlashInit()
--
-- Add all Slash Commands
--
function MI2_SlashInit()
SlashCmdList["MOBINFO"] = MI2_SlashParse
SLASH_MOBINFO1 = "/mobinfo"
SLASH_MOBINFO2 = "/mobinfo2"
SLASH_MOBINFO3 = "/mi2"
end -- MI2_SlashInit()
-----------------------------------------------------------------------------
-- MI2_SlashParse()
--
-- Parses the msg entered as a slash command. This function is also used
-- for the internal purpose of setting all options in the options dialog.
-- When used by the options dialog there is no need to actually update the
-- dialog, which is indicated by the "updateOptions" parameter.
--
-- FrameXML/Chatframe.lua gives us back only the parameter msg
-- ==> happens only when Mobinfo2 is called via command-line
-- ==> Therefor the optional variable self now the second in the function (only used, when config-parameters are changed)
--
function MI2_SlashParse(msg, self)
-- extract option name and option argument from message string
local _, _, cmd, param = string.find( string.lower(msg), "([%w_]*)[ ]*([-%w]*)")
-- handle all simple commands that dont require parsing right here
-- handle show/hide of options dialog first of all
if not cmd or cmd == "" or cmd == "config" then
if MI2_OptionsFrame:IsVisible() then
MI2_OptionsFrame:Hide()
else
MI2_OptionsFrame:Show()
end
return
elseif cmd == 'version' then
chattext( ' MobInfo-2 Version '..miVersionNo )
return
elseif cmd == 'update' and MI2_UpdatePrices then
MI2_UpdatePrices()
return
elseif cmd == 'help' then
chattext( ' Usage: enter /mobinfo2 or /mi2 to open interface' )
chattext( ' for AddOn help go to '..MI_LightGreen..'http://www.dizzarian.com/forums/viewforum.php?f=16' )
return
end
-- search for the option data structure matching the command
local optionName, optionData
for idx, val in pairs(MI2_OPTIONS) do
local lower_opt = string.lower( idx )
local optionCommand = string.sub(lower_opt, 8)
if cmd == lower_opt or cmd == optionCommand then
optionName = string.sub(idx, 8)
optionData = val
break
end
end
-- now call the option handler for the more complex commands
if optionData then
MI2_OptionParse( self, optionName, optionData, param )
end
end -- of MI2_SlashParse()
-----------------------------------------------------------------------------
-- MI2_OptionParse()
--
-- Parses the more complex option toggle/set commands. There are 4
-- categories of options:
-- * options that can toggle between an on and off state
-- * options that represent a numeric value
-- * options that represent a text
-- * options that activate a special functionality represented by a
-- handler function that must correspond to a specific naming convention
--
function MI2_OptionParse( self, optionName, optionData, param )
-- handle the option according to its option type: its either a
-- switch being toggleg, a value being set, or a special action
if optionData.val then
-- it is a slider setting a value
-- get new option value from parameter and set it
local optValue = tonumber( param ) or 0
MobInfoConfig[optionName] = optValue
if updateOptions then
chattext( optionData.text.." : "..MI_Green..optValue )
end
elseif optionData.txt then
-- it is a text based option
MobInfoConfig[optionName] = param
if updateOptions then
chattext( optionData.text.." : "..MI_Green..param )
end
elseif MobInfoConfig[optionName] then
-- it is a switch toggle option:
-- get current option value and toggle it to the opposite state (On<->Off)
local valTxt = { val0 = "-OFF-", val1 = "-ON-" }
local optValue = MobInfoConfig[ optionName ]
optValue = 1 - optValue -- toggle option
MobInfoConfig[optionName] = optValue
if optionData.text and optionData.text ~= "" then
chattext( optionData.text.." : "..MI_Green..valTxt["val"..optValue] )
end
-- some toggle switches control recording options which in turn controls events
MI2_InitializeEventTable(self)
else
-- special action commands have a corresponding handler function
local actionHandlerName = "MI2_SlashAction_"..optionName
local actionHandler = _G[actionHandlerName]
if actionHandler then
actionHandler(self)
updateOptions = true -- for AllOn, AllOff, etc.
end
end
-- update position and visibility of minimap button
-- (80/80 is circle radius, 50/52 is circle center)
if optionName == "MMButtonPos" then
local pos = MobInfoConfig.MMButtonPos
MI2_MinimapButton:SetPoint("TOPLEFT", "Minimap", "TOPLEFT",50 - (80 * cos(pos)), (80 * sin(pos)) - 52 )
end
if MobInfoConfig.ShowMMButton == 1 then
MI2_MinimapButton:Show()
else
MI2_MinimapButton:Hide()
end
-- update everything that might depend on config actions
MI2_MobHealth_SetPos()
MI2_UpdateOptions()
MI2_SetupTooltip()
end -- MI2_OptionParse()