All pastes #329783 Raw Edit

Mine

public text v1 · immutable
#329783 ·published 2007-01-27 01:23 UTC
rendered paste body
--[[
Name: Rating Buster
Revision: $Revision: 2 $
Developed by: Whitetooth@Cenarius (hotdogee@巴哈姆特baha)
Description: Converts combat ratings in tooltips into normal percentages.
]]

-----------------
-- Addon Setup --
-----------------

-- Version Info
local MAJOR_VERSION = "0.7.1"
--local MINOR_VERSION = tonumber((string.gsub("$Revision: 2 $", "^.-(%d+).-$", "%1")))
-- Librarys
local Compost = AceLibrary("Compost-2.0")
local L = AceLibrary("AceLocale-2.2"):new("RatingBuster")
-- AceAddon Initialization
RatingBuster = AceLibrary("AceAddon-2.0"):new("AceDB-2.0", "AceConsole-2.0", "AceEvent-2.0", "AceDebug-2.0")
RatingBuster.title = "Rating Buster"
RatingBuster.version = MAJOR_VERSION
--RatingBuster.date = string.gsub("$Date: 2006-11-05 15:10:31 -0700 (Sun, 03 Sep 2006) $", "^.-(%d%d%d%d%-%d%d%-%d%d).-$", "%1")

-- Set Debugging
RatingBuster:SetDebugging(false)

---------------------
-- Saved Variables --
---------------------
-- Register DB
RatingBuster:RegisterDB("RatingBusterDB")
-- Default values
RatingBuster.DBDefaults = {
	showItemLevel = true,
	showItemID = false,
	useRequiredLevel = true,
}
-- Register Defaults
RatingBuster:RegisterDefaults("profile", RatingBuster.DBDefaults)

---------------------------
-- Slash Command Options --
---------------------------

local consoleOptions = {
	type='group',
	args = {
		itemid = {
			type = 'toggle',
			name = L["Show Item ID"],
			desc = L["Toggle display of Item ID in tooltip"],
			get = function() return RatingBuster.db.profile.showItemID end,
			set = function(v) RatingBuster.db.profile.showItemID = v end,
		},
		itemlevel = {
			type = 'toggle',
			name = L["Show Item Level"],
			desc = L["Toggle display of Item Level in tooltip"],
			get = function() return RatingBuster.db.profile.showItemLevel end,
			set = function(v) RatingBuster.db.profile.showItemLevel = v end,
		},
		usereqlv = {
			type = 'toggle',
			name = L["Use Required Level"],
			desc = L["Toggle calculate using the required level if you are below the required level"],
			get = function() return RatingBuster.db.profile.useRequiredLevel end,
			set = function(v) RatingBuster.db.profile.useRequiredLevel = v end,
		},
		targetlevel = {
			type = 'range',
			name = L["Target Level"],
			desc = L["Changes the Target Level used in rating calculations"],
			get = function() return RatingBuster.targetLevel end,
			set = function(v) RatingBuster.targetLevel = v end,
			min = 10,
			max = 73, -- set to level cap + 3
			step = 1,
		},
	},
}

RatingBuster:RegisterChatCommand({"/rb", "/ratingbuster"}, consoleOptions)

---------------------
-- Initializations --
---------------------

function RatingBuster:OnInitialize()
	-- Build the RatingNameToID table from L["ratingNames"] table
	self:BuildRatingNameToID()
	-- Hook tooltip SetX methods
	self:HookTooltipMethods()
end

function RatingBuster:BuildRatingNameToID()
	RatingBuster.RatingNameToID = {}
	for _, v in ipairs(L["ratingNames"]) do
		self.RatingNameToID[v[1]] = v[2]
	end
end

function RatingBuster:OnEnable()
	-- Initialize targetLevel
	RatingBuster.targetLevel = UnitLevel("player")
	-- Check player level for lowbies
	if RatingBuster.targetLevel < 10 then
		RatingBuster.targetLevel = 10
	end
	-- for setting a new level
	self:RegisterEvent("PLAYER_LEVEL_UP")
end

function RatingBuster:OnDisable()
end

-- event = PLAYER_LEVEL_UP
-- arg1 = New player level
function RatingBuster:PLAYER_LEVEL_UP()
	RatingBuster.targetLevel = arg1
end


------------------------------------------
-- Hook Tooltip OnTooltipSetItem Script --
------------------------------------------
RatingBuster.TooltipList = {
	ItemRefTooltip,
	GameTooltip,
	ShoppingTooltip1,
	ShoppingTooltip2,
	--EquipCompare support
	ComparisonTooltip1,
	ComparisonTooltip2,
	--EQCompare support
	EQCompareTooltip1,
	EQCompareTooltip2,
	-- socketing interface
	--ItemSocketingDescription, -- commented out cause can't get it to work
	-- LinkWrangler support
	IRR_ItemRefTooltip1,
	IRR_ItemCompTooltip1,
	IRR_ItemCompTool11,
	IRR_ItemRefTooltip2,
	IRR_ItemCompTooltip2,
	IRR_ItemCompTool12,
	IRR_ItemRefTooltip3,
	IRR_ItemCompTooltip3,
	IRR_ItemCompTool13,
	IRR_ItemRefTooltip4,
	IRR_ItemCompTooltip4,
	IRR_ItemCompTool14,
	IRR_ItemRefTooltip5,
	IRR_ItemCompTooltip5,
	IRR_ItemCompTool15,
	-- MultiTips support
	ItemRefTooltip2,
	ItemRefTooltip3,
	ItemRefTooltip4,
	ItemRefTooltip5,
}

-- OnTooltipSetItem handler
function RatingBuster.SetItem(tooltip)
	local _, link = tooltip:GetItem()
	RatingBuster.ProcessTooltip(tooltip, link)
end

function RatingBuster:HookTooltipMethods()
	for _, tooltip in pairs(self.TooltipList) do
		if tooltip then
			-- Use new script handler: OnTooltipSetItem: function(self) - Triggered when a Tooltip shows an item *2.0.3*
			-- name, link = Tooltip:GetItem() *2.0.3*
			if tooltip:HasScript("OnTooltipSetItem") then
				if tooltip:GetScript("OnTooltipSetItem") then
					local oldScript = tooltip:GetScript("OnTooltipSetItem")
					tooltip:SetScript("OnTooltipSetItem", function(tooltip)
						oldScript(tooltip)
						RatingBuster.SetItem(tooltip)
					end)
				else
					tooltip:SetScript("OnTooltipSetItem", RatingBuster.SetItem)
				end
			end
		end
	end
end


--------------------------
-- Process Tooltip Core --
--------------------------
--[[
Method 1
"+15 Crit Rating"
-> "+15(1.20%) Crit Rating"
"Equip: Increases your hit rating by 10."
-> "Equip: Increases your hit rating by 10(1.20%)."
Method 2
"+15 Crit Rating"
-> "+15 Crit Rating (1.20%)"
"Equip: Increases your hit rating by 10."
-> "Equip: Increases your hit rating by 10. (1.20%)"
Method 3
"+15 Crit Rating"
-> "+15 Crit Rating (1.20%)"
"Equip: Increases your hit rating by 10."
-> "Equip: +10 Hit Rating (1.20%)"
Method 4
+10 Sta (100HP) 
Equip: Adds 15 critical hit rating (.85% ) 
Equip: Adds 12 resilience (-.58% crit, -1.16% crit damage) 
--]]
function RatingBuster.ProcessTooltip(tooltip, link)
	-- check if in standby mode
	if not RatingBuster:IsActive() then return end
	-- Get Required Level
	local requiredLevel
	if RatingBuster.db.profile.useRequiredLevel and link then
		_, _, _, _, requiredLevel = GetItemInfo(link)
		--RatingBuster:Print(link..", "..requiredLevel)
		if requiredLevel and requiredLevel <= RatingBuster.targetLevel then
			requiredLevel = nil
		end
	end
	--Anlegen: Erhöht die Chance aller Gruppenmitglieder, die sich im Umkreis von 30 befinden, auf einen kritischen Treffer mit Zaubern um 28%. 
	for i = 2, tooltip:NumLines() do
		local fontString = getglobal(tooltip:GetName().."TextLeft"..i)
		local lineText = fontString:GetText()
		if lineText and strfind(lineText, "%d") then -- do nothing if we don't find a number
			for _, p in ipairs(L["numberPatterns"]) do
				local text = lineText
				local lowerText = string.lower(text) -- convert to lower so we don't have to worry about same ratings with different cases
				-- Capture the increased amount
				local s, e, amount = strfind(lowerText, p.pattern)
				if amount then
					-- Check for separators
					local separator
					for _, s in ipairs(p.separators) do
						if strfind(lineText, s) then
							separator = s
							-- Replace separator strings found in lineText with a single character "/", so we can use strsplit()
							lineText = string.gsub(lineText, s, "/")
							-- only need support for 1 kind of separator per line, we will use the first one found, so break out here
							break
						end
					end
					-- If this line has a separator, then we do the split and combine algorithm
					if separator then
						local splitText = Compost:Acquire(strsplit("/", lineText))
						--RatingBuster:Debug(p.pattern)
						for i, text in ipairs(splitText) do
							--RatingBuster:Debug(text)
							local lowerText = string.lower(text) -- convert to lower so we don't have to worry about same ratings with different cases
							-- Capture the increased amount
							local s, e, amount = strfind(lowerText, p.pattern)
							if amount then
								-- Capture the rating name
								local ratingID
								for _, v in ipairs(L["ratingNames"]) do
									if strfind(lowerText, v[1]) then
										ratingID = RatingBuster.RatingNameToID[v[1]]
										break
									end
								end
								if ratingID then
									-- calculate reverse value
									local reversedAmount = ReverseRating(tonumber(amount), ratingID, requiredLevel or RatingBuster.targetLevel)
									--RatingBuster:Debug(reversedAmount..", "..amount..", "..ratingID..", "..RatingBuster.targetLevel)-- debug
									-- build reversed string
									local reversedString
									-- check if rating is a skill type
									if ratingID == 1 or ratingID == 2 then
										reversedString = "("..format("%.2f", reversedAmount)..")"
									-- check if rating is resilience
									elseif ratingID == 15 then
										reversedString = "(-"..format("%.2f", reversedAmount).."%%%%)"
									else
										reversedString = "("..format("%.2f", reversedAmount).."%%%%)"
									end
									-- build converted string
									splitText[i] = string.gsub(text, p.pattern, string.gsub(strsub(text, s, e), "%d+", "%0 "..reversedString), 1)
								end
							end
						end -- for i, text in ipairs(splitText)
						lineText = strjoin(separator, unpack(splitText))
						Compost:Reclaim(splitText)
					else
						-- Capture the rating name
						local ratingID
						for _, v in ipairs(L["ratingNames"]) do
							if strfind(lowerText, v[1]) then
								ratingID = RatingBuster.RatingNameToID[v[1]]
								break
							end
						end
						if ratingID then
							-- calculate reverse value
							local reversedAmount = ReverseRating(tonumber(amount), ratingID, requiredLevel or RatingBuster.targetLevel)
							--RatingBuster:Debug(reversedAmount..", "..amount..", "..ratingID..", "..RatingBuster.targetLevel)-- debug
							-- build reversed string
							local reversedString
							-- check if rating is a skill type
							if ratingID == 1 or ratingID == 2 then
								reversedString = "("..format("%.2f", reversedAmount)..")"
							-- check if rating is resilience
							elseif ratingID == 15 then
								reversedString = "(-"..format("%.2f", reversedAmount).."%%%%)"
							else
								reversedString = "("..format("%.2f", reversedAmount).."%%%%)"
							end
							-- build converted string
							lineText = string.gsub(text, p.pattern, string.gsub(strsub(text, s, e), "%d+", "%0 "..reversedString), 1)
						end
					end
				end
			end
			-- SetText
			fontString:SetText(lineText)
		end -- if text then
	end -- for i = 2, tooltip:NumLines() do
	-- Item Level and Item ID
	-- Check for ItemLevel addon, do nothing if found
	if not ItemLevel_AddInfo and (RatingBuster.db.profile.showItemLevel or RatingBuster.db.profile.showItemID)then
		local id, level
		-- Get the Item ID from the link string
		if link then
			_, link, _, level = GetItemInfo(link)
			if link then
				_, _, id = strfind(link, "item:(%d+)")
			end
			local newLine = ""
			if level and RatingBuster.db.profile.showItemLevel then
				newLine = newLine..L["ItemLevel: "]..level
			end
			if id and RatingBuster.db.profile.showItemID then
				if newLine ~= "" then
					newLine = newLine..", "
				end
				newLine = newLine..L["ItemID: "]..id
			end
			if newLine ~= "" then
				tooltip:AddLine(newLine)
			end
		end
	end
	tooltip:Show()
end