Quantcast
--[[
Event Flow --> UNIT_SPELLCAST_SUCCEDED --> Prospecting --> Set Multiplier --> LOOT_OPENED --> parse --> ITEM_LOCKED (Get Item ID) --> LOOT_CLOSED (Add Results)
									   --> Mass Prospecting (Get Item ID from spell) --> Set Multiplier --> CHAT_MSG_LOOT --> parse --> TRADE_SKILL_UPDATE (Add Results)

]]

--[[
Declarations of constants.
]]

local PROSPECT_SPELLID = 31252
local MILLING_SPELLID = 51005
local MASS_PROSPECT_FELSLATE_SPELLID = 225904
local MASS_PROSPECT_LEYSTONE_SPELLID = 225902
local MASS_PROSPECT_EMPYRIUM_SPELLID = 247761

local MASS_PROSPECT_MONELITE_SPELLID = 256612
local MASS_PROSPECT_STORMSILVER_SPELLID = 256614
local MASS_PROSPECT_PLATINUM_SPELLID = 256623

local MASS_MILLING_YSERALLINE_SPELLID = 210116
local MASS_MILLING_DREAMLEAF_SPELLID = 209659
local MASS_MILLING_FOXFLOWER_SPELLID = 209660
local MASS_MILLING_FOXFLOWER_SPELLID = 209660
local MASS_MILLING_FJARNSKAGGL_SPELLID = 209661
local MASS_MILLING_FELWORT_SPELLID = 209664
local MASS_MILLING_AETHRIL_SPELLID = 209658
local MASS_MILLING_ASTRALGLORY_SPELLID = 247861
local MASS_MILLING_STARLIGHTROSE_SPELLID = 209662

local MASS_MILLING_RiVERBUD_SPELLID = 256217
local MASS_MILLING_SEASTALK_SPELLID = 256223
local MASS_MILLING_STARMOSS_SPELLID = 256218
local MASS_MILLING_AKUNDASBITE_SPELLID = 256219
local MASS_MILLING_WINTERSKISS_SPELLID = 256220
local MASS_MILLING_SIRENSPOLLEN_SPELLID = 256221
local MASS_MILLING_ANCHORWEED_SPELLID = 256308


local PROSPECT = GetSpellInfo(PROSPECT_SPELLID)
local MILLING = GetSpellInfo(MILLING_SPELLID)
local MASS_PROSPECT_FELSLATE = GetSpellInfo(MASS_PROSPECT_FELSLATE_SPELLID)
local MASS_PROSPECT_LEYSTONE = GetSpellInfo(MASS_PROSPECT_LEYSTONE_SPELLID)
local MASS_PROSPECT_EMPYRIUM = GetSpellInfo(MASS_PROSPECT_EMPYRIUM_SPELLID)

local MASS_PROSPECT_MONELITE    = GetSpellInfo(MASS_PROSPECT_MONELITE_SPELLID)
local MASS_PROSPECT_STORMSILVER = GetSpellInfo(MASS_PROSPECT_STORMSILVER_SPELLID)
local MASS_PROSPECT_PLATINUM    = GetSpellInfo(MASS_PROSPECT_PLATINUM_SPELLID)

local MASS_MILLING_YSERALLINE = GetSpellInfo(MASS_MILLING_YSERALLINE_SPELLID)
local MASS_MILLING_DREAMLEAF = GetSpellInfo(MASS_MILLING_DREAMLEAF_SPELLID)
local MASS_MILLING_FOXFLOWER = GetSpellInfo(MASS_MILLING_FOXFLOWER_SPELLID)
local MASS_MILLING_FJARNSKAGGL = GetSpellInfo(MASS_MILLING_FJARNSKAGGL_SPELLID)
local MASS_MILLING_FELWORT = GetSpellInfo(MASS_MILLING_FELWORT_SPELLID)
local MASS_MILLING_AETHRIL = GetSpellInfo(MASS_MILLING_AETHRIL_SPELLID)
local MASS_MILLING_ASTRALGLORY = GetSpellInfo(MASS_MILLING_ASTRALGLORY_SPELLID)
local MASS_MILLING_STARLIGHTROSE = GetSpellInfo(MASS_MILLING_STARLIGHTROSE_SPELLID)

