vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 GuildTools v 2.4
3  
4 Addon that allows you to store in game info into SV.lua for further parsing.
5 Contains:
6 BankScan - scans all posessions of the character, including bank, bags amd bags in bank.
7 IncomingMail - grabs all the nonCOD items and money from mailbox as soon as it's opened.
8 Also keeps track of all the incoming mail including sender, item sent, money sent
9 and date when the message was received.
10  
11 Author: Roman Tarakanov (RTE/Arthas)
12 Date: Mar 28 '06
13  
14 ]]--
15 ---------------------------------------------------------------
16 ----------------------Standart mesages-------------------------
17 ---------------------------------------------------------------
18  
19 local GT_VER = "2.4";
20 local GT_BS_VER = "2.3";
21 local GT_IM_VER = "2.3";
22  
23 local MSG_GT_GREETING1 = "GuildTools v" .. GT_VER .. " is loaded.";
24 local MSG_GT_GREETING2 = "For more information type /gts info.";
25 local MSG_GT_GREETING = "GuildTools v" .. GT_VER .. ".";
26 local MSG_BS_GREETING = "BankScan v" .. GT_BS_VER .. ".";
27 local MSG_IM_GREETING = "IncomingMail v" .. GT_IM_VER .. ".";
28  
29 local MSG_GT_ADDONOFF = "GT> AddOn is turned off, use /gts on to turn it on for this character";
30 local MSG_GT_ON = "GT> AddOn is now ENABLED for this character";
31 local MSG_GT_OFF = "GT> AddOn is now DISABLED for this character";
32 local MSG_GT_CLEARED = "GT> Variables have been cleared.";
33 local MSG_GT_INVALID = "GT> Not a valid command. See /gts info for list of commands.";
34 local MSG_DEBUG_ON = "GT> Debug mode is now ENABLED for this character";
35 local MSG_DEBUG_OFF = "GT> Debug mode is now DISABLED for this character";
36  
37 local MSG_BS_ON = "GT> BankScan part of this AddOn is now ENABLED for this character";
38 local MSG_BS_OFF = "GT> BankScan part of this AddOn is now DISABLED for this character";
39 local MSG_BS_SORTON = "GT> BankScan's sort function is ENABLED";
40 local MSG_BS_SORTOFF = "GT> BankScan's sort function is DISABED";
41 local MSG_BS_OK = "GT> BankScan | Success : prescan was successful, now you can run finalize to save the data";
42 local MSG_BS_DONE = "GT> BankScan | Success : finilize was successful, now you can log out and run BankParser.exe to get html file";
43 local MSG_BS_BANKCLOSED = "GT> BankScan | Error : bank must be opened for this script to work.";
44  
45 local MSG_IM_ON = "GT> IncomingMail part of this AddOn is now ENABLED for this character";
46 local MSG_IM_OFF = "GT> IncomingMail part of this AddOn is now DISABLED for this character";
47 local MSG_IM_START = "GT> IncomingMail started taking mail. Please do not do anything.";
48 local MSG_IM_CANCELED = "GT> IncomingMail grabbing procedure was canceled, some of the data might be corupted.";
49 local MSG_IM_SUCCESS = "GT> IncomingMail successfully grabbed your mail.";
50 local MSG_IM_SAVED = "GT> Mailed items database have been saved in SV. You can now run MailParser to get html file.";
51 local MSG_IM_CLEARED = "GT> IncomingMail varibles are cleared.";
52 local MSG_IM_NOBAG = "GT> There are no empy slots in your bags. Canceling IncomingMail.";
53  
54 ---------------------------------------------------------------
55 ----------------Global GuildTools variables--------------------
56 ---------------------------------------------------------------
57  
58 --BankOpen indicator
59 -- 0 - bank closed
60 -- 1 - bank opened
61 local BankStatus = 0;
62 --Position in the array GT_Saved of the current character
63 -- -1 - not in array
64 local Position=-1;
65 --Debug mode indicator
66 -- 1 - debug mode on
67 -- 0 - debug mode off (default)
68 local DebugMode = 0;
69  
70 ---------------------------------------------------------------
71 ------------------Global BankScan variables--------------------
72 ---------------------------------------------------------------
73  
74 --List of the bags to look in
75 local BanknBags = { -1, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4};
76 local MoneyScanned = 1;
77  
78 ---------------------------------------------------------------
79 ---------------Global IncomingMail variables-------------------
80 ---------------------------------------------------------------
81  
82 local MailOpened = false;
83 local State = 0;
84  
85 local availableSlots = {};
86 local numSlots = 0;
87 local emptySlots = 0;
88 local currentMessage = 1;
89 local From = 0;
90 local To = 0;
91  
92 ---------------------------------------------------------------
93 ----------------Global GuildTools functions--------------------
94 ---------------------------------------------------------------
95 function GT_Bsclear()
96 if (GT_Saved[Position].addonOn) then
97 --GT_Saved = {};
98 GT_BS_ScannedItems = {};
99 GT_BS_PreScannedItems = {};
100 GT_BS_Money = 0;
101 GT_IM_Saved = {};
102 GT_IM_MailedItems[Position] = {};
103 GT_IM_NumMailedItems[Position] = 0;
104 GT_Echo(MSG_GT_CLEARED);
105 else
106 GT_Echo(MSG_ADDONOFF);
107 end
108 end
109  
110 function GT_On()
111 GT_Saved[Position].addonOn = true;
112 GT_Echo(MSG_GT_ON);
113 end
114  
115 function GT_Bson()
116 GT_Saved[Position].BSOn = true;
117 GT_Echo(MSG_BS_ON);
118 end
119  
120 function GT_Bscan()
121 if (GT_Saved[Position].BSOn) then
122 NumItems = 1;
123 if (BankStatus == 1) then
124 GT_BS_PreScannedItems = {};
125 GT_Debug("GT_BS_DoScan is called.");
126 GT_BS_DoScan();
127 GT_Debug("GT_BS_DoScan exited.");
128 GT_BS_Money = " "..GetMoney().." ";
129 GT_BS_Finalize();
130 GT_Echo(MSG_BS_DONE);
131 else
132 GT_Echo(MSG_BS_BANKCLOSED);
133 end
134 else
135 GT_Echo(MSG_ADDONOFF);
136 end
137 end
138  
139 function GT_RegisterUltimateUI()
140 UltimateUI_RegisterConfiguration(
141 "UUI_GT",
142 "SECTION",
143 "Guild Tools",
144 "Shortcuts to use Guild Tools."
145 );
146 UltimateUI_RegisterConfiguration(
147 "UUI_GT_SEPARATOR",
148 "SEPARATOR",
149 "Guild Tools",
150 "Shortcuts to use Guild Tools."
151 );
152 UltimateUI_RegisterConfiguration(
153 "UUI_GT_CLEAR",
154 "BUTTON",
155 "",
156 "",
157 GT_Bsclear,
158 0,
159 0,
160 0,
161 0,
162 "/gts bsclear"
163 );
164 UltimateUI_RegisterConfiguration(
165 "UUI_GT_CLEAR",
166 "BUTTON",
167 "",
168 "",
169 GT_On,
170 0,
171 0,
172 0,
173 0,
174 "/gts on"
175 );
176 UltimateUI_RegisterConfiguration(
177 "UUI_GT_CLEAR",
178 "BUTTON",
179 "",
180 "",
181 GT_Bson,
182 0,
183 0,
184 0,
185 0,
186 "/gts bson"
187 );
188 UltimateUI_RegisterConfiguration(
189 "UUI_GTT_SEPARATOR",
190 "SEPARATOR",
191 "Scan!",
192 "Scan button below!"
193 );
194 UltimateUI_RegisterConfiguration(
195 "UUI_GT_CLEAR",
196 "BUTTON",
197 "",
198 "",
199 GT_Bscan,
200 0,
201 0,
202 0,
203 0,
204 "/gts bscan"
205 );
206 end
207  
208  
209 --OnEvent function
210 function GT_OnEvent()
211 GT_Debug("OnEvent is called");
212  
213 if (event == "VARIABLES_LOADED") then
214 GT_Debug("Variables are loaded.");
215 GT_RegisterUltimateUI();
216 SlashCmdList["GUILDTOOLS"] = GT_SlashCommand;
217 SLASH_GUILDTOOLS1 = "/guildtools";
218 SLASH_GUILDTOOLS2 = "/gts";
219  
220 --GT_Saved = {};
221 --GT_BS_ScannedItems = {};
222 --GT_IM_MailedItems = {};
223 --GT_IM_NumMailedItems = {};
224  
225 --Saved variables of the AddOn
226 --Initialize if blank
227 if (not GT_Saved) then
228 GT_Saved = {};
229 end
230  
231 --Find varibles in GT_Saved if exist
232 local i;
233 GT_Debug("GT_Saved size: "..table.getn(GT_Saved));
234 if (GT_Saved) then
235 for i = 1, table.getn(GT_Saved), 1 do
236 if (string.find(GT_Saved[i].name, UnitName("player")) and string.find(GT_Saved[i].server, GetCVar("realmName"))) then
237 if (not GT_Saved[i].addonOn) then GT_Saved[i].addonOn=false; end
238 if (not GT_Saved[i].BSOn) then GT_Saved[i].BSOn=false; end
239 if (not GT_Saved[i].mailOn) then GT_Saved[i].mailOn=false; end
240 Position = i;
241 end
242 end
243 end
244  
245 --If new character is observed - initialize the data.
246 --On new character AddOn and mail grabber part (future functionality) are off by default
247 if (Position == -1) then
248 if (GT_Saved) then
249 i = table.getn(GT_Saved)+1;
250 else
251 i = 1;
252 end
253 GT_Saved[i] = { name=UnitName("player"), server=GetCVar("realmName"),
254 mailOn=false, addonOn=false, BSOn=false, BSSort=1};
255 Position = i;
256 end
257  
258 GT_Debug("Position: "..Position);
259 GT_Echo(MSG_GT_GREETING1);
260 GT_Echo(MSG_GT_GREETING2);
261  
262 if (not GT_IM_Saved) then GT_IM_Saved = {}; end
263 if (not GT_IM_NumMailedItems) then GT_IM_NumMailedItems = {}; end
264 if (not GT_IM_MailedItems) then GT_IM_MailedItems = {}; end
265 if (not GT_IM_NumMailedItems[Position]) then GT_IM_NumMailedItems[Position] = 0; end
266 if (not GT_IM_MailedItems[Position]) then GT_IM_MailedItems[Position] = {}; end
267 if (not GT_BS_PreScannedItems) then GT_BS_PreScannedItems = {}; end
268 if (not GT_BS_Money) then GT_BS_Money = 0; MoneyScanned = nil; end
269  
270 elseif (event == "BANKFRAME_OPENED") then
271 GT_Debug("Bank was opened.");
272 BankStatus = 1;
273  
274 elseif (event == "BANKFRAME_CLOSED") then
275 GT_Debug("Bank was closed.");
276 BankStatus = 0;
277  
278 elseif (not GT_Saved[Position].addonOn) then return;
279  
280 elseif (event == "MAIL_SHOW") then
281 if (GT_Saved[Position].mailOn) then
282 MailOpened = true;
283 State = 0;
284 GT_Echo(MSG_IM_START);
285 GT_IM_ScanSlots();
286 end
287  
288 elseif (event == "MAIL_CLOSED") then
289 if (GT_Saved[Position].mailOn) then
290 GT_Debug("State = "..State);
291 if (State < 11 and State ~= -99) then GT_Echo(MSG_IM_CANCELED); end
292 MailOpened = false;
293 State = -99;
294 GT_IM_Message:Hide();
295 GT_IM_Update:Hide();
296 end
297  
298 elseif (event == "MAIL_INBOX_UPDATE") then
299 if (GT_Saved[Position].mailOn) then
300 GT_Debug("grab!!!!!");
301 GT_IM_Update:Show();
302 end
303  
304 elseif (event == "BAG_UPDATE") then
305 if (GT_Saved[Position].addonOn and GT_Saved[Position].mailOn) then
306 GT_IM_ScanItems();
307 end
308 end
309 end
310  
311 --Slash command handler
312 function GT_SlashCommand(msg)
313 msg = string.lower(msg);
314 GT_Debug("/command: "..msg);
315  
316 ---------------------------------------------------------------
317 ------------------GT standart commands-------------------------
318 ---------------------------------------------------------------
319 if (msg == "on") then
320 GT_Saved[Position].addonOn = true;
321 GT_Echo(MSG_GT_ON);
322  
323 elseif (msg == "off") then
324 GT_Saved[Position].addonOn = false;
325 GT_Echo(MSG_GT_OFF);
326  
327 elseif (msg == "debug") then
328  
329 if (DebugMode == 1) then
330 GT_Echo(MSG_DEBUG_OFF);
331 DebugMode = 0;
332 else
333 GT_Echo(MSG_DEBUG_ON);
334 DebugMode = 1;
335 end
336  
337 elseif (msg == "status") then
338 if (GT_Saved[Position].addonOn) then
339  
340 local status = "DISABLED";
341 if (GT_Saved[Position].addonOn) then
342 status = "ENABLED";
343 end
344 GT_Echo("GT> AddOn is "..status..", see /gts info for information on the AddOn.");
345  
346 status = "DISABLED";
347 if (GT_Saved[Position].BSOn) then
348 status = "ENABLED";
349 end
350 GT_Echo("GT> BankScan is "..status..", see /gts bsinfo for list of commands.");
351  
352 status = "DISABLED";
353 if (GT_Saved[Position].mailOn) then
354 status = "ENABLED";
355 end
356 GT_Echo("GT> MailGrabber is "..status..", see /gts iminfo for list of commands.");
357  
358 status = "DISABLED";
359 if (DebugMode == 1) then
360 status = "ENABLED";
361 end
362 GT_Echo("GT> Debug mode is "..status..".");
363  
364 else
365 GT_Echo(MSG_ADDONOFF);
366 end
367  
368 elseif (msg == "info") then
369 GT_Echo(MSG_GT_GREETING);
370 GT_Echo("GT> Scans possesions of the char, including bank into SV.lua.");
371 GT_Echo("GT> List of components:");
372 GT_Echo("GT> BankScan - scanns possesions of char. See /gts bsinfo for more info.");
373 GT_Echo("GT> IncomingMail - tracks incoming mail. See /gts iminfo for more info.");
374 GT_Echo("GT> -------------------------------------------------------------------");
375 GT_Echo("GT> Available commands:");
376 GT_Echo("GT> /gts status shows the status of GuildTools and components.");
377 GT_Echo("GT> /gts <on/off> turns the GuildTools on/off for this char.");
378 GT_Echo("GT> /gts clear clears all saved variables for this addon. If used all data will be lost.");
379 GT_Echo("GT> /gts debug tuggles Debug Mode on/off.");
380 GT_Echo("GT> /gts info shows this screen.");
381  
382 elseif (not GT_Saved[Position].addonOn) then
383 GT_Echo(MSG_GT_ADDONOFF);
384 return;
385  
386 elseif (msg == "clear") then
387 if (GT_Saved[Position].addonOn) then
388 --GT_Saved = {};
389 GT_BS_ScannedItems = {};
390 GT_BS_PreScannedItems = {};
391 GT_BS_Money = 0;
392 GT_IM_Saved = {};
393 GT_IM_MailedItems[Position] = {};
394 GT_IM_NumMailedItems[Position] = 0;
395 GT_Echo(MSG_GT_CLEARED);
396 else
397 GT_Echo(MSG_ADDONOFF);
398 end
399  
400 elseif (msg == "vars") then
401 if (MailOpened) then
402 GT_Debug("MailOpened: true");
403 else
404 GT_Debug("MailOpened: false");
405 end
406 GT_Debug("State: "..State);
407 GT_Debug("From = "..From..", To = "..To);
408  
409 ---------------------------------------------------------------
410 -------------------------BS commands---------------------------
411 ---------------------------------------------------------------
412 elseif (msg == "bs") then
413 local status = "DISABLED";
414 if (GT_Saved[Position].BSOn) then
415 status = "ENABLED";
416 end
417 GT_Echo(MSG_BS_GREETING1);
418 GT_Echo("GT> BankScan BankScan is "..status..", see /gts bsinfo for list of commands.");
419  
420 elseif (msg == "bson") then
421 GT_Saved[Position].BSOn = true;
422 GT_Echo(MSG_BS_ON);
423  
424 elseif (msg == "bsoff") then
425 GT_Saved[Position].BSOn = false;
426 GT_Echo(MSG_BS_OFF);
427  
428 elseif (msg == "bssort") then
429 if (GT_Saved[Position].BSSort==1) then
430 GT_Saved[Position].BSSort=2;
431 GT_Echo(MSG_BS_SORTOFF);
432 else
433 GT_Saved[Position].BSSort=1;
434 GT_Echo(MSG_BS_SORTON);
435 end
436  
437 elseif (msg == "bscan") then
438 if (GT_Saved[Position].BSOn) then
439 NumItems = 1;
440 if (BankStatus == 1) then
441 GT_BS_PreScannedItems = {};
442 GT_Debug("GT_BS_DoScan is called.");
443 GT_BS_DoScan();
444 GT_Debug("GT_BS_DoScan exited.");
445 GT_BS_Money = " "..GetMoney().." ";
446 GT_BS_Finalize();
447 GT_Echo(MSG_BS_DONE);
448 else
449 GT_Echo(MSG_BS_BANKCLOSED);
450 end
451 else
452 GT_Echo(MSG_ADDONOFF);
453 end
454  
455 elseif (msg == "bsprescan") then
456 if (GT_Saved[Position].BSOn) then
457 NumItems = 1;
458 if (BankStatus == 1) then
459 if (not MoneyScanned) then GT_Debug("Clear money"); GT_BS_Money = 0; end
460 GT_BS_Money = GT_BS_Money+GetMoney();
461 MoneyScanned = 1;
462 GT_Debug("GT_BS_DoScan is called.");
463 GT_BS_DoScan();
464 GT_Debug("GT_BS_DoScan exited.");
465 GT_Echo(MSG_BS_OK);
466 else
467 GT_Echo(MSG_BS_BANKCLOSED);
468 end
469 else
470 GT_Echo(MSG_ADDONOFF);
471 end
472  
473 elseif (msg == "bsfinal") then
474 if (GT_Saved[Position].BSOn) then
475 GT_BS_Money = " "..GT_BS_Money.." ";
476 GT_BS_Finalize();
477 GT_Echo(MSG_BS_DONE);
478 else
479 GT_Echo(MSG_ADDONOFF);
480 end
481  
482 elseif (msg == "bsinfo") then
483 GT_Echo(MSG_BS_GREETING);
484 GT_Echo("GT> Scans possesions of the char, including bank into SV.lua.");
485 GT_Echo("GT> Available commands:");
486 GT_Echo("GT> /gts bs shows the status of BankScan.");
487 GT_Echo("GT> /gts bs<on/off> turns the BankScan on/off for this char.");
488 GT_Echo("GT> /gts sort tuggles the BankScan's sorting function on/off for this char.");
489 GT_Echo("GT> /gts bscan performs the scan of possesions of this char for parsing into SV.lua.");
490 GT_Echo("GT> /gts bsprescan performs the scan of possesions of this char and adds them to priviously scanned.");
491 GT_Echo("GT> /gts bsfinal sends all the scanned data from bsprescan for parsing into SV.lua.");
492 GT_Echo("GT> /gts bsinfo shows this screen.");
493  
494 ---------------------------------------------------------------
495 -------------------------IM commands---------------------------
496 ---------------------------------------------------------------
497 elseif (msg == "im") then
498 local status = "DISABLED";
499 if (GT_Saved[Position].addonOn) then
500 status = "ENABLED";
501 end
502 GT_Echo("GT> IncomingMail is "..status..", see /gts iminfo for list of commands.");
503  
504 elseif (msg == "imon") then
505 GT_Saved[Position].mailOn = true;
506 GT_Echo(MSG_IM_ON);
507  
508 elseif (msg == "imoff") then
509 GT_Saved[Position].mailOn = false;
510 GT_Echo(MSG_IM_OFF);
511  
512 elseif (msg == "imsave") then
513 GT_IM_Save();
514 GT_Echo(MSG_IM_SAVED);
515  
516 elseif (msg == "imclear") then
517 if (GT_Saved[Position].mailOn) then
518 GT_IM_MailedItems[Position] = {};
519 GT_IM_NumMailedItems[Position] = 0;
520 GT_Echo(MSG_IM_CLEARED);
521 else
522 GT_Echo(MSG_IMOFF);
523 end
524  
525 elseif (msg == "iminfo") then
526 GT_Echo(MSG_IM_GREETING1);
527 GT_Echo("GT> Grabs all the nonCoD mail from your mailbox.");
528 GT_Echo("GT> Also stores info about all the incoming mail in database.");
529 GT_Echo("GT> Available commands:");
530 GT_Echo("GT> /gts im shows the status of IncomingMail.");
531 GT_Echo("GT> /gts im<on/off> turns the IncomingMail on/off for this char.");
532 GT_Echo("GT> /gts imsave saves database of this char for parsing in SV.lua.");
533 GT_Echo("GT> /gts imclear clears database for this character (Saved with /gm imsave stays intact).");
534 GT_Echo("GT> /gts iminfo shows this screen.");
535  
536 ---------------------------------------------------------------
537 ----------------------End of commands-------------------------
538 ---------------------------------------------------------------
539 else
540 GT_Echo(MSG_GT_INVALID);
541  
542 end
543 end
544  
545 --Returns full info of the item on bag_id, slot_id, nil if item is not there
546 function GT_GetItemInfo(bag_id, slot_id)
547 local count, texture, itemLink, itemName, i, command, itemQuality, itemDesc, itemType, itemSubType, itemId;
548  
549 if (not GetContainerItemInfo(bag_id, slot_id)) then
550 GT_Debug("No item in the slot "..bag_id..", "..slot_id);
551 return nil;
552 end
553  
554 --Get texture and count of the item in the current slot
555 texture, count = GetContainerItemInfo(bag_id, slot_id);
556 _,_,texture = string.find(texture, "%a+\\%a+\\([%w_]+)");
557  
558 --Get link and the name of the item in the current slot
559 itemLink = GetContainerItemLink(bag_id, slot_id);
560 _, _, itemLink, itemName = string.find(itemLink,
561 "|H(item:%d+:%d+:%d+:%d+)|h%[([^]]+)%]|h|r$");
562 _, _, itemQuality, _, itemType, itemSubType = GetItemInfo(itemLink);
563  
564 GT_Debug("Link: "..itemLink);
565 --Set tooltip to the current item
566 GT_ItemTooltip:SetOwner(this,"ANCHOR_BOTTOMRIGHT");
567 GT_ItemTooltip:ClearLines();
568 GT_ItemTooltip:SetHyperlink(itemLink);
569  
570 --Copy the description test from the tooltip to the variable
571 --<n> - new line symbol
572 --<t> - tab symbol
573 for i=1, GT_ItemTooltip:NumLines(),1 do
574  
575 command = getglobal("GT_ItemTooltipTextLeft" .. i);
576 if (command:IsShown()) then
577 text_left = command:GetText();
578 else
579 text_left = nil;
580 end
581  
582 command = getglobal("GT_ItemTooltipTextRight" .. i);
583 if (command:IsShown()) then
584 text_right = command:GetText();
585 else
586 text_right = nil;
587 end
588  
589 if (text_left and string.find(text_left, "\n")) then
590 text_left = " ";
591 end
592  
593 if (text_right and string.find(text_right, "\n")) then
594 text_right = " ";
595 end
596  
597 if (i == 1) then
598 itemDesc = text_left;
599 else
600 if (text_left) then
601 itemDesc = itemDesc.." <n> "..text_left;
602 end
603 end
604 if (text_right) then
605 itemDesc = itemDesc.." <t> "..text_right;
606 end
607 end
608 _,_,itemId = string.find(itemLink,"item:(%d+):%d+:%d+:%d+");
609 GT_Debug("ID: "..itemId);
610 return itemName, itemQuality, itemDesc, count, texture, itemType, itemSubType, itemId;
611 end
612  
613 --Prints message into the text chat window
614 function GT_Echo(message)
615 if ( DEFAULT_CHAT_FRAME ) then
616 DEFAULT_CHAT_FRAME:AddMessage(message, 0.5, 0.5, 1.0);
617 end
618 end
619  
620 --Prints debug message into the chat window iff local variable DebugMode is set to 1
621 --otherwise does nothing
622 function GT_Debug(message)
623 if (DebugMode == 1) then
624 message = "GT><**Debug**> " .. message;
625 if ( DEFAULT_CHAT_FRAME ) then
626 DEFAULT_CHAT_FRAME:AddMessage(message, 1.0, 0.0, 0.0);
627 end
628 end
629 end
630  
631 ---------------------------------------------------------------
632 ---------------------BankScan functions------------------------
633 ---------------------------------------------------------------
634  
635 --This function actually scans all bags for items
636 function GT_BS_DoScan()
637 --Array of items that will be saved in SavedVariables.lua
638 --Initiated as empty, if BankScan is not called during the session all data will be wiped upon logout
639 GT_BS_ScannedItems = {};
640  
641 local index, bag_id, slot_id, count, texture, itemLink, itemName, i, command, itemQuality, itemDesc, itemType, itemSubType, itemId;
642  
643 --Go through every slot in the bags (and bank)
644 for index, bag_id in BanknBags do
645 if (GetContainerNumSlots(bag_id)) then
646 for slot_id = 1, GetContainerNumSlots(bag_id), 1 do
647 if (GetContainerItemLink(bag_id, slot_id)) then
648  
649 itemName, itemQuality, itemDesc, count, texture, itemType, itemSubType, itemId = GT_GetItemInfo(bag_id, slot_id);
650  
651 --Save info on current item in the array
652 if (not GT_BS_PreScannedItems[itemName]) then
653 GT_BS_PreScannedItems[itemName] = {description=" "..itemDesc.." ",
654 number=count,
655 pic=" "..texture.." ",
656 quality=itemQuality,
657 subtype = " "..itemSubType.." ",
658 type = " "..itemType.." ",
659 id = " "..itemId.." "};
660 --Sorting function
661 if (GT_Saved[Position].BSSort == 1) then
662 if(itemType=="Trade Goods") then
663 GT_BS_PreScannedItems[itemName].sort = 900;
664 if (itemSubType=="Devices") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 36 - 300;
665 elseif (itemSubType=="Explosives") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 35 - 300;
666 elseif (itemSubType=="Parts") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 58;
667 elseif (itemSubType=="Trade Goods") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 59;
668 elseif (itemSubType=="Enchanting") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 57;
669 end
670 elseif(itemType=="Reagent") then
671 GT_BS_PreScannedItems[itemName].sort = 895;
672 elseif(itemType=="Weapon") then
673 GT_BS_PreScannedItems[itemName].sort = 700;
674 if (itemSubType=="Fishing Pole") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 39 - 100;
675 elseif (itemSubType=="Miscellaneous") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 38 - 100;
676 elseif (itemSubType=="Thrown") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 9 - 200;
677 else GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 50;
678 end
679 elseif(itemType=="Armor") then
680 GT_BS_PreScannedItems[itemName].sort = 600;
681 if (itemSubType=="Shield") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 60;
682 elseif (itemSubType=="Cloth") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 59;
683 elseif (itemSubType=="Leather") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 58;
684 elseif (itemSubType=="Mail") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 57;
685 elseif (itemSubType=="Plate") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 56;
686 elseif (itemSubType=="Miscellaneous") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 37;
687 end
688 elseif(itemType=="Recipe") then
689 GT_BS_PreScannedItems[itemName].sort = 500;
690 if (itemSubType=="Book") then GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 59;
691 else GT_BS_PreScannedItems[itemName].sort = GT_BS_PreScannedItems[itemName].sort + 50;
692 end
693 elseif(itemType=="Projectile") then
694 GT_BS_PreScannedItems[itemName].sort = 450;
695 elseif(itemType=="Consumable") then
696 GT_BS_PreScannedItems[itemName].sort = 350;
697 elseif(itemType=="Container") then
698 GT_BS_PreScannedItems[itemName].sort = 250;
699 else GT_BS_PreScannedItems[itemName].sort = 50; end
700 else
701 GT_BS_PreScannedItems[itemName].sort = 10000 - (bag_id+2)*100 - (slot_id+2);
702 end
703 else
704 GT_BS_PreScannedItems[itemName].number = GT_BS_PreScannedItems[itemName].number + count;
705 end
706 end
707 end
708 end
709 end
710 end
711  
712 function GT_BS_Finalize()
713 --Array of items that will be saved in SavedVariables.lua
714 --Initiated as empty, if BankScan is not called during the session all data will be wiped upon logout
715 GT_BS_ScannedItems = {};
716  
717 for itemName, param in pairs(GT_BS_PreScannedItems) do
718 local element = {name = itemName, description = param.description,
719 number = param.number, pic = param.pic, quality = param.quality,
720 sort = param.sort, type = param.type, subtype = param.subtype, id = param.id};
721 table.insert(GT_BS_ScannedItems, element);
722 end
723 GT_BS_PreScannedItems = {};
724 MoneyScanned = nil;
725  
726 --Sorting done in the following way:
727 --first sort according to sort function, then alphabetically by subtype then by quality, then alphabeticaly by name
728 table.sort(GT_BS_ScannedItems, function(i,j) return ((i.sort>j.sort) or (i.sort==j.sort and i.subtype<j.subtype) or
729 (i.sort==j.sort and i.subtype==j.subtype and i.quality>j.quality) or
730 (i.sort==j.sort and i.subtype==j.subtype and i.quality==j.quality and i.name<j.name)) end);
731  
732 for itemName, param in pairs(GT_BS_ScannedItems) do
733 GT_BS_ScannedItems[itemName].name = " "..param.name.." ";
734 GT_BS_ScannedItems[itemName].number = " "..param.number.." ";
735 GT_BS_ScannedItems[itemName].quality = " "..param.quality.." ";
736 end
737 GT_BS_Date = " "..date("%m/%d/%y").." ";
738 GT_Debug("BS Date: "..GT_BS_Date);
739 end
740  
741 ---------------------------------------------------------------
742 ------------------Incoming Mail functions----------------------
743 ---------------------------------------------------------------
744  
745 --Scans inventory for empty slots and stores the list of them
746 --in global variable availableSlots
747 function GT_IM_ScanSlots()
748 local index, bag_id, slot_id, i;
749 GT_Debug("ScanSlots.");
750 if (MailOpened and State == 0) then
751  
752 GT_IM_Message:Show();
753  
754 numSlots = 0;
755 --availableSlots = nil;
756 availableSlots = {};
757  
758 for index, bag_id in {4,3,2,1,0} do
759 if (GetContainerNumSlots(bag_id)) then
760 for slot_id = GetContainerNumSlots(bag_id), 1, -1 do
761 if (not GetContainerItemLink(bag_id, slot_id)) then
762 availableSlots[numSlots+1] = {bag_id = bag_id, slot_id = slot_id};
763 numSlots = numSlots+1;
764 end
765 end
766 end
767 end
768  
769 emptySlots = numSlots;
770  
771 GT_Debug("Empty space is scanned. There are "..numSlots.." empty slots.");
772  
773 if (numSlots < 1) then
774 GT_Echo(MSG_IM_NOBAG);
775 State = -1;
776 return;
777 end
778  
779 State = 1;
780 From = GT_IM_NumMailedItems[Position] + 1;
781 currentMessage = 1;
782 --GT_IM_Update:Show();
783 end
784 end
785  
786 --Takes mail from the mailbox
787 function GT_IM_GrabMail()
788 GT_IM_Update:Hide();
789 local index, bag_id, slot_id, i;
790 GT_Debug("GrabMail is called.");
791 if (MailOpened and State == 1) then
792  
793 GT_Debug("------>Next item. "..currentMessage.." out of "..GetInboxNumItems());
794 if (GetInboxNumItems() < currentMessage) then
795 GT_Debug("End of messages.");
796 State = 10;
797 GT_IM_ScanItems();
798 return;
799 end
800  
801 if (emptySlots<=1) then
802 GT_Debug("End of slots.");
803 State = 10;
804 GT_IM_ScanItems();
805 return;
806 end
807  
808 local sender, money, CODAmount, hasItem;
809 _, _, sender, _, money, CODAmount, _, hasItem = GetInboxHeaderInfo(currentMessage);
810  
811 while ((not hasItem and money==0) or CODAmount>0) do
812 GT_Debug("Message has no item nor cash or is COD - skipping.");
813 currentMessage = currentMessage + 1;
814 if (currentMessage > GetInboxNumItems()) then
815 GT_Debug("End of messages.");
816 State = 10;
817 GT_IM_ScanItems();
818 return;
819 end
820 _, _, sender, _, money, CODAmount, _, hasItem = GetInboxHeaderInfo(currentMessage);
821 end
822  
823 local _item = "None";
824 local _pic = "INV_Misc_QuestionMark";
825 GT_Debug("Grabbing stuff from message. From: "..sender);
826  
827 if (hasItem) then
828 GT_Debug("Taking item ...");
829 TakeInboxItem(currentMessage);
830 _item = "Unknown";
831 emptySlots = emptySlots - 1;
832 end
833  
834 if (money>0) then
835 if (_item == "None") then
836 GT_Debug("No item, but there's money, taking ...");
837 TakeInboxMoney(currentMessage);
838 _pic = "INV_Misc_Coin_01";
839  
840 elseif (_item == "Unknown") then
841 GT_Debug("There's money AND item ...");
842 State = 2;
843  
844 else
845 GT_Debug("WTF?????");
846 State = -1;
847  
848 end
849 end
850  
851 GT_Debug("Grabbing complete.");
852  
853 --Save info on current item in the array
854 GT_IM_NumMailedItems[Position] = GT_IM_NumMailedItems[Position] + 1;
855 GT_IM_MailedItems[Position][GT_IM_NumMailedItems[Position]] = {description=" ".._item.." ",
856 name=" ".._item.." ",
857 number=" ".._item.." ",
858 pic=" ".._pic.." ",
859 quality=" ".._item.." ",
860 from=" "..sender.." ",
861 coin=" "..money.." ",
862 when=" "..date("%m/%d/%y").." ",
863 id=" 00000 ",
864 type=" None ",
865 subtype=" None "};
866  
867 To = GT_IM_NumMailedItems[Position];
868 GT_Debug("From = "..From.." To = "..To);
869  
870 if (State == 1) then
871 if (string.find(sender, "Auction") and string.find(sender, "House")) then
872 State = 3;
873 else
874 State = 4;
875 end
876 end
877  
878 return;
879  
880 --Item was taken - time to take the money
881 elseif (MailOpened and State == 2) then
882 TakeInboxMoney(currentMessage);
883 State = 4;
884 return;
885  
886 --Item from AH was taken, waiting for message to disapear
887 elseif (MailOpened and State == 3) then
888 GT_Debug("AH - waiting ...");
889 State = 1;
890 return;
891  
892 --Everything from the message was taken - deleting the message
893 elseif (MailOpened and State == 4) then
894 GT_Debug("Deleting ...");
895 DeleteInboxItem(currentMessage);
896 State = 1;
897 return;
898  
899 else
900 GT_Debug("Just doing nothing.");
901 end
902 end
903  
904 --Scans global variable availableSlots for items, if found - updates MailedItems.
905 function GT_IM_ScanItems()
906 GT_Debug("ScanItem is called.");
907 if (MailOpened and State == 10) then
908  
909 if (To < From) then
910 State = 11;
911 GT_IM_Update:Hide();
912 GT_IM_Message:Hide();
913 GT_Echo(MSG_IM_SUCCESS);
914 return;
915 end
916  
917 local bag_id, slot_id, count, texture, itemName, i, itemQuality, itemDesc, itemType, itemSubType, itemId;
918  
919 bag_id = availableSlots[numSlots].bag_id;
920 slot_id = availableSlots[numSlots].slot_id;
921 GT_Debug("Checking: "..bag_id..", "..slot_id);
922 if (not GT_GetItemInfo(bag_id, slot_id)) then return; end
923  
924 for i = From, To, 1 do
925 GT_Debug("Checking: "..bag_id..", "..slot_id);
926 if (bag_id > -1 and slot_id > -1) then
927 itemName, itemQuality, itemDesc, count, texture, itemType, itemSubType, itemId = GT_GetItemInfo(bag_id, slot_id);
928 else
929 itemName = "Error";
930 itemQuality = "1";
931 itemDesc = "Error has occured.";
932 count = "1";
933 texture = "INV_Misc_QuestionMark";
934 end
935  
936 --if (not itemName) then return; end
937  
938 if (GT_IM_MailedItems[Position][i].name == " None ") then
939 GT_Debug("Skipping...");
940 GT_IM_MailedItems[Position][i].description = " No item ";
941 GT_IM_MailedItems[Position][i].name = " None ";
942 GT_IM_MailedItems[Position][i].pic = " INV_Misc_Coin_01 ";
943 GT_IM_MailedItems[Position][i].number = " 0 ";
944 GT_IM_MailedItems[Position][i].quality = " 1 ";
945 GT_IM_MailedItems[Position][i].id = " 00000 ";
946 GT_IM_MailedItems[Position][i].type = " None ";
947 GT_IM_MailedItems[Position][i].subtype = " None ";
948 else
949 if (not itemName) then From = i; GT_Debug("Ejecting..."); return; end
950 if (numSlots > 0) then
951 availableSlots[numSlots] = nil;
952 numSlots = numSlots - 1;
953 end
954  
955 GT_Debug("itemName='"..GT_IM_MailedItems[Position][i].name.."', replacing with '"..itemName.."'");
956 GT_Debug("Changing...");
957 GT_IM_MailedItems[Position][i].number = " "..count.." ";
958 GT_IM_MailedItems[Position][i].quality = " "..itemQuality.." ";
959 GT_IM_MailedItems[Position][i].description = " "..itemDesc.." ";
960 GT_IM_MailedItems[Position][i].name = " "..itemName.." ";
961 GT_IM_MailedItems[Position][i].pic = " "..texture.." ";
962 GT_IM_MailedItems[Position][i].id = " "..itemId.." ";
963 GT_IM_MailedItems[Position][i].type = " "..itemType.." ";
964 GT_IM_MailedItems[Position][i].subtype = " "..itemSubType.." ";
965  
966 if (numSlots > 0) then
967 bag_id = availableSlots[numSlots].bag_id;
968 slot_id = availableSlots[numSlots].slot_id;
969 else
970 bag_id = -1;
971 slot_id = -1;
972 end
973 end
974 end
975  
976 State = 11;
977 GT_IM_Update:Hide();
978 GT_IM_Message:Hide();
979 GT_Echo(MSG_IM_SUCCESS);
980 end
981 end
982  
983 --Transfers data from GT_IM_NumMailedItems to BS_GT_Saved in reverse order
984 function GT_IM_Save()
985 local i, j;
986 j = 0;
987 BS_GT_Saved = {};
988 for i = GT_IM_NumMailedItems[Position], 1, -1 do
989 if (GT_IM_MailedItems[Position][i]) then
990 GT_IM_Saved[GT_IM_NumMailedItems[Position]+1-i-j] = GT_IM_MailedItems[Position][i];
991 else
992 j = j + 1;
993 end
994 end
995 end