-
Notifications
You must be signed in to change notification settings - Fork 3
/
TrainerServices.lua
170 lines (169 loc) · 5.4 KB
/
TrainerServices.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
local _, ctp = ...
local ignoreStore = LibStub:GetLibrary("FusionIgnoreStore-1.0")
ctp.TrainerServices = {
totalServices = 0,
availableCost = 0,
totalAvailable = 0,
visibleServices = 0,
showIgnored = TRAINER_FILTER_IGNORED,
allHeadersCollapsed = false,
_updateCandidates = function(self)
self._byServiceId = {}
self.showIgnored = TRAINER_FILTER_IGNORED == true
self.totalServices = GetNumTrainerServices()
local currentSection = nil
local candidateSections = {}
for i = 1, self.totalServices, 1 do
local serviceName, serviceSubText, serviceType, isExpanded = GetTrainerServiceInfo(i)
if (serviceType == "header") then
currentSection = {
name = serviceName,
type = serviceType,
subText = serviceSubText,
isExpanded = isExpanded,
serviceId = i,
skills = {},
isHidden = false
}
self._byServiceId[i] = currentSection
tinsert(candidateSections, currentSection)
else
if (ctp.TooltipNameMap[serviceName] == nil) then
ctp.TooltipNameMap[serviceName] = {}
end
if (serviceSubText and
ctp.TooltipNameMap[serviceName][serviceSubText] == nil
and not IsTradeskillTrainer()) then
GameTooltip:SetTrainerService(i)
local tooltipName = GameTooltipTextLeft1:GetText()
if (tooltipName and string.find(tooltipName, serviceName, 1, true)) then
ctp.TooltipNameMap[serviceName][serviceSubText] = tooltipName
end
end
local ai = ctp.Abilities:GetByNameAndSubText(serviceName, serviceSubText)
local isIgnored = ai and ignoreStore:IsIgnored(ai.spellId) or false
local ability = {
serviceId = i,
name = serviceName,
lowerName = strlower(serviceName),
subText = serviceSubText,
isIgnored = isIgnored,
type = serviceType,
isHidden = false
}
if (serviceSubText ~= nil and serviceSubText ~= "") then
ability.menuTitle = serviceName .. " " .. format(PARENS_TEMPLATE, serviceSubText)
else
ability.menuTitle = serviceName
end
self._byServiceId[i] = ability
if (not isIgnored or self.showIgnored or serviceType == 'used') then
tinsert(currentSection.skills, ability)
end
if (isIgnored and serviceType ~= "used") then
if (not self.showIgnored) then
ability.isHidden = true
end
end
end
end
self._candidates = candidateSections
end,
VisibleAvailableServiceIds = function(self)
if (self._byPosition == nil) then return {} end
local ids = {}
for _, ability in ipairs(self._byPosition) do
if (ability.type == "available" and not ability.isIgnored) then
tinsert(ids, ability.serviceId)
end
end
return ids
end,
SetFilter = function(self, text)
local oldFilter = self._filter
self._filter = strlower(text)
return oldFilter ~= self._filter
end,
ApplyFilter = function(self)
self._byPosition = {}
self.visibleServices = 0
self.availableCost = 0
self.totalAvailable = 0
local candidateSections = self._candidates
local numHeaders = #candidateSections
local numNotExpanded = 0
for _, candidate in ipairs(candidateSections) do
local skillsInCandidate = #candidate.skills
if (self.showIgnored or skillsInCandidate > 0 or not candidate.isExpanded) then
if (self._filter ~= nil and self._filter ~= "" and candidate.isExpanded) then
local headerInserted = false
for _, skill in ipairs(candidate.skills) do
if (strfind(skill.lowerName, self._filter, 1, true)) then
if (not headerInserted) then
tinsert(self._byPosition, candidate)
headerInserted = true
end
if (skill.type == "available" and not skill.isIgnored) then
self.availableCost = self.availableCost + GetTrainerServiceCost(skill.serviceId)
self.totalAvailable = self.totalAvailable + 1
end
tinsert(self._byPosition, skill)
end
end
else
tinsert(self._byPosition, candidate)
for _, skill in ipairs(candidate.skills) do
tinsert(self._byPosition, skill)
if (skill.type == "available" and not skill.isIgnored) then
self.availableCost = self.availableCost + GetTrainerServiceCost(skill.serviceId)
self.totalAvailable = self.totalAvailable + 1
end
end
end
else
candidate.isHidden = true
end
if (not candidate.isExpanded and not candidate.isHidden) then
numNotExpanded = numNotExpanded + 1
end
end
self.visibleServices = #self._byPosition
self.allHeadersCollapsed = numHeaders == numNotExpanded
end,
Update = function(self)
self:_updateCandidates()
self:ApplyFilter()
end,
IsSelected = function(self, serviceId)
if (not serviceId or serviceId == 0) then
return false
end
local service = self._byServiceId[serviceId]
return (service and not service.isHidden) and GetTrainerSelectionIndex() == serviceId
end,
GetFirstVisibleNonHeaderService = function(self)
for _, service in ipairs(self._byPosition) do
if (service.type ~= "header") then
return service
end
end
end,
GetNextAvailableServiceId = function(self, serviceId)
for id, service in ipairs(self._byPosition) do
if (service.serviceId == serviceId and id < #self._byPosition) then
local nextService = self._byPosition[id + 1]
if (nextService.type == "available") then
return nextService.serviceId
else
serviceId = nextService.serviceId
end
end
end
end,
GetServiceAtPosition = function(self, position)
return self._byPosition[position]
end,
GetService = function(self, id)
return self._byServiceId[id]
end
}