-
Notifications
You must be signed in to change notification settings - Fork 0
/
Item.lua
194 lines (183 loc) · 4.58 KB
/
Item.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
-- namespace
local _, ns = ...;
---
-- The item contains the roles, classes and raids information for a particular
-- item.
--
local Item = {
-- the unique identifier for the item
itemId,
-- the name of the item
name,
-- a texture used to display the item
texture,
-- the link used to post the item in the chat
link,
-- the item quality color
color,
-- a map (role -> priority) of roles assigned to this item
roles,
-- a set of classes that can use this item
classes,
-- a set of raids where this item drops
raids
}
Item.__index = Item
ns.Item = Item
---
-- Loads the item information into the given item, e.g. the localized name and
-- the texture.
--
-- @param #Item item
-- the item to load the item information into
--
local function load(item)
if (not item.loaded) then
local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType,
itemSubType, itemStackCount, itemEquipLoc, itemTexture,
itemSellPrice = GetItemInfo(item.itemId)
if (itemName) then
item.name = itemName
item.texture = itemTexture
item.loaded = true
item.link = itemLink
item.color = ITEM_QUALITY_COLORS[itemRarity]
else
item.texture = select(5, GetItemInfoInstant(item.itemId)) or 134400
item.color = { r = 1, g = 1, b = 1 }
end
end
end
---
-- Creates a new Item with the given item information from the 'ITEM_LIST'.
--
-- @param #table itemInfo
-- the item information
--
-- @return #Item
-- the item
--
function Item.fromInfo(itemInfo)
local self = setmetatable(itemInfo, Item)
return self
end
---
-- Returns the localized name of the item.
--
-- @return #string
-- the name of the item
--
function Item:getName()
local item = self
load(item)
return item.name
end
---
-- Returns the item link of this item.
--
-- @return #string
-- the item link
--
function Item:getLink()
local item = self
load(item)
return item.link
end
---
-- Returns the texture path for the item.
--
-- @return #number
-- the texture path
--
function Item:getTexture()
local item = self
load(item)
return item.texture
end
---
-- Returns the item quality color, a table with the key values (r,g,b).
--
-- @return #table
-- the quality color
--
function Item:getColor()
local item = self
load(item)
return item.color
end
---
-- Indicates whether the given class can use this item.
--
-- @param #string class
-- the class constant, e.g. WARRIOR
--
-- @return #boolean
-- true if the class can use this item, nil otherwise
--
function Item:isForClass(class)
local item = self
if (class and item.classes[class]) then
return true
end
end
---
-- Indicates whether this item is assigned to one of the given roles.
--
-- @param #table roles
-- the roles to check (the role must be the key of the table)
--
-- @return #boolean
-- true if this item has one of the roles, nil otherwise
--
function Item:hasRole(roles)
local item = self
for role in pairs (roles) do
if (item.roles[role]) then
return true
end
end
end
---
-- Indicates whether the this item drops in at least one of the given raids.
--
-- @param #set raids
-- the raid names to check for
--
-- @return #boolean
-- true if this item drops in the raid, nil otherwise
--
function Item:dropsIn(raids)
local item = self
for raid in pairs(raids or {}) do
if (item.raids[raid]) then
return true
end
end
end
---
-- Returns the priority for this item for the given player or returns
-- nil if the player has no priority.
--
-- @param #Player player
-- the player to retrieve the priority on this item
--
-- @return #number
-- the priority or nil
--
function Item:getPriority(player)
local item = self
if (player) then
-- get the best role priority
local rolePriority
for role, prio in pairs(item.roles) do
if (player.roles[role]) then
if (rolePriority) then
rolePriority = min(rolePriority, prio)
else
rolePriority = prio
end
end
end
return rolePriority
end
end