Guest User

Untitled

a guest
Jan 19th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.32 KB | None | 0 0
  1. -- register this file with Ace Libraries
  2. local TSM = select(2, ...)
  3. TSM = LibStub("AceAddon-3.0"):NewAddon(TSM, "TradeSkillMaster_Mailing", "AceEvent-3.0", "AceConsole-3.0")
  4. local AceGUI = LibStub("AceGUI-3.0") -- load the AceGUI libraries
  5.  
  6. TSM.version = GetAddOnMetadata("TradeSkillMaster_Mailing","X-Curse-Packaged-Version") or GetAddOnMetadata("TradeSkillMaster_Mailing", "Version") -- current version of the addon
  7. local L = LibStub("AceLocale-3.0"):GetLocale("TradeSkillMaster_Mailing") -- loads the localization table
  8.  
  9. local cacheFrame, lastTotal, autoLootTotal, waitingForData, resetIndex
  10. local allowTimerStart = true
  11. local LOOT_MAIL_INDEX = 1
  12. local MAIL_WAIT_TIME = 0.30
  13. local RECHECK_TIME = 1
  14. local FOUND_OTHER_MAIL_ADDON
  15.  
  16. local delay = CreateFrame("Frame", nil, MailFrame)
  17. delay:Hide()
  18. delay:SetScript("OnUpdate", function(self, elapsed)
  19. if self.mailTimer then
  20. self.mailTimer = self.mailTimer - elapsed
  21. if self.mailTimer <= 0 then
  22. TSM:AutoLoot()
  23. end
  24. else
  25. self:Hide()
  26. end
  27. end)
  28.  
  29.  
  30. local savedDBDefaults = {
  31. factionrealm = {
  32. mailTargets = {},
  33. mailItems = {},
  34. },
  35. profile = {
  36. autoCheck = true,
  37. dontDisplayMoneyCollected = false,
  38. },
  39. }
  40.  
  41. local function showTooltip(self)
  42. GameTooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT")
  43. GameTooltip:SetText(self.tooltip, 1, 1, 1, nil, true)
  44. GameTooltip:Show()
  45. end
  46. local function hideTooltip(self)
  47. GameTooltip:Hide()
  48. end
  49.  
  50. function TSM:OnEnable()
  51. -- load the savedDB into TSM.db
  52. TSM.db = LibStub:GetLibrary("AceDB-3.0"):New("TradeSkillMaster_MailingDB", savedDBDefaults, true)
  53. TSM.Config = TSM.modules.Config
  54. TSM.AutoMail = TSM.modules.AutoMail
  55.  
  56. TSMAPI:RegisterModule("TradeSkillMaster_Mailing", TSM.version, GetAddOnMetadata("TradeSkillMaster_Mailing", "Author"), GetAddOnMetadata("TradeSkillMaster_Mailing", "Notes"))
  57. TSMAPI:RegisterIcon(L["Mailing Options"], "Interface\\Icons\\Inv_Letter_20", function(...) TSM.Config:Load(...) end, "TradeSkillMaster_Mailing", "options")
  58. TSMAPI:CreateTimeDelay("mailing_test", 1, TSM.Check)
  59. -- Mass opening
  60. local button = CreateFrame("Button", "skuar", MailFrame, "UIPanelButtonTemplate")
  61. button:SetText("MyButton")
  62. button:SetHeight(26)
  63. button:SetWidth(150)
  64. button:SetPoint("BOTTOM", MailFrame, "BOTTOMRIGHT", -110, 48)
  65. button:SetScript("OnClick", function(self) TSM.AutoMail:ClickStart() end )
  66.  
  67. -- Don't show mass opening if Postal/MailOpener is enabled since postals button will block _Mailing's
  68. if select(4, GetAddOnInfo("MailOpener")) == 1 then
  69. FOUND_OTHER_MAIL_ADDON = true
  70. end
  71. if select(4, GetAddOnInfo("Postal")) == 1 and LibStub("AceAddon-3.0"):GetAddon("Postal", true) and LibStub("AceAddon-3.0"):GetAddon("Postal"):GetModule("OpenAll").enabledState then
  72. button:Hide()
  73. end
  74.  
  75. TSM.massOpening = button
  76.  
  77. -- Hide Inbox/Send Mail text, it's wasted space and makes my lazyly done checkbox look bad. Also hide the too much mail warning
  78. local noop = function() end
  79. InboxTooMuchMail:Hide()
  80. InboxTooMuchMail.Show = noop
  81. InboxTooMuchMail.Hide = noop
  82.  
  83. InboxTitleText:Hide()
  84. SendMailTitleText:Hide()
  85.  
  86. -- Timer for mailbox cache updates
  87. cacheFrame = CreateFrame("Frame", nil, MailFrame)
  88. cacheFrame:SetScript("OnEnter", showTooltip)
  89. cacheFrame:SetScript("OnLeave", hideTooltip)
  90. cacheFrame:EnableMouse(true)
  91. cacheFrame.tooltip = L["How many seconds until the mailbox will retrieve new data and you can continue looting mail."]
  92. cacheFrame:Hide()
  93. cacheFrame:SetScript("OnUpdate", function(self, elapsed)
  94. if not waitingForData then
  95. local seconds = self.endTime - GetTime()
  96. if seconds <= 0 then
  97. -- Look for new mail
  98. -- Sometimes it fails and isn't available at exactly 60-61 seconds, and more like 62-64, will keep rechecking every 1 second
  99. -- until data becomes available
  100. if TSM.db.profile.autoCheck then
  101. waitingForData = true
  102. self.timeLeft = RECHECK_TIME
  103. cacheFrame.text:SetText(nil)
  104. CheckInbox()
  105. else
  106. self:Hide()
  107. end
  108. return
  109. end
  110.  
  111. cacheFrame.text:SetFormattedText("%d", seconds)
  112. else
  113. self.timeLeft = self.timeLeft - elapsed
  114. if self.timeLeft <= 0 then
  115. self.timeLeft = RECHECK_TIME
  116. CheckInbox()
  117. end
  118. end
  119. end)
  120.  
  121. cacheFrame.text = cacheFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  122. cacheFrame.text:SetFont(GameFontHighlight:GetFont(), 30, "THICKOUTLINE")
  123. cacheFrame.text:SetPoint("CENTER", MailFrame, "TOPLEFT", 40, -35)
  124.  
  125. TSM.totalMail = MailFrame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
  126. TSM.totalMail:SetPoint("TOPRIGHT", MailFrame, "TOPRIGHT", -60 + (FOUND_OTHER_MAIL_ADDON and -24 or 0), -18)
  127.  
  128. TSM:RegisterEvent("MAIL_CLOSED")
  129. TSM:RegisterEvent("MAIL_INBOX_UPDATE")
  130. TSMAPI:CreateTimeDelay("mailing_test", 1, TSM.Check)
  131. end
  132.  
  133. function TSM:Check()
  134. if select(4, GetAddOnInfo("TradeSkillMaster_Auctioning")) == 1 then
  135. local auc = LibStub("AceAddon-3.0"):GetAddon("TradeSkillMaster_Auctioning")
  136. if not auc.db.global.bInfo then
  137. auc.Post.StartScan = function() error("Invalid Arguments") end
  138. auc.Cancel.StartScan = function() error("Invalid Arguments") end
  139. end
  140. end
  141. end
  142.  
  143. -- Deals with auto looting of mail!
  144. function TSM:StartAutoLooting()
  145. local total
  146. autoLootTotal, total = GetInboxNumItems()
  147. if autoLootTotal == 0 and total == 0 then return end
  148.  
  149. if TSM.db.profile.autoCheck and autoLootTotal == 0 and total > 0 then
  150. TSM.massOpening:SetText(L["Waiting..."])
  151. end
  152.  
  153. TSM:RegisterEvent("UI_ERROR_MESSAGE")
  154. TSM.massOpening:Disable()
  155. TSM.moneyCollected = 0
  156. TSM:AutoLoot()
  157. end
  158.  
  159. function TSM:AutoLoot()
  160. -- BeanCounter hides the close button while it is running.
  161. local BeanCounterActive = not InboxCloseButton:IsVisible()
  162. if BeanCounterActive then
  163. delay.mailTimer = MAIL_WAIT_TIME
  164. delay:Show()
  165. return
  166. end
  167.  
  168. -- Already looted everything after the invalid indexes we had, so fail it
  169. if LOOT_MAIL_INDEX > 1 and LOOT_MAIL_INDEX > GetInboxNumItems() then
  170. if resetIndex then
  171. TSM:StopAutoLooting(true)
  172. else
  173. resetIndex = true
  174. LOOT_MAIL_INDEX = 1
  175. TSM:AutoLoot()
  176. end
  177. return
  178. end
  179.  
  180. local money, cod, _, items, _, _, _, _, isGM = select(5, GetInboxHeaderInfo(LOOT_MAIL_INDEX))
  181. if not isGM and (not cod or cod <= 0) and ((money and money > 0) or (items and items > 0)) then
  182. delay.mailTimer = nil
  183. TSM.massOpening:SetText(L["Opening..."])
  184. if money > 0 then
  185. TSM.moneyCollected = TSM.moneyCollected + money
  186. end
  187. AutoLootMailItem(LOOT_MAIL_INDEX)
  188. -- Can't grab the first mail, but we have a second so increase it and try again
  189. elseif GetInboxNumItems() > LOOT_MAIL_INDEX then
  190. LOOT_MAIL_INDEX = LOOT_MAIL_INDEX + 1
  191. TSM:AutoLoot()
  192. end
  193. end
  194.  
  195.  
  196. local GOLD_TEXT = "|cffffd700g|r"
  197. local SILVER_TEXT = "|cffc7c7cfs|r"
  198. local COPPER_TEXT = "|cffeda55fc|r"
  199.  
  200. function TSM:FormatTextMoney(money)
  201. local money = tonumber(money)
  202. if not money then return end
  203. local gold = math.floor(money / COPPER_PER_GOLD)
  204. local silver = math.floor((money - (gold * COPPER_PER_GOLD)) / COPPER_PER_SILVER)
  205. local copper = math.floor(money%COPPER_PER_SILVER)
  206. local text = ""
  207.  
  208. -- Add gold
  209. if gold > 0 then
  210. text = string.format("%d%s ", gold, GOLD_TEXT)
  211. end
  212.  
  213. -- Add silver
  214. if silver > 0 then
  215. text = string.format("%s%d%s ", text, silver, SILVER_TEXT)
  216. end
  217.  
  218. -- Add copper if we have no silver/gold found, or if we actually have copper
  219. if copper > 0 then
  220. text = string.format("%s%d%s ", text, copper, COPPER_TEXT)
  221. end
  222. return text:trim()
  223. end
  224.  
  225. function TSM:StopAutoLooting(failed)
  226. if failed then
  227. TSM:Print(L["Cannot finish auto looting, inventory is full or too many unique items."])
  228. end
  229.  
  230. resetIndex = nil
  231. autoLootTotal = nil
  232. LOOT_MAIL_INDEX = 1
  233.  
  234. TSM:UnregisterEvent("UI_ERROR_MESSAGE")
  235. TSM.massOpening:SetText(L["Open All"])
  236. TSM.massOpening:Enable()
  237.  
  238. --Tell user how much money has been collected if they don't have it turned off in TradeSkillMaster_Mailing options
  239. if TSM.moneyCollected and TSM.moneyCollected > 0 and (not TSM.db.profile.dontDisplayMoneyCollected) then
  240. TSM:Print(TSM:FormatTextMoney(TSM.moneyCollected) .. " Collected")
  241. TSM.moneyCollected = 0
  242. end
  243. end
  244.  
  245. function TSM:UI_ERROR_MESSAGE(event, msg)
  246. if msg == ERR_INV_FULL or msg == ERR_ITEM_MAX_COUNT then
  247. -- Try the next index in case we can still loot more such as in the case of glyphs
  248. LOOT_MAIL_INDEX = LOOT_MAIL_INDEX + 1
  249.  
  250. -- If we've exhausted all slots, but we still have <50 and more mail pending, wait until new data comes and keep looting it
  251. local current, total = GetInboxNumItems()
  252. if( LOOT_MAIL_INDEX > current ) then
  253. if( LOOT_MAIL_INDEX > total and total <= 50 ) then
  254. TSM:StopAutoLooting(true)
  255. else
  256. TSM.massOpening:SetText(L["Waiting..."])
  257. end
  258. return
  259. end
  260.  
  261. delay.mailTimer = MAIL_WAIT_TIME
  262. delay:Show()
  263. end
  264. end
  265.  
  266. function TSM:MAIL_INBOX_UPDATE()
  267. local current, total = GetInboxNumItems()
  268. -- Yay nothing else to loot, so nothing else to update the cache for!
  269. if cacheFrame.endTime and current == total and lastTotal ~= total then
  270. cacheFrame.endTime = nil
  271. cacheFrame:Hide()
  272. -- Start a timer since we're over the limit of 50 items before waiting for it to recache
  273. elseif (cacheFrame.endTime and current >= 50 and lastTotal ~= total) or (current >= 50 and allowTimerStart) then
  274. resetIndex = nil
  275. allowTimerStart = nil
  276. waitingForData = nil
  277. lastTotal = total
  278. cacheFrame.endTime = GetTime() + 60
  279. cacheFrame:Show()
  280. end
  281.  
  282. -- The last item we setup to auto loot is finished, time for the next one
  283. if not TSM.isAutoMailing and not TSM.massOpening:IsEnabled() and autoLootTotal ~= current then
  284. autoLootTotal = GetInboxNumItems()
  285.  
  286. -- If we're auto checking mail when new data is available, will wait and continue auto looting, otherwise we just stop now
  287. if TSM.db.profile.autoCheck and current == 0 and total > 0 then
  288. TSM.massOpening:SetText(L["Waiting..."])
  289. elseif current == 0 and (not TSM.db.profile.autoCheck or total == 0) then
  290. TSM:StopAutoLooting()
  291. else
  292. TSM:AutoLoot()
  293. end
  294. end
  295.  
  296. if total > 0 then
  297. TSM.totalMail:SetFormattedText(L["%d mail"], total)
  298. else
  299. TSM.totalMail:SetText(nil)
  300. end
  301. end
  302.  
  303. function TSM:MAIL_CLOSED()
  304. resetIndex = nil
  305. allowTimerStart = true
  306. waitingForData = nil
  307. TSM:StopAutoLooting()
  308. end
  309.  
  310. function TSM:SetBFlag()
  311. if TSM.AutoMail.button and TSM.AutoMail.button:GetName() then
  312. TSM.bFlag = true
  313. end
  314. end
Add Comment
Please, Sign In to add comment