-
Notifications
You must be signed in to change notification settings - Fork 2
/
itemCache.lua
113 lines (104 loc) · 3.47 KB
/
itemCache.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
108
109
110
111
112
113
PlayerbotsPanel.itemCache = {}
local _self = PlayerbotsPanel.itemCache
local _data = PlayerbotsPanel.data
local _cfg = PlayerbotsPanel.config
local _updateHandler = PlayerbotsPanel.updateHandler
local _cacheTable = {}
local _queryTooltip = CreateFrame("GameTooltip", "PlayerbotsPanelTooltip_query", UIParent, "GameTooltipTemplate")
local _pairs = pairs
local _util = PlayerbotsPanel.broker.util
-- Using big strings as keys is fine in lua, since all strings are interned by default and the table uses reference as key
local _queue = {}
local _tickrate = 1.0 / _cfg.itemCacheAsyncItemsPerSecond
local _nextTick = 0
local _invTypeToEquipSlotTable = {
["INVTYPE_AMMO"] = 0,
["INVTYPE_HEAD"] = 1,
["INVTYPE_NECK"] = 2,
["INVTYPE_SHOULDER"] = 3,
["INVTYPE_BODY"] = 4,
["INVTYPE_CHEST"] = 5,
["INVTYPE_ROBE"] = 5,
["INVTYPE_WAIST"] = 6,
["INVTYPE_LEGS"] = 7,
["INVTYPE_FEET"] = 8,
["INVTYPE_WRIST"] = 9,
["INVTYPE_HAND"] = 10,
["INVTYPE_FINGER"] = 11, -- 12
["INVTYPE_TRINKET"] = 13, -- 14
["INVTYPE_CLOAK"] = 15,
["INVTYPE_WEAPON"] = 16, -- 17
["INVTYPE_SHIELD"] = 17,
["INVTYPE_2HWEAPON"] = 16,
["INVTYPE_WEAPONMAINHAND"] = 16,
["INVTYPE_WEAPONOFFHAND"] = 17,
["INVTYPE_HOLDABLE"] = 17,
["INVTYPE_RANGED"] = 18,
["INVTYPE_THROWN"] = 18,
["INVTYPE_RANGEDRIGHT"] = 18,
["INVTYPE_RELIC"] = 18,
["INVTYPE_TABARD"] = 19,
["INVTYPE_BAG"] = 20, -- 21, 22, 23
["INVTYPE_QUIVER"] = 20, -- 21, 22, 23
}
_self.associatedSlots = {
[11] = 12,
[13] = 14,
[16] = 17
}
function _self:Init()
_updateHandler.onUpdate:Add(_self.ProcessQueue)
end
function _self.ProcessQueue(elapsed)
local time = _updateHandler.totalTime
if _nextTick < time then -- max 1 query per tick
_nextTick = time + _tickrate
local completeQuery = nil
for link, cache in _pairs(_queue) do
local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(link)
if texture ~= nil then -- query complete
cache.name = name
--cache.link = link
cache.quality = quality
cache.iLevel = iLevel
cache.reqLevel = reqLevel
cache.class = class
cache.subclass = subclass
cache.maxStack = maxStack
cache.equipSlot = _invTypeToEquipSlotTable[equipSlot]
cache.texture = texture
cache.vendorPrice = vendorPrice
completeQuery = cache
break
end
end
if completeQuery then
completeQuery.updating = false;
completeQuery.onQueryComplete:Invoke()
completeQuery.onQueryComplete:Clear()
_queue[completeQuery.link] = nil
end
end
end
function _self.GetItemCache(itemLink)
local cache = _cacheTable[itemLink]
if not cache then
cache = {}
_queryTooltip:SetHyperlink(itemLink)
cache.updating = true
cache.onQueryComplete = _util.event.Create()
cache.name = "Updating ..."
cache.link = itemLink
cache.quality = 0
cache.iLevel = 0
cache.reqLevel = 0
cache.texture = _data.textures.slotLoading
cache.vendorPrice = 0
_cacheTable[itemLink] = cache
_queue[itemLink] = cache
end
return cache
end
function _self:QueryItemID(id)
SetItemRef(('item:%d'):format(tonumber(id)))
end