vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --===========================================================================--
2 ---------------------------- LootTracker by PNB ----------------------------
3 --===========================================================================--
4 -- LootTrackerUtility.lua
5 --
6 -- Common utility methods
7 --===========================================================================--
8  
9 ------------------------------------------------------------------------------
10 -- AddIfNotPresent
11 -- Adds the given value to the given table, but only if the table does not
12 -- already have that value.
13 ------------------------------------------------------------------------------
14  
15 function LT_AddIfNotPresent(t, value)
16  
17 if (LT_TableHasValue(t, value)) then
18 return;
19 end
20  
21 tinsert(t, value);
22 end
23  
24  
25 ------------------------------------------------------------------------------
26 -- TableHasValue
27 -- Checks if the table contains the given value.
28 ------------------------------------------------------------------------------
29  
30 function LT_TableHasValue(t, value)
31  
32 local match = false;
33  
34 foreach(t, function(k,v)
35 if (v == value) then
36 match = true;
37 return;
38 end
39 end);
40  
41 return match;
42 end
43  
44  
45 ------------------------------------------------------------------------------
46 -- TableHasKey
47 -- Checks if the table contains the given key.
48 ------------------------------------------------------------------------------
49  
50 function LT_TableHasKey(t, key)
51  
52 local match = false;
53  
54 foreach(t, function(k,v)
55 if (k == key) then
56 match = true;
57 return;
58 end
59 end);
60  
61 return match;
62 end
63  
64  
65 ------------------------------------------------------------------------------
66 -- IndexOf
67 -- Gets the key for a value in the table.
68 ------------------------------------------------------------------------------
69  
70 function LT_IndexOf(t, value)
71  
72 foreach(t, function(k,v)
73  
74 if (v == value) then
75 return k;
76 end
77  
78 end);
79  
80 return nil;
81  
82 end
83  
84  
85 ------------------------------------------------------------------------------
86 -- GetCount
87 -- Because getn(t) doesn't seem to always work...
88 -- TODO: Figure out if there's a dictionary friendly version of the array
89 -- oriented getn(t) function.
90 ------------------------------------------------------------------------------
91  
92 function LT_GetCount(t)
93  
94 if (t == nil) then
95 return 0;
96 end
97  
98 local count = 0;
99  
100 foreach(t, function(k,v)
101 count = count + 1;
102 end);
103  
104 return count;
105  
106 end
107  
108  
109 ------------------------------------------------------------------------------
110 -- InsertSorted
111 -- Inserts and item into a table based on the output of some compare function
112 ------------------------------------------------------------------------------
113  
114 function LT_InsertSorted(t, v, compareFunction)
115  
116 if (v == nil) then
117 return;
118 end
119 if (compareFunction == nil) then
120 tinsert(t, v)
121 end
122  
123 local count = getn(t);
124  
125 for i = 1, count, 1 do
126 local tv = t[i];
127 local compare = 1;
128  
129 if (tv ~= nil) then
130 compare = compareFunction(tv, v);
131 end
132  
133 if (compare > 0) then
134 tinsert(t, i, v);
135 return;
136 elseif (compare < 0) then
137 -- do nothing
138 else
139 tinsert(t, i, v);
140 return;
141 end
142 end
143  
144 tinsert(t, v);
145  
146 end
147  
148  
149 ------------------------------------------------------------------------------
150 -- StrCmp
151 -- String comparison - returns 0 if equal, positive if A is greater, negative
152 -- if B is greater
153 ------------------------------------------------------------------------------
154  
155 function LT_StrCmp(a, b)
156  
157 local aLen = strlen(a);
158 local bLen = strlen(b);
159  
160 for i = 1, aLen, 1 do
161 local aByte = strbyte(a, i);
162 local bByte = strbyte(b, i);
163  
164 local compare = 0;
165 if (bByte ~= nil) then
166 compare = aByte - bByte;
167 end
168  
169 if (compare ~= 0) then
170 return compare;
171 end
172 end
173  
174 if (aLen > bLen) then
175 return 1;
176 else
177 return -1;
178 end
179  
180 end
181  
182  
183 ------------------------------------------------------------------------------
184 -- StrAppend
185 -- Appends A to B, with a separator.
186 ------------------------------------------------------------------------------
187  
188 function LT_StrAppend(a, b, separator)
189  
190 if (a == nil) then
191 return b;
192 else
193 return a .. separator .. b;
194 end
195  
196 end
197  
198  
199 ------------------------------------------------------------------------------
200 -- StrIsNilOrEmpty
201 -- Checks if the string is nil or empty.
202 ------------------------------------------------------------------------------
203  
204 function LT_StrIsNilOrEmpty(s)
205  
206 return (s == nil) or (s == "")
207  
208 end
209  
210 ------------------------------------------------------------------------------
211 -- GetMatchingKeys
212 -- Returns a list of keys that have a substring match to the query string.
213 ------------------------------------------------------------------------------
214  
215 function LT_GetMatchingKeys(t, query)
216  
217 local matches = {};
218  
219 local queryLowercase = strlower(query);
220  
221 foreach(t, function(k,v)
222  
223 local keyLowercase = strlower(k);
224  
225 LT_DebugMessage(5, string.format("Matching \"%s\" against \"%s\"", k, query));
226  
227 local beginMatch, endMatch = string.find(keyLowercase, queryLowercase);
228  
229 if (beginMatch) then
230 LT_DebugMessage(4, string.format("Matched \"%s\" to \"%s\"", k, query));
231 tinsert(matches, k);
232 end
233  
234 end);
235  
236 return matches;
237  
238 end
239  
240 ------------------------------------------------------------------------------
241 -- GetZoneString
242 -- Gets a nice formatted zone description.
243 ------------------------------------------------------------------------------
244  
245 function LT_GetZoneString()
246  
247 local zoneText = GetZoneText();
248 local subZoneText = GetSubZoneText();
249  
250 if ((subZoneText == nil) or (subZoneText == "")) then
251 return zoneText;
252 end;
253  
254 return zoneText .. ", " .. subZoneText;
255  
256 end
257  
258  
259 ------------------------------------------------------------------------------
260 -- GetCommandAndValue
261 -- Given a console command, get the verb and target.
262 ------------------------------------------------------------------------------
263  
264 function LT_GetCommandAndValue(text)
265  
266 -- Example: "color 255, 255, 255"
267 -- Result: command = "color"
268 -- value = "255, 255, 255"
269 local beginMatch, endMatch, command, value = string.find(text, "([^%s]*)%s(.*)");
270  
271 -- No match: return the string as it was
272 if (command == nil) then
273 return text;
274 end;
275  
276 return command, value;
277  
278 end
279  
280  
281 ------------------------------------------------------------------------------
282 -- GetPlayerUnitID
283 -- Gets the UnitID for the player with the given name. May return nil.
284 ------------------------------------------------------------------------------
285  
286 function LT_GetPlayerUnitID(name)
287  
288 -- TODO: The perf here can't be good. It'd be better to trap party change
289 -- events and just have a cache of UnitName() for all members in the
290 -- party up front.
291 local testIds = {"player", "party1", "party2", "party3", "party4", "target",
292 "raid1", "raid2", "raid3", "raid4", "raid5", "raid6", "raid7", "raid8", "raid9", "raid10",
293 "raid11", "raid12", "raid13", "raid14", "raid15", "raid16", "raid17", "raid18", "raid19", "raid20",
294 "raid21", "raid22", "raid23", "raid24", "raid25", "raid26", "raid27", "raid28", "raid29", "raid30",
295 "raid31", "raid32", "raid33", "raid34", "raid35", "raid36", "raid37", "raid38", "raid39", "raid40"};
296  
297 local unitId = nil;
298  
299 foreach(testIds, function(k,v)
300 local unitName = UnitName(v);
301 if (name == unitName) then
302 unitId = v;
303 return v;
304 end
305 end);
306  
307 return unitId;
308  
309 end
310  
311  
312 ------------------------------------------------------------------------------
313 -- GetInventoryItem
314 -- Get an item given the unit and inventory slot. May return nil.
315 ------------------------------------------------------------------------------
316  
317 function LT_GetInventoryItem(unitId, slotId)
318  
319 local inventoryItemLink = GetInventoryItemLink(unitId, slotId);
320  
321 if (inventoryItemLink == nil) then
322 return nil;
323 end
324  
325 LT_DebugMessage(4, "Inventory item link = " .. inventoryItemLink .. " for unit \"" .. unitId .. "\", slot " .. slotId);
326  
327  
328 local beginMatch, endMatch, itemId = strfind(inventoryItemLink, "(%d+):");
329  
330 LT_DebugMessage(4, "Stripped out item id: " .. itemId);
331  
332 if (beginMatch == nil) then
333 LT_Message("Error parsing item link: " .. inventoryItemLink);
334 return nil;
335 end
336  
337  
338 local rawItem = LT_GetItem(itemId);
339  
340 if (rawItem == nil) then
341 LT_DebugMessage(2, "Error getting item info from link " .. inventoryItemLink .. " (id = " .. itemId .. ")");
342 return nil;
343 end
344  
345 return rawItem;
346  
347 end
348  
349  
350 ------------------------------------------------------------------------------
351 -- GetItem
352 -- Lookup an item by id and return an object to wrap that. May return nil.
353 ------------------------------------------------------------------------------
354  
355 function LT_GetItem(itemId)
356  
357 -- Call the WoW API to get the item details
358 local name, link, quality, minLevel, class, subClass, maxStack, equipLoc, texture = GetItemInfo(itemId);
359  
360 if (name == nil) then
361 return nil;
362 end
363  
364  
365 local rawItem = {};
366 rawItem.Name = name;
367 rawItem.Link = link;
368 rawItem.Quality = quality;
369 rawItem.MinLevel = minLevel;
370 rawItem.Class = class;
371 rawItem.SubClass = subClass;
372 rawItem.MaxStack = maxStack;
373 rawItem.EquipLoc = equipLoc;
374 rawItem.Texture = texture;
375  
376 return rawItem;
377  
378 end
379  
380  
381 ------------------------------------------------------------------------------
382 -- GetGear
383 -- Create a table of all the gear on the given unit.
384 ------------------------------------------------------------------------------
385  
386 function LT_GetGear(unitId)
387  
388 gear = {};
389  
390 -- Note: The strings here aren't used for anything in the result. They're
391 -- just here in the code for clarity.
392 local slots = {};
393 slots[0]="ammo";
394 slots[1]="head";
395 slots[2]="neck";
396 slots[3]="shoulder";
397 slots[4]="shirt";
398 slots[5]="chest";
399 slots[6]="belt";
400 slots[7]="legs";
401 slots[8]="feet";
402 slots[9]="wrist";
403 slots[10]="gloves";
404 slots[11]="finger 1";
405 slots[12]="finger 2";
406 slots[13]="trinket 1";
407 slots[14]="trinket 2";
408 slots[15]="back";
409 slots[16]="main hand";
410 slots[17]="off hand";
411 slots[18]="ranged";
412 slots[19]="tabard";
413  
414 foreach(slots, function(slot,slotName)
415  
416 local rawItem = LT_GetInventoryItem(unitId, slot);
417  
418 if (rawItem ~= nil) then
419 LT_DebugMessage(3, "Inventory[" .. slot .. "] " .. rawItem.Name);
420 tinsert(gear, rawItem);
421 end
422 end);
423  
424 return gear;
425  
426 end
427  
428  
429 ------------------------------------------------------------------------------
430 -- LT_ExtractItemIDFromLink
431 -- Parses an item link to extract the item id.
432 -- This version based on Auctioneer code.
433 ------------------------------------------------------------------------------
434  
435 function LT_ExtractItemIDFromLink(link)
436  
437 --Format: "item:2673:0:0:0"
438 local beginMatch, endMatch, itemId = string.find(link, "item:(%d+):%d+:%d+:%d+");
439  
440 if (itemId ~= nil) then
441 return tonumber(itemId);
442 end
443  
444 return nil;
445  
446 end
447  
448  
449 ------------------------------------------------------------------------------
450 -- ExtractItemIDFromChatLink
451 -- Parses an item link to extract the item id.
452 -- This version based on Looto code.
453 ------------------------------------------------------------------------------
454  
455 function LT_ExtractItemIDFromChatLink(itemLink)
456  
457 local beginMatch, endMatch, itemId, itemName = string.find(itemLink, "|H(.*)|h(.*)|h");
458  
459 if (not beginMatch) then
460 LT_DebugMessage(2, "Did not match pattern: " .. itemLink);
461 return nil;
462 end
463  
464 if (itemId == nil) then
465 LT_DebugMessage(2, "itemId was nil");
466 return nil;
467 end
468  
469 if (itemName == nil) then
470 LT_DebugMessage(2, "itemName was nil");
471 return nil;
472 end
473  
474 return itemId;
475  
476 end
477  
478  
479  
480 ------------------------------------------------------------------------------
481 -- ColorText
482 -- Emits the correct color tags so that the text will be rendered with color.
483 ------------------------------------------------------------------------------
484  
485 function LT_ColorText(text, color)
486  
487 local r = color.r;
488 local g = color.g;
489 local b = color.b;
490 local alpha = color.alpha;
491  
492 if alpha == nil then
493 alpha = 1.0;
494 end
495  
496 return "|c"
497 .. format("%02x", alpha * 255)
498 .. format("%02x", r * 255)
499 .. format("%02x", g * 255)
500 .. format("%02x", b * 255)
501 .. text .. FONT_COLOR_CODE_CLOSE;
502 end
503  
504  
505 ------------------------------------------------------------------------------
506 -- GetValueByCoinType
507 -- Gets a the gold, silver, copper breakdown of a raw value.
508 ------------------------------------------------------------------------------
509  
510 function LT_GetValueByCoinType(value)
511  
512 -- Split out the g/s/c values
513 local gold = math.floor(value / 10000);
514 local silver = math.mod(math.floor(value / 100), 100);
515 local copper = math.mod(value, 100);
516  
517 return gold, silver, copper;
518  
519 end
520  
521  
522 ------------------------------------------------------------------------------
523 -- GetValueString
524 -- Gets a text description of a money value.
525 ------------------------------------------------------------------------------
526  
527 function LT_GetValueString(value, applyColor)
528  
529 local gold, silver, copper = LT_GetValueByCoinType(value);
530  
531 local goldString = string.format(LT_GOLD, gold);
532 local silverString = string.format(LT_SILVER, silver);
533 local copperString = string.format(LT_COPPER, copper);
534  
535 -- Apply coloring to the string value
536 if (applyColor) then
537 goldString = LT_ColorText(goldString, LT_MoneyColors[0]);
538 silverString = LT_ColorText(silverString, LT_MoneyColors[1]);
539 copperString = LT_ColorText(copperString, LT_MoneyColors[2]);
540 end
541  
542  
543 local string = nil;
544  
545 -- Append the Gold value
546 if (gold > 0) then
547 string = goldString;
548 end
549  
550 -- Append the Silver value
551 if (silver > 0) then
552 if (string == nil) then
553 string = silverString;
554 else
555 string = string .. " " .. silverString;
556 end
557 end
558  
559 -- Append the Copper value
560 if (copper > 0) then
561 if (string == nil) then
562 string = copperString;
563 else
564 string = string .. " " .. copperString;
565 end
566 end
567  
568 -- Don't return nil - always return a valid string
569 if (string == nil) then
570 string = "";
571 end
572  
573 return string;
574  
575 end
576  
577  
578 ------------------------------------------------------------------------------
579 -- MoneyParseHelper
580 -- Extracts a certain coin value from a money string.
581 -- (e.g. 1 Gold, 2 Silver, 21 Copper)
582 ------------------------------------------------------------------------------
583  
584 function LT_MoneyParseHelper(text, coinType, messageOnError)
585  
586 local pattern = "(%d*) " .. coinType;
587 local beginMatch, endMatch, coinAmount = string.find(text, pattern);
588  
589 if (beginMatch) then
590 local amount = tonumber(coinAmount);
591  
592 if (amount == nil) then
593 LT_DebugMessage(2, "Unable to convert " .. coinAmount .. " " .. coinType .. " to a number");
594 return 0;
595 end
596  
597 return amount;
598 else
599 if (messageOnError) then
600 LT_DebugMessage(2, "Money value did not match pattern for " .. coinType .. ": " .. text);
601 end
602 return 0;
603 end
604  
605 end
606  
607  
608 ------------------------------------------------------------------------------
609 -- ParseMoney
610 -- Extracts the total value from a money string.
611 ------------------------------------------------------------------------------
612  
613 function LT_ParseMoney(text)
614  
615 local gold = LT_MoneyParseHelper(text, LT_COIN_NAME[0], false);
616 local silver = LT_MoneyParseHelper(text, LT_COIN_NAME[1], false);
617 local copper = LT_MoneyParseHelper(text, LT_COIN_NAME[2], false);
618  
619 -- Get the total (copper) value given the gold/silver/copper contributions
620 local value = LT_GetValueFromCoins(gold, silver, copper);
621  
622 return value, gold, silver, copper;
623  
624 end
625  
626  
627 ------------------------------------------------------------------------------
628 -- GetValueFromCoins
629 -- Get the total (copper) value given the gold/silver/copper contributions
630 ------------------------------------------------------------------------------
631  
632 function LT_GetValueFromCoins(gold, silver, copper)
633  
634 return (gold * 10000) + (silver * 100) + (copper);
635  
636 end
637  
638  
639 ------------------------------------------------------------------------------
640 -- IsMoneyString
641 -- Tests if the given text is a money string.
642 -- (e.g. 1 Gold, 2 Silver, 21 Copper)
643 ------------------------------------------------------------------------------
644  
645 function LT_IsMoneyString(text)
646  
647 local value = LT_ParseMoney(text);
648  
649 if (value ~= 0) then
650 return true;
651 else
652 return false;
653 end
654  
655 end
656  
657  
658 ------------------------------------------------------------------------------
659 -- IsPlayerSolo
660 ------------------------------------------------------------------------------
661  
662 function LT_IsPlayerSolo()
663  
664 local settings = LT_GetSettings()
665 if (settings.JustMyLoot) then
666 return true;
667 end
668  
669 if (GetNumPartyMembers() > 0) then
670 return false;
671 end
672  
673 return true;
674  
675 end
676  
677  
678 ------------------------------------------------------------------------------
679 -- GetColoredQualityName
680 ------------------------------------------------------------------------------
681  
682 function LT_GetColoredQualityName(quality)
683  
684 local qualityAsString = tostring(quality);
685  
686 local name = LT_QUALITY_NAME[qualityAsString]
687 return LT_ColorText(name, LT_QualityColors[qualityAsString]);
688  
689 end
690