Mercurial > wow > mailopener
diff Modules/Collected.lua @ 0:823e33465b6e
Initial commit
| author | Zerotorescue |
|---|---|
| date | Fri, 03 Sep 2010 12:43:36 +0200 |
| parents | |
| children | 6f17035de058 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Modules/Collected.lua Fri Sep 03 12:43:36 2010 +0200 @@ -0,0 +1,465 @@ +local MailOpener = LibStub("AceAddon-3.0"):GetAddon("MailOpener"); +local Collected = MailOpener:NewModule("Collected", "AceEvent-3.0", "AceTimer-3.0"); + +local previousGold, earned, sessionEarned; +local previousFreeSlotsAvailable, itemsGained, sessionItemsgained; +local previousMailCount, mailOpened, sessionMailOpened; +local timeStarted, sessionTimeSpent; -- even though the first contains GetTime(), the second var will be filled with actual seconds +local updated; + +function Collected:OnInitialize() + local defaults = { + profile = { + trackGold = true, + trackItems = true, + trackMail = true, + sessionSummary = false, + batchSummary = false, + }, + }; + + -- Register our saved variables NameSpace + self.db = MailOpener.db:RegisterNamespace("Collected", defaults); +end + +function Collected:OnEnable() + self:Debug("OnEnable"); + + self:RegisterEvent("MAIL_SHOW"); + + MailOpener:TogglePostalModule("Rake", false); + + sessionEarned = 0; + sessionItemsgained = 0; + sessionMailOpened = 0; + sessionTimeSpent = 0; + + -- If we were toggling this module on while the mailbox is opened we must register all events again + if MailFrame:IsVisible() then + self:MAIL_SHOW(); + end +end + +-- Even though Ace can unregister our events it's neater to do it manually too +function Collected:OnDisable() + self:Debug("OnDisable"); + + self:UnregisterEvent("MAIL_SHOW"); + + self:Stop(); + + MailOpener:TogglePostalModule("Rake", true); +end + +function Collected:MAIL_SHOW() + self:Debug("MAIL_SHOW"); + + -- Unbind / reset all previous events and settings + self:Stop(); + + self:RegisterEvent("MAIL_CLOSED"); + + if self.db.profile.batchSummary then + self:RegisterMessage("MO_OPEN_COMPLETE"); + end + + -- Money + if self.db.profile.trackGold then + self:RegisterEvent("PLAYER_MONEY"); + + previousGold = GetMoney(); + earned = 0; + end + + -- Items + if self.db.profile.trackItems then + self:RegisterEvent("BAG_UPDATE"); + + previousFreeSlotsAvailable = self:GetNumFreeSlots(); + itemsGained = 0; + end + + -- Mail + if self.db.profile.trackMail then + self:RegisterEvent("MAIL_INBOX_UPDATE"); + + previousMailCount = nil; + mailOpened = 0; + end + + -- Time Spent + if self.db.profile.trackTimeSpent then + timeStarted = GetTime(); + end +end + +function Collected:MAIL_CLOSED() + self:Debug("MAIL_CLOSED"); + + self:Summarize(); + + self:Stop(); +end + +function Collected:PLAYER_MONEY() + -- Sending mail does not interest us, so only remember what we earned + local currentGold = GetMoney(); + + if currentGold > previousGold then + local goldEarned = ( currentGold - previousGold ); + + earned = ( earned + goldEarned ); + + updated = true; + + if self.db.profile.sessionSummary then + sessionEarned = ( sessionEarned + goldEarned ); + end + end + + previousGold = currentGold; +end + +function Collected:BAG_UPDATE() + -- Sending mail does not interest us, so only remember what we earned + local freeSlotAvailable = self:GetNumFreeSlots(); + + if freeSlotAvailable < previousFreeSlotsAvailable then + -- we lost a slot, so gained an item + + local gained = ( previousFreeSlotsAvailable - freeSlotAvailable ); + + itemsGained = ( itemsGained + gained ); + + updated = true; + + if self.db.profile.sessionSummary then + sessionItemsgained = ( sessionItemsgained + gained ); + end + + --We may need the lines below if we get inaccurate counts + -- previousFreeSlotsAvailable = ( previousFreeSlotsAvailable - gained ); + --elseif freeSlotAvailable > previousFreeSlotsAvailable then + -- an additional slot available, so we lost an item + + -- local lost = ( freeSlotAvailable - previousFreeSlotsAvailable ); + + -- previousFreeSlotsAvailable = ( previousFreeSlotsAvailable + lost ); + end + + previousFreeSlotsAvailable = freeSlotAvailable; +end + +function Collected:MAIL_INBOX_UPDATE() + local numItems, _ = GetInboxNumItems(); + + if previousMailCount == nil then + previousMailCount = numItems; + else + if numItems < previousMailCount then + -- We lost a mail, which means we opened one without text + + --local opened = ( previousMailCount - numItems ); + + mailOpened = ( mailOpened + 1 ); + + updated = true; + + if self.db.profile.sessionSummary then + sessionMailOpened = ( sessionMailOpened + 1 ); + end + end + + previousMailCount = numItems; + end +end + +function Collected:MO_OPEN_COMPLETE() + -- Only summarize when changed + if updated then + self:Summarize(); + + updated = false; + end +end + +function Collected:Stop() + self:UnregisterEvent("MAIL_CLOSED"); + + -- Batch summary + self:UnregisterMessage("MO_OPEN_COMPLETE"); + + -- Clear any var in the memory remaining + + -- Money + self:UnregisterEvent("PLAYER_MONEY"); + previousGold = nil; + earned = nil; + + -- Items + self:UnregisterEvent("BAG_UPDATE"); + previousFreeSlotsAvailable = nil; + itemsGained = nil; + + -- Mail + self:UnregisterEvent("MAIL_INBOX_UPDATE"); + previousMailCount = nil; + mailOpened = nil; + + -- Time Spent + timeStarted = nil; +end + +function Collected:Summarize() + -- Message buffer, append details we have data for + local printMessage = ""; + + local timeSpent; + if timeStarted then + timeSpent = ( GetTime() - timeStarted ); + + if self.db.profile.sessionSummary and self.db.profile.trackTimeSpent then + sessionTimeSpent = ( sessionTimeSpent + timeSpent ); + end + end + + -- Did we record any mail being opened? + if mailOpened and mailOpened > 0 then + -- Time Spent + if timeSpent then + printMessage = printMessage .. format("Collected a total of %d mails within %d minutes and %d seconds. ", mailOpened, floor( timeSpent / 60 ), ( timeSpent % 60 )); + else + printMessage = printMessage .. format("Collected a total of %d mails. ", mailOpened); + end + elseif timeSpent then + printMessage = printMessage .. format("Spent %d minutes and %d seconds collecting mail. ", floor( timeSpent / 60 ), ( timeSpent % 60 )); + end + + -- Did we record any items or gold being looted? + if (itemsGained and itemsGained > 0) and (earned and earned > 0) then + printMessage = printMessage .. format("From these mails you gained %d items and %s.", itemsGained, MailOpener:FormatMoney(earned)); + elseif itemsGained and itemsGained > 0 then + printMessage = printMessage .. format("From these mails you gained %d items.", itemsGained); + elseif earned and earned > 0 then + printMessage = printMessage .. format("From these mails you gained %s.", MailOpener:FormatMoney(earned)); + end + + -- Did we record anything? print that! + if printMessage ~= "" then + print("|cff15ff00Mail Opener|r: " .. printMessage); + end + + 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 + -- Message buffer, append details we have data for + printMessage = "|cff15ff00Mail Opener|r: (Session summary) "; + + -- Did we record any mail being opened? + if sessionMailOpened and sessionMailOpened > 0 then + -- Time Spent + if sessionTimeSpent then + printMessage = printMessage .. format("Collected a total of %d mails within %d minutes and %d seconds this session. ", sessionMailOpened, floor( sessionTimeSpent / 60 ), ( sessionTimeSpent % 60 )); + else + printMessage = printMessage .. format("Collected a total of %d mails this session. ", sessionMailOpened); + end + elseif sessionTimeSpent then + printMessage = printMessage .. format("Spent %d minutes and %d seconds collecting mail this session. ", floor( sessionTimeSpent / 60 ), ( sessionTimeSpent % 60 )); + end + + -- Did we record any items or gold being looted? + if (sessionItemsgained and sessionItemsgained > 0) and (sessionEarned and sessionEarned > 0) then + printMessage = printMessage .. format("From these mails you gained %d items and %s.", sessionItemsgained, MailOpener:FormatMoney(sessionEarned)); + elseif sessionItemsgained and sessionItemsgained > 0 then + printMessage = printMessage .. format("From these mails you gained %d items.", sessionItemsgained); + elseif sessionEarned and sessionEarned > 0 then + printMessage = printMessage .. format("From these mails you gained %s.", MailOpener:FormatMoney(sessionEarned)); + end + + -- Did we record anything? print that! + if printMessage ~= "" then + print(printMessage); + end + end +end + +function Collected:GetNumFreeSlots() + local slotsAvailable = 0; + for bag = 0, 4 do + slotsAvailable = ( slotsAvailable + GetContainerNumFreeSlots(bag) ); + end + + return slotsAvailable; +end + +function Collected:GetOptionsGroup() + local configGroup = { + order = 400, + type = "group", + name = "Collected", + desc = "Change settings for the collected module.", + args = { + General = { + order = 10, + type = "group", + inline = true, + name = "General", + args = { + description = { + order = 10, + type = "description", + name = function() + 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"; + + if self:IsEnabled() then + return default .. "Status: |cff00ff00Enabled|r"; + else + return default .. "Status: |cffff0000Disabled|r"; + end + end, + }, + disable = { + order = 20, + type = "execute", + name = function() + if self:IsEnabled() then + return "Disable Module"; + else + return "Enable Module"; + end + end, + desc = "Click here to completely toggle this module on or off.", + width = "double", + func = function() + if self:IsEnabled() then + self:Disable(); + + MailOpener.db.profile.modules[self:GetName()] = false; + else + self:Enable(); + + MailOpener.db.profile.modules[self:GetName()] = true; + end + end, + }, + }, + }, + TrackStats = { + order = 20, + type = "group", + inline = true, + name = "Track Stats", + args = { + description = { + order = 10, + type = "description", + 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).", + }, + AHHeader = { + order = 15, + type = "header", + name = "", + }, + trackGold = { + order = 20, + type = "toggle", + name = "Track |cfffed000gold gained|r", + desc = "Track the amount of gold gained and display it when closing the mailbox.", + set = function(i, v) + self.db.profile.trackGold = v; + + if MailFrame:IsVisible() then + self:MAIL_SHOW(); + end + end, + get = function() return self.db.profile.trackGold; end, + }, + trackItems = { + order = 30, + type = "toggle", + name = "Track |cfffed000items gained|r", + desc = "Track the amount of items gained and display it when closing the mailbox.", + set = function(i, v) + self.db.profile.trackItems = v; + + if MailFrame:IsVisible() then + self:MAIL_SHOW(); + end + end, + get = function() return self.db.profile.trackItems; end, + }, + trackMail = { + order = 40, + type = "toggle", + name = "Track |cfffed000mail opened|r", + desc = "Track the amount of mail received and display it when closing the mailbox.", + set = function(i, v) + self.db.profile.trackMail = v; + + if MailFrame:IsVisible() then + self:MAIL_SHOW(); + end + end, + get = function() return self.db.profile.trackMail; end, + }, + trackTimeSpent = { + order = 50, + type = "toggle", + name = "Track |cfffed000time spent|r", + desc = "Track the amount of time spent at the mailbox.", + set = function(i, v) + self.db.profile.trackTimeSpent = v; + + if MailFrame:IsVisible() then + self:MAIL_SHOW(); + end + end, + get = function() return self.db.profile.trackTimeSpent; end, + }, + }, + }, + Summarize = { + order = 30, + type = "group", + inline = true, + name = "Summarize", + args = { + sessionSummary = { + order = 20, + type = "toggle", + name = "Also show a summary of the recorded stats within the entire session", + desc = "Also show a summary of the recorded stats within the entire session (since your last login or /reload).", + set = function(i, v) + self.db.profile.sessionSummary = v; + + if MailFrame:IsVisible() then + self:MAIL_SHOW(); + end + end, + get = function() return self.db.profile.sessionSummary; end, + width = "full", + }, + batchSummary = { + order = 30, + type = "toggle", + name = "Show a summary of the recorded stats whenever it updated after opening the current batch has finished", + desc = "Show a summary of the recorded stats whenever it has been updated after opening the current batch of mails has finished.", + set = function(i, v) + self.db.profile.batchSummary = v; + + if MailFrame:IsVisible() then + self:MAIL_SHOW(); + end + end, + get = function() return self.db.profile.batchSummary; end, + width = "full", + }, + }, + }, + }, + }; + + return configGroup; +end + +function Collected:Debug(t) + return MailOpener:Debug("|cffff0000Collected|r:" .. t); +end \ No newline at end of file
