I am tired of this timing out
[QuestHelper.git] / collect_equip.lua
blobf0964025b22c28cc8cf75c88e87a5e69a153fd71
1 QuestHelper_File["collect_equip.lua"] = "Development Version"
2 QuestHelper_Loadtime["collect_equip.lua"] = GetTime()
4 local debug_output = false
5 if QuestHelper_File["collect_equip.lua"] == "Development Version" then debug_output = true end
7 local GetItemType
8 local Notifier
9 local GetSpecBolus
11 local QHCI
13 -- why does this need to exist
14 local invloc_lookup_proto = {
15 INVTYPE_HEAD = {"HeadSlot"},
16 INVTYPE_NECK = {"NeckSlot"},
17 INVTYPE_SHOULDER = {"ShoulderSlot"},
18 INVTYPE_CHEST = {"ChestSlot"},
19 INVTYPE_ROBE = {"ChestSlot"},
20 INVTYPE_WAIST = {"WaistSlot"},
21 INVTYPE_LEGS = {"LegsSlot"},
22 INVTYPE_FEET = {"FeetSlot"},
23 INVTYPE_WRIST = {"WristSlot"},
24 INVTYPE_HAND = {"HandsSlot"},
25 INVTYPE_FINGER = {"Finger0Slot", "Finger1Slot"},
26 INVTYPE_TRINKET = {"Trinket0Slot", "Trinket1Slot"},
27 INVTYPE_CLOAK = {"BackSlot"},
28 INVTYPE_WEAPON = {"MainHandSlot", "SecondaryHandSlot"},
29 INVTYPE_SHIELD = {"SecondaryHandSlot"},
30 INVTYPE_2HWEAPON = {"MainHandSlot"},
31 INVTYPE_WEAPONMAINHAND = {"MainHandSlot"},
32 INVTYPE_WEAPONOFFHAND = {"SecondaryHandSlot"},
33 INVTYPE_HOLDABLE = {"RangedSlot"},
34 INVTYPE_RANGED = {"RangedSlot"},
35 INVTYPE_THROWN = {"RangedSlot"},
36 INVTYPE_RANGEDRIGHT = {"RangedSlot"},
37 INVTYPE_RELIC = {"RangedSlot"},
40 local invloc_lookup = {}
42 for k, v in pairs(invloc_lookup_proto) do
43 local temp = {}
44 for _, tv in pairs(v) do
45 local gisi = GetInventorySlotInfo(tv)
46 QuestHelper:TextOut(string.format("%s %s", tv, gisi))
47 table.insert(temp, (GetInventorySlotInfo(tv)))
48 end
49 invloc_lookup[k] = temp
50 end
52 local function Recheck(item, location, competing)
53 local replaced = nil
54 local confused = false
56 for i, v in pairs(invloc_lookup[location]) do
57 if competing[i] then
58 local ilink = GetInventoryItemLink("player", v)
59 if ilink then
60 local itype = GetItemType(ilink)
61 local eqtext = nil
62 if itype == item then
63 replaced = competing[i]
64 elseif itype == competing[i] then
65 else
66 confused = true
67 end
68 end
69 end
70 end
72 if confused then
73 if debug_output then QuestHelper:TextOut(string.format("Confused about %s", GetItemInfo(item))) end
74 return
75 end
77 if not QHCI[item] then QHCI[item] = {} end
78 if replaced then
79 if debug_output then QuestHelper:TextOut(string.format("Equipped %s over %s", GetItemInfo(item), GetItemInfo(replaced))) end
80 QHCI[item].equip_yes = (QHCI[item].equip_yes or "") .. string.format("I%di%s", replaced, GetSpecBolus())
81 else
82 for _, v in pairs(competing) do
83 if debug_output then QuestHelper:TextOut(string.format("Did not equip %s over %s", GetItemInfo(item), GetItemInfo(v))) end
84 QHCI[item].equip_no = (QHCI[item].equip_no or "") .. string.format("I%di%s", v, GetSpecBolus())
85 end
86 end
87 end
89 local function Looted(message)
90 if string.find(message, string.gsub(LOOT_ITEM_CREATED_SELF, "%%.*", "")) then -- YOU CANNOT EAT A PURSE
91 return
92 end
94 local item = GetItemType(message, true)
96 local name, _, quality, ilvl, min, itype, isubtype, _, equiploc, _ = GetItemInfo(item)
98 if name and IsEquippableItem(item) and min <= UnitLevel("player") and invloc_lookup[equiploc] then -- The level comparison may be redundant
99 local competing = {}
100 local nonempty = false
101 for i, v in pairs(invloc_lookup[equiploc]) do
102 local litem = GetInventoryItemLink("player", v)
103 if litem then litem = GetItemType(litem) end
104 if litem and litem ~= item then competing[i] = litem nonempty = true end
107 if not nonempty then return end -- congratulations you are better than nothing, we do not care
109 Notifier(GetTime() + 5 * 60, function () Recheck(item, equiploc, competing) end)
113 function QH_Collect_Equip_Init(QHCData, API)
114 if not QHCData.item then QHCData.item = {} end
115 QHCI = QHCData.item
117 API.Registrar_EventHook("CHAT_MSG_LOOT", Looted)
119 GetItemType = API.Utility_GetItemType
120 Notifier = API.Utility_Notifier
121 GetSpecBolus = API.Utility_GetSpecBolus
122 QuestHelper: Assert(GetItemType)
123 QuestHelper: Assert(Notifier)
124 QuestHelper: Assert(GetSpecBolus)