- Improved search algorithm to avoid trading house error messages

Yaron Kfir [07-05-14 - 02:24]
 - Improved search algorithm to avoid trading house error messages
 - Properly changing guilds between searches to avoid error messages
Filename
Changelog
PriceTracker.lua
PriceTracker.txt
diff --git a/Changelog b/Changelog
index a9fe3be..62546ca 100644
--- a/Changelog
+++ b/Changelog
@@ -1,5 +1,9 @@
 Changelog

+v1.6
+ - Improved search algorithm to avoid trading house error messages
+ - Properly changing guilds between searches to avoid error messages
+
 v1.5
  - Support API version 100007
  - Using LibAddonMenu 2.0
diff --git a/PriceTracker.lua b/PriceTracker.lua
index e38c872..dd681e1 100644
--- a/PriceTracker.lua
+++ b/PriceTracker.lua
@@ -23,6 +23,7 @@ function PriceTracker:OnLoad(eventCode, addOnName)
 	if(addOnName ~= "PriceTracker") then return end

 	EVENT_MANAGER:RegisterForEvent("OnSearchResultsReceived", EVENT_TRADING_HOUSE_SEARCH_RESULTS_RECEIVED, function(...) self:OnSearchResultsReceived(...) end)
+	EVENT_MANAGER:RegisterForEvent("OnSearchResultsError", EVENT_TRADING_HOUSE_ERROR, function(...) self:OnSearchResultsError(...) end)
 	EVENT_MANAGER:RegisterForEvent("OnTradingHouseClosed", EVENT_CLOSE_TRADING_HOUSE, function(...) self:OnTradingHouseClosed(...) end)

 	LINK_HANDLER:RegisterCallback(LINK_HANDLER.LINK_CLICKED_EVENT, PriceTracker.OnLinkClicked, self)
@@ -152,11 +153,13 @@ function PriceTracker:OnScanPrices()
 	self.isSearching = true
 	self.numOfGuilds = GetNumTradingHouseGuilds()
 	self.currentGuild = 0
+	self.currentPage = 0
 	while not CanSellOnTradingHouse(self.currentGuild) and self.currentGuild < self.numOfGuilds do
 		self.currentGuild = self.currentGuild + 1
 	end

-	zo_callLater(function() ExecuteTradingHouseSearch(0, TRADING_HOUSE_SORT_SALE_PRICE, true) end, self.queryDelay)
+	SelectTradingHouseGuildId(self.currentGuild)
+	zo_callLater(function() ExecuteTradingHouseSearch(0, TRADING_HOUSE_SORT_SALE_PRICE, true) end, GetTradingHouseCooldownRemaining() + 1000)
 end

 function PriceTracker:OnSearchResultsReceived(eventId, guildId, numItemsOnPage, currentPage, hasMorePages)
@@ -166,19 +169,30 @@ function PriceTracker:OnSearchResultsReceived(eventId, guildId, numItemsOnPage,
 		self:AddItem(GetTradingHouseSearchResultItemInfo(i))
 	end

+	self.currentPage = currentPage
+
 	if hasMorePages then
-		zo_callLater(function() ExecuteTradingHouseSearch(currentPage + 1, TRADING_HOUSE_SORT_SALE_PRICE, true) end, self.queryDelay)
+		zo_callLater(function() ExecuteTradingHouseSearch(currentPage + 1, TRADING_HOUSE_SORT_SALE_PRICE, true) end, GetTradingHouseCooldownRemaining() + 1000)
 	else
 		if self.currentGuild < self.numOfGuilds then
 			self.currentGuild = self.currentGuild + 1
-			SelectTradingHouseGuildId(self.currentGuild)
-			zo_callLater(function() ExecuteTradingHouseSearch(0, TRADING_HOUSE_SORT_SALE_PRICE, true) end, self.queryDelay)
+			while not CanSellOnTradingHouse(self.currentGuild) and self.currentGuild < self.numOfGuilds do
+				self.currentGuild = self.currentGuild + 1
+			end
+
+			zo_callLater(function() SelectTradingHouseGuildId(self.currentGuild) end, self.queryDelay)
+			zo_callLater(function() ExecuteTradingHouseSearch(0, TRADING_HOUSE_SORT_SALE_PRICE, true) end, GetTradingHouseCooldownRemaining() + 1000)
 		else
 			self:OnTradingHouseClosed()
 		end
 	end
 end

+function PriceTracker:OnSearchResultsError(eventCode, errorCode)
+	self:OnSearchResultsReceived(eventCode, self.currentGuild, 0, self.currentPage, false)
+	zo_callLater(function() ExecuteTradingHouseSearch(self.currentPage, TRADING_HOUSE_SORT_SALE_PRICE, true) end, GetTradingHouseCooldownRemaining() + 1000)
+end
+
 function PriceTracker:OnTradingHouseClosed()
 	self.isSearching = false
 	self.button:SetEnabled(true)
diff --git a/PriceTracker.txt b/PriceTracker.txt
index f2d0d23..99edc24 100644
--- a/PriceTracker.txt
+++ b/PriceTracker.txt
@@ -1,6 +1,6 @@
 ## Title: PriceTracker
 ## Author: Barvazon
-## Version: 1.5
+## Version: 1.6
 ## SavedVariables: PriceTrackerSettings
 ## APIVersion: 100007
 ## OptionalDependsOn: LibAddonMenu-2.0