Added min / max and number of times seen in store

Yaron Kfir [05-24-14 - 20:58]
Added min / max and number of times seen in store
Filename
MathUtils.lua
PriceTracker.lua
diff --git a/MathUtils.lua b/MathUtils.lua
index bdc8902..299d694 100644
--- a/MathUtils.lua
+++ b/MathUtils.lua
@@ -16,10 +16,20 @@ function MathUtils:WeightedAverage(itemTable)
 end

 function MathUtils:Max(itemTable)
-	local price = 0
-	for item in itemTable do
-		if item.price > price then
-			price = item.price
+	local price = itemTable[1].purchasePrice
+	for i = 1, #itemTable do
+		if itemTable[i].purchasePrice > price then
+			price = itemTable[i].purchasePrice
+		end
+	end
+	return price
+end
+
+function MathUtils:Min(itemTable)
+	local price = itemTable[1].purchasePrice
+	for i = 1, #itemTable do
+		if itemTable[i].purchasePrice < price then
+			price = itemTable[i].purchasePrice
 		end
 	end
 	return price
diff --git a/PriceTracker.lua b/PriceTracker.lua
index 2e29ddd..75bcb0e 100644
--- a/PriceTracker.lua
+++ b/PriceTracker.lua
@@ -32,12 +32,12 @@ function PriceTracker:OnLoad(eventCode, addOnName)
 	}

 	-- Load saved settings
-	PriceTracker.settings = ZO_SavedVars:NewAccountWide("PriceTrackerSettings", PriceTracker.settingsVersion, nil, defaults)
+	self.settings = ZO_SavedVars:NewAccountWide("PriceTrackerSettings", self.settingsVersion, nil, defaults)

 	-- Create a button in the trading house window
-	PriceTracker.button = PriceTrackerControlButton
-	PriceTracker.button:SetParent(ZO_TradingHouseLeftPaneBrowseItemsCommon)
-	PriceTracker.button:SetWidth(ZO_TradingHouseLeftPaneBrowseItemsCommonQuality:GetWidth())
+	self.button = PriceTrackerControlButton
+	self.button:SetParent(ZO_TradingHouseLeftPaneBrowseItemsCommon)
+	self.button:SetWidth(ZO_TradingHouseLeftPaneBrowseItemsCommonQuality:GetWidth())

 end

@@ -49,7 +49,7 @@ function PriceTracker:CommandHandler(text)
 	end

 	if text == "reset" or text == "clear" then
-		PriceTracker.settings.itemList = {}
+		self.settings.itemList = {}
 		return
 	end
 end
@@ -62,13 +62,18 @@ function PriceTracker:ShowHelp()
 end

 function PriceTracker:OnUpdateTooltip(item)
-	if not item or not item.dataEntry or not item.dataEntry.data or PriceTracker.selectedItem == item then
+	if not item or not item.dataEntry or not item.dataEntry.data or self.selectedItem == item then
 		return
 	end

-	PriceTracker.selectedItem = item
+	self.selectedItem = item

-	local price = self:SuggestPrice(item.dataEntry.data.name)
+	local matches = self:GetMatches(item.dataEntry.data.name)
+	if not matches then
+		return
+	end
+
+	local price = self:SuggestPrice(matches)
 	if not price then
 		return
 	end
@@ -76,35 +81,39 @@ function PriceTracker:OnUpdateTooltip(item)
 	ZO_Tooltip_AddDivider(ItemTooltip)
 	ItemTooltip:AddLine("Price Tracker", "ZoFontHeader2")
 	local r, g, b = ZO_TOOLTIP_DEFAULT_COLOR:UnpackRGB()
+	local stackCount = item.dataEntry.data.stackCount or item.dataEntry.data.stack
 	ItemTooltip:AddLine("Suggested Price: |r", "ZoFontGame", r, g, b, TOPLEFT, MODIFY_TEXT_TYPE_NONE, LEFT, false)
 	-- ItemTooltip:AddLine(self:FormatTooltipLine(price, item.dataEntry.data.stackCount or item.dataEntry.data.stack), "ZoFontGame", r, g, b, LEFT, MODIFY_TEXT_TYPE_NONE, CENTER, false)
-	ItemTooltip:AddLine(self:FormatTooltipLine("Item price: ", price, item.dataEntry.data.stackCount or item.dataEntry.data.stack), "ZoFontGame", r, g, b, LEFT, MODIFY_TEXT_TYPE_NONE, CENTER, false)
-	ItemTooltip:AddLine(self:FormatTooltipLine("Stack price: ", price * item.dataEntry.data.stackCount or item.dataEntry.data.stack), "ZoFontGame", r, g, b, LEFT, MODIFY_TEXT_TYPE_NONE, CENTER, false)
+	ItemTooltip:AddLine(self:FormatTooltipLine("Item price: ", price, stackCount), "ZoFontGame", r, g, b, CENTER, MODIFY_TEXT_TYPE_NONE, CENTER, false)
+	if stackCount > 1 then
+		ItemTooltip:AddLine(self:FormatTooltipLine("Stack price: ", price * stackCount), "ZoFontGame", r, g, b, CENTER, MODIFY_TEXT_TYPE_NONE, CENTER, false)
+	end
+	ItemTooltip:AddLine(self:FormatTooltipLine("Min / Max: ", self.mathUtils:Min(matches) .. " / " .. self.mathUtils:Max(matches)), "ZoFontGame", r, g, b, CENTER, MODIFY_TEXT_TYPE_NONE, CENTER, false)
+	ItemTooltip:AddLine("Seen " .. #matches .. " times", "ZoFontGame", r, g, b, CENTER, MODIFY_TEXT_TYPE_NONE, CENTER, false)
 end

 function PriceTracker:OnHideTooltip()
-	PriceTracker.selectedItem = nil
+	self.selectedItem = nil
 end

 function PriceTracker:OnScanPrices()
-	if PriceTracker.isSearching then
+	if self.isSearching then
 		return
 	end

-	PriceTracker.button:SetEnabled(false)
-	PriceTracker.isSearching = true
-	PriceTracker.numOfGuilds = GetNumTradingHouseGuilds()
-	PriceTracker.currentGuild = 0
-	-- PriceTracker.currentPage = 0
-	while not CanSellOnTradingHouse(PriceTracker.currentGuild) and PriceTracker.currentGuild < PriceTracker.numOfGuilds do
-		PriceTracker.currentGuild = PriceTracker.currentGuild + 1
+	self.button:SetEnabled(false)
+	self.isSearching = true
+	self.numOfGuilds = GetNumTradingHouseGuilds()
+	self.currentGuild = 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, PriceTracker.queryDelay)
+	zo_callLater(function() ExecuteTradingHouseSearch(0, TRADING_HOUSE_SORT_SALE_PRICE, true) end, self.queryDelay)
 end

 function PriceTracker:OnSearchResultsReceived(eventId, guildId, numItemsOnPage, currentPage, hasMorePages)
