forked from TekNoLogic/Buffet
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEngine.lua
More file actions
499 lines (444 loc) · 17.1 KB
/
Copy pathEngine.lua
File metadata and controls
499 lines (444 loc) · 17.1 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
local _, ns = ...
-- Imports
local Utility = ns.Utility
local Locales = ns.Locales
-- Local namespace
local Engine = ns.Engine or {}
local ActiveConst = ns.ActiveConst
local string_gsub = string.gsub
local string_match = string.match
local string_lower = string.lower
if not Engine.ScanTooltip then
function Engine.ScanTooltip(itemLink)
local texts = {}
local tooltip = Utility.GetTooltip()
tooltip:SetHyperlink(itemLink)
local lineCount = 0
for i = 2, tooltip:NumLines() do
local text = _G["buffetTooltipTextLeft" .. i]:GetText() or ""
text = Utility.Trim(text)
if text ~= "" then
texts[lineCount] = text
lineCount = lineCount + 1
end
end
tooltip:Hide()
return texts
end
end
function Engine.ParseTexts(texts, itemData)
local itemDescription = ""
local usable = false
for i, v in pairs(texts) do
local text = string_lower(v);
-- Food and Drink
if Locales.KeyWords.FoodAndDrink then
if not itemData.isFoodAndDrink and Utility.StringContains(text, Locales.KeyWords.FoodAndDrink:lower()) then
itemData.isFoodAndDrink = true
end
end
-- Conjured item
if not itemData.isConjured then
if type(Locales.KeyWords.ConjuredItem) == "table" then
for _, s in pairs(Locales.KeyWords.ConjuredItem) do
if Utility.StringContains(text, s:lower()) then
itemData.isConjured = true
break
end
end
elseif type(Locales.KeyWords.ConjuredItem) == "string" then
if Utility.StringContains(text, Locales.KeyWords.ConjuredItem:lower()) then
itemData.isConjured = true
end
end
end
-- Bandage
if not itemData.isBandage and Engine.CheckBandage(text, itemData.itemClassId, itemData.itemSubClassId) then
itemData.isBandage = true
end
-- Potion
if not itemData.isPotion and Engine.CheckPotion(text, itemData.itemClassId, itemData.itemSubClassId) then
itemData.isPotion = true
end
-- Toxic potion
if Locales.KeyWords.ToxicPotion then
if not itemData.isToxicPotion and Utility.StringContains(text, Locales.KeyWords.ToxicPotion:lower()) then
itemData.isToxicPotion = true
end
end
-- well fed
if not itemData.isWellFed then
if type(Locales.KeyWords.WellFed) == "table" then
for _, s in pairs(Locales.KeyWords.WellFed) do
if Utility.StringContains(text, s:lower()) then
itemData.isWellFed = true
break
end
end
elseif type(Locales.KeyWords.WellFed) == "string" then
if Utility.StringContains(text, Locales.KeyWords.WellFed:lower()) then
itemData.isWellFed = true
end
end
end
-- OverTime
if not itemData.isOverTime and Utility.StringContains(text, Locales.KeyWords.OverTime:lower()) then
itemData.isOverTime = true
end
-- Usable item
if not usable and Engine.CheckUsable(text) then
usable = true
end
-- if usable, we should be on the line with health/mana value
if usable and itemDescription == "" then
itemDescription = text
end
end
if usable and itemDescription ~= "" then
-- health
itemData.isHealth = Engine.CheckHealth(itemDescription, itemData.isBandage)
-- mana
if Utility.StringContains(itemDescription, Locales.KeyWords.Mana:lower()) then
itemData.isMana = true
end
if itemData.isHealth or itemData.isMana then
-- FU Blizzard
itemDescription = Engine.ReplaceFakeSpace(itemDescription)
itemDescription = Engine.FixPercentValue(itemDescription)
-- Utility.Debug("desc: ", itemDescription)
-- parse for health and/or mana
itemData = Engine.ParseValues(itemData, itemDescription)
end
end
itemData = Engine.PostParseUpdate(itemData)
itemData = Engine.CheckStaticData(itemData)
return itemData
end
function Engine.GetPvpStatus()
-- all off by default
local inBg, inRatedBg, inArena, inRatedArena, inBrawl = false, false, false, false, false
if Utility.IsClassic then
local r = UnitInBattleground("player")
inBg = (r ~= nil)
end
if Utility.IsTBC then
local r = UnitInBattleground("player")
inBg = (r ~= nil)
inArena, inRatedArena = IsActiveBattlefieldArena()
end
if Utility.IsWLK then
local r = UnitInBattleground("player")
inBg = (r ~= nil)
inArena, inRatedArena = IsActiveBattlefieldArena()
end
if Utility.IsCataclysm then
local r = UnitInBattleground("player")
inBg = (r ~= nil)
inArena, inRatedArena = IsActiveBattlefieldArena()
end
if Utility.IsMists then
local r = UnitInBattleground("player")
inBg = (r ~= nil)
inArena, inRatedArena = IsActiveBattlefieldArena()
end
if Utility.IsRetail then
inBg = C_PvP.IsBattleground() -- since version 8.2.0
inRatedBg = C_PvP.IsRatedBattleground() -- since version 8.2.0
inArena = C_PvP.IsArena() -- since version 8.2.0
inRatedArena = C_PvP.IsRatedArena() -- since version 8.2.0
inBrawl = C_PvP.IsInBrawl() -- since version 7.2.0
end
return inBg, inRatedBg, inArena, inRatedArena, inBrawl
end
function Engine.CheckRestrictionEntry(entry)
local matchMode = entry.matchMode or "any"
local conditions = 0
local matches = 0
if entry.inInstanceIds ~= nil then
conditions = conditions + 1
if Utility.IsPlayerInInstanceId(entry.inInstanceIds) then
matches = matches + 1
end
end
if entry.inMapIds ~= nil then
conditions = conditions + 1
if Utility.IsPlayerInMapId(entry.inMapIds) then
matches = matches + 1
end
end
if entry.inInstanceTypes ~= nil then
conditions = conditions + 1
if Utility.IsPlayerInInstanceType(entry.inInstanceTypes) then
matches = matches + 1
end
end
if entry.inSubZones ~= nil then
conditions = conditions + 1
if Utility.IsPlayerInSubZoneName(entry.inSubZones) then
matches = matches + 1
end
end
if entry.pvp ~= nil then
local inBg, inRatedBg, inArena, inRatedArena, inBrawl = Engine.GetPvpStatus()
if entry.pvp.bg ~= nil then
conditions = conditions + 1
if entry.pvp.bg == true and inBg then
matches = matches + 1
end
end
if entry.pvp.arena ~= nil then
conditions = conditions + 1
if entry.pvp.arena == true and inArena then
matches = matches + 1
end
end
if entry.pvp.brawl ~= nil then
conditions = conditions + 1
if entry.pvp.brawl == true and inBrawl then
matches = matches + 1
end
end
if entry.pvp.ratedBg ~= nil then
conditions = conditions + 1
if entry.pvp.ratedBg == true and inRatedBg then
matches = matches + 1
end
end
if entry.pvp.ratedArena ~= nil then
conditions = conditions + 1
if entry.pvp.ratedArena == true and inRatedArena then
matches = matches + 1
end
end
end
if matchMode == "none" then
return matches == 0
end
if matchMode == "one" then
return matches == 1
end
if matchMode == "any" then
return matches >= 1
end
if matchMode == "all" then
return matches == conditions
end
-- allow for exact matches
if type(matchMode) == "number" then
return matches == matchMode
end
return false
end
function Engine.CheckStaticData(itemData)
if ActiveConst.StaticItemData then
if ActiveConst.StaticItemData[itemData.itemId] ~= nil then
local staticData = ActiveConst.StaticItemData[itemData.itemId]
if staticData.isHealth ~= nil then
itemData.isHealth = staticData.isHealth
end
if staticData.isMana ~= nil then
itemData.isMana = staticData.isMana
end
if staticData.isFoodAndDrink ~= nil then
itemData.isFoodAndDrink = staticData.isFoodAndDrink
end
if staticData.isPotion ~= nil then
itemData.isPotion = staticData.isPotion
end
if staticData.isBandage ~= nil then
itemData.isBandage = staticData.isBandage
end
if staticData.isConjured ~= nil then
itemData.isConjured = staticData.isConjured
end
if staticData.modifiers ~= nil then
for _, entry in pairs(staticData.modifiers) do
conditionValid = true
if entry.conditions then
for _, condition in pairs(entry.conditions) do
if condition.profession then
if not Utility.TableContainsValue(Utility.skillLineIds, condition.profession) then
conditionValid = false
break
end
end
end
end
if conditionValid then
if itemData.isHealth and entry.healthFactor then
itemData.health = itemData.health * entry.healthFactor
end
if itemData.isHealth and entry.healthMaxCap then
itemData.healthMaxCap = entry.healthMaxCap
end
if itemData.isMana and entry.manaFactor then
itemData.mana = itemData.mana * entry.manaFactor
end
end
end
end
end
end
return itemData
end
-- return true if the item is restricted, false otherwise
function Engine.CheckRestriction(itemId)
-- check restricted items against rules
if ActiveConst.Restrictions[itemId] ~= nil then
for _, entry in pairs(ActiveConst.Restrictions[itemId]) do
local valid = Engine.CheckRestrictionEntry(entry)
if valid then
return false, true -- if one entry is valid, item is not currently restricted
end
end
return true, true -- no valid entry
end
return false, false -- no entry
end
function Engine.ExtractValue(value, indexes)
if indexes then
if type(indexes) == "table" then
local value1 = Engine.StripThousandSeparator(value[indexes[1]])
local value2 = Engine.StripThousandSeparator(value[indexes[2]])
value1 = Engine.ReplaceDecimalSeparator(value1)
value2 = Engine.ReplaceDecimalSeparator(value2)
return (tonumber(value1) + tonumber(value2)) / 2
elseif type(indexes) == "number" then
local value = Engine.StripThousandSeparator(value[indexes])
value = Engine.ReplaceDecimalSeparator(value)
return tonumber(value)
end
end
return 0
end
function Engine.LoopPattern(itemData, itemDescription, patterns)
for k, v in ipairs(patterns) do
local value = Engine.Match(itemDescription, v.pattern)
if value and (#value > 0) then
if v.healthIndex then
itemData.health = Engine.ExtractValue(value, v.healthIndex)
if v.healthFactor and (v.healthFactor >0) then
itemData.health = itemData.health * v.healthFactor
end
if v.factor and (v.factor > 0) then
itemData.health = itemData.health * v.factor
end
if v.pct then
itemData.health = itemData.health / 100
end
end
if v.manaIndex then
itemData.mana = Engine.ExtractValue(value, v.manaIndex)
if v.factor and (v.factor > 0) then
itemData.mana = itemData.mana * v.factor
end
if v.pct then
itemData.mana = itemData.mana / 100
end
end
itemData.isPct = v.pct
break
end
end
return itemData
end
function Engine.ParseValues(itemData, itemDescription)
if itemData.isHealth and itemData.isMana then
if Utility.StringContains(itemDescription, Locales.KeyWords.Restores:lower()) then
-- loop on mixed Health+Mana pattern here
itemData = Engine.LoopPattern(itemData, itemDescription, Locales.Patterns.HealthAndMana)
if itemData.isOverTime and itemData.health and (itemData.health > 0) and itemData.mana and (itemData.mana > 0) then
local overTime = string_match(itemDescription, Locales.Patterns.OverTime)
if overTime then
itemData.isOverTime = true
itemData.overTime = tonumber(overTime)
end
end
end
else
if itemData.isHealth then
if itemData.isBandage then
if Utility.StringContains(itemDescription, Locales.KeyWords.Heals:lower()) then
-- loop on Bandage pattern here
itemData = Engine.LoopPattern(itemData, itemDescription, Locales.Patterns.Bandage)
end
else
if Utility.StringContains(itemDescription, Locales.KeyWords.Restores:lower()) then
-- loop on Health pattern here
itemData = Engine.LoopPattern(itemData, itemDescription, Locales.Patterns.Health)
if itemData.health and (itemData.health > 0) and itemData.isOverTime then
local overTime = string_match(itemDescription, Locales.Patterns.OverTime)
if overTime then
itemData.isOverTime = true
itemData.overTime = tonumber(overTime)
end
end
elseif Locales.KeyWords.Consume ~= nil and Utility.StringContains(itemDescription, Locales.KeyWords.Consume:lower()) then
-- loop on Health pattern here
itemData = Engine.LoopPattern(itemData, itemDescription, Locales.Patterns.Health)
if itemData.health and (itemData.health > 0) and itemData.isOverTime then
local overTime = string_match(itemDescription, Locales.Patterns.OverTime)
if overTime then
itemData.isOverTime = true
itemData.overTime = tonumber(overTime)
end
end
end
end
end
if itemData.isMana then
if Utility.StringContains(itemDescription, Locales.KeyWords.Restores:lower()) then
-- loop on Mana pattern here
itemData = Engine.LoopPattern(itemData, itemDescription, Locales.Patterns.Mana)
if itemData.mana and (itemData.mana > 0) and itemData.isOverTime then
local overTime = string_match(itemDescription, Locales.Patterns.OverTime)
if overTime then
itemData.isOverTime = true
itemData.overTime = tonumber(overTime)
end
end
end
end
end
return itemData
end
function Engine.Match(text, pattern)
local p = {string_match(text, pattern)}
return p
end
function Engine.ReplaceFakeSpace(text)
local t = string_gsub(text, " ", " ") -- WTF Blizzard !
return t
end
function Engine.FixPercentValue(text)
local t = text
while Utility.StringContains(t, " %") do
t = string_gsub(t, "%s%%", "%%")
end
return t
end
-- Utility.StringContains(string, needle)
function Engine.StripThousandSeparator(text)
if type(Locales.ThousandSeparator) == "string" then
text = string_gsub(text, Locales.ThousandSeparator, "")
elseif type(Locales.ThousandSeparator) == "table" then
for i, v in ipairs(Locales.ThousandSeparator) do
text = string_gsub(text, v, "")
end
end
return text
end
function Engine.ReplaceDecimalSeparator(text)
if not Locales.DecimalSeparator then
return text
end
if type(Locales.DecimalSeparator) == "string" then
text = string_gsub(text, Locales.DecimalSeparator, ".")
elseif type(Locales.DecimalSeparator) == "table" then
for i, v in ipairs(Locales.DecimalSeparator) do
text = string_gsub(text, v, ".")
end
end
return text
end
ns.Engine = Engine