Quantcast

Added weight normalization (divide final weight by sum of all weights in scale)

Kevin Lyles [09-26-09 - 16:39]
Added weight normalization (divide final weight by sum of all weights in scale)

Also truncates to 3 decimal places
Configured by ww_vars.options.normalizeWights (true/false)
Filename
Upgrade.lua
WeightsWatcher.lua
config.lua
diff --git a/Upgrade.lua b/Upgrade.lua
index 604d062..38a2255 100644
--- a/Upgrade.lua
+++ b/Upgrade.lua
@@ -112,6 +112,26 @@ function stringsToFuncs(strTable)
 	return funcTable
 end

+noop_down = [[
+	return function(vars)
+		vars.dataMinorVersion = vars.dataMinorVersion - 1
+		return vars
+	end
+]]
+
+
+function upgradeAccountToNormalization(vars)
+	if not vars.options then
+		vars.options = {}
+	end
+	if vars.options.normalizeWeights == nil then
+		vars.options.normalizeWeights = true
+	end
+
+	vars.dataMinorVersion = 2
+	return vars
+end
+
 function copyDefaultAccountVars()
 	return deepTableCopy(defaultVars)
 end
@@ -139,10 +159,14 @@ end
 upgradeAccountFunctions = {
 	[0] = {
 		[0] = function(vars) return copyDefaultAccountVars() end,
+		[1] = function(vars) return upgradeAccountToNormalization(vars) end,
 	},
 }

 downgradeAccountFunctions = {
+	[0] = {
+		[2] = noop_down,
+	},
 }

 upgradeCharFunctions = {
diff --git a/WeightsWatcher.lua b/WeightsWatcher.lua
index cdcd1c8..1cc313f 100644
--- a/WeightsWatcher.lua
+++ b/WeightsWatcher.lua
@@ -114,7 +114,7 @@ function WeightsWatcher:displayItemStats(tooltip, ttname)
 			if ww_vars.weightsList[class] then
 				for _, weight in pairs(weights) do
 					if ww_vars.weightsList[class][weight] then
-						tooltip:AddDoubleLine(weight, WeightsWatcher:calculateWeight(normalStats, socketBonusActive, socketBonusStat, gemStats, ww_vars.weightsList[class][weight]))
+						tooltip:AddDoubleLine(weight, string.format("%.3f", WeightsWatcher:calculateWeight(normalStats, socketBonusActive, socketBonusStat, gemStats, ww_vars.weightsList[class][weight])))
 					end
 				end
 			end
@@ -161,6 +161,18 @@ function WeightsWatcher:calculateWeight(normalStats, socketBonusActive, socketBo
 			weight = weight + WeightsWatcher:getWeight(value, weightsScale)
 		end
 	end
+	if ww_vars.options.normalizeWeights == true then
+		local total = 0
+
+		for _, value in pairs(weightsScale) do
+			total = total + value
+		end
+		if total == 0 then
+			-- Avoids a divide by zero
+			return 0
+		end
+		weight = weight / total
+	end
 	return weight
 end

diff --git a/config.lua b/config.lua
index ced7e54..1170de2 100644
--- a/config.lua
+++ b/config.lua
@@ -27,7 +27,7 @@ end

 defaultVars = {
 	dataMajorVersion = 0,
-	dataMinorVersion = 1,
+	dataMinorVersion = 2,
 	weightsList = {
 		["DEATHKNIGHT"] = {
 			["Blood DPS"] = {
@@ -348,6 +348,9 @@ defaultVars = {
 			},
 		},
 	},
+	options = {
+		normalizeWeights = true,
+	},
 }

 defaultCharVars = {