view ProspectMe.lua @ 21:dc011841fcb4

Added handling of remenant Gem Chips on load - this should clean up and consolidate any lingering chips from 1.7.0.2
author Geoff Brock <mischivin@gmail.com>
date Sun, 04 Sep 2016 21:53:29 -0400
parents 8e12723d408d
children c11461d5095c
line wrap: on
line source
local PROSPECT_ID = 31252
local MILLING_ID = 51005
local MASS_PROSPECT_FELSLATE_ID = 225902
local MASS_PROSPECT_LEYSTONE_ID = 225902
local MASS_MILLING_YSERALLINE_ID = 210116
local PROSPECT = GetSpellInfo(PROSPECT_ID):lower()
local MILLING = GetSpellInfo(MILLING_ID):lower()
local MASS_PROSPECT_FELSLATE = GetSpellInfo(MASS_PROSPECT_FELSLATE_ID):lower()
local MASS_PROSPECT_LEYSTONE = GetSpellInfo(MASS_PROSPECT_FELSLATE_ID):lower()
local MASS_MILLING_YSERALLINE = GetSpellInfo(MASS_PROSPECT_FELSLATE_ID):lower()
local containerID, containerLink = nil, nil
local getContents = false
local bulkMultiplier = 1 --This will be used for mass prospecting/milling in Legion

local function CreateTableEntry(id)
	local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(id)
	PM_ItemTable[id] = {}
	PM_ItemTable[id].name = name 
	PM_ItemTable[id].link = link
	PM_ItemTable[id].quality = quality
	PM_ItemTable[id].iLevel = iLevel
	PM_ItemTable[id].reqLevel = reqLevel
	PM_ItemTable[id].class = class
	PM_ItemTable[id].subclass = subclass
	PM_ItemTable[id].maxStack = maxStack
	PM_ItemTable[id].equipSlot = equipSlot
	PM_ItemTable[id].texture = texture
	PM_ItemTable[id].vendorPrice = vendorPrice
	PM_ItemTable[id].price = PM_GetItemValue(id)
end

function PM_GemCleanup()
	local cleanupIDs = { "129099", "130200", "130201", "130202", "130203", "130204" }
	if not PM_ItemTable["129100"] then
		CreateTableEntry("129100")
	end
	for i, r in pairs(PM_ResultsTable) do
		for k, v in pairs(cleanupIDs) do 
			if PM_ResultsTable[i][v] then 
				if not PM_ResultsTable[i]["129100"] then
					PM_ResultsTable[i]["129100"] = 0
				end
				PM_ResultsTable[i]["129100"] = PM_ResultsTable[i]["129100"] + PM_ResultsTable[i][v]
				PM_ResultsTable[i][v] = nil
			end
		end
	end
	for k, v in pairs(cleanupIDs) do
		if PM_ItemTable[v] then
			PM_ItemTable[v] = nil
		end
	end
end

local function PM_Init()
	if not PM_ResultsTable then
		PM_ResultsTable = {}
		PM_ItemTable = {}
	end
	PM_SessionTable = {}
	PM_GemCleanup() --Run cleanup on excess Gem Chips created in v1.7.0.2 and earlier
end

--debugging function to print the databases results
function PM_PrintResults()
	for container, k in pairs(PM_ResultsTable) do
		print(PM_ItemTable[container].link, PM_ResultsTable[container].timesProspected)
		for i, num in pairs(k) do 
			if i ~= "timesProspected" then
				print(PM_ItemTable[i].link, num)
			end
		end
	end
end

local function GetResults()
	--Create tables for the Container if it doesn't exist yet
	if not PM_ResultsTable[containerID] then
		PM_ResultsTable[containerID] = {timesProspected = 0}
		CreateTableEntry(containerID)
	end
	
	--Creates a session table entry, this will be cleared on log out/UI reload
	if not PM_SessionTable[containerID] then
		PM_SessionTable[containerID] = {timesProspected = 0}
	end

	for i = 1, GetNumLootItems() do
		local itemID = GetLootSlotLink(i):match("Hitem:(%d+)")
		if itemID == "129099" or itemID == "130200" or itemID == "130201" or itemID == "130202" or itemID == "130203" or itemID == "130204" then --consolidate Colored Gem Chips into their resulting item
			itemID = "129100"
		end
		local quantity = select(3, GetLootSlotInfo(i))
		if not PM_ItemTable[itemID]	then
			CreateTableEntry(itemID)
		end
		if PM_ResultsTable[containerID][itemID] then
			PM_ResultsTable[containerID][itemID] = PM_ResultsTable[containerID][itemID] + quantity
		else
			PM_ResultsTable[containerID][itemID] = quantity
		end
		if PM_SessionTable[containerID][itemID] then
			PM_SessionTable[containerID][itemID] = PM_SessionTable[containerID][itemID] + quantity
		else
			PM_SessionTable[containerID][itemID] = quantity
		end
	end
	
	PM_ResultsTable[containerID].timesProspected = PM_ResultsTable[containerID].timesProspected + bulkMultiplier
	PM_SessionTable[containerID].timesProspected = PM_SessionTable[containerID].timesProspected + bulkMultiplier
end

local function EventHandler(self, event, ...)
	if event == "VARIABLES_LOADED" then
		PM_Init()
		PM_UpdateValues()
	end
	if event == "UNIT_SPELLCAST_INTERRUPTED" then
		local unitID, spell, rank = ...
		spell = spell:lower()
		if unitID == "player" and (spell == PROSPECT or spell == MILLING or spell == MASS_PROSPECT_FELSLATE or spell == MASS_PROSPECT_LEYSTONE or spell == MASS_MILLING_YSERALLINE)then
			getContents = false
		end
	end
	if event == "LOOT_OPENED" then
		if getContents then
			GetResults()
		end
	end
	if event == "LOOT_CLOSED" then
		getContents = false
	end
	if event == "AUCTION_ITEM_LIST_UPDATE" then
		PM_UpdateValues()
	end
end

local frame = CreateFrame("FRAME", "PM_Frame")
frame:RegisterEvent("VARIABLES_LOADED")
frame:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
frame:RegisterEvent("LOOT_OPENED")
frame:RegisterEvent("LOOT_CLOSED")
frame:RegisterEvent("AUCTION_ITEM_LIST_UPDATE")
frame:SetScript("OnEvent", EventHandler)

hooksecurefunc("UseContainerItem", function(...)
	if getContents then
		containerLink = GetContainerItemLink(...)
		containerID = containerLink:match("Hitem:(%d+)")
	end

end)

hooksecurefunc("UseItemByName", function(itemName)
	if getContents then
		containerLink = select(2, GetItemInfo(itemName))
		containerID = containerLink:match("Hitem:(%d+)")
	end
end)

hooksecurefunc("SpellTargetItem", function(itemName) 
	if getContents then 
		containerLink = select(2, GetItemInfo(itemName)) 
		containerID = containerLink:match("Hitem:(%d+)") 
	end 
end)

hooksecurefunc("CastSpell", function(...)
	local spellName = GetSpellInfo(...):lower()
	if spellName == PROSPECT or spellName == MILLING or spellName == MASS_PROSPECT_FELSLATE or spellName == MASS_PROSPECT_LEYSTONE or spellName == MASS_MILLING_YSERALLINE then
		getContents = true
		if spellName == MASS_PROSPECT_FELSLATE or spellName == MASS_PROSPECT_LEYSTONE or spellName == MASS_MILLING_YSERALLINE then
			bulkMultiplier = 4
		else
			bulkMultiplier = 1
		end
	end
end)

hooksecurefunc("CastSpellByID", function(spellID)
	if spellID == PROSPECT_ID or spellID == MILLING_ID or spellID == MASS_PROSPECT_FELSTATE_ID or spellID == MASS_PROSPECT_LEYSTONE_ID or spellID == MASS_MILLING_YSERALLINE_ID then
		getContents = true
		if spellID == MASS_PROSPECT_FELSTATE_ID or spellID == MASS_PROSPECT_LEYSTONE_ID or spellID == MASS_MILLING_YSERALLINE_ID then
			bulkMultiplier = 4
		else
			bulkMultiplier = 1
		end
	end
end)

hooksecurefunc("UseAction", function(actionID)
	local spellID = select(2, GetActionInfo(actionID))
	if spellID == PROSPECT_ID or spellID == MILLING_ID or spellID == MASS_PROSPECT_FELSTATE_ID or spellID == MASS_PROSPECT_LEYSTONE_ID or spellID == MASS_MILLING_YSERALLINE_ID then
		getContents = true
		if spellID == MASS_PROSPECT_FELSTATE_ID or spellID == MASS_PROSPECT_LEYSTONE_ID or spellID == MASS_MILLING_YSERALLINE_ID then
			bulkMultiplier = 4
		else
			bulkMultiplier = 1
		end
	end
end)

hooksecurefunc("CastSpellByName", function(spellName, onSelf)
	spellName = spellName:lower()
	if spellName == PROSPECT or spellName == MILLING or spellName == MASS_PROSPECT_FELSLATE or spellName == MASS_PROSPECT_LEYSTONE or spellName == MASS_MILLING_YSERALLINE then
		getContents = true
		if spellName == MASS_PROSPECT_FELSLATE or spellName == MASS_PROSPECT_LEYSTONE or spellName == MASS_MILLING_YSERALLINE then
			bulkMultiplier = 4
		else
			bulkMultiplier = 1
		end
	end
end)