Quantcast

Testing out a new updated way to grab the mail.

Xruptor [10-22-16 - 12:44]
Testing out a new updated way to grab the mail.
Filename
xanAutoMail.lua
xanAutoMail.toc
diff --git a/xanAutoMail.lua b/xanAutoMail.lua
index 258e263..fe38b58 100644
--- a/xanAutoMail.lua
+++ b/xanAutoMail.lua
@@ -211,8 +211,7 @@ end

 local delayCount = {}
 local moneyCount = 0
-local errorCount = 0
-local mailCount = 0
+local skipCount = 0
 local errorCheckCount = 0
 local currentStatus = "STOP"

@@ -246,6 +245,7 @@ local function colorMoneyText(value)
 end

 local function freeSpace()
+	Debug("freeSpace")
 	local totalFree = 0
 	for i=0, NUM_BAG_SLOTS do
 		local numberOfFreeSlots = GetContainerNumFreeSlots(i)
@@ -264,12 +264,14 @@ local function inboxFullCheck()
 			inboxAllButton.movedBottom = true
 			inboxInfoText:ClearAllPoints()
 			inboxInfoText:SetPoint("TOPLEFT", inboxAllButton, "TOPRIGHT", 5, -5)
+			Debug("inboxFullCheck", "moved bottom")
 		elseif (( nTotal < nItem) or not InboxTooMuchMail:IsVisible()) and inboxAllButton.movedBottom then
 			inboxAllButton.movedBottom = nil
 			inboxAllButton:ClearAllPoints()
 			inboxAllButton:SetPoint("CENTER", InboxFrame, "TOP", -80, -55)
 			inboxInfoText:ClearAllPoints()
 			inboxInfoText:SetPoint("TOPLEFT", inboxAllButton, "TOPRIGHT", 5, -5)
+			Debug("inboxFullCheck", "not moved")
 		end
 	end
 end
@@ -292,10 +294,15 @@ function xanAutoMail:Delay(name, duration, callbackFunction, force)
 	if not force and currentStatus == "STOP" then return end
 	for k, q in ipairs(delayCount) do
 		if q.name == name then
-			--don't run the same delay more than once
+			--don't run the same delay more than once, we can however refresh it
+			q.duration = duration
+			q.endTime = (GetTime()+duration)
+			q.callbackFunction = callbackFunction
+			Debug("UPDATED", q.name, q.duration, q.endTime)
 			return
 		end
 	end
+	Debug("ADDED", name, duration, (GetTime()+duration))
 	tinsert(delayCount, {name=name, duration=duration, endTime=(GetTime()+duration), callbackFunction=callbackFunction})
 end

@@ -313,6 +320,7 @@ function xanAutoMail:UpdateInfoText()
 end

 function xanAutoMail:MAIL_SHOW()
+	Debug("MAIL_SHOW")
 	inboxFullCheck()
 	CheckInbox()
 	inboxInfoText:SetText("Waiting...")
@@ -320,53 +328,58 @@ function xanAutoMail:MAIL_SHOW()
 end

 function xanAutoMail:MAIL_CLOSED()
-	xanAutoMail:StopMailGrab(true)
+	Debug("MAIL_CLOSED")
+	xanAutoMail:StopMailGrab(true, 1)
 end

 function xanAutoMail:MAIL_INBOX_UPDATE()
 	xanAutoMail:Delay("mailInfoText", 0.5, xanAutoMail.UpdateInfoText, true)
 	if currentStatus == "STOP" then return end
-	xanAutoMail:Delay("mailCheckMailUpdate", 0.2, xanAutoMail.CheckMailUpdate)
+	--keep increasing the delay before the next mail grab until all items are taken from current opened mail
+	xanAutoMail:Delay("mailGrabNextItem", 0.5, xanAutoMail.GrabNextMailItem)
+	Debug("MAIL_INBOX_UPDATE", "mailGrabNextItem")
 end

-function xanAutoMail:UI_ERROR_MESSAGE(event, arg1)
+function xanAutoMail:UI_ERROR_MESSAGE(event, num, msg)
+
 	if currentStatus == "STOP" then return end
 	local stopMailGrab = false

-	if arg1 == ERR_MAIL_DATABASE_ERROR then
+	if msg == ERR_MAIL_DATABASE_ERROR then
 		DEFAULT_CHAT_FRAME:AddMessage("xanAutoMail: (ERROR) There was a mailbox database error from the server.")
 		stopMailGrab = true
-	elseif arg1 == ERR_INV_FULL then
+	elseif msg == ERR_INV_FULL then
 		DEFAULT_CHAT_FRAME:AddMessage("xanAutoMail: (ERROR) Your inventory is full.")
 		stopMailGrab = true
-	elseif arg1 == ERR_ITEM_MAX_COUNT then
+	elseif msg == ERR_ITEM_MAX_COUNT then
 		DEFAULT_CHAT_FRAME:AddMessage("xanAutoMail: (ERROR) Cannot loot anymore unique items from Mailbox.")
+		DEFAULT_CHAT_FRAME:AddMessage("xanAutoMail: Please the delete item(s) from the Mailbox before trying again.")
+		stopMailGrab = true
 	end
-
+
 	if stopMailGrab then
-		xanAutoMail:StopMailGrab()
+		xanAutoMail:StopMailGrab(false, 2)
 		return
 	end
 end

---this function makes the delay wait until the inbox has decreased in size when we loot something
---if we don't do this then the it will continously try to autoloot an item from the same mail
---this leads to the ERR_ITEM_NOT_FOUND error.
-function xanAutoMail:CheckMailUpdate()
-	--check to see if our inbox has decreased by one since we last autolooted
-	local nItem, nTotal = GetInboxNumItems()
-	if nItem <= mailCount - 1 or nTotal == 0 then
-		xanAutoMail:Delay("mailGrabNextItem", 0.2, xanAutoMail.GrabNextMailItem)
-	else
-		xanAutoMail:Delay("mailCheckMailUpdate", 0.2, xanAutoMail.CheckMailUpdate)
-	end
+function xanAutoMail:IsMailItemEmpty(index)
+	local packageIcon, stationeryIcon, sender, subject, money, COD, daysLeft, numItems, wasRead, wasReturned, textCreated, canReply, isGM, itemQuantity = GetInboxHeaderInfo(index)
+	if isGM then return false end
+	if canReply then return false end
+	if numItems then return false end
+	if COD > 0 then return false end
+	if money > 0 then return false end
+	if wasReturned then return false end
+	return true
 end

 function xanAutoMail:GrabNextMailItem()
 	if currentStatus == "STOP" then return end
-
+	Debug("GrabNextMailItem", "BEGIN")
 	--do inbox check
 	if currentStatus == "CHECK" then
+		Debug("GrabNextMailItem", currentStatus)
 		CheckInbox()
 		currentStatus = "SKIPCHECK"
 		xanAutoMail:Delay("mailGrabNextItem", 0.5, xanAutoMail.GrabNextMailItem)
@@ -374,70 +387,79 @@ function xanAutoMail:GrabNextMailItem()
 	end

 	local nItem, nTotal = GetInboxNumItems()
+	Debug("GrabNextMailItem", "START", nItem, nTotal)

 	xanAutoMail:UpdateInfoText()

-	--check to see if the last messages were read or if we have nothing to work with, or we have done more than 10 error checks
-	if nTotal == errorCount or nTotal <= 0 or errorCheckCount > 10 then
-		xanAutoMail:StopMailGrab()
+	--check to see if the last messages were read or if we have nothing to work with, or we have done more than 50 error checks, which is roughly about 10 to 11 seconds
+	if nTotal <= 0 or skipCount >= nTotal or errorCheckCount > 50 then
+		if errorCheckCount > 50 then
+			DEFAULT_CHAT_FRAME:AddMessage("xanAutoMail: (ERROR) Mailbox latency error. (Try Again)")
+		end
+		Debug("GrabNextMailItem", 2, nItem, nTotal, skipCount, errorCheckCount)
+		xanAutoMail:StopMailGrab(false, 4)
 		return
 	elseif freeSpace() < 1 then
-		xanAutoMail:StopMailGrab()
+		xanAutoMail:StopMailGrab(false, 5)
 		DEFAULT_CHAT_FRAME:AddMessage("xanAutoMail: (ERROR) Your inventory is full.")
 		return
-	elseif nItem <= 0 and nTotal > 0 then
-		--if we still have something to work with then fire in another 60 seconds
+	elseif (nItem <= 0 or skipCount >= nItem) and nTotal > 0 then
+		--if we still have something to work with then fire in another 45 seconds
 		currentStatus = "CHECK"
 		errorCheckCount = 0
 		xanAutoMail:Delay("mailInboxCheck", 45, xanAutoMail.GrabNextMailItem)
 		inboxInfoText:SetText("Waiting 45 seconds")
 		DEFAULT_CHAT_FRAME:AddMessage("xanAutoMail: Waiting 45 seconds for next mail batch.")
+		Debug("GrabNextMailItem", 3, currentStatus, nItem, nTotal, skipCount)
 		return
 	end

-	errorCount = 0
-	mailCount = 0
+	skipCount = 0 --reset

-	for i = 1, nItem do
-		local _, _, _, _, money, COD, _, numItems, wasRead, _, _, _, isGM = GetInboxHeaderInfo(i)
+	for mIndex = nItem, 1, -1 do
+		local _, _, _, _, money, COD, _, numItems, wasRead, _, _, _, isGM = GetInboxHeaderInfo(mIndex)

 		if money > 0 or (numItems and numItems > 0) and COD <= 0 and not isGM then
-			if wasRead then
-				errorCount = errorCount + 1
-			else
-				if money > 0 then moneyCount = moneyCount + money end
-				mailCount = GetInboxNumItems()
-				AutoLootMailItem(i)
-				--we looted something so lets wait for next update
-				return
-			end
-		else
-			errorCount = errorCount + 1
+			if money > 0 then moneyCount = moneyCount + money end
+			TakeInboxMoney(mIndex)
+			AutoLootMailItem(mIndex)
+			--we looted something so lets wait for next update
+			Debug("GrabNextMailItem", 5, money, mIndex)
+			xanAutoMail:Delay("mailGrabNextItem", 0.5, xanAutoMail.GrabNextMailItem) --just in case
+			return
+		end
+		Debug("GrabNextMailItem", "LOGIC_ELSE", money, COD, numItems, wasRead, isGM)
+		if xanAutoMail:IsMailItemEmpty(mIndex) then
+			Debug("GrabNextMailItem", "DeleteInboxItem", mIndex)
+			DeleteInboxItem(mIndex)
+			xanAutoMail:Delay("mailGrabNextItem", 0.5, xanAutoMail.GrabNextMailItem) --just in case
+			return
 		end
+
+		skipCount = skipCount + 1
 	end

-	--if absolutely nothing happened then exit the loop just in case
-	if errorCount <= 0 then
-		xanAutoMail:StopMailGrab()
-		return
-	else
-		--there was an error so lets try to grab again just in case our total = total errors (which could mean all mail has been read)
-		xanAutoMail:Delay("mailGrabNextItem", 0.2, xanAutoMail.GrabNextMailItem)
-		errorCheckCount = errorCheckCount + 1 --this is another emergency stop, don't do more than 10 error checks
-	end
+	Debug("GrabNextMailItem", "skipCount", skipCount)
+
+	xanAutoMail:Delay("mailGrabNextItem", 0.2, xanAutoMail.GrabNextMailItem)
+	errorCheckCount = errorCheckCount + 1
+	Debug("errorCheckCount", errorCheckCount)
+
 end

 function xanAutoMail:StartMailGrab()
+	Debug("StartMailGrab")
 	if GetInboxNumItems() == 0 then return end
 	currentStatus = "START"
 	inboxAllButton:Disable()
 	moneyCount = 0
-	errorCount = 0
+	skipCount = 0
 	errorCheckCount = 0
 	xanAutoMail:Delay("mailGrabNextItem", 0.2, xanAutoMail.GrabNextMailItem)
 end

-function xanAutoMail:StopMailGrab(force)
+function xanAutoMail:StopMailGrab(force, flag)
+	Debug("StopMailGrab", flag)
 	currentStatus = "STOP"
 	delayCount = {}
 	inboxAllButton:Enable()
diff --git a/xanAutoMail.toc b/xanAutoMail.toc
index a909399..4533f07 100644
--- a/xanAutoMail.toc
+++ b/xanAutoMail.toc
@@ -2,7 +2,7 @@
 ## Title: xanAutoMail
 ## Notes: Expands blizzards automatic name generation for sending Mail to include people outside the guild.
 ## Author: Xruptor
-## Version: 2.3
+## Version: 2.4
 ## SavedVariables: xanAutoMailDB

 xanAutoMail.lua