-	if not PriceTracker.isSearching then
+	if not self.isSearching then
 		return
 	end

@@ -113,12 +122,12 @@ function PriceTracker:OnSearchResultsReceived(eventId, guildId, numItemsOnPage,
 	end

 	if hasMorePages then
-		zo_callLater(function() ExecuteTradingHouseSearch(currentPage + 1, TRADING_HOUSE_SORT_SALE_PRICE, true) end, PriceTracker.queryDelay)
+		zo_callLater(function() ExecuteTradingHouseSearch(currentPage + 1, TRADING_HOUSE_SORT_SALE_PRICE, true) end, self.queryDelay)
 	else
-		if PriceTracker.currentGuild < PriceTracker.numOfGuilds then
-			PriceTracker.currentGuild = PriceTracker.currentGuild + 1
-			SelectTradingHouseGuildId(PriceTracker.currentGuild)
-			zo_callLater(function() ExecuteTradingHouseSearch(0, TRADING_HOUSE_SORT_SALE_PRICE, true) end, PriceTracker.queryDelay)
+		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)
 		else
 			self:OnTradingHouseClosed()
 		end
@@ -126,8 +135,8 @@ function PriceTracker:OnSearchResultsReceived(eventId, guildId, numItemsOnPage,
 end

 function PriceTracker:OnTradingHouseClosed()
-	PriceTracker.isSearching = false
-	PriceTracker.button:SetEnabled(true)
+	self.isSearching = false
+	self.button:SetEnabled(true)
 end

 function PriceTracker:AddItem(icon, itemName, quality, stackCount, sellerName, timeRemaining, purchasePrice)
@@ -140,44 +149,48 @@ function PriceTracker:AddItem(icon, itemName, quality, stackCount, sellerName, t
 	item.stackCount = stackCount
 	item.sellerName = sellerName
 	item.purchasePrice = purchasePrice
-	item.guildId = PriceTracker.currentGuild
+	item.guildId = self.currentGuild
 	item.guildName = GetGuildName(item.guildId)

-	if not PriceTracker.settings.itemList[item.normalizedName] then
-		PriceTracker.settings.itemList[item.normalizedName] = {}
+	if not self.settings.itemList[item.normalizedName] then
+		self.settings.itemList[item.normalizedName] = {}
 	end

 	-- Do not add items that are already in the database
-	if not PriceTracker.settings.itemList[item.normalizedName][item.expiry] then
-		PriceTracker.settings.itemList[item.normalizedName][item.expiry] = item
+	if not self.settings.itemList[item.normalizedName][item.expiry] then
+		self.settings.itemList[item.normalizedName][item.expiry] = item
 	end
 end

--- TODO: Base calculation on user preference
-function PriceTracker:SuggestPrice(itemName)
+function PriceTracker:GetMatches(itemName)
 	local normalizedName = self:NormalizeName(itemName)
-	if not PriceTracker.settings.itemList or not PriceTracker.settings.itemList[normalizedName] then
+	if not self.settings.itemList or not self.settings.itemList[normalizedName] then
 		return nil
 	end

-	local index = next(PriceTracker.settings.itemList[normalizedName])
+	local index = next(self.settings.itemList[normalizedName])
 	if index == nil then
 		return nil
 	end

 	local matches = {}
 	while index do
-		table.insert(matches, PriceTracker.settings.itemList[normalizedName][index])
-		index = next(PriceTracker.settings.itemList[normalizedName], index)
+		table.insert(matches, self.settings.itemList[normalizedName][index])
+		index = next(self.settings.itemList[normalizedName], index)
 	end

-	PriceTracker.matches = matches
-	return PriceTracker.mathUtils:WeightedAverage(matches)
+	self.matches = matches
+	return matches
+end
+
+
+-- TODO: Base calculation on user preference
+function PriceTracker:SuggestPrice(matches)
+	return self.mathUtils:WeightedAverage(matches)
 end

 function PriceTracker:FormatTooltipLine(title, price, stackCount)
-	-- return string.format("%7s%s %-10s %7s%s %-10s", price, zo_iconFormat(PriceTracker.icons.gold, 16, 16), "(each)", price * stackCount, zo_iconFormat(PriceTracker.icons.gold, 16, 16), "(stack)")
-	return string.format("%-30s %7s%s", title, price, zo_iconFormat(PriceTracker.icons.gold, 16, 16))
+	return string.format("%-30s %7s%s", title, price, zo_iconFormat(self.icons.gold, 16, 16))
 end

 function PriceTracker:NormalizeName(name)