local MASS_MILLING_RiVERBUD = GetSpellInfo(MASS_MILLING_RiVERBUD_SPELLID)
local MASS_MILLING_SEASTALK = GetSpellInfo(MASS_MILLING_SEASTALK_SPELLID)
local MASS_MILLING_STARMOSS = GetSpellInfo(MASS_MILLING_STARMOSS_SPELLID)
local MASS_MILLING_AKUNDASBITE = GetSpellInfo(MASS_MILLING_AKUNDASBITE_SPELLID)
local MASS_MILLING_WINTERSKISS = GetSpellInfo(MASS_MILLING_WINTERSKISS_SPELLID)
local MASS_MILLING_SIRENSPOLLEN = GetSpellInfo(MASS_MILLING_SIRENSPOLLEN_SPELLID)
local MASS_MILLING_ANCHORWEED = GetSpellInfo(MASS_MILLING_ANCHORWEED_SPELLID)

local VALIDSPELLS = {
	[PROSPECT] = true,
	[MILLING] = true,
	[MASS_PROSPECT_LEYSTONE] = true,
	[MASS_PROSPECT_FELSLATE] = true,
	[MASS_PROSPECT_EMPYRIUM] = true,
	[MASS_MILLING_YSERALLINE] = true,
	[MASS_MILLING_DREAMLEAF] = true,
	[MASS_MILLING_FOXFLOWER] = true,
	[MASS_MILLING_FJARNSKAGGL] = true,
	[MASS_MILLING_FELWORT] = true,
	[MASS_MILLING_AETHRIL] = true,
	[MASS_MILLING_ASTRALGLORY] = true,
	[MASS_MILLING_STARLIGHTROSE] = true,
	[MASS_MILLING_RiVERBUD] = true,
	[MASS_MILLING_SEASTALK] = true,
	[MASS_MILLING_STARMOSS] = true,
	[MASS_MILLING_AKUNDASBITE] = true,
	[MASS_MILLING_WINTERSKISS] = true,
	[MASS_MILLING_SIRENSPOLLEN] = true,
	[MASS_MILLING_ANCHORWEED] = true,
}

--[[
Local Variables
]]
local ContainerID = nil
local Table = {}
local ParseResults = false
local MassMultiplier = 1
local Results = {}

--[[
Initialize - Dets default config variables if they don't exist, ensures all our functions exist and are included in the table in their current form.
]]
local function Initialize()
	--[[
	Sets Default Variables
	]]
	if not ProspectMe.Config then
		ProspectMe.Config = {
			ShowQualities = {
				Junk = false,
				Common = false,
				Uncommon = true,
				Rare = true,
				Epic = true,
			},
			PerSession = false,
			ShowPercent = true,
			ShowNumber = true,
			ShowEconomics = true,
		}
	end
	if not ProspectMe.Results then
		ProspectMe.Results = {}
	end
	ProspectMe.Session = {}

	--[[
	Sets Global Constants
	]]
	if not ProspectMe.Vars then
		ProspectMe.Vars = {}
	end
	ProspectMe.Vars.ORE = select(7,GetItemInfo(123918)) -- Get Ore Subclass from a known quantity (leystone ore)
	ProspectMe.Vars.HERB = select(7,GetItemInfo(128304)) -- Get Herb Subclass from a known quantity (yseralline seed)


	ProspectMe.Debug = function (...)
		for k, v in pairs(...) do
			print("key: " .. k " | value: " .. v)
		end
	end

	--[[
	Begins the capture process, sets variables where we have them. Prospecting and Milling (non MASS) require an extra step to get the ContainerID
	]]
	ProspectMe.BeginCapture = function (spell)
		if spell == PROSPECT or spell == MILLING then
			MassMultiplier = 1
			ParseResults = true
		elseif spell == MASS_PROSPECT_FELSLATE or spell == MASS_PROSPECT_LEYSTONE or spell == MASS_PROSPECT_EMPYRIUM or spell == MASS_PROSPECT_MONELITE or spell == MASS_PROSPECT_STORMSILVER or spell == MASS_PROSPECT_PLATINUM or spell == MASS_MILLING_YSERALLINE or spell == MASS_MILLING_DREAMLEAF or spell == MASS_MILLING_FOXFLOWER or spell == MASS_MILLING_FJARNSKAGGL or spell == MASS_MILLING_FELWORT or spell == MASS_MILLING_AETHRIL or spell == MASS_MILLING_ASTRALGLORY or spell == MASS_MILLING_STARLIGHTROSE or spell == MASS_MILLING_RiVERBUD or spell == MASS_MILLING_SEASTALK or spell == MASS_MILLING_STARMOSS or spell == MASS_MILLING_AKUNDASBITE or spell == MASS_MILLING_WINTERSKISS or spell == MASS_MILLING_SIRENSPOLLEN or spell == MASS_MILLING_ANCHORWEED then
			MassMultiplier = 20
			ParseResults = true
			C_Timer.After(0.5, function () if ParseResults then ProspectMe.EndCapture() end end ) --Fallback if you're using the tradeskill buttons to craft with the window closed.
			if spell == MASS_PROSPECT_FELSLATE then
				ContainerID = 123919
			end
			if spell ==  MASS_PROSPECT_LEYSTONE then
				ContainerID = 123918
			end
			if spell ==  MASS_PROSPECT_EMPYRIUM then
				ContainerID = 151564
			end
			if spell == MASS_PROSPECT_MONELITE then
				ContainerID = 152512
			end
			if spell ==  MASS_PROSPECT_STORMSILVER then
				ContainerID = 152579
			end
			if spell ==  MASS_PROSPECT_PLATINUM then
				ContainerID = 152513
			end
			if spell == MASS_MILLING_YSERALLINE then
				ContainerID = 128304
			end
			if spell == MASS_MILLING_DREAMLEAF then
				ContainerID = 124102
			end
			if spell == MASS_MILLING_FOXFLOWER then
				ContainerID = 124103
			end
			if spell == MASS_MILLING_FJARNSKAGGL then
				ContainerID = 124104
			end
			if spell == MASS_MILLING_FELWORT then
				ContainerID = 124106
			end
			if spell == MASS_MILLING_AETHRIL then
				ContainerID = 124101
			end
			if spell == MASS_MILLING_ASTRALGLORY then
				ContainerID = 151565
			end
			if spell == MASS_MILLING_STARLIGHTROSE then
				ContainerID = 124105
			end
			if spell == MASS_MILLING_RiVERBUD then
				ContainerID = 152505
			end
			if spell == MASS_MILLING_SEASTALK then
				ContainerID = 152511
			end
			if spell == MASS_MILLING_STARMOSS then
				ContainerID = 152506
			end
			if spell == MASS_MILLING_AKUNDASBITE then
				ContainerID = 152507
			end
			if spell == MASS_MILLING_WINTERSKISS then
				ContainerID = 152508
			end
			if spell == MASS_MILLING_SIRENSPOLLEN then
				ContainerID = 152509
			end
			if spell == MASS_MILLING_ANCHORWEED then
				ContainerID = 152510
			end
		else
			ParseResults = false
		end

		Results = {}
	end

	--[[
	Ends the capture process and resets variables so they're ready for use the next time.
	]]
	ProspectMe.EndCapture = function (event, ...)
		if ParseResults then
			ProspectMe.AddEntry(ContainerID, MassMultiplier, Results)
		end
		ParseResults = false
		MassMultiplier = 1
		ContainerID = nil
	end

	--[[
	Creates an table entry, if it does not exist, and adds results to the entry.
	Expects the Item ID and pairs of arguments in table with key being the result's ItemID and value being the quantity returned
	]]
	ProspectMe.AddEntry = function (ItemID, BatchSize, ResultsTable)
		if ProspectMe.Tables.Ores[ItemID] or ProspectMe.Tables.Herbs[ItemID] then
			if not ProspectMe.Results[ItemID] then
				ProspectMe.Results[ItemID] = { TimesProspected = 0 }
			end
			if not ProspectMe.Session[ItemID] then
				ProspectMe.Session[ItemID] = { TimesProspected = 0 }
			end
			for k, v in pairs(ResultsTable) do
				if not ProspectMe.Results[ItemID][k] then
					ProspectMe.Results[ItemID][k] = v
				else
					ProspectMe.Results[ItemID][k] = ProspectMe.Results[ItemID][k] + v
				end
				if not ProspectMe.Session[ItemID][k] then
					ProspectMe.Session[ItemID][k] = v
				else
					ProspectMe.Session[ItemID][k] = ProspectMe.Session[ItemID][k] + v
				end
			end
			ProspectMe.Results[ItemID].TimesProspected = ProspectMe.Results[ItemID].TimesProspected + BatchSize
			ProspectMe.Session[ItemID].TimesProspected = ProspectMe.Session[ItemID].TimesProspected + BatchSize
		else
			print("Attempted to create invalid entry for ID: ", ItemID)
		end
	end

	--[[
	Parses the results of the spellcast or loot containerand returns a table of those results in key/value pairs of item/quantity.
	Expects an event and a set of arguments if the event has them.
	]]
	ProspectMe.GetResults = function (event, ...)
		if event == "CHAT_MSG_LOOT" then
			local ItemID = tonumber((...):match("Hitem:(%d+)"))
			if ItemID == 129099 or ItemID == 130200 or ItemID == 130201 or ItemID == 130202 or ItemID == 130203 or ItemID == 130204 then
				ItemID = 129100
			end
			local Quantity = tonumber((...):match("|h|rx(%d+)"))
			if Quantity == nil then
				Quantity = 1
			end
			Results[ItemID] = Quantity
		end
		if event == "LOOT_OPENED" then
			for i = 1, GetNumLootItems() do
	        	local ItemID = tonumber(GetLootSlotLink(i):match("Hitem:(%d+)"))
	        	if ItemID == 129099 or ItemID == 130200 or ItemID == 130201 or ItemID == 130202 or ItemID == 130203 or ItemID == 130204 then
	           		ItemID = 129100
	        	end
				local Quantity = select(3, GetLootSlotInfo(i))
	        	Results[ItemID] = Quantity
	        end
	    end
	end
end


local function EventHandler(self, event, ...)
	if event == "VARIABLES_LOADED" then
		Initialize()
	end

	if event == "UNIT_SPELLCAST_SUCCEEDED" then
		local unit, _ , spellID = ...
		local spellInfo = GetSpellInfo(spellID);
		if unit == "player" and VALIDSPELLS[spellInfo] then
			ProspectMe.BeginCapture(spellInfo)
		end
	end
	if event == "CHAT_MSG_LOOT" or "LOOT_OPENED" then
		if ParseResults then
			ProspectMe.GetResults(event, ...)
		end
		ProspectMeDebug = Results
	end
	if event == "ITEM_LOCKED" then
		if ParseResults then
			local bag, slot = ...
			ContainerID = select(10, GetContainerItemInfo(bag, slot))
		end
	end
	if event == "TRADE_SKILL_LIST_UPDATE" or event == "LOOT_CLOSED" then
		if ParseResults then
			ProspectMe.EndCapture()
		end
	end
end

local frame = CreateFrame("FRAME", "ProspectMe")
frame:RegisterEvent("VARIABLES_LOADED")
frame:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
frame:RegisterEvent("CHAT_MSG_LOOT")
frame:RegisterEvent("LOOT_OPENED")
frame:RegisterEvent("ITEM_LOCKED")
frame:RegisterEvent("LOOT_CLOSED")
frame:RegisterEvent("TRADE_SKILL_LIST_UPDATE")
frame:SetScript("OnEvent", EventHandler)