-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFilters.lua
271 lines (226 loc) · 8.01 KB
/
Filters.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
local _, ADDON = ...
local function StringContains(haystack, needle)
if haystack == nil or haystack == '' then
return false
end
local pos = strfind(haystack:lower(), needle, 1, true)
return pos ~= nil
end
local function FilterBySearch(itemId, searchString)
local _, name = C_ToyBox.GetToyInfo(itemId)
if name == nil or name == '' then
return false
end
if StringContains(name, searchString) then
return true
end
if ADDON.settings.ui.searchInDescription then
local GetSpellDescription = C_Spell.GetSpellDescription or GetSpellDescription
local _, spellId = GetItemSpell(itemId)
local spellDescription = GetSpellDescription(spellId)
if StringContains(spellDescription, searchString) then
return true
end
if C_TooltipInfo then
local tooltip = C_TooltipInfo.GetItemByID(itemId)
for _, line in ipairs(tooltip.lines) do
local text = line.leftText or ""
-- search in flavor texts
if text and strsub(text, 1, 1) == '"' and strsub(text, -1) == '"' and StringContains(text, searchString) then
return true
end
end
end
end
return false
end
local function FilterUserHiddenToys(itemId)
return ADDON.settings.filter.hidden or not ADDON.settings.hiddenToys[itemId]
end
local function FilterSecretToys(itemId)
return ADDON.settings.filter.secret or ADDON.db.ingameList[itemId] == true
end
local function FilterCollectedToys(itemId)
local collected = PlayerHasToy(itemId)
return (ADDON.settings.filter.collected and collected) or (ADDON.settings.filter.notCollected and not collected)
end
local function FilterFavoriteToys(itemId)
return not ADDON.settings.filter.onlyFavorites or not ADDON.settings.filter.collected or ADDON.Api:GetIsFavorite(itemId)
end
local function FilterUsableToys(itemId)
return not ADDON.settings.filter.onlyUsable or C_ToyBox.IsToyUsable(itemId)
end
local function CheckAllSettings(settings)
local allDisabled = true
local allEnabled = true
for _, value in pairs(settings) do
if type(value) == "table" then
local subResult = CheckAllSettings(value)
if subResult == nil then
allDisabled = false
allEnabled = false
break
elseif subResult == true then
allDisabled = false
elseif subResult == false then
allEnabled = false
end
elseif value then
allDisabled = false
else
allEnabled = false
end
if allEnabled == false and allDisabled == false then
break
end
end
if allEnabled then
return true
elseif allDisabled then
return false
end
return nil
end
local function CheckItemInList(settings, sourceData, itemId)
local isInList = false
for setting, value in pairs(settings) do
if sourceData[setting] and sourceData[setting][itemId] then
if (value) then
return true
else
isInList = true
end
end
end
if isInList then
return false
end
return nil
end
local function FilterToysByFaction(itemId)
local allSettings = CheckAllSettings(ADDON.settings.filter.faction)
if allSettings then
return true
end
local settingResult = CheckItemInList(ADDON.settings.filter.faction, ADDON.db.faction, itemId)
if settingResult ~= nil then
return settingResult
end
return ADDON.settings.filter.faction.noFaction
end
local function FilterToysBySource(itemId)
return CheckItemInList(ADDON.settings.filter.source, ADDON.db.source, itemId)
end
local function FilterToysByProfession(itemId)
return CheckItemInList(ADDON.settings.filter.profession, ADDON.db.profession, itemId)
end
local function FilterToysByWorldEvent(itemId)
return CheckItemInList(ADDON.settings.filter.worldEvent, ADDON.db.worldEvent, itemId)
end
local function FilterTradableToys(itemId)
return not ADDON.settings.filter.onlyTradable or select(14, C_Item.GetItemInfo(itemId)) == Enum.ItemBind.OnUse
end
local function FilterRecentToys(itemId)
return not ADDON.settings.filter.onlyRecent
or (ADDON.db.Recent.minID <= itemId and not tContains(ADDON.db.Recent.blacklist, itemId))
or tContains(ADDON.db.Recent.whitelist, itemId)
end
local function FilterToysByExpansion(itemId)
local settingsResult = CheckAllSettings(ADDON.settings.filter.expansion)
if settingsResult then
return true
end
local settingResult = CheckItemInList(ADDON.settings.filter.expansion, ADDON.db.expansion, itemId)
if settingResult ~= nil then
return settingResult
end
for expansion, value in pairs(ADDON.settings.filter.expansion) do
if ADDON.db.expansion[expansion] and
ADDON.db.expansion[expansion]["minID"] <= itemId and
itemId <= ADDON.db.expansion[expansion]["maxID"] then
return value
end
end
return false
end
local function FilterToysByEffect(itemId)
local settings = ADDON.settings.filter.effect
local allSettingsResult = CheckAllSettings(settings)
if allSettingsResult then
return true
end
for name, categoriesOrToys in pairs(ADDON.db.effect) do
local _ , subCategoryOrItem = next(categoriesOrToys)
if type(subCategoryOrItem) == "table" and CheckItemInList(settings[name], categoriesOrToys, itemId) then
return true
end
end
local settingResult = CheckItemInList(settings, ADDON.db.effect, itemId)
if settingResult ~= nil then
return settingResult
end
return false
end
local function UpdateDataProvider(result)
local dataProvider = ADDON.DataProvider
if #result == 0 then
dataProvider:Flush()
else
local flippedResult = CopyValuesAsKeys(result)
local skipAdd = {}
local toRemove = {}
dataProvider:ForEach(function(itemId)
local resultPosition = flippedResult[itemId]
if resultPosition then
-- already in provider
skipAdd[itemId] = true
else
toRemove[#toRemove + 1] = itemId
end
end)
if #toRemove > 0 then
dataProvider:Remove(unpack(toRemove))
end
local toAdd = tFilter(result, function(itemId)
return not skipAdd[itemId]
end, true)
if #toAdd > 0 then
dataProvider:InsertTable(toAdd)
end
end
end
function ADDON:FilterToys()
local result = {}
local searchString = ToyBox and ToyBox.searchString or ""
if searchString ~= "" then
searchString = searchString:lower()
for itemId in pairs(ADDON.db.ingameList) do
if FilterBySearch(itemId, searchString) then
result[#result + 1] = itemId
end
end
else
for itemId in pairs(ADDON.db.ingameList) do
if FilterUserHiddenToys(itemId)
and FilterSecretToys(itemId)
and FilterCollectedToys(itemId)
and FilterFavoriteToys(itemId)
and FilterTradableToys(itemId)
and FilterRecentToys(itemId)
and FilterUsableToys(itemId)
and FilterToysByFaction(itemId)
and FilterToysByExpansion(itemId)
and FilterToysByEffect(itemId)
and (
(CheckAllSettings(ADDON.settings.filter.source) and CheckAllSettings(ADDON.settings.filter.profession) and CheckAllSettings(ADDON.settings.filter.worldEvent))
or FilterToysBySource(itemId) or FilterToysByProfession(itemId) or FilterToysByWorldEvent(itemId)
)
then
result[#result + 1] = itemId
end
end
end
UpdateDataProvider(result)
ADDON.Events:TriggerEvent("OnFiltered")
return result
end