v0.3

Yaron Kfir [05-25-14 - 20:30]
v0.3
 - Added settings menu
 - Providing three ways to calculate the suggested price: weighted average (default), median, and mode (most popular value)
 - Number of times item seen in stores can be turned off
 - Min / max can be turned off
Filename
Changelog
MathUtils.lua
PriceTracker.lua
PriceTracker.txt
luac.out
diff --git a/Changelog b/Changelog
index 5319622..5d3b669 100644
--- a/Changelog
+++ b/Changelog
@@ -7,3 +7,10 @@ v0.2
  - Suggested prices per item and per stack are displayed in separate lines
  - Added number of times seen in guild stores
  - Minor cosmetic and code fixes
+
+v0.3
+ - Added settings menu
+ - Providing three ways to calculate the suggested price: weighted average (default), median, and mode (most popular value)
+ - Number of times item seen in stores can be turned off
+ - Min / max can be turned off
+
\ No newline at end of file
diff --git a/MathUtils.lua b/MathUtils.lua
index 299d694..4abc55a 100644
--- a/MathUtils.lua
+++ b/MathUtils.lua
@@ -5,6 +5,15 @@ end
 local MathUtils = {}
 PriceTracker.mathUtils = MathUtils

+function MathUtils:GetSortedPriceTable(itemTable)
+	local prices = {}
+	for i = 1, #itemTable do
+		table.insert(prices, math.floor(itemTable[i].purchasePrice / itemTable[i].stackCount))
+	end
+	table.sort(prices)
+	return prices
+end
+
 function MathUtils:WeightedAverage(itemTable)
 	local sum = 0
 	local weight = 0
@@ -15,21 +24,54 @@ function MathUtils:WeightedAverage(itemTable)
 	return math.floor(sum / weight)
 end

+function MathUtils:Median(itemTable)
+	local prices = self:GetSortedPriceTable(itemTable)
+	local index = (#prices + 1) / 2
+	if (#prices / 2) == (math.floor(#prices / 2)) then
+		return math.floor((prices[index] + prices[index + 1]) / 2)
+	else
+		return prices[index]
+	end
+end
+
+function MathUtils:Mode(itemTable)
+	local prices = self:GetSortedPriceTable(itemTable)
+	local number = prices[1]
+	local mode = number	local count = 1
+	local countMode = 1
+
+	for i = 2, #prices do
+		if prices[i] == number then
+			count = count + 1
+		else
+			if count > countMode then
+				countMode = count
+				mode = number
+			end
+			count = 1
+			number = prices[i]
+		end
+	end
+	return mode
+end
+
 function MathUtils:Max(itemTable)
-	local price = itemTable[1].purchasePrice
+	local price = math.floor(itemTable[1].purchasePrice / itemTable[1].stackCount)
 	for i = 1, #itemTable do
-		if itemTable[i].purchasePrice > price then
-			price = itemTable[i].purchasePrice
+		local newPrice = math.floor(itemTable[i].purchasePrice / itemTable[i].stackCount)
+		if newPrice > price then
+			price = newPrice
 		end
 	end
 	return price
 end

 function MathUtils:Min(itemTable)
-	local price = itemTable[1].purchasePrice
+	local price = math.floor(itemTable[1].purchasePrice / itemTable[1].stackCount)
 	for i = 1, #itemTable do
-		if itemTable[i].purchasePrice < price then
-			price = itemTable[i].purchasePrice
+		local newPrice = math.floor(itemTable[i].purchasePrice / itemTable[i].stackCount)
+		if newPrice < price then
+			price = newPrice
 		end
 	end
 	return price
diff --git a/PriceTracker.lua b/PriceTracker.lua
index 75bcb0e..f58c6c1 100644
--- a/PriceTracker.lua
+++ b/PriceTracker.lua
@@ -5,7 +5,7 @@
 PriceTracker = {
 	queryDelay = 3000,
 	isSearching = false,
-	settingsVersion = 0.1,
+	settingsVersion = 0.2,
 	icons = {
 		gold = "EsoUI/Art/currency/currency_gold.dds"
 	}
@@ -28,7 +28,8 @@ function PriceTracker:OnLoad(eventCode, addOnName)
 	SLASH_COMMANDS["/pricetracker"] = function(...) self:CommandHandler(...) end

 	local defaults = {
-		itemList = {}
+		itemList = {},
+		algorithm = self.menu.algorithmTable[1]
 	}

 	-- Load saved settings
@@ -39,6 +40,8 @@ function PriceTracker:OnLoad(eventCode, addOnName)
 	self.button:SetParent(ZO_TradingHouseLeftPaneBrowseItemsCommon)
 	self.button:SetWidth(ZO_TradingHouseLeftPaneBrowseItemsCommonQuality:GetWidth())

+	self.menu:InitAddonMenu(addOnName)
+
 end

 -- Handle slash commands
@@ -83,13 +86,17 @@ function PriceTracker:OnUpdateTooltip(item)
 	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, 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)
+	if self.settings.showMinMax then
+		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(self:FormatTooltipLine("Min / Max (stack): ", self.mathUtils:Min(matches) * stackCount .. " / " .. self.mathUtils:Max(matches) * stackCount), "ZoFontGame", r, g, b, CENTER, MODIFY_TEXT_TYPE_NONE, CENTER, false)
+	end
+	if self.settings.showSeen then
+		ItemTooltip:AddLine("Seen " .. #matches .. " times", "ZoFontGame", r, g, b, CENTER, MODIFY_TEXT_TYPE_NONE, CENTER, false)
+	end
 end

 function PriceTracker:OnHideTooltip()
@@ -183,10 +190,22 @@ function PriceTracker:GetMatches(itemName)
 	return matches
 end

-
 -- TODO: Base calculation on user preference
 function PriceTracker:SuggestPrice(matches)
-	return self.mathUtils:WeightedAverage(matches)
+	if self.settings.algorithm == self.menu.algorithmTable[1] then
+		return self.mathUtils:WeightedAverage(matches)
+	end
+
+	if self.settings.algorithm == self.menu.algorithmTable[2] then
+		return self.mathUtils:Median(matches)
+	end
+
+	if self.settings.algorithm == self.menu.algorithmTable[3] then
+		return self.mathUtils:Mode(matches)
+	end
+
+	d("Error deciding how to calculate suggested price")
+	return nil
 end

 function PriceTracker:FormatTooltipLine(title, price, stackCount)
diff --git a/PriceTracker.txt b/PriceTracker.txt
index 8b15344..3fa5076 100644
--- a/PriceTracker.txt
+++ b/PriceTracker.txt
@@ -3,7 +3,11 @@
 ## Version: 0.2
 ## SavedVariables: PriceTrackerSettings
 ## APIVersion: 100004
+## OptionalDependsOn: LibAddonMenu-1.0

-PriceTracker.lua
 PriceTracker.xml
+PriceTracker.lua
+PriceTrackerMenu.lua
 MathUtils.lua
+lib/LibStub/LibStub.lua
+lib/LibAddonMenu-1.0/LibAddonMenu-1.0
\ No newline at end of file
diff --git a/luac.out b/luac.out
deleted file mode 100644
index 31934f9..0000000
Binary files a/luac.out and /dev/null differ