Quantcast

Handles all direct stats and percentages, even when there are more than one in a line

Kevin Lyles [09-14-09 - 22:41]
Handles all direct stats and percentages, even when there are more than one in a line

Still need to do sockets, socket bonuses (handled as guaranteed atm) and weapon speed
Filename
Regexps.lua
WeightsWatcher.lua
diff --git a/Regexps.lua b/Regexps.lua
index 856bb5e..de848e3 100644
--- a/Regexps.lua
+++ b/Regexps.lua
@@ -6,6 +6,7 @@ Preprocess = {
 	{"Increases the target's", "Increases"},
 	{"Unique%-Equipped", "Unique"},
 	{"^Use: Teaches you how to permanently enchant ", "Use: Permanently enchant "},
+	{"(%d+) to ", "%1 "},
 }

 IgnoredLines = {
@@ -17,7 +18,20 @@ IgnoredLines = {
 	"^<.+>$",
 	"^\".+\"$",
 	"^Use: Restores %d+ %a[%a ]+ over %d+ sec%.  Must remain seated while %a+ing%.",
+	"^Use: Restores %d+%% of your %a[%a ]+ per second for %d+ sec%.  Must remain seated while %a+ing%.",
 	"^Use: Heals %d+ damage over %d+ sec%.$",
+	"^Use: Restores %d+ %d+ %a+%.",
+}
+
+MultipleStatLines = {
+	{" and ",
+		function(text)
+			return multipleStats(text)
+		end},
+	{"%d %- %d",
+		function(text)
+			return damageRange(text)
+		end},
 }

 SingleStatLines = {
@@ -27,19 +41,15 @@ SingleStatLines = {
 		end},
 	{"Restores (%d+) mana per 5 sec%.",
 		function(text, pattern)
-			local start, value
-			start, _, value = string.find(text, pattern)
-			if start then
-				return {"MP5", value}
-			end
+			return singleStatValueOnly(text, pattern, "MP5")
+		end},
+	{"^Use: Increases mana regeneration by (%d+) mana per 5 seconds for ",
+		function(text, pattern)
+			return singleStatValueOnly(text, pattern, "MP5")
 		end},
 	{"^%((%d[%d.]+) damage per second%)$",
 		function(text, pattern)
-			local start, value
-			start, _, value = string.find(text, pattern)
-			if start then
-				return {"DPS", value}
-			end
+			return singleStatValueOnly(text, pattern, "DPS")
 		end},
 	{"^%+?(%d+) (%a[%a ]+)",
 		function(text, pattern)
@@ -51,40 +61,15 @@ SingleStatLines = {

 	"^(%a[%a ]+) (%d+)",
 	"^Equip: Increases (%a[%a ]+) by (%d+)%.",
+	"^Equip: Increased (%a[%a ]+) %+(%d+)%.",
 	"^Use: Increases (%a[%a ]+) by (%d+) for .*%.",
-	"^Use: Permanently increase the (%a[%a ]+) of .* by (%d+)%..*",
-	"^Use: Permanently enchant .* to increase (%a[%a ]+) by (%d+)%..*",
+	"^Use: Permanently increase the (%a[%a ]+) of .* by (%d+)%.",
+	"^Use: Permanently enchant .* to increase (%a[%a ]+) by (%d+)%.",
 	"^Use: When applied to your fishing pole, increases (Fishing) by (%d+) for ",
-	{"^Use: Increases mana regeneration by (%d+) mana per 5 seconds for ",
-		function(text, pattern)
-			local start, value
-			start, _, value = string.find(text, pattern)
-			if start then
-				return {"MP5", value}
-			end
-		end},
 	"^Use: Permanently increase the (%a[%a ]+) of .* by (%d+)%.",
 	"^Use: Permanently enchant .* to increase (%a[%a ]+) by (%d+)%.",
 }

-ProcessedLines = {
-	-- TODO: split this into min and max damage
-	{"(%d+) %- (%d+) Damage",
-		function(text, pattern)
-			local start, val1, val2
-			start, _, val1, val2 = string.find(text, pattern)
-			if start then
-				return {"Damage", val1 .. "-" .. val2}
-			end
-		end},
-
-	"^(Classes): (%a[%a ,]+)",
-	"^(Requires %a[%a ]+) %((%d+)%)",
-
-	-- TODO: figure out how to properly handle this and other multi-stat lines
-	"^Use: .*%.  If you spend at least %d+ seconds eating you will become well fed and gain ([%a%d][%a%d, ]+) for .*%.",
-}
-
 DoubleSlotLines = {
 	"^Head$",
 	"^Shoulder$",
@@ -100,6 +85,8 @@ DoubleSlotLines = {
 	"^Two%-Hand$",
 	"^Relic$",
 	"^Ranged$",
+	"^Thrown$",
+	"^Projectile$",
 }

 SingleSlotLines = {
@@ -112,3 +99,74 @@ SingleSlotLines = {
 	"^Trinket$",
 	"^Held In Off%-hand$",
 }
+
+ProcessedLines = {
+	"^(Classes): (%a[%a ,]+)",
+	"^(Requires %a[%a ]+) %((%d+)%)",
+
+	-- TODO: figure out how to properly handle this and other multi-stat lines
+	"^Use: .*%.  If you spend at least %d+ seconds eating you will become well fed and gain ([%a%d][%a%d, ]+) for .*%.",
+}
+
+function multipleStats(text)
+	local stat, stringTable, valid
+	local stats = {}
+
+	text = string.gsub(string.gsub(text, ",? and ", "\a"), ", ", "\a")
+	stringTable = { strsplit("\a", text) }
+	for _, statString in pairs(stringTable) do
+		valid, stat = extractSingleStat(statString)
+		if valid then
+			table.insert(stats, stat)
+		end
+	end
+	if #(stats) > 0 then
+		return stats
+	end
+end
+
+function damageRange(text)
+	local start, _, added, minVal, maxVal, name = string.find(text, "^(%+?)(%d+) %- (%d+) (%a* ?Damage)")
+	if start then
+		if added == "+" then
+			added = "Added "
+		end
+		return {
+			{"Minimum " .. added .. name, minVal},
+			{"Maximum " .. added .. name, maxVal},
+		}
+	end
+end
+
+function extractSingleStat(text)
+	local stat
+	for _, regex in pairs(SingleStatLines) do
+		if type(regex) == "table" then
+			local pattern, func = unpack(regex)
+			if string.find(text, pattern) then
+				stat = func(text, pattern)
+				if stat then
+					break
+				end
+			end
+		else
+			start, _, name, value = string.find(text, regex)
+			if start then
+				stat = {name, value}
+				break
+			end
+		end
+	end
+	if stat then
+		return true, stat
+	else
+		return false
+	end
+end
+
+function singleStatValueOnly(text, pattern, name)
+	local start, _, value = string.find(text, pattern)
+	if start then
+		return {name, value}
+	end
+end
diff --git a/WeightsWatcher.lua b/WeightsWatcher.lua
index d29764f..5cda3df 100644
--- a/WeightsWatcher.lua
+++ b/WeightsWatcher.lua
@@ -90,71 +90,57 @@ function DisplayItemInfo(tooltip, ttname)

 			matched = false
 			for _, regex in pairs(IgnoredLines) do
-				if string.find(origTextL, regex) then
+				if string.find(textL, regex) then
 					matched = true
 					break
 				end
 			end
 			if not matched then
-				for _, regex in pairs(SingleStatLines) do
-					if type(regex) == "table" then
-						pattern, func = unpack(regex)
-						if string.find(textL, pattern) then
-							stat = func(textL, pattern)
-							if stat then
-								tooltip:AddDoubleLine(unpack(stat))
-								matched = true
-								break
-							end
-						end
-					else
-						start, _, name, value = string.find(textL, regex)
-						if start then
-							tooltip:AddDoubleLine(name, value)
-							matched = true
-							break
-						end
+				for _, regex in pairs(DoubleSlotLines) do
+					if string.find(textL, regex) then
+						matched = true
+						tooltip:AddDoubleLine(textL, textR)
+						break
 					end
 				end
 				if not matched then
-					for _, regex in pairs(DoubleSlotLines) do
+					for _, regex in pairs(SingleSlotLines) do
 						if string.find(textL, regex) then
 							matched = true
-							tooltip:AddDoubleLine(textL, textR)
+							tooltip:AddLine(textL)
 							break
 						end
 					end
 					if not matched then
-						for _, regex in pairs(SingleSlotLines) do
-							if string.find(textL, regex) then
-								matched = true
-								tooltip:AddLine(textL)
-								break
+						for _, regex in pairs(MultipleStatLines) do
+							pattern, func = unpack(regex)
+							if string.find(textL, pattern) then
+								statsList = func(textL)
+								if statsList then
+									for _, stat in pairs(statsList) do
+										tooltip:AddDoubleLine(unpack(stat))
+									end
+									matched = true
+									break
+								end
 							end
 						end
 						if not matched then
-							for _, regex in pairs(ProcessedLines) do
-								if type(regex) == "table" then
-									pattern, func = unpack(regex)
-									if string.find(textL, pattern) then
-										stat = func(textL, pattern)
-										if stat then
-											tooltip:AddDoubleLine(unpack(stat))
-											matched = true
-											break
-										end
-									end
-								else
+							matched, stat = extractSingleStat(textL)
+							if matched then
+								tooltip:AddDoubleLine(unpack(stat))
+							else
+								for _, regex in pairs(ProcessedLines) do
 									start, _, name, value = string.find(textL, regex)
 									if start then
-										tooltip:AddDoubleLine(name, value)
 										matched = true
+										tooltip:AddDoubleLine(name, value)
 										break
 									end
 								end
-							end
-							if not matched then
-								ttleft:SetText(origTextL .. " *")
+								if not matched then
+									ttleft:SetText(origTextL .. " *")
+								end
 							end
 						end
 					end