-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloot.lua
360 lines (269 loc) · 9.03 KB
/
loot.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
--[[
Loot class
An attempt to gather all the loot info in one object so that it can be retrieved identically and safely.
In the event of changes to targetInfo/itemInfo, I hope this class will prove its worth.
I still let it give out the full targetInfo and itemInfo tables though, as that can be useful when using libraries and stuff.
--]]
Loot = {}
Loot.__index = Loot -- When calling functions on the instance tables, fallback to the one Loot table.
setmetatable(Loot, {
-- Makes it possible to create instances like this: 'local loot = Loot(args)'
__call = function(class, ...)
return class.Create(...)
end,
})
local Private = {
idCounter = 0,
}
--[[
***************************
Static Functions
***************************
--]]
--[[
Loot.Create(entityId, targetInfo, itemInfo)
Constructor of Loot.
--]]
function Loot.Create(entityId, targetInfo, itemInfo)
if Options['Debug']['LogLootCreateData'] then
Debug.Log('Loot.Create on ' .. tostring(targetInfo.name))
Debug.Log('entityId ' .. tostring(entityId))
Debug.Table('targetInfo', targetInfo)
Debug.Table('itemInfo', itemInfo)
end
-- Create instance
local self = setmetatable({}, Loot)
self.id = Private.GenerateId()
self.createdAt = System.GetClientTime()
self.entityId = entityId
self.targetInfo = targetInfo
self.itemInfo = itemInfo
self.category = Loot.DetermineCategory(targetInfo, itemInfo) -- NewDetermineCategory(self)
self.state = Private.DetermineState(self)
return self
end
--[[
Loot.DetermineCategory(targetInfo, itemInfo)
Determines the LootCategory from target and itemInfo (mostly itemInfo)
--]]
function Loot.DetermineCategory(targetInfo, itemInfo)
local category = LootCategory.Unknown
if IsEquipment(itemInfo) then
category = LootCategory.Equipment
elseif IsModule(itemInfo) then
category = LootCategory.Modules
elseif IsSalvage(itemInfo) then
category = LootCategory.Salvage
elseif IsConsumable(itemInfo) then
category = LootCategory.Consumable
elseif IsMetal(itemInfo) then
category = LootCategory.Metals
elseif IsComponent(itemInfo) then
category = LootCategory.Components
elseif IsCurrency(itemInfo) then
category = LootCategory.Currency
end
if category == LootCategory.Unknown then
--Debug.Warn('Private.Identify LootCategory.Unknown', targetInfo, itemInfo)
-- Well this is basically the 'shit we dont care about category' now ;o
end
if Options['Debug']['LogLootDetermineCategory'] then
Debug.Log(tostring(targetInfo.name) .. ' identified as ' .. tostring(category))
end
return category
end
--[[
Loot.GetDisplayNameOfRarity(rarity)
Some shit right here...
--]]
function Loot.GetDisplayNameOfRarity(rarity)
return ucfirst(rarity) or 'ukwn'
end
--[[
DEPRECATED
Loot.GetRarityIndex(rarity)
Returns the rarity as a number in a series representative of the rarity
I originally wrote and started using this function before I discovered that it had already been implemented in LIB_ITEMS.
--]]
function Loot.GetRarityIndex(rarity)
return LIB_ITEMS.GetRarityValue(rarity) or 0
end
--[[
***************************
Instance Functions
***************************
--]]
function Loot:Update()
self.state = Private.DetermineState(self)
end
function Loot:Destroy()
--Debug.Log('Loot:Destroy for ' ..self.id.. ' '..self:GetName())
self = nil
end
-- Below follows a slew of Getters and Setters.
function Loot:GetId()
return self.id
end
function Loot:GetEntityId()
return self.entityId
end
function Loot:GetTypeId()
return self.targetInfo.itemTypeId
end
function Loot:GetName()
return self.targetInfo.name or self.itemInfo.name
end
function Loot:GetCategory()
return self.category
end
function Loot:GetState()
return self.state
end
function Loot:SetState(newState)
self.state = newState
end
function Loot:GetPos()
return self.targetInfo.lootPos or {x=0, y=0, z=0}
end
function Loot:GetItemLevel()
return self.itemInfo.item_level or 1
end
function Loot:GetRequiredLevel()
return self.itemInfo.required_level or 0
end
function Loot:GetRarity()
return self.itemInfo.rarity or tostring(-1)
end
function Loot:GetRarityValue()
return LIB_ITEMS.GetRarityValue(self:GetRarity()) or 0
end
function Loot:GetQuantity()
return self.targetInfo.quantity or 1
end
function Loot:GetColor()
return LIB_ITEMS.GetItemColor(self.itemInfo)
end
function Loot:GetWebIcon()
return self.itemInfo.web_icon or self.itemInfo.web_icon_id
end
function Loot:GetMultiArt(PARENT, forceWebIcon)
-- Handle optional args
forceWebIcon = forceWebIcon or false
-- Create multiart
local ICON = MultiArt.Create(PARENT)
-- If ability, use ability icon
if not forceWebIcon and self.itemInfo.type == 'ability_module' and self.itemInfo.abilityId then
local abilityinfo = Player.GetAbilityInfo(tonumber(self.itemInfo.abilityId))
if abilityinfo and abilityinfo.iconId then
ICON:SetIcon(abilityinfo.iconId)
return ICON
end
end
-- Otherwise, use web icon
local webIcon = self:GetWebIcon()
if type(webIcon) == "string" then
ICON:SetUrl(webIcon)
else
ICON:SetIcon(webIcon)
end
return ICON
end
function Loot:GetLootedBy()
return self.lootedBy or false
end
function Loot:SetLootedBy(input)
self.lootedBy = input
end
function Loot:GetLootedTo()
return self.lootedTo or false
end
function Loot:SetLootedTo(input)
self.lootedTo = input
end
function Loot:GetCerts()
return self.itemInfo.certifications or false
end
function Loot:GetAsLink()
return ChatLib.EncodeItemLink(self:GetTypeId(), self.itemInfo.hidden_modules, self.itemInfo.slotted_modules) or self:GetName()
end
function Loot:GetCoordLink()
-- In order to work around a problem caused by ChatLib.EncodeCoordLink erroring when there were issues with the Chat server, this function had to be extended a bit.
-- In the event that a proper link cannot be created, this function returns normal text.
local zone = State.zoneId
local instance = Chat.WriteInstanceKey()
local player = Player.GetCharacterId()
if zone and instance and player then
return ChatLib.EncodeCoordLink(self:GetPos(), zone, instance, player)
end
return tostring(self:GetPos())
end
function Loot:ToString()
local output = {}
table.insert(output, 'Loot')
table.insert(output, tostring(self:GetName()))
table.insert(output, 'Category:' .. tostring(self:GetCategory()))
table.insert(output, 'Id:' .. tostring(self:GetId()))
table.insert(output, 'TypeId:' .. tostring(self:GetTypeId()))
table.insert(output, 'State:' .. tostring(self:GetState()))
if(self:GetState() == LootState.Available) then
table.insert(output, 'Entity:' .. tostring(self:GetEntityId()))
elseif(self:GetState() == LootState.Looted) then
table.insert(output, 'LootedBy:' .. tostring(self.lootedBy))
table.insert(output, 'LootedTo:' .. tostring(self.lootedTo))
end
return table.concat(output, ' | ')
end
function Loot:AppendToChat()
ChatLib.AddItemLinkToChatInput(self:GetTypeId(), self.itemInfo.hidden_modules, self.itemInfo.slotted_modules)
end
function Loot:AppendCoordToChat()
local pos = self:GetPos()
local text = tostring(self:GetPos())
local replace = ''
local zone = State.zoneId
local instance = Chat.WriteInstanceKey()
local player = Player.GetCharacterId()
if zone and instance and player then
text = ChatLib.CreateCoordText(pos, zoneId, true)
replace = ChatLib.EncodeCoordLink(pos, zoneId, instance, player)
end
local args = {
text = text,
replaces = {{
match = text,
replace = replace
}},
}
ChatLib.AddTextToChatInput(args)
end
--[[
***************************
Private Functions
***************************
--]]
function Private.GenerateId()
local time = tostring(System.GetLocalUnixTime())
Private.idCounter = Private.idCounter + 1
local occurance = tostring(Private.idCounter)
-- Put it all together
local id = time..occurance
return id
end
function Private.DetermineState(loot)
local state = LootState.Unknown
if loot:GetEntityId() then
if Game.IsTargetAvailable(loot:GetEntityId()) then
state = LootState.Available
elseif IsDebugLoot({entityId=loot:GetEntityId()}) and tonumber(System.GetElapsedTime(loot.createdAt)) < 30 then
state = LootState.Available -- Debug
else
if loot.lootedBy then
state = LootState.Looted
else
state = LootState.Lost
end
end
end
if not state then Debug.Warn('dafaq, determine state returning nil, why?') Debug.Table('determinestate loot', loot) end
return state
end