This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
forked from HgAlexx/Buffet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.lua
165 lines (137 loc) · 4.95 KB
/
Engine.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
local _, ns = ...
-- Imports
local Utility = ns.Utility
local Locales = ns.Locales
-- Local namespace
local Engine = ns.Engine or {}
local string_gsub = string.gsub
local string_match = string.match
function Engine.ScanTooltip(itemLink, itemLevel)
local failedAttempt
local texts = {}
local tooltip = Utility.GetTooltip()
tooltip:SetHyperlink(itemLink)
local isConjured = false
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
local lowerText = text:lower()
lineCount = lineCount + 1
if Utility.StringContains(lowerText, Locales.KeyWords.ConjuredItem:lower()) then
isConjured = true
end
end
end
tooltip:Hide()
-- sometimes tooltips are not properly generated on first pass, all interesting items should have at least 3 lines, 4 for conjured items
local neededLines = 3
if isConjured then
neededLines = 4
end
-- except low level item which can have only 2 lines..
if itemLevel and itemLevel < 10 then
neededLines = neededLines - 1
end
-- for item name (skipped in loop above)
neededLines = neededLines - 1
if (lineCount >= neededLines) then
failedAttempt = false
else
failedAttempt = true
end
return texts, failedAttempt
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 and Utility.StringContains(text, Locales.KeyWords.ConjuredItem:lower()) then
itemData.isConjured = true
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
-- 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
itemData.isWellFed = false
break
end
end
elseif type(Locales.KeyWords.WellFed) == "string" then
if Utility.StringContains(text, Locales.KeyWords.WellFed:lower()) then
-- itemData.isWellFed = true
itemData.isWellFed = false
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
-- don't parse wellfed
if not itemData.isWellFed and (itemData.isHealth or itemData.isMana) then
-- FU Blizzard
itemDescription = Engine.ReplaceFakeSpace(itemDescription)
-- Utility.Debug("desc: ", itemDescription)
-- parse for health and/or mana
itemData = Engine.ParseValues(itemData, itemDescription)
end
end
itemData = Engine.PostParseUpdate(itemData)
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.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
ns.Engine = Engine