ReputationReward.lua 8.12 KB
Newer Older
Azilroka's avatar
Azilroka committed
1
local PA = _G.ProjectAzilroka
Azilroka's avatar
Azilroka committed
2
if PA.Classic then return end
Azilroka's avatar
Azilroka committed
3

Azilroka's avatar
Azilroka committed
4 5 6
local RR = PA:NewModule('ReputationReward', 'AceEvent-3.0', 'AceTimer-3.0', 'AceHook-3.0')
PA.RR = RR

Azilroka's avatar
Azilroka committed
7
RR.Title = PA.ACL['|cFF16C3F2Reputation|r|cFFFFFFFFRewards|r']
Azilroka's avatar
Azilroka committed
8 9 10 11
RR.Description = PA.ACL['Adds Reputation into Quest Log & Quest Frame.']
RR.Authors = 'Azilroka'
RR.isEnabled = false

Azilroka's avatar
Azilroka committed
12 13 14
local _G = _G
local floor = floor
local pairs = pairs
Azilroka's avatar
Azilroka committed
15
local ipairs = ipairs
Azilroka's avatar
Azilroka committed
16 17
local select = select
local wipe = wipe
Azilroka's avatar
Azilroka committed
18
local mod = mod
Azilroka's avatar
Azilroka committed
19 20 21 22 23 24

local GetFactionInfo = _G.GetFactionInfo
local GetFactionInfoByID = _G.GetFactionInfoByID
local GetNumFactions = _G.GetNumFactions
local GetNumQuestLogRewardFactions = _G.GetNumQuestLogRewardFactions
local GetQuestLogRewardFactionInfo = _G.GetQuestLogRewardFactionInfo
25
local C_QuestLog_GetSelectedQuest = _G.C_QuestLog.GetSelectedQuest()
Azilroka's avatar
Azilroka committed
26 27
local GetNumQuestLogChoices = _G.GetNumQuestLogChoices
local GetNumQuestChoices = _G.GetNumQuestChoices
Azilroka's avatar
Azilroka committed
28
local UnitAura = _G.UnitAura
Azilroka's avatar
Azilroka committed
29 30 31 32 33 34
local QuestInfo_GetRewardButton = _G.QuestInfo_GetRewardButton
local SetItemButtonCount = _G.SetItemButtonCount
local SetItemButtonTexture = _G.SetItemButtonTexture
local ExpandFactionHeader = _G.ExpandFactionHeader
local CollapseFactionHeader = _G.CollapseFactionHeader

Azilroka's avatar
Azilroka committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
local REWARDS_SECTION_OFFSET = 5

function RR:BuildFactionHeaders()
	RR.FactionHeaders = {}

	local numFactions, CollapsedHeaders, header = GetNumFactions(), {}

	local i = 1

	while i <= numFactions do
		local _, _, _, _, _, _, _, _, isHeader, isCollapsed, _, _, _, factionID = GetFactionInfo(i)

		if isHeader and isCollapsed then
			CollapsedHeaders[#CollapsedHeaders + 1] = i
			ExpandFactionHeader(i)
			numFactions = GetNumFactions()
		end

		if isHeader then
			header = factionID
		end

		if factionID and header then
			RR.FactionHeaders[factionID] = header
		end

		i = i + 1
	end

	if #CollapsedHeaders > 0 then
		for k = #CollapsedHeaders, 1, -1 do
			CollapseFactionHeader(CollapsedHeaders[k])
		end
	end
end

function RR:GetFactionHeader(factionID)
	return RR.FactionHeaders[factionID]
end

function RR:GetBonusReputation(amtBase, factionID)
	local mult = 1
	local rep = amtBase

	if factionID == 609 or factionID == 576 or factionID == 529 then
		rep = rep * 2
	elseif factionID == 59 then
		rep = rep * 4
	end

	for i = 1, 40 do
		local ID = select(11, UnitAura('player', i))
		if not ID then break end
		if RR.AuraInfo[ID] and ((RR.AuraInfo[ID].faction == factionID) or (RR.AuraInfo[ID].faction == 0)) then
			mult = mult + RR.AuraInfo[ID].bonus
		end
	end

	local hasBonusRepGain = select(15, GetFactionInfoByID(factionID))
	if hasBonusRepGain then
		mult = mult * 2
	end

	return (rep * mult) - rep
end

function RR:Show()
	local numRepFactions = GetNumQuestLogRewardFactions()

	if numRepFactions == 0 then
		return
	end

Azilroka's avatar
Azilroka committed
108
	local numQuestChoices = 0
Azilroka's avatar
Azilroka committed
109

Azilroka's avatar
Azilroka committed
110
	if ( _G.QuestInfoFrame.questLog ) then
111
		local questID = C_QuestLog_GetSelectedQuest()
Azilroka's avatar
Azilroka committed
112
		if _G.C_QuestLog.ShouldShowQuestRewards(questID) then
113
			numQuestChoices = GetNumQuestLogChoices(questID)
Azilroka's avatar
Azilroka committed
114 115 116 117 118 119 120 121 122
		end
	else
		numQuestChoices = GetNumQuestChoices()
	end

	wipe(RR.ReputationInfo)

	for i = 1, numRepFactions do
		local factionID, amtBase = GetQuestLogRewardFactionInfo(i)
Azilroka's avatar
Azilroka committed
123
		local factionName, factionDescription, standingID, barMin, barMax, _, AtWar, ToggleAtWar, isHeader = GetFactionInfoByID(factionID)
Azilroka's avatar
Azilroka committed
124 125 126 127 128 129 130 131 132 133

		if factionName and (AtWar and ToggleAtWar or (not AtWar)) and (not (barMin == barMax)) then
			amtBase = floor(amtBase / 100)

			if PA.MyRace == 'Human' then
				amtBase = amtBase * 1.1
			end

			local amtBonus = RR:GetBonusReputation(amtBase, factionID)

Azilroka's avatar
Azilroka committed
134
			RR.ReputationInfo[factionID] = { Name = factionName, Description = factionDescription, Base = amtBase, Bonus = amtBonus, Header = isHeader, FactionID = factionID, Child = RR:GetFactionHeader(factionID), Standing = standingID }
Azilroka's avatar
Azilroka committed
135 136 137
		end
	end

Azilroka's avatar
Azilroka committed
138 139 140 141 142 143 144 145 146 147 148
	if RR.db.ShowAll then
		for _, Info in pairs(RR.ReputationInfo) do
			if Info.isHeader then
				RR.ReputationInfo[Info.FactionID] = nil
			end
		end
	else
		for _, Info in pairs(RR.ReputationInfo) do
			if (Info.FactionID ~= RR:GetFactionHeader(Info.Child)) and (Info.Child == RR:GetFactionHeader(Info.FactionID)) and (Info.Base == (RR.ReputationInfo[Info.Child] and RR.ReputationInfo[Info.Child].Base or 0)) then
				RR.ReputationInfo[Info.FactionID] = nil
			end
Azilroka's avatar
Azilroka committed
149 150 151
		end
	end

Azilroka's avatar
Azilroka committed
152
	local rewardsFrame, lastFrame = _G.QuestInfo_ShowRewards()
Azilroka's avatar
Azilroka committed
153
	if not rewardsFrame then return end
Azilroka's avatar
Azilroka committed
154
	local buttonHeight = rewardsFrame.RewardButtons[1]:GetHeight()
Azilroka's avatar
Azilroka committed
155

Azilroka's avatar
Azilroka committed
156 157 158 159 160 161
	local rewardButtons = rewardsFrame.RewardButtons;
	local buttonIndex = 1
	for index, rewardButton in ipairs(rewardButtons) do
		if not rewardButton:IsShown() then
			buttonIndex = index
			break
Azilroka's avatar
Azilroka committed
162 163 164
		end
	end

Merathilis's avatar
Merathilis committed
165
	local Height = _G.QuestInfoFrame.rewardsFrame:GetHeight()
Azilroka's avatar
Azilroka committed
166

Azilroka's avatar
Azilroka committed
167 168
	if not _G.QuestInfoFrame.rewardsFrame:IsShown() then
		_G.QuestInfoFrame.rewardsFrame:Show()
Azilroka's avatar
Azilroka committed
169 170
	end

Azilroka's avatar
Azilroka committed
171
	for _, Info in pairs(RR.ReputationInfo) do
Azilroka's avatar
Azilroka committed
172
		local questItem = QuestInfo_GetRewardButton(rewardsFrame, buttonIndex)
Azilroka's avatar
1.39  
Azilroka committed
173 174
		if questItem then
			questItem:Show()
Azilroka's avatar
Azilroka committed
175

Azilroka's avatar
Azilroka committed
176 177
			questItem.type = "reward"
			questItem.objectType = "reputation"
Azilroka's avatar
Azilroka committed
178

Azilroka's avatar
1.39  
Azilroka committed
179
			questItem.Name:SetText(Info.Name)
Azilroka's avatar
Azilroka committed
180
			SetItemButtonCount(questItem, Info.Base + Info.Bonus)
Azilroka's avatar
Cleanup  
Azilroka committed
181
			SetItemButtonTexture(questItem, PA.MyFaction and (PA.MyFaction == 'Neutral' and 'Interface/Icons/Achievement_Character_Pandaren_Female' or ('Interface/Icons/PVPCurrency-Conquest-%s'):format(PA.MyFaction)))
Azilroka's avatar
Azilroka committed
182
			questItem.IconBorder:Hide()
Azilroka's avatar
Azilroka committed
183

Azilroka's avatar
Azilroka committed
184 185 186 187
			local r, g, b = 1, 1, 1
			if Info.Base < 0 then r, g, b = 1, 0, 0 elseif Info.Bonus > 0 then r, g, b = 0, 1, 0 end

			questItem.Count:SetTextColor(r, g, b)
Azilroka's avatar
Azilroka committed
188

Azilroka's avatar
Azilroka committed
189
			if (buttonIndex > 1) then
Azilroka's avatar
Azilroka committed
190
				if mod(buttonIndex, 2) == 1 then
Azilroka's avatar
1.39  
Azilroka committed
191 192 193 194
					Height = Height + buttonHeight + REWARDS_SECTION_OFFSET
					lastFrame = questItem
				end
			else
Azilroka's avatar
Azilroka committed
195 196 197
				Height = Height + buttonHeight + REWARDS_SECTION_OFFSET
				lastFrame = questItem
			end
Azilroka's avatar
Azilroka committed
198 199

			buttonIndex = buttonIndex + 1
Azilroka's avatar
1.39  
Azilroka committed
200
		end
Azilroka's avatar
Azilroka committed
201 202
	end

Azilroka's avatar
Azilroka committed
203
	if ( numQuestChoices == 1 ) then
Azilroka's avatar
Azilroka committed
204
		local a, b, c, d, e = _G.QuestInfoFrame.rewardsFrame.ItemReceiveText:GetPoint()
Azilroka's avatar
Azilroka committed
205
		_G.QuestInfoFrame.rewardsFrame.ItemReceiveText:SetPoint(a, b, c, d, e - ((((buttonIndex - 1) % 2) == 1 and (((buttonIndex - 1) / 2) * (buttonHeight + REWARDS_SECTION_OFFSET)) or 0)))
Azilroka's avatar
Maybe?  
Azilroka committed
206
	end
Azilroka's avatar
Azilroka committed
207

Azilroka's avatar
Azilroka committed
208 209
	_G.QuestInfoFrame.rewardsFrame:Show()
	_G.QuestInfoFrame.rewardsFrame:SetHeight(Height)
Azilroka's avatar
Azilroka committed
210 211 212
end

function RR:GetOptions()
Azilroka's avatar
Azilroka committed
213
	PA.Options.args.ReputationReward = PA.ACH:Group(RR.Title, RR.Description, nil, nil, function(info) return RR.db[info[#info]] end, function(info, value) RR.db[info[#info]] = value end)
Azilroka's avatar
Azilroka committed
214
	PA.Options.args.ReputationReward.args.Description = PA.ACH:Description(RR.Description, 0)
Azilroka's avatar
Azilroka committed
215
	PA.Options.args.ReputationReward.args.Enable = PA.ACH:Toggle(PA.ACL['Enable'], nil, 1, nil, nil, nil, nil, function(info, value) RR.db[info[#info]] = value if (not RR.isEnabled) then RR:Initialize() else _G.StaticPopup_Show('PROJECTAZILROKA_RL') end end)
Azilroka's avatar
Azilroka committed
216

Azilroka's avatar
Azilroka committed
217
	PA.Options.args.ReputationReward.args.General = PA.ACH:Group(PA.ACL['General'], nil, 2)
Azilroka's avatar
Azilroka committed
218
	PA.Options.args.ReputationReward.args.General.inline = true
Azilroka's avatar
Azilroka committed
219 220 221 222
	PA.Options.args.ReputationReward.args.General.args.ShowAll = PA.ACH:Toggle(PA.ACL['Show All Reputation'], nil, 1)

	PA.Options.args.ReputationReward.args.AuthorHeader = PA.ACH:Header(PA.ACL['Authors:'], -2)
	PA.Options.args.ReputationReward.args.Authors = PA.ACH:Description(RR.Authors, -1, 'large')
Azilroka's avatar
Azilroka committed
223 224 225
end

function RR:BuildProfile()
Azilroka's avatar
Azilroka committed
226
	PA.Defaults.profile.ReputationReward = { Enable = true }
Azilroka's avatar
Azilroka committed
227 228
end

Azilroka's avatar
Azilroka committed
229
function RR:UpdateSettings()
Azilroka's avatar
Azilroka committed
230
	RR.db = PA.db.ReputationReward
Azilroka's avatar
Azilroka committed
231 232 233 234
end

function RR:Initialize()
	RR:UpdateSettings()
Azilroka's avatar
Azilroka committed
235 236 237 238 239

	if RR.db.Enable ~= true then
		return
	end

Azilroka's avatar
Azilroka committed
240 241
	RR.isEnabled = true

Azilroka's avatar
Azilroka committed
242 243 244 245 246 247
	RR.ReputationInfo = {}

	-- ID = { bonus = .%, faction = factionID or 0 }
	RR.AuraInfo = {
		[61849] = { bonus = .1, faction = 0 },		--
		[24705] = { bonus = .1, faction = 0 },		--
248
		[95987] = { bonus = .1, faction = 0 },		--
Azilroka's avatar
Azilroka committed
249 250 251 252 253 254 255 256 257 258 259 260 261
		[39913] = { bonus = .1, faction = 947 },	-- Thrallmar
		[39911] = { bonus = .1, faction = 946 },	-- Honor Hold
		[39953] = { bonus = .1, faction = 1031 },	-- Sha'tar
		[46668] = { bonus = .1, faction = 0 },		-- Darkmoon Faire
		[136583] = { bonus = .1 , faction = 0 },	-- Darkmoon Faire

	--	["Banner of Cooperation"] = { bonus = .05, faction = 0 },
	--	["Standard of Unity"] = { bonus = .1, faction = 0 },
	--	["Battle Standard of Coordination"] = { bonus = .15, faction = 0 },
	}

	RR:BuildFactionHeaders()

Azilroka's avatar
RR  
Azilroka committed
262
	RR:SecureHook('QuestInfo_Display', 'Show')
Azilroka's avatar
Azilroka committed
263
end