vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --
2 -- MI2_Search.lua
3 --
4 -- MobInfo module to control the Mob database search feature.
5 -- Search option settings and actual search algorithm are located in here.
6 --
7  
8 --
9 -- start up defaults for search options settings
10 MI2_SearchOptions = {}
11 MI2_SearchOptions.MinLevel = 1
12 MI2_SearchOptions.MaxLevel = 65
13 MI2_SearchOptions.Normal = 1
14 MI2_SearchOptions.Elite = 0
15 MI2_SearchOptions.Boss = 0
16 MI2_SearchOptions.MinLoots = 2
17 MI2_SearchOptions.MobName = ""
18 MI2_SearchOptions.ItemName = ""
19 MI2_SearchOptions.CompactResult = 1
20 MI2_SearchOptions.ListMode = "Mobs"
21 MI2_SearchOptions.SortMode = "profit"
22  
23 local MI2_SearchResultList = {}
24 local MI2_ItemsIdxList = ":"
25 MI2_NumMobsFound = 0
26  
27  
28 -----------------------------------------------------------------------------
29 -- MI2_SearchForItems()
30 --
31 -- Search for all items matching the item name entered in the search dialog.
32 -- Display the list of items in the result list control (if requested).
33 -----------------------------------------------------------------------------
34 local function MI2_SearchForItems( itemName, enterItemsIntoList )
35 MI2_ItemsIdxList = ":"
36 MI2_NumMobsFound = 0
37 if enterItemsIntoList then
38 MI2_SearchResultList = {}
39 end
40  
41 if itemName ~= "" or enterItemsIntoList then
42 for idx in MI2_ItemNameTable do
43 local itemFound = true
44 local itemText, itemColor = MI2_GetLootItemString( idx )
45 if itemName ~= "*" then
46 itemFound = string.find( string.lower(itemText), string.lower(itemName) ) ~= nil
47 end
48 if itemFound then
49 MI2_ItemsIdxList = MI2_ItemsIdxList..idx..":"
50 if enterItemsIntoList then
51 MI2_NumMobsFound = MI2_NumMobsFound + 1
52 MI2_SearchResultList[MI2_NumMobsFound] = { idx = itemText, val = "", col = itemColor }
53 end
54 end
55 end
56 end
57  
58 MI2_DisplaySearchResult( "Items" )
59 end -- MI2_SearchForItems()
60  
61  
62 -----------------------------------------------------------------------------
63 -- MI2_UpdateSearchResultList()
64 --
65 -- Update contents of search result list according to current search options
66 -- settings. This includes starting a new search run, sorting the result and
67 -- displaying the result in the scrollable result list.
68 -----------------------------------------------------------------------------
69 local function MI2_UpdateSearchResultList( updateItems )
70 local startTime = GetTime()
71  
72 if updateItems then
73 local enterItemsIntoList = MI2_SearchOptions.ListMode == "Items"
74 MI2_SearchForItems( MI2_SearchOptions.ItemName, enterItemsIntoList )
75 end
76  
77 if MI2_SearchOptions.ListMode == "Mobs" then
78 MI2_SearchAndSort( "profit" )
79 end
80  
81 MI2_DisplaySearchResult( MI2_SearchOptions.ListMode )
82 -- chattext( "<MobInfo> total search time = "..(GetTime()-startTime).." seconds" )
83 end -- MI2_UpdateSearchResultList()
84  
85  
86 -----------------------------------------------------------------------------
87 -- MI2_SearchOptionsOnShow()
88 --
89 -- OnShow event handler for search options page
90 -- Write current search option settings into the search option controls.
91 -- Validate all values and update colors accordingly.
92 -- Allow Search only if all search options are valid.
93 -----------------------------------------------------------------------------
94 function MI2_SearchOptionsOnShow()
95 MI2_OptSearchMinLevel:SetText( tostring(MI2_SearchOptions.MinLevel) )
96 MI2_OptSearchMaxLevel:SetText( tostring(MI2_SearchOptions.MaxLevel) )
97 MI2_OptSearchMinLoots:SetText( tostring(MI2_SearchOptions.MinLoots) )
98 MI2_OptSearchMobName:SetText( MI2_SearchOptions.MobName )
99 MI2_OptSearchItemName:SetText( MI2_SearchOptions.ItemName )
100  
101 MI2_OptSearchNormal:SetChecked( MI2_SearchOptions.Normal )
102 MI2_OptSearchElite:SetChecked( MI2_SearchOptions.Elite )
103 MI2_OptSearchBoss:SetChecked( MI2_SearchOptions.Boss )
104  
105 MI2_UpdateSearchResultList()
106 end -- MI2_SearchOptionsOnShow()
107  
108  
109 -----------------------------------------------------------------------------
110 -- MI2_ValidateSearchOptions()
111 --
112 -- Validate all values and update colors accordingly.
113 -- Allow Search only if all search options are valid.
114 -----------------------------------------------------------------------------
115 local function MI2_ValidateSearchOptions()
116 if MI2_SearchOptions.MinLevel < 1 then
117 MI2_SearchOptions.MinLevel = 1
118 if this:GetText() == "0" then
119 this:SetText( "1" )
120 end
121 end
122 if MI2_SearchOptions.MaxLevel < 1 then
123 MI2_SearchOptions.MaxLevel = 1
124 if this:GetText() == "0" then
125 this:SetText( "1" )
126 end
127 end
128 end -- MI2_ValidateSearchOptions()
129  
130  
131 -----------------------------------------------------------------------------
132 -- MI2_SearchCheckboxClicked()
133 --
134 -- OnClicked event handler for checkboxes on search options page
135 -- Store the checkbox state in the corresponding search options variable.
136 -----------------------------------------------------------------------------
137 function MI2_SearchCheckboxClicked()
138 local checkboxName = this:GetName()
139 local optionName = string.sub( checkboxName, 14 )
140 local optionValue = this:GetChecked() or 0
141  
142 MI2_SearchOptions[optionName] = optionValue
143 MI2_UpdateSearchResultList()
144 end -- MI2_SearchCheckboxClicked()
145  
146  
147 -----------------------------------------------------------------------------
148 -- MI2_SearchValueChanged()
149 --
150 -- OnChar event handler for editbox controls on search options page
151 -- This handler is called whenever the contents of an EditBox control changes.
152 -- It gets the new value and stores it in the corresponding search options
153 -- variable
154 -----------------------------------------------------------------------------
155 function MI2_SearchValueChanged()
156 local editboxName = this:GetName()
157 local optionName = string.sub( editboxName, 14 )
158 local optionValue = tonumber(this:GetText()) or 0
159  
160 if MI2_SearchOptions[optionName] ~= optionValue then
161 MI2_SearchOptions[optionName] = optionValue
162 MI2_ValidateSearchOptions()
163 MI2_UpdateSearchResultList()
164 end
165 end -- MI2_SearchValueChanged()
166  
167  
168 -----------------------------------------------------------------------------
169 -- MI2_SearchTextChanged()
170 --
171 -- OnChar event handler for textual editbox controls on search options page
172 -- This handler is called whenever the contents of an EditBox control changes.
173 -- It gets the new value and stores it in the corresponding search options
174 -- variable
175 -----------------------------------------------------------------------------
176 function MI2_SearchTextChanged()
177 local editboxName = this:GetName()
178 local optionName = string.sub( editboxName, 14 )
179  
180 if MI2_SearchOptions[optionName] ~= this:GetText() then
181 MI2_SearchOptions[optionName] = this:GetText()
182 MI2_UpdateSearchResultList( true )
183 end
184 end -- MI2_SearchTextChanged()
185  
186  
187 -----------------------------------------------------------------------------
188 -- MI2_CalculateRank()
189 --
190 -- Calculate ranking and corresponding actual value for a given mob.
191 -- Ranking depends on search mode. For search mode "profit" ranking is
192 -- based on the mobs total profit value plus bonus points for rare loot
193 -- items. For search mode "itemCount" ranking is identical to the overall
194 -- items count for the loot items being searched for (in this mode
195 -- rank and value are identical).
196 -----------------------------------------------------------------------------
197 local function MI2_CalculateRank( mobData, mobLevel, sortMode )
198 local rank, value = 0, 0
199  
200 if sortMode == "profit" then
201 -- calculate rank based on mob level and loot items quality
202 local bonusFactor = mobLevel / 20
203 if mobData.loots > 0 then
204 value = (mobData.copper or 0) + (mobData.itemValue or 0)
205 rank = value + ((mobData.r3 or 0) * 200 * bonusFactor) + ((mobData.r4 or 0) * 1000 * bonusFactor) + ((mobData.r5 or 0) * 2000 * bonusFactor)
206 rank = ceil( rank / mobData.loots )
207 value = copper2text( ceil(value / mobData.loots) )
208 end
209 elseif sortMode == "item" and mobData.itemList then
210 for idx, val in mobData.itemList do
211 local itemFound = string.find( MI2_ItemsIdxList, idx ) ~= nil
212 if itemFound then rank = rank + val end
213 end
214 value = rank.." "
215 end
216  
217 return rank, value
218 end -- MI2_CalculateRank()
219  
220  
221 -----------------------------------------------------------------------------
222 -- MI2_CheckMob()
223 --
224 -- Check a given Mob against the current search criteria. Return the
225 -- mob data if the mob matches the criteria, or return nil if the Mob
226 -- does not match.
227 -----------------------------------------------------------------------------
228 local function MI2_CheckMob( mobInfo, mobName, mobLevel )
229 local levelOk, lootsOk, typeOk, itemsOK, mobData
230 local nameOk = true
231  
232 -- check name and level of Mob
233 if MI2_SearchOptions.MobName ~= "" then
234 nameOk = string.find(string.lower(mobName),string.lower(MI2_SearchOptions.MobName),1,true) ~= nil
235 end
236 if nameOk and mobName ~= "" then
237 levelOk = mobLevel >= MI2_SearchOptions.MinLevel and mobLevel <= MI2_SearchOptions.MaxLevel
238 end
239  
240 -- check mob data related search conditions
241 if levelOk or mobLevel == -1 then
242 mobData = {}
243 MI2_DecodeBasicMobData( mobInfo, mobData )
244 mobData.loots = mobData.loots or 0
245 lootsOk = mobData.loots >= MI2_SearchOptions.MinLoots
246 typeOk = (MI2_SearchOptions.Normal == 1 and mobData.mobType == 1) or (MI2_SearchOptions.Elite == 1 and mobData.mobType == 2) or (MI2_SearchOptions.Boss == 1 and mobData.mobType == 3)
247 if lootsOk and typeOk then
248 if MI2_ItemsIdxList ~= ":" then
249 MI2_DecodeItemList( mobInfo, mobData )
250 if mobData.itemList then
251 for idx, val in mobData.itemList do
252 itemsOK = string.find( MI2_ItemsIdxList, ":"..idx..":" ) ~= nil
253 if itemsOK then break end
254 end
255 end
256 if not itemsOK then mobData = nil end
257 end
258 else
259 mobData = nil
260 end
261 end
262  
263 return mobData
264 end -- MI2_CheckMob()
265  
266  
267 -----------------------------------------------------------------------------
268 -- MI2_SearchAndSort()
269 --
270 -- Search for most valuable mobs by comparing the mobs average total value.
271 -- The function creates a result list to be displayed in the search results
272 -- list.
273 -----------------------------------------------------------------------------
274 function MI2_SearchAndSort( )
275 local mobName, mobLevel, insertPos, value, rank, mobIndex, mobInfo
276  
277 -- initialise search result list
278 MI2_SearchResultList = {}
279 MI2_NumMobsFound = 0
280  
281 -- create a sorted list of mobs matching the search criteria
282 -- loop across all mobs in the MobInfo database
283 for mobIndex, mobInfo in MobInfoDB do
284 mobName, mobLevel = MI2_GetIndexComponents( mobIndex )
285 mobData = MI2_CheckMob( mobInfo, mobName, mobLevel )
286  
287 -- if mob is identified as belonging into the search result its
288 -- search result sorting position is calculated based on a ranking
289 -- value which in turn is based on the search mode
290 if mobData then
291 rank, value = MI2_CalculateRank( mobData, mobLevel, MI2_SearchOptions.SortMode )
292 MI2_NumMobsFound = MI2_NumMobsFound + 1
293  
294 -- insert mob at correct sorted position and store all info we need for printing the result list
295 MI2_SearchResultList[MI2_NumMobsFound] = { idx=mobIndex, val=value, rank=rank }
296 if mobData.mobType and mobData.mobType > 1 then
297 MI2_SearchResultList[MI2_NumMobsFound].type = mobData.mobType
298 end
299 end
300 end
301  
302 if MI2_NumMobsFound > 1 then
303 table.sort( MI2_SearchResultList, function(a,b) return (a.rank > b.rank) end )
304 end
305 end -- MI2_SearchAndSort()
306  
307  
308 -----------------------------------------------------------------------------
309 -- MI2_DisplaySearchResult()
310 --
311 -- Display the result of a search in the search results scrollable list.
312 -- The mobs to be displayed depend on the current list scroll position.
313 -----------------------------------------------------------------------------
314 function MI2_DisplaySearchResult( resultType )
315 -- update slider and get slider position
316 FauxScrollFrame_Update( MI2_SearchResultSlider, MI2_NumMobsFound, 15, 14 );
317 local sliderPos = FauxScrollFrame_GetOffset(MI2_SearchResultSlider)
318  
319 if resultType then
320 MI2_TxtSearchCount:SetText( mifontSubWhite.."("..MI2_NumMobsFound.." "..resultType..")" )
321 end
322  
323 -- update 15 search result lines with correct search result data
324 local resultLine
325 for i = 1, 15 do
326 if (i + sliderPos) <= MI2_NumMobsFound then
327 resultLine = getglobal( "MI2_SearchResult"..i.."Index" )
328 resultLine:SetText( i + sliderPos )
329 resultLine = getglobal( "MI2_SearchResult"..i.."Value" )
330 resultLine:SetText( MI2_SearchResultList[i + sliderPos].val )
331 resultLine = getglobal( "MI2_SearchResult"..i.."Name" )
332 local mobName = MI2_SearchResultList[i + sliderPos].idx
333 if MI2_SearchResultList[i + sliderPos].type then
334 mobName = mobName.."+"
335 elseif MI2_SearchResultList[i + sliderPos].col then
336 mobName = MI2_SearchResultList[i + sliderPos].col..mobName
337 end
338 resultLine:SetText( mobName )
339 else
340 resultLine = getglobal( "MI2_SearchResult"..i.."Index" )
341 resultLine:SetText( "" )
342 resultLine = getglobal( "MI2_SearchResult"..i.."Value" )
343 resultLine:SetText( "" )
344 resultLine = getglobal( "MI2_SearchResult"..i.."Name" )
345 resultLine:SetText( "" )
346 end
347 end
348 end -- MI2_DisplaySearchResult()
349  
350  
351 -----------------------------------------------------------------------------
352 -- MI2_SlashAction_SortByValue()
353 --
354 -- Sort the search result list by mob profit
355 -----------------------------------------------------------------------------
356 function MI2_SlashAction_SortByValue()
357 MI2_SearchOptions.SortMode = "profit"
358 MI2_UpdateSearchResultList()
359 end -- end of MI2_SlashAction_SortByValue()
360  
361  
362 -----------------------------------------------------------------------------
363 -- MI2_SlashAction_SortByItem()
364 --
365 -- Sort the search result list by mob item count
366 -----------------------------------------------------------------------------
367 function MI2_SlashAction_SortByItem()
368 MI2_SearchOptions.SortMode = "item"
369 MI2_UpdateSearchResultList()
370 end -- end of MI2_SlashAction_SortByItem()
371  
372  
373 -----------------------------------------------------------------------------
374 -- MI2_SearchResult_Update()
375 --
376 -- Update contents of search results list based on current scroll bar
377 -- position. Update tooltip for selected mob if tooltip is visible.
378 -----------------------------------------------------------------------------
379 function MI2_SearchResult_Update()
380 FauxScrollFrame_Update( MI2_SearchResultSlider, MI2_NumMobsFound, 15, 14 );
381 MI2_DisplaySearchResult()
382 end -- end of MI2_SearchResult_Update()
383  
384  
385 -----------------------------------------------------------------------------
386 -- MI2_ShowSearchResultTooltip()
387 --
388 -- Show mob tooltip for search result mob currently under mouse cursor.
389 -----------------------------------------------------------------------------
390 function MI2_ShowSearchResultTooltip()
391 local sliderPos = FauxScrollFrame_GetOffset(MI2_SearchResultSlider)
392 local selection = tonumber(string.sub(this:GetName(), 17)) + sliderPos
393  
394 if selection <= MI2_NumMobsFound then
395 GameTooltip_SetDefaultAnchor( GameTooltip, UIParent )
396  
397 if MI2_SearchOptions.ListMode == "Mobs" then
398 local index = MI2_SearchResultList[selection].idx
399 local mobName, mobLevel = MI2_GetIndexComponents( index )
400 local mobCaption = mobName.." L"..mobLevel
401 if MI2_SearchResultList[selection].type then
402 mobCaption = mobCaption.."+"
403 end
404 -- create Mob data tooltip with full location info
405 GameTooltip:SetText( mobCaption )
406 MI2_BuildMobInfoTooltip( mobName, mobLevel, 1 )
407 elseif MI2_SearchOptions.ListMode == "Items" then
408 local itemName = MI2_SearchResultList[selection].idx
409 GameTooltip:SetText( MI2_SearchResultList[selection].col..itemName )
410 MI2_BuildItemDataTooltip( itemName )
411 end
412 GameTooltip:Show()
413 end
414 end -- end of MI2_ShowSearchResultTooltip()
415  
416  
417 -----------------------------------------------------------------------------
418 -- MI2_SearchTab_OnClick()
419 --
420 -- The "OnClick" event handler for the TAB buttons on the search result list.
421 -- These TAB buttons switch the list content between two modes: mob list
422 -- and item list
423 -----------------------------------------------------------------------------
424 function MI2_SearchTab_OnClick()
425 PanelTemplates_Tab_OnClick( MI2_SearchResultFrame )
426 local selected = MI2_SearchResultFrame.selectedTab
427 if selected == 1 then
428 MI2_OptSortByValue:Enable()
429 MI2_OptSortByItem:Enable()
430 if MI2_NumMobsFound > 0 then
431 MI2_OptDeleteSearch:Enable()
432 else
433 MI2_OptDeleteSearch:Disable()
434 end
435 MI2_SearchOptions.ListMode = "Mobs"
436 MI2_UpdateSearchResultList( true )
437 elseif selected == 2 then
438 MI2_OptSortByValue:Disable()
439 MI2_OptSortByItem:Disable()
440 MI2_OptDeleteSearch:Disable()
441 MI2_SearchOptions.ListMode = "Items"
442 MI2_UpdateSearchResultList( true )
443 end
444 end -- MI2_SearchTab_OnClick()
445  
446  
447 -----------------------------------------------------------------------------
448 -- MI2_DeleteSearchResultMobs()
449 --
450 -- Delete all Mobs in the search result list from the MobInfo database.
451 -- This function is called when the user confirms the delete.
452 -----------------------------------------------------------------------------
453 function MI2_DeleteSearchResultMobs()
454 for idx, val in MI2_SearchResultList do
455 local mobIndex = val.idx
456 MI2_DeleteMobData( mobIndex, true )
457 end
458 chattext( mifontLightBlue.."<MobInfo> deleted "..MI2_NumMobsFound.." Mobs from the MobInfo databases" )
459 MI2_UpdateSearchResultList()
460 end -- MI2_DeleteSearchResultMobs()
461  
462