<commit_before>--[[--------------------------------------------------------------------
    Ovale Spell Priority
    Copyright (C) 2012 Sidoine

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License in the LICENSE
    file accompanying this program.
----------------------------------------------------------------------]]

-- This addon keep the list of all the aura for all the units
-- Fore each aura, it saves the state of the player when it was refreshed

OvaleAura = LibStub("AceAddon-3.0"):NewAddon("OvaleAura", "AceEvent-3.0")

--<public-static-properties>
OvaleAura.aura = {}
OvaleAura.mastery = nil
OvaleAura.stance = 0
OvaleAura.serial = 0
OvaleAura.spellHaste = 1
OvaleAura.meleeHaste = 1
OvaleAura.playerGUID = nil
--</public-static-properties>

--<private-static-properties>
local baseDamageMultiplier = 1

local pairs, select, strfind = pairs, select, string.find
local GetSpecialization, GetShapeshiftForm, UnitAura = GetSpecialization, GetShapeshiftForm, UnitAura
--</private-static-properties>

-- Events
--<public-static-methods>
function OvaleAura:OnEnable()
	self.playerGUID = UnitGUID("player")
	self:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
	--self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
	self:RegisterEvent("PLAYER_ENTERING_WORLD")
	self:RegisterEvent("UNIT_AURA")
	self:RegisterEvent("UPDATE_SHAPESHIFT_FORM")
	self:RegisterEvent("UPDATE_SHAPESHIFT_FORMS")
end

function OvaleAura:OnDisable()
	self:UnregisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
	--self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
	self:UnregisterEvent("PLAYER_ENTERING_WORLD")
	self:UnregisterEvent("UNIT_AURA")
	self:UnregisterEvent("UPDATE_SHAPESHIFT_FORM")
	self:UnregisterEvent("UPDATE_SHAPESHIFT_FORMS")
end

function OvaleAura:ACTIVE_TALENT_GROUP_CHANGED(event)
	self.mastery = GetSpecialization()
end

function OvaleAura:COMBAT_LOG_EVENT_UNFILTERED(event, ...)
	local time, event, hideCaster, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags = select(1, ...)

	-- KNOWN BUG: an aura refreshed by a spell other than then one that applies it won't cause the CLEU event to fire.
	if strfind(event, "SPELL_AURA_") == 1 then
		local spellId, spellName, spellSchool, auraType = select(12, ...)

		if (sourceGUID == self.playerGUID and OvaleData.spellFilter.mine[spellId]) or OvaleData.spellFilter.any[spellId] then
			local unitId = OvaleGUID:GetUnitId(destGUID)

			-- Only update for "*target" unit IDs.  All others are handled by UNIT_AURA event handler.
			if unitId and unitId ~= "target" and strfind(unitId, "target") then
				self:UpdateAuras(unitId, destGUID)
			end

			if sourceGUID == self.playerGUID and (event == "SPELL_AURA_APPLIED" or event == "SPELL_AURA_REFRESH" or event == "SPELL_AURA_APPLIED_DOSE") then
				local aura = self:GetAuraByGUID(destGUID, spellId, true)
				if aura then
					aura.spellHaste = self.spellHaste
				end
			end
		end
	end

	if event == "UNIT_DIED" then
		self.aura[destGUID] = nil
		local unitId = OvaleGUID:GetUnitId(destGUID)
		if unitId then
			Ovale.refreshNeeded[unitId] = true
		end
	end
end

function OvaleAura:PLAYER_ENTERING_WORLD(event)
	self.mastery = GetSpecialization()
	self.stance = GetShapeshiftForm()
end

function OvaleAura:UNIT_AURA(event, unitId)
	local guid
	if unitId == "player" then
		guid = self.playerGUID
	else
		guid = UnitGUID(unitId)
	end
	self:UpdateAuras(unitId, guid)
end

function OvaleAura:UPDATE_SHAPESHIFT_FORM(event)
	self:ShapeshiftEventHandler()
end

function OvaleAura:UPDATE_SHAPESHIFT_FORMS(event)
	self:ShapeshiftEventHandler()
end

function OvaleAura:AddAura(unitGUID, spellId, unitCaster, icon, count, debuffType, duration, expirationTime, isStealable, name)
	local auraList = self.aura[unitGUID]

	if not auraList[spellId] then
		auraList[spellId] = {}
	end
		
	local mine = (unitCaster == "player")
	local aura
	if mine then
		if not auraList[spellId].mine then
			auraList[spellId].mine = { gain = OvaleState.maintenant }
		end
		aura = auraList[spellId].mine
	else
		if not auraList[spellId].other then
			auraList[spellId].other = { gain = OvaleState.maintenant }
		end
		aura = auraList[spellId].other
	end
	
	aura.serial = self.serial
	
	if count == 0 then
		count = 1
	end
	
	if not aura.ending or aura.ending < expirationTime or aura.stacks ~= count then
		aura.icon = icon
		aura.stacks = count
		aura.debuffType = debuffType
		if duration > 0 then
			aura.duration = duration
			aura.ending = expirationTime
		else
			aura.duration = nil
			aura.ending = nil
		end
		aura.start = expirationTime - duration
		aura.stealable = isStealable
		aura.mine = mine
		aura.source = unitCaster
		aura.name = name
	end
end

-- Private methods
function OvaleAura:ShapeshiftEventHandler()
	local newStance = GetShapeshiftForm()
	if self.stance ~= newStance then
		self.stance = newStance
		self:SendMessage("Ovale_UpdateShapeshiftForm")
	end
end

function OvaleAura:UpdateAuras(unitId, unitGUID)
	self.serial = self.serial + 1
	
	local hateBase
	local hateCommune
	local hateSorts
	local hateCaC
	local hateHero
	local hateClasse
	local damageMultiplier
	
	if unitId == "player" then
		hateBase = GetCombatRatingBonus(18)
		hateCommune = 0
		hateSorts = 0
		hateCaC = 0
		hateHero = 0
		hateClasse = 0
		damageMultiplier = 1
	end
		
	if not unitGUID then
		if unitId == "player" then
			unitGUID = self.playerGUID
		else
			unitGUID = UnitGUID(unitId)
		end
	end

	if not self.aura[unitGUID] then
		self.aura[unitGUID] = {}
	end
	
	local i = 1
	
	local mode = "HELPFUL"
	while (true) do
		local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId =  UnitAura(unitId, i, mode)
		if not name then
			if mode == "HELPFUL" then
				mode = "HARMFUL"
				i = 1
			else
				break
			end
		else
			if (unitCaster == "player" and OvaleData.spellFilter.mine[spellId]) or OvaleData.spellFilter.any[spellId] then
				self:AddAura(unitGUID, spellId, unitCaster, icon, count, debuffType, duration, expirationTime, isStealable, name)
				if debuffType then
					-- TODO: not very clean
					-- should be computed by OvaleState:GetAura
					self:AddAura(unitGUID, debuffType, unitCaster, icon, count, debuffType, duration, expirationTime, isStealable, name)
				end
			end
			
			if unitId == "player" then
				if OvaleData.buffSpellList.spell_haste[spellId] then<commit_msg>Fix ticket 186 & re-enable CLEU event handler for *target unit IDs.<commit_after>--[[--------------------------------------------------------------------
    Ovale Spell Priority
    Copyright (C) 2012 Sidoine

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License in the LICENSE
    file accompanying this program.
----------------------------------------------------------------------]]

-- This addon keep the list of all the aura for all the units
-- Fore each aura, it saves the state of the player when it was refreshed

OvaleAura = LibStub("AceAddon-3.0"):NewAddon("OvaleAura", "AceEvent-3.0")

--<public-static-properties>
OvaleAura.aura = {}
OvaleAura.mastery = nil
OvaleAura.stance = 0
OvaleAura.serial = 0
OvaleAura.spellHaste = 1
OvaleAura.meleeHaste = 1
OvaleAura.playerGUID = nil
--</public-static-properties>

--<private-static-properties>
local baseDamageMultiplier = 1

local pairs, select, strfind = pairs, select, string.find
local GetSpecialization, GetShapeshiftForm, UnitAura = GetSpecialization, GetShapeshiftForm, UnitAura
--</private-static-properties>

-- Events
--<public-static-methods>
function OvaleAura:OnEnable()
	self.playerGUID = UnitGUID("player")
	self:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
	self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
	self:RegisterEvent("PLAYER_ENTERING_WORLD")
	self:RegisterEvent("UNIT_AURA")
	self:RegisterEvent("UPDATE_SHAPESHIFT_FORM")
	self:RegisterEvent("UPDATE_SHAPESHIFT_FORMS")
end

function OvaleAura:OnDisable()
	self:UnregisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
	self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
	self:UnregisterEvent("PLAYER_ENTERING_WORLD")
	self:UnregisterEvent("UNIT_AURA")
	self:UnregisterEvent("UPDATE_SHAPESHIFT_FORM")
	self:UnregisterEvent("UPDATE_SHAPESHIFT_FORMS")
end

function OvaleAura:ACTIVE_TALENT_GROUP_CHANGED(event)
	self.mastery = GetSpecialization()
end

function OvaleAura:COMBAT_LOG_EVENT_UNFILTERED(event, ...)
	local time, event, hideCaster, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags = select(1, ...)

	-- KNOWN BUG: an aura refreshed by a spell other than then one that applies it won't cause the CLEU event to fire.
	if strfind(event, "SPELL_AURA_") == 1 then
		local spellId, spellName, spellSchool, auraType = select(12, ...)

		if (sourceGUID == self.playerGUID and OvaleData.spellFilter.mine[spellId]) or OvaleData.spellFilter.any[spellId] then
			local unitId = OvaleGUID:GetUnitId(destGUID)

			-- Only update for "*target" unit IDs.  All others are handled by UNIT_AURA event handler.
			if unitId and unitId ~= "target" and strfind(unitId, "target") then
				self:UpdateAuras(unitId, destGUID)
			end

			if sourceGUID == self.playerGUID and (event == "SPELL_AURA_APPLIED" or event == "SPELL_AURA_REFRESH" or event == "SPELL_AURA_APPLIED_DOSE") then
				local aura = self:GetAuraByGUID(destGUID, spellId, true)
				if aura then
					aura.spellHaste = self.spellHaste
				end
			end
		end
	end

	if event == "UNIT_DIED" then
		self.aura[destGUID] = nil
		local unitId = OvaleGUID:GetUnitId(destGUID)
		if unitId then
			Ovale.refreshNeeded[unitId] = true
		end
	end
end

function OvaleAura:PLAYER_ENTERING_WORLD(event)
	self.mastery = GetSpecialization()
	self.stance = GetShapeshiftForm()
end

function OvaleAura:UNIT_AURA(event, unitId)
	if unitId == "player" then
		self:UpdateAuras("player", self.playerGUID)
	else
		self:UpdateAuras(unitId)
	end
end

function OvaleAura:UPDATE_SHAPESHIFT_FORM(event)
	self:ShapeshiftEventHandler()
end

function OvaleAura:UPDATE_SHAPESHIFT_FORMS(event)
	self:ShapeshiftEventHandler()
end

function OvaleAura:AddAura(unitGUID, spellId, unitCaster, icon, count, debuffType, duration, expirationTime, isStealable, name)
	local auraList = self.aura[unitGUID]

	if not auraList[spellId] then
		auraList[spellId] = {}
	end
		
	local mine = (unitCaster == "player")
	local aura
	if mine then
		if not auraList[spellId].mine then
			auraList[spellId].mine = { gain = OvaleState.maintenant }
		end
		aura = auraList[spellId].mine
	else
		if not auraList[spellId].other then
			auraList[spellId].other = { gain = OvaleState.maintenant }
		end
		aura = auraList[spellId].other
	end
	
	aura.serial = self.serial
	
	if count == 0 then
		count = 1
	end
	
	if not aura.ending or aura.ending < expirationTime or aura.stacks ~= count then
		aura.icon = icon
		aura.stacks = count
		aura.debuffType = debuffType
		if duration > 0 then
			aura.duration = duration
			aura.ending = expirationTime
		else
			aura.duration = nil
			aura.ending = nil
		end
		aura.start = expirationTime - duration
		aura.stealable = isStealable
		aura.mine = mine
		aura.source = unitCaster
		aura.name = name
	end
end

-- Private methods
function OvaleAura:ShapeshiftEventHandler()
	local newStance = GetShapeshiftForm()
	if self.stance ~= newStance then
		self.stance = newStance
		self:SendMessage("Ovale_UpdateShapeshiftForm")
	end
end

function OvaleAura:UpdateAuras(unitId, unitGUID)
	self.serial = self.serial + 1
	
	local hateBase
	local hateCommune
	local hateSorts
	local hateCaC
	local hateHero
	local hateClasse
	local damageMultiplier

	if not unitId then return end

	if unitId == "player" then
		hateBase = GetCombatRatingBonus(18)
		hateCommune = 0
		hateSorts = 0
		hateCaC = 0
		hateHero = 0
		hateClasse = 0
		damageMultiplier = 1
	end
		
	if not unitGUID and unitId == "player" then
		unitGUID = self.playerGUID
	end
	if not unitGUID then
		unitGUID = UnitGUID(unitId)
	end

	if not self.aura[unitGUID] then
		self.aura[unitGUID] = {}
	end
	
	local i = 1
	
	local mode = "HELPFUL"
	while (true) do
		local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId =  UnitAura(unitId, i, mode)
		if not name then
			if mode == "HELPFUL" then
				mode = "HARMFUL"
				i = 1
			else
				break
			end
		else
			if (unitCaster == "player" and OvaleData.spellFilter.mine[spellId]) or OvaleData.spellFilter.any[spellId] then
				self:AddAura(unitGUID, spellId, unitCaster, icon, count, debuffType, duration, expirationTime, isStealable, name)
				if debuffType then
					-- TODO: not very clean
					-- should be computed by OvaleState:GetAura
					self:AddAura(unitGUID, debuffType, unitCaster, icon, count, debuffType, duration, expirationTime, isStealable, name)
				end
			end
			
			if unitId == "player" then
				if OvaleData.buffSpellList.spell_haste[spellId] then
{'fraction_non_alphanumeric': 0.11126553934059145, 'fraction_numerical': 0.005018917458111343, 'mean_word_length': 4.07921568627451, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3654628', 'n_tokens_mistral': 4921, 'n_tokens_neox': 4546, 'n_words': 1126}