All pastes #1915939 Raw Edit

grid module by shelleycakes

public text v1 · immutable
#1915939 ·published 2010-08-12 19:06 UTC
rendered paste body

local L = AceLibrary("AceLocale-2.2"):new("Grid")
local GridRange = Grid:GetModule("GridRange")
local GridRoster = Grid:GetModule("GridRoster")

local GridStatusRole = Grid:GetModule("GridStatus"):NewModule("GridStatusRole")
GridStatusRole.menuName = "Role"

--roledb.role[guid] = "M/R/H" (Melee/Ranged/Healer)
local roledb = {
	role = {
	},
	unknown = {
	},
}

local inspect_ready = true
local current_inspect = ""

GridStatusRole.defaultDB = {
	debug = false,
	role = {
		text = "Role",
		enable = true,
		color = { r = 0.8, g = 0.8, b = 0.8, a = 0.8 },
		priority = 99,
		class = false,
		range = false,
	},
}

GridStatusRole.options = false

local roleOptions = {
	["class"] = {
		type = 'toggle',
		name = "Use class color",
		desc = "Color by class",
		get = function() return GridStatusRole.db.profile.role.class end,
		set = function()
			GridStatusRole.db.profile.role.class = not GridStatusRole.db.profile.role.class
			GridStatusRole:UpdateAllUnits()
		end,
	},
}

function GridStatusRole:OnInitialize()
	self.super.OnInitialize(self)
	self:RegisterStatus('role', "Role", nil, true)
end

function GridStatusRole:OnStatusEnable(status)
	if status == "role" then
		self:RegisterEvent("UNIT_NAME_UPDATE", "UpdateUnit")
		--self:RegisterEvent("UNIT_PORTRAIT_UPDATE", "UpdateUnit")
		--self:RegisterEvent("UNIT_ENTERED_VEHICLE", "UpdateVehicle")
		--self:RegisterEvent("UNIT_EXITED_VEHICLE", "UpdateVehicle")
		self:RegisterEvent("PLAYER_ALIVE", "UpdatePlayer")
		--self:RegisterEvent("PARTY_MEMBERS_CHANGED", "UpdateAllUnits")
		self:RegisterEvent("Grid_UnitJoined", "UpdateGUID")
		self:RegisterEvent("Grid_UnitChanged", "UpdateGUID")
		self:RegisterEvent("Grid_UnitLeft", "UpdateGUID")
		
		self:RegisterEvent("Grid_ColorsChanged", "UpdateAllUnits")
		self:UpdateAllUnits()
	end
end


--[[  Update Functions  ]]

function GridStatusRole:UpdateVehicle(unitid)
	self:UpdateUnit(unitid)
	local pet_unit = unitid .. "pet"
	if UnitExists(pet_unit) then
		self:UpdateUnit(pet_unit)
	end
end

function GridStatusRole:UpdatePlayer()
	--guid = GridRoster:GetGUIDByName("Safina")
	local guid = UnitGUID("player")
	self:UpdateGUID(guid)
end

function GridStatusRole:UpdateUnit(unitid)
	local guid = UnitGUID(unitid)
	self:UpdateGUID(guid)
end

function GridStatusRole:UpdateGUID(guid)
	local settings = self.db.profile.role
	local name = GridRoster:GetNameByGUID(guid)
	local unitid = GridRoster:GetUnitidByGUID(guid)
	local owner_unitid = GridRoster:GetOwnerUnitidByUnitid(unitid)
	if not name or not settings.enable then return end
	
	local text = name
	local unit_role = GridStatusRole:GetUnitRole(guid)
	
	if owner_unitid then
			if UnitHasVehicleUI(owner_unitid) then
				local owner_guid = UnitGUID(owner_unitid)
				local owner_name = GridRoster:GetNameByGUID(owner_guid)
				text = owner_name
			end
	else
		if unit_role == nil then
			--print("UpdateGUID() has no role for " .. UnitName(unitid) .. ". Calling UpdateRole()")
			GridStatusRole:UpdateRole(guid)
		else
			text = format("%s: %s", unit_role, name)
		end
	end
	local color = settings.class and self.core:UnitColor(guid) or settings.color
	self.core:SendStatusGained(guid, "role",
		settings.priority,
		nil,
		color,
		text)
end

function GridStatusRole:UpdateAllUnits()
	roledb.role = {}
	roledb.unknown = {}
	for guid, unitid in GridRoster:IterateRoster() do
		self:UpdateGUID(guid)
	end
end

--[[   Inspect functions   ]]

--should make seperate timer to reset inspect_ready in case of inspect throttles.

function GridStatusRole:RepeatNotifyInspect()
	if #roledb.unknown ~= 0 then
		for i,guid in ipairs(roledb.unknown) do
				if ( not GridRoster:IsGUIDInRaid(guid) ) then
					table.remove(roledb.unknown, i)
				else
					local unitid = GridRoster:GetUnitidByGUID(guid)
					if CanInspect(unitid) then
						GridStatusRole:SetNotifyInspect(guid)
						return
					end
				end
		end
	else
		self:CancelScheduledEvent("GridStatusRole_RoleCheck")
	end
end

function GridStatusRole:SetNotifyInspect(guid)
		if inspect_ready then
			local unitid = GridRoster:GetUnitidByGUID(guid)
			current_inspect = guid
			self:RegisterEvent("INSPECT_TALENT_READY", "GetNotifyInspect")
			NotifyInspect(unitid)
		end
end

function GridStatusRole:GetNotifyInspect()
		local guid = current_inspect
		local spec = GridStatusRole:FindSpec(guid)
		GridStatusRole:UpdateRole(guid, spec)
		GridStatusRole:UpdateGUID(guid)
		current_inspect = ""
		inspect_ready = true
		self:UnregisterEvent("INSPECT_TALENT_READY")
end

--[[  Spec Parser  ]]

function GridStatusRole:FindSpec(guid)
	local unitid = GridRoster:GetUnitidByGUID(guid)
	local pointsSpent = {}
	local specname = {}
	local inspect = 1
	
	if GetUnitName(unitid) == GetUnitName("player") then
		inspect = 0
	end
	
	local current_spec = GetActiveTalentGroup(inspect)
	local trees = GetNumTalentTabs(inspect)
	
	for i = 1, trees do
		specname[i], _, pointsSpent[i] = GetTalentTabInfo(i, inspect, 0, current_spec)
	end
	
	if #pointsSpent ~= 0 then
		if pointsSpent[1] > pointsSpent[2] and pointsSpent[1] > pointsSpent[3] then
			return specname[1]
		elseif pointsSpent[2] > pointsSpent[1] and pointsSpent[2] > pointsSpent[3] then
			return specname[2]
		else 
			return specname[3]
		end
	end
end

--[[   Role Roster functions   ]]

function GridStatusRole:UpdateRole(guid, spec)
	local spec = spec
	local unitid = GridRoster:GetUnitidByGUID(guid)
	local _, class = UnitClass(unitid)
	
	if class == "WARRIOR" or class == "ROGUE" or class == "DEATHKNIGHT" then
		GridStatusRole:SetUnitRole(guid, "M")
	elseif class == "WARLOCK" or class == "HUNTER" or class == "MAGE" then
		GridStatusRole:SetUnitRole(guid, "R")
	end
	
	--if GUID is not of the above classes or the player himself
	--Then must be hybrid and must be inspected to discover role. 
	if not spec then
		GridStatusRole:SetUnitUnknown(guid)
		return
	end

	if class == "PRIEST" then
		if spec == "Discipline" or spec == "Holy" then
			GridStatusRole:SetUnitRole(guid, "H")
		elseif spec == "Shadow" then
			GridStatusRole:SetUnitRole(guid, "R")
		end
	elseif class == "PALADIN" then
		if spec == "Holy" then
			GridStatusRole:SetUnitRole(guid, "H")
		elseif spec == "Retribution" or spec == "Protection" then
			GridStatusRole:SetUnitRole(guid, "M")
		end
	elseif class == "DRUID" then
		if spec == "Restoration" then
			GridStatusRole:SetUnitRole(guid, "H")
		elseif spec == "Balance" then
			GridStatusRole:SetUnitRole(guid, "R")
		elseif spec == "Feral Combat" then
			GridStatusRole:SetUnitRole(guid, "M")
		end
	elseif class == "SHAMAN" then
		if spec == "Elemental" then
			GridStatusRole:SetUnitRole(guid, "R")
		elseif spec == "Restoration" then			
			GridStatusRole:SetUnitRole(guid, "H")
		elseif spec == "Enhancement" then
			GridStatusRole:SetUnitRole(guid, "M")
		end
	end
	
	if GridStatusRole:GetUnitRole(guid) then
		for i,unknown_guid in ipairs(roledb.unknown) do
			if unknown_guid == guid then
				table.remove(roledb.unknown, i)
			end
		end
		GridStatusRole:UpdateGUID(guid)
	end
end
		
function GridStatusRole:GetUnitRole(guid)
			return roledb.role[guid]
end

function GridStatusRole:SetUnitRole(guid, letter)
		roledb.role[guid] = letter
end

function GridStatusRole:SetUnitUnknown(guid)
	for index,unknown_guid in ipairs(roledb.unknown) do
			if unknown_guid == guid then
				return
			end
	end
	table.insert(roledb.unknown, guid)
	self:ScheduleRepeatingEvent("GridStatusRole_RoleCheck", self.RepeatNotifyInspect, 2, self)		
end