comparison Modules/Collected.lua @ 0:823e33465b6e

Initial commit
author Zerotorescue
date Fri, 03 Sep 2010 12:43:36 +0200
parents
children 6f17035de058
comparison
equal deleted inserted replaced
-1:000000000000 0:823e33465b6e
1 local MailOpener = LibStub("AceAddon-3.0"):GetAddon("MailOpener");
2 local Collected = MailOpener:NewModule("Collected", "AceEvent-3.0", "AceTimer-3.0");
3
4 local previousGold, earned, sessionEarned;
5 local previousFreeSlotsAvailable, itemsGained, sessionItemsgained;
6 local previousMailCount, mailOpened, sessionMailOpened;
7 local timeStarted, sessionTimeSpent; -- even though the first contains GetTime(), the second var will be filled with actual seconds
8 local updated;
9
10 function Collected:OnInitialize()
11 local defaults = {
12 profile = {
13 trackGold = true,
14 trackItems = true,
15 trackMail = true,
16 sessionSummary = false,
17 batchSummary = false,
18 },
19 };
20
21 -- Register our saved variables NameSpace
22 self.db = MailOpener.db:RegisterNamespace("Collected", defaults);
23 end
24
25 function Collected:OnEnable()
26 self:Debug("OnEnable");
27
28 self:RegisterEvent("MAIL_SHOW");
29
30 MailOpener:TogglePostalModule("Rake", false);
31
32 sessionEarned = 0;
33 sessionItemsgained = 0;
34 sessionMailOpened = 0;
35 sessionTimeSpent = 0;
36
37 -- If we were toggling this module on while the mailbox is opened we must register all events again
38 if MailFrame:IsVisible() then
39 self:MAIL_SHOW();
40 end
41 end
42
43 -- Even though Ace can unregister our events it's neater to do it manually too
44 function Collected:OnDisable()
45 self:Debug("OnDisable");
46
47 self:UnregisterEvent("MAIL_SHOW");
48
49 self:Stop();
50
51 MailOpener:TogglePostalModule("Rake", true);
52 end
53
54 function Collected:MAIL_SHOW()
55 self:Debug("MAIL_SHOW");
56
57 -- Unbind / reset all previous events and settings
58 self:Stop();
59
60 self:RegisterEvent("MAIL_CLOSED");
61
62 if self.db.profile.batchSummary then
63 self:RegisterMessage("MO_OPEN_COMPLETE");
64 end
65
66 -- Money
67 if self.db.profile.trackGold then
68 self:RegisterEvent("PLAYER_MONEY");
69
70 previousGold = GetMoney();
71 earned = 0;
72 end
73
74 -- Items
75 if self.db.profile.trackItems then
76 self:RegisterEvent("BAG_UPDATE");
77
78 previousFreeSlotsAvailable = self:GetNumFreeSlots();
79 itemsGained = 0;
80 end
81
82 -- Mail
83 if self.db.profile.trackMail then
84 self:RegisterEvent("MAIL_INBOX_UPDATE");
85
86 previousMailCount = nil;
87 mailOpened = 0;
88 end
89
90 -- Time Spent
91 if self.db.profile.trackTimeSpent then
92 timeStarted = GetTime();
93 end
94 end
95
96 function Collected:MAIL_CLOSED()
97 self:Debug("MAIL_CLOSED");
98
99 self:Summarize();
100
101 self:Stop();
102 end
103
104 function Collected:PLAYER_MONEY()
105 -- Sending mail does not interest us, so only remember what we earned
106 local currentGold = GetMoney();
107
108 if currentGold > previousGold then
109 local goldEarned = ( currentGold - previousGold );
110
111 earned = ( earned + goldEarned );
112
113 updated = true;
114
115 if self.db.profile.sessionSummary then
116 sessionEarned = ( sessionEarned + goldEarned );
117 end
118 end
119
120 previousGold = currentGold;
121 end
122
123 function Collected:BAG_UPDATE()
124 -- Sending mail does not interest us, so only remember what we earned
125 local freeSlotAvailable = self:GetNumFreeSlots();
126
127 if freeSlotAvailable < previousFreeSlotsAvailable then
128 -- we lost a slot, so gained an item
129
130 local gained = ( previousFreeSlotsAvailable - freeSlotAvailable );
131
132 itemsGained = ( itemsGained + gained );
133
134 updated = true;
135
136 if self.db.profile.sessionSummary then
137 sessionItemsgained = ( sessionItemsgained + gained );
138 end
139
140 --We may need the lines below if we get inaccurate counts
141 -- previousFreeSlotsAvailable = ( previousFreeSlotsAvailable - gained );
142 --elseif freeSlotAvailable > previousFreeSlotsAvailable then
143 -- an additional slot available, so we lost an item
144
145 -- local lost = ( freeSlotAvailable - previousFreeSlotsAvailable );
146
147 -- previousFreeSlotsAvailable = ( previousFreeSlotsAvailable + lost );
148 end
149
150 previousFreeSlotsAvailable = freeSlotAvailable;
151 end
152
153 function Collected:MAIL_INBOX_UPDATE()
154 local numItems, _ = GetInboxNumItems();
155
156 if previousMailCount == nil then
157 previousMailCount = numItems;
158 else
159 if numItems < previousMailCount then
160 -- We lost a mail, which means we opened one without text
161
162 --local opened = ( previousMailCount - numItems );
163
164 mailOpened = ( mailOpened + 1 );
165
166 updated = true;
167
168 if self.db.profile.sessionSummary then
169 sessionMailOpened = ( sessionMailOpened + 1 );
170 end
171 end
172
173 previousMailCount = numItems;
174 end
175 end
176
177 function Collected:MO_OPEN_COMPLETE()
178 -- Only summarize when changed
179 if updated then
180 self:Summarize();
181
182 updated = false;
183 end
184 end
185
186 function Collected:Stop()
187 self:UnregisterEvent("MAIL_CLOSED");
188
189 -- Batch summary
190 self:UnregisterMessage("MO_OPEN_COMPLETE");
191
192 -- Clear any var in the memory remaining
193
194 -- Money
195 self:UnregisterEvent("PLAYER_MONEY");
196 previousGold = nil;
197 earned = nil;
198
199 -- Items
200 self:UnregisterEvent("BAG_UPDATE");
201 previousFreeSlotsAvailable = nil;
202 itemsGained = nil;
203
204 -- Mail
205 self:UnregisterEvent("MAIL_INBOX_UPDATE");
206 previousMailCount = nil;
207 mailOpened = nil;
208
209 -- Time Spent
210 timeStarted = nil;
211 end
212
213 function Collected:Summarize()
214 -- Message buffer, append details we have data for
215 local printMessage = "";
216
217 local timeSpent;
218 if timeStarted then
219 timeSpent = ( GetTime() - timeStarted );
220
221 if self.db.profile.sessionSummary and self.db.profile.trackTimeSpent then
222 sessionTimeSpent = ( sessionTimeSpent + timeSpent );
223 end
224 end
225
226 -- Did we record any mail being opened?
227 if mailOpened and mailOpened > 0 then
228 -- Time Spent
229 if timeSpent then
230 printMessage = printMessage .. format("Collected a total of %d mails within %d minutes and %d seconds. ", mailOpened, floor( timeSpent / 60 ), ( timeSpent % 60 ));
231 else
232 printMessage = printMessage .. format("Collected a total of %d mails. ", mailOpened);
233 end
234 elseif timeSpent then
235 printMessage = printMessage .. format("Spent %d minutes and %d seconds collecting mail. ", floor( timeSpent / 60 ), ( timeSpent % 60 ));
236 end
237
238 -- Did we record any items or gold being looted?
239 if (itemsGained and itemsGained > 0) and (earned and earned > 0) then
240 printMessage = printMessage .. format("From these mails you gained %d items and %s.", itemsGained, MailOpener:FormatMoney(earned));
241 elseif itemsGained and itemsGained > 0 then
242 printMessage = printMessage .. format("From these mails you gained %d items.", itemsGained);
243 elseif earned and earned > 0 then
244 printMessage = printMessage .. format("From these mails you gained %s.", MailOpener:FormatMoney(earned));
245 end
246
247 -- Did we record anything? print that!
248 if printMessage ~= "" then
249 print("|cff15ff00Mail Opener|r: " .. printMessage);
250 end
251
252 if self.db.profile.sessionSummary and ((sessionMailOpened and (not mailOpened or sessionMailOpened > mailOpened)) or (sessionTimeSpent and (not timeSpent or sessionTimeSpent > timeSpent)) or (sessionEarned and (not earned or sessionEarned > earned)) or (sessionItemsgained and (not itemsGained or sessionItemsgained > itemsGained))) then
253 -- Message buffer, append details we have data for
254 printMessage = "|cff15ff00Mail Opener|r: (Session summary) ";
255
256 -- Did we record any mail being opened?
257 if sessionMailOpened and sessionMailOpened > 0 then
258 -- Time Spent
259 if sessionTimeSpent then
260 printMessage = printMessage .. format("Collected a total of %d mails within %d minutes and %d seconds this session. ", sessionMailOpened, floor( sessionTimeSpent / 60 ), ( sessionTimeSpent % 60 ));
261 else
262 printMessage = printMessage .. format("Collected a total of %d mails this session. ", sessionMailOpened);
263 end
264 elseif sessionTimeSpent then
265 printMessage = printMessage .. format("Spent %d minutes and %d seconds collecting mail this session. ", floor( sessionTimeSpent / 60 ), ( sessionTimeSpent % 60 ));
266 end
267
268 -- Did we record any items or gold being looted?
269 if (sessionItemsgained and sessionItemsgained > 0) and (sessionEarned and sessionEarned > 0) then
270 printMessage = printMessage .. format("From these mails you gained %d items and %s.", sessionItemsgained, MailOpener:FormatMoney(sessionEarned));
271 elseif sessionItemsgained and sessionItemsgained > 0 then
272 printMessage = printMessage .. format("From these mails you gained %d items.", sessionItemsgained);
273 elseif sessionEarned and sessionEarned > 0 then
274 printMessage = printMessage .. format("From these mails you gained %s.", MailOpener:FormatMoney(sessionEarned));
275 end
276
277 -- Did we record anything? print that!
278 if printMessage ~= "" then
279 print(printMessage);
280 end
281 end
282 end
283
284 function Collected:GetNumFreeSlots()
285 local slotsAvailable = 0;
286 for bag = 0, 4 do
287 slotsAvailable = ( slotsAvailable + GetContainerNumFreeSlots(bag) );
288 end
289
290 return slotsAvailable;
291 end
292
293 function Collected:GetOptionsGroup()
294 local configGroup = {
295 order = 400,
296 type = "group",
297 name = "Collected",
298 desc = "Change settings for the collected module.",
299 args = {
300 General = {
301 order = 10,
302 type = "group",
303 inline = true,
304 name = "General",
305 args = {
306 description = {
307 order = 10,
308 type = "description",
309 name = function()
310 local default = "With this button you can completely toggle this module |cff00ff00on|r or |cffff0000off|r. This setting will be remembered and the module will be automatically toggled |cff00ff00on|r or |cffff0000off|r upon logon as it was last set.\n\n";
311
312 if self:IsEnabled() then
313 return default .. "Status: |cff00ff00Enabled|r";
314 else
315 return default .. "Status: |cffff0000Disabled|r";
316 end
317 end,
318 },
319 disable = {
320 order = 20,
321 type = "execute",
322 name = function()
323 if self:IsEnabled() then
324 return "Disable Module";
325 else
326 return "Enable Module";
327 end
328 end,
329 desc = "Click here to completely toggle this module on or off.",
330 width = "double",
331 func = function()
332 if self:IsEnabled() then
333 self:Disable();
334
335 MailOpener.db.profile.modules[self:GetName()] = false;
336 else
337 self:Enable();
338
339 MailOpener.db.profile.modules[self:GetName()] = true;
340 end
341 end,
342 },
343 },
344 },
345 TrackStats = {
346 order = 20,
347 type = "group",
348 inline = true,
349 name = "Track Stats",
350 args = {
351 description = {
352 order = 10,
353 type = "description",
354 name = "You can select what things to track. Toggling something off will stop tracking of it completely and reduce the resources used by Mail Opener (although you shouldn't notice a difference).",
355 },
356 AHHeader = {
357 order = 15,
358 type = "header",
359 name = "",
360 },
361 trackGold = {
362 order = 20,
363 type = "toggle",
364 name = "Track |cfffed000gold gained|r",
365 desc = "Track the amount of gold gained and display it when closing the mailbox.",
366 set = function(i, v)
367 self.db.profile.trackGold = v;
368
369 if MailFrame:IsVisible() then
370 self:MAIL_SHOW();
371 end
372 end,
373 get = function() return self.db.profile.trackGold; end,
374 },
375 trackItems = {
376 order = 30,
377 type = "toggle",
378 name = "Track |cfffed000items gained|r",
379 desc = "Track the amount of items gained and display it when closing the mailbox.",
380 set = function(i, v)
381 self.db.profile.trackItems = v;
382
383 if MailFrame:IsVisible() then
384 self:MAIL_SHOW();
385 end
386 end,
387 get = function() return self.db.profile.trackItems; end,
388 },
389 trackMail = {
390 order = 40,
391 type = "toggle",
392 name = "Track |cfffed000mail opened|r",
393 desc = "Track the amount of mail received and display it when closing the mailbox.",
394 set = function(i, v)
395 self.db.profile.trackMail = v;
396
397 if MailFrame:IsVisible() then
398 self:MAIL_SHOW();
399 end
400 end,
401 get = function() return self.db.profile.trackMail; end,
402 },
403 trackTimeSpent = {
404 order = 50,
405 type = "toggle",
406 name = "Track |cfffed000time spent|r",
407 desc = "Track the amount of time spent at the mailbox.",
408 set = function(i, v)
409 self.db.profile.trackTimeSpent = v;
410
411 if MailFrame:IsVisible() then
412 self:MAIL_SHOW();
413 end
414 end,
415 get = function() return self.db.profile.trackTimeSpent; end,
416 },
417 },
418 },
419 Summarize = {
420 order = 30,
421 type = "group",
422 inline = true,
423 name = "Summarize",
424 args = {
425 sessionSummary = {
426 order = 20,
427 type = "toggle",
428 name = "Also show a summary of the recorded stats within the entire session",
429 desc = "Also show a summary of the recorded stats within the entire session (since your last login or /reload).",
430 set = function(i, v)
431 self.db.profile.sessionSummary = v;
432
433 if MailFrame:IsVisible() then
434 self:MAIL_SHOW();
435 end
436 end,
437 get = function() return self.db.profile.sessionSummary; end,
438 width = "full",
439 },
440 batchSummary = {
441 order = 30,
442 type = "toggle",
443 name = "Show a summary of the recorded stats whenever it updated after opening the current batch has finished",
444 desc = "Show a summary of the recorded stats whenever it has been updated after opening the current batch of mails has finished.",
445 set = function(i, v)
446 self.db.profile.batchSummary = v;
447
448 if MailFrame:IsVisible() then
449 self:MAIL_SHOW();
450 end
451 end,
452 get = function() return self.db.profile.batchSummary; end,
453 width = "full",
454 },
455 },
456 },
457 },
458 };
459
460 return configGroup;
461 end
462
463 function Collected:Debug(t)
464 return MailOpener:Debug("|cffff0000Collected|r:" .. t);
465 end