-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSort.lua
More file actions
347 lines (309 loc) · 10.4 KB
/
Sort.lua
File metadata and controls
347 lines (309 loc) · 10.4 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
local _, ADDON = ...
local function CreateFleetingCache()
local cache = {}
local setTimer = false
return {
Has = function(_, cacheKey)
return cache[cacheKey] ~= nil
end,
Get = function(self, cacheKey, initializerFunc)
if initializerFunc and not self:Has(cacheKey) then
self:Set(cacheKey, initializerFunc())
end
if cache[cacheKey] then
return unpack(cache[cacheKey])
end
end,
Set = function(_, cacheKey, ...)
cache[cacheKey] = {...}
if not setTimer then
C_Timer.After(0.1, function()
cache = {}
setTimer = false
end)
setTimer = true
end
end,
}
end
local function TransliterateName(name)
-- has latin-1 supplement
if string.find(name, "\195", 1, true) then
local replaceMap = {
["À"] = "A",
["Á"] = "A",
["Â"] = "A",
["Ã"] = "A",
["Ä"] = "A",
["Å"] = "A",
["Æ"] = "AE",
["Ç"] = "C",
["È"] = "E",
["É"] = "E",
["Ê"] = "E",
["Ë"] = "E",
["Ì"] = "I",
["Í"] = "I",
["Î"] = "I",
["Ï"] = "I",
["Ð"] = "D",
["Ñ"] = "N",
["Ò"] = "O",
["Ó"] = "O",
["Ô"] = "O",
["Õ"] = "O",
["Ö"] = "O",
["Ø"] = "O",
["Ù"] = "U",
["Ú"] = "U",
["Û"] = "U",
["Ü"] = "U",
["Ý"] = "Y",
["ß"] = "ss",
["à"] = "a",
["á"] = "a",
["â"] = "a",
["ã"] = "a",
["ä"] = "a",
["å"] = "a",
["æ"] = "ae",
["ç"] = "c",
["è"] = "e",
["é"] = "e",
["ê"] = "e",
["ë"] = "e",
["ì"] = "i",
["í"] = "i",
["î"] = "i",
["ï"] = "i",
["ñ"] = "n",
["ò"] = "o",
["ó"] = "o",
["ô"] = "o",
["õ"] = "o",
["ö"] = "o",
["ø"] = "o",
["ù"] = "u",
["ú"] = "u",
["û"] = "u",
["ü"] = "u",
["ý"] = "y",
["ÿ"] = "y",
}
name = string.gsub(name, "\195.", replaceMap)
end
return name
end
local nameCache
local function CacheAndTransliterateName(name, cacheKey)
nameCache = nameCache or CreateFleetingCache()
return nameCache:Get(cacheKey, function()
return TransliterateName(name)
end)
end
local TrackingIndex = {
usage_count = 1,
last_usage = 2,
travel_duration = 3,
travel_distance = 4,
learned_date = 5,
}
local mountCache
local function CacheMount(mountId)
mountCache = mountCache or CreateFleetingCache()
return mountCache:Get(mountId, function()
local name, _, _, _, isUsable, _, _, _, _, _, isCollected = ADDON.Api:GetMountInfoByID(mountId)
local needsFanfare = isCollected and C_MountJournal.NeedsFanfare(mountId)
local isFavorite = isCollected and ADDON.Api:GetIsFavoriteByID(mountId)
return name, isUsable, isFavorite, isCollected, needsFanfare
end)
end
local function FallbackByName(mountIdA, mountIdB)
local nameA = CacheMount(mountIdA)
local nameB = CacheMount(mountIdB)
return CacheAndTransliterateName(nameA, mountIdA) < CacheAndTransliterateName(nameB, mountIdB)
end
local function CheckDescending(result)
if ADDON.settings.sort.descending then
result = not result
end
return result
end
--region sort by type
local typeCache
local RemappedTypes
local function RemapTypeIds()
local result = {}
for category, values in pairs(ADDON.DB.Type) do
for _, typeId in pairs(values.typeIDs or {}) do
result[typeId] = category
end
end
return result
end
local function GetMountTypeId(mountId)
typeCache = typeCache or CreateFleetingCache()
return typeCache:Get(mountId, function()
return select(5, C_MountJournal.GetMountInfoExtraByID(mountId))
end)
end
local function SortByType(mountIdA, mountIdB)
RemappedTypes = RemappedTypes or RemapTypeIds()
local mountTypeA = RemappedTypes[GetMountTypeId(mountIdA)] or nil
local mountTypeB = RemappedTypes[GetMountTypeId(mountIdB)] or nil
local result = false
if mountTypeA == mountTypeB then
return FallbackByName(mountIdA, mountIdB)
elseif mountTypeA == "flying" then
result = true
elseif mountTypeA == "ground" then
result = (mountTypeB == "underwater")
elseif mountTypeA == "underwater" then
result = false
end
return CheckDescending(result)
end
--endregion
local function SortByTracking(mountIdA, mountIdB)
local sortBy = ADDON.settings.sort.by
local result = false
local valueA = select(TrackingIndex[sortBy], ADDON:GetMountStatistics(mountIdA)) or 0
local valueB = select(TrackingIndex[sortBy], ADDON:GetMountStatistics(mountIdB)) or 0
if valueA == valueB then
return FallbackByName(mountIdA, mountIdB)
elseif sortBy == 'learned_date' or sortBy == 'last_usage' then
result = valueA > valueB
else
result = valueA < valueB
end
return CheckDescending(result)
end
local LibMountsRarity
local function SortByRarity(mountIdA, mountIdB)
LibMountsRarity = LibMountsRarity or LibStub("MountsRarity-2.0")
local rarityA = LibMountsRarity:GetRarityByID(mountIdA)
local rarityB = LibMountsRarity:GetRarityByID(mountIdB)
if nil ~= rarityA and nil ~= rarityB and rarityA ~= rarityB then
return CheckDescending(rarityA > rarityB)
elseif nil ~= rarityA and nil == rarityB then
return true
elseif nil == rarityA and nil ~= rarityB then
return false
end
return FallbackByName(mountIdA, mountIdB)
end
local sortedFamilies
local familyCache
local function LoadAndSortFamilies()
local L = ADDON.L
local result = {}
for topFamily, topValues in pairs(ADDON.DB.Family) do
local topName = L[topFamily]
topName = topName and TransliterateName(topName) or topFamily
if type(select(2, next(topValues))) == "table" then
for subFamily, _ in pairs(topValues) do
local subName = L[subFamily]
subName = subName and TransliterateName(subName) or subFamily
result[topFamily.."--"..subFamily] = topName.."--"..subName
end
else
result[topFamily] = topName
end
end
local sortedResult= {}
for index, familyPath in ipairs(GetKeysArraySortedByValue(result)) do
sortedResult[familyPath] = index
end
return sortedResult
end
local function GetFamilyByMount(mountId)
familyCache = familyCache or CreateFleetingCache()
return familyCache:Get(mountId, function()
local filterSettings = ADDON.settings.filter.family
for topFamily, topValues in pairs(ADDON.DB.Family) do
if topValues[mountId] and filterSettings[topFamily] then
return topFamily
end
if type(select(2, next(topValues))) == "table" then
for subFamily, subValues in pairs(topValues) do
if subValues[mountId] and filterSettings[topFamily][subFamily] then
return topFamily.."--"..subFamily
end
end
end
end
end)
end
local function SortByFamily(mountIdA, mountIdB)
sortedFamilies = sortedFamilies or LoadAndSortFamilies()
local familyA = GetFamilyByMount(mountIdA) or ""
local familyB = GetFamilyByMount(mountIdB) or ""
local familyIndexA = sortedFamilies[familyA]
local familyIndexB = sortedFamilies[familyB]
if nil ~= familyIndexA and nil ~= familyIndexB and familyIndexA ~= familyIndexB then
return CheckDescending(familyIndexA < familyIndexB)
elseif nil ~= familyIndexA and nil == familyIndexB then
return true
elseif nil == familyIndexA and nil ~= familyIndexB then
return false
end
return FallbackByName(mountIdA, mountIdB)
end
local expansionCache
local function GetExpansionOfMount(mountId)
expansionCache = expansionCache or CreateFleetingCache()
return expansionCache:Get(mountId, function()
local matchedExpansion = 0
for expansion, config in pairs(ADDON.DB.Expansion) do
if config[mountId] then
return expansion
end
if mountId >= config.minID and mountId <= config.maxID then
matchedExpansion = expansion
end
end
return matchedExpansion
end)
end
local function SortByExpansion(mountIdA, mountIdB)
local expansionA = GetExpansionOfMount(mountIdA)
local expansionB = GetExpansionOfMount(mountIdB)
if expansionA == expansionB then
return FallbackByName(mountIdA, mountIdB)
end
return CheckDescending(expansionA < expansionB)
end
function ADDON:SortHandler(mountA, mountB)
local mountIdA, mountIdB = mountA.mountID, mountB.mountID
if mountIdA == mountIdB then
return false
end
local _, isUsableA, isFavoriteA, isCollectedA, needsFanfareA = CacheMount(mountIdA)
local _, isUsableB, isFavoriteB, isCollectedB, needsFanfareB = CacheMount(mountIdB)
if needsFanfareA ~= needsFanfareB then
return needsFanfareA and not needsFanfareB
end
if ADDON.settings.sort.favoritesOnTop and isFavoriteA ~= isFavoriteB then
return isFavoriteA and not isFavoriteB
end
if ADDON.settings.sort.unusableToBottom and isUsableA ~= isUsableB then
return isUsableA and not isUsableB
end
if ADDON.settings.sort.unownedOnBottom and isCollectedA ~= isCollectedB then
return isCollectedA and not isCollectedB
end
local sortBy = ADDON.settings.sort.by
if sortBy == 'type' then
return SortByType(mountIdA, mountIdB)
elseif sortBy == 'expansion' then
return SortByExpansion(mountIdA, mountIdB)
elseif sortBy == 'rarity' then
return SortByRarity(mountIdA, mountIdB)
elseif sortBy == 'family' then
return SortByFamily(mountIdA, mountIdB)
elseif TrackingIndex[sortBy] then
return SortByTracking(mountIdA, mountIdB)
else
return CheckDescending(FallbackByName(mountIdA, mountIdB))
end
end