Back to Home

ESO Lua File v100034

ingame/antiquities/antiquitydatamanager.lua

[◄ back to folders ]
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
ZO_AntiquityDataManager = ZO_CallbackObject:Subclass()
ZO_ANTIQUITY_TYPE_INDIVIDUAL = 1
ZO_ANTIQUITY_TYPE_SET = 2
function ZO_AntiquityDataManager:New(...)
    ANTIQUITY_DATA_MANAGER = ZO_CallbackObject.New(self)
    ANTIQUITY_DATA_MANAGER:Initialize(...)
    return ANTIQUITY_DATA_MANAGER
end
function ZO_AntiquityDataManager:Initialize()
    self.antiquities = {}
    self.antiquityCategories = {}
    self.antiquitySets = {}
    self.topLevelCategories = {}
    self.highestScryableDifficulty = GetHighestScryableDifficulty()
    self:InitializeEventHandlers()
    self:RebuildAntiquities()
end
function ZO_AntiquityDataManager:InitializeEventHandlers()
    local function OnAntiquitiesUpdated()
        self:RefreshAll()
    end
    local function OnSingleAntiquityUpdated(event, antiquityId)
        self:RefreshAntiquity(antiquityId)
    end
    local function OnSingleAntiquityDigSitesUpdated(event, antiquityId)
        self:OnSingleAntiquityDigSitesUpdated(antiquityId)
    end
    local function OnAntiquitySearchResultsReady()
        self:UpdateSearchResults()
    end
    local function OnSkillsUpdated()
        self:OnSkillsUpdated()
    end
    local function OnAntiquityLeadAcquired(event, antiquityId)
        self:OnSingleAntiquityLeadAcquired(antiquityId)
    end
    local function OnAntiquityShowCodexEntry(event, antiquityId)
        self:OnAntiquityShowCodexEntry(antiquityId)
    end
    EVENT_MANAGER:RegisterForEvent("AntiquityDataManager", EVENT_ANTIQUITIES_UPDATED, OnAntiquitiesUpdated)
    EVENT_MANAGER:RegisterForEvent("AntiquityDataManager", EVENT_ANTIQUITY_UPDATED, OnSingleAntiquityUpdated)
    EVENT_MANAGER:RegisterForEvent("AntiquityDataManager", EVENT_ANTIQUITY_DIG_SITES_UPDATED, OnSingleAntiquityDigSitesUpdated)
    EVENT_MANAGER:RegisterForEvent("AntiquityDataManager", EVENT_ANTIQUITY_SEARCH_RESULTS_READY, OnAntiquitySearchResultsReady)
    EVENT_MANAGER:RegisterForEvent("AntiquityDataManager", EVENT_ANTIQUITY_LEAD_ACQUIRED, OnAntiquityLeadAcquired)
    EVENT_MANAGER:RegisterForEvent("AntiquityDataManager", EVENT_ANTIQUITY_SHOW_CODEX_ENTRY, OnAntiquityShowCodexEntry)
    SKILLS_DATA_MANAGER:RegisterCallback("FullSystemUpdated", OnSkillsUpdated)
    SKILLS_DATA_MANAGER:RegisterCallback("SkillLineAdded", OnSkillsUpdated)
    SKILLS_DATA_MANAGER:RegisterCallback("SkillLineRankUpdated", OnSkillsUpdated)
    SKILLS_DATA_MANAGER:RegisterCallback("SkillProgressionUpdated", OnSkillsUpdated)
end
do
    local filterFunctions = {
        [ANTIQUITY_FILTER_SHOW_ALL] = nil,
        [ANTIQUITY_FILTER_SHOW_COMPLETED] = function(antiquityData, antiquitySetData)
            if antiquitySetData then
                return antiquitySetData:HasRecovered()
            else
                return antiquityData:HasRecovered()
            end
        end,
        [ANTIQUITY_FILTER_SHOW_IN_PROGRESS] = function(antiquityData, antiquitySetData)
            if antiquitySetData then
                return antiquitySetData:HasDiscoveredDigSites()
            else
                return antiquityData:HasDiscoveredDigSites()
            end
        end,
        [ANTIQUITY_FILTER_SHOW_NOT_STARTED] = function(antiquityData, antiquitySetData)
            if antiquitySetData then
                return antiquitySetData:HasNoDiscoveredDigSites() and not antiquitySetData:HasRecovered()
            else
                return antiquityData:HasNoDiscoveredDigSites() and not antiquityData:HasRecovered()
            end
        end,
    }
    function ZO_AntiquityDataManager:GetAntiquityFilterFunction(antiquityFilter)
        return filterFunctions[antiquityFilter]
    end
end
-- ZO_AntiquityCategory
function ZO_AntiquityDataManager:AntiquityCategoryIterator(filterFunctions)
    return ZO_FilteredNonContiguousTableIterator(self.antiquityCategories, filterFunctions)
end
function ZO_AntiquityDataManager:ClearAntiquityCategories()
    ZO_ClearTable(self.antiquityCategories)
end
function ZO_AntiquityDataManager:ClearTopLevelCategories()
    ZO_ClearNumericallyIndexedTable(self.topLevelCategories)
end
function ZO_AntiquityDataManager:GetAntiquityCategoryData(antiquityCategoryId)
    return self.antiquityCategories[antiquityCategoryId]
end
-- Internal function
function ZO_AntiquityDataManager:GetOrCreateAntiquityCategoryData(antiquityCategoryId)
    if antiquityCategoryId and antiquityCategoryId ~= ZO_SCRYABLE_ANTIQUITY_CATEGORY_ID then
        local antiquityCategoryData = self:GetAntiquityCategoryData(antiquityCategoryId)
        if not antiquityCategoryData then
            antiquityCategoryData = ZO_AntiquityCategory:New(antiquityCategoryId)
            self.antiquityCategories[antiquityCategoryId] = antiquityCategoryData
            if not antiquityCategoryData:GetParentCategoryData() then
                table.insert(self.topLevelCategories, antiquityCategoryData)
            end
        end
        return antiquityCategoryData
    end
end
function ZO_AntiquityDataManager:TopLevelAntiquityCategoryIterator(filterFunctions)
    return ZO_FilteredNumericallyIndexedTableIterator(self.topLevelCategories, filterFunctions)
end
function ZO_AntiquityDataManager:SortTopLevelAntiquityCategories()
    table.sort(self.topLevelCategories, ZO_AntiquityCategory.CompareTo)
    for _, antiquityCategoryData in ipairs(self.topLevelCategories) do
        antiquityCategoryData:SortAntiquities()
        antiquityCategoryData:SortSubcategories()
    end
end
-- ZO_AntiquitySet
function ZO_AntiquityDataManager:AntiquitySetIterator(filterFunctions)
    return ZO_FilteredNonContiguousTableIterator(self.antiquitySets, filterFunctions)
end
function ZO_AntiquityDataManager:ClearAntiquitySets()
    ZO_ClearTable(self.antiquitySets)
end
function ZO_AntiquityDataManager:GetAntiquitySetData(antiquitySetId)
    return self.antiquitySets[antiquitySetId]
end
function ZO_AntiquityDataManager:GetOrCreateAntiquitySetData(antiquitySetId)
    if antiquitySetId and antiquitySetId ~= 0 then
        local antiquitySetData = self:GetAntiquitySetData(antiquitySetId)
        if not antiquitySetData then
            antiquitySetData = ZO_AntiquitySet:New(antiquitySetId)
            self.antiquitySets[antiquitySetId] = antiquitySetData
        end
        return antiquitySetData
    end
end
-- ZO_Antiquity
function ZO_AntiquityDataManager:AntiquityIterator(filterFunctions)
    return ZO_FilteredNonContiguousTableIterator(self.antiquities, filterFunctions)
end
function ZO_AntiquityDataManager:ClearAntiquities()
    ZO_ClearTable(self.antiquities)
end
function ZO_AntiquityDataManager:GetAntiquityData(antiquityId)
    return self.antiquities[antiquityId]
end
function ZO_AntiquityDataManager:GetOrCreateAntiquityData(antiquityId)
    if antiquityId and antiquityId ~= 0 then
        local antiquityData = self:GetAntiquityData(antiquityId)
        if not antiquityData then
            antiquityData = ZO_Antiquity:New(antiquityId)
            self.antiquities[antiquityId] = antiquityData
        end
        return antiquityData
    end
end
function ZO_AntiquityDataManager:RefreshAntiquity(antiquityId)
    local antiquityData = self:GetAntiquityData(antiquityId)
    if antiquityData then
        antiquityData:Refresh()
        self:SortTopLevelAntiquityCategories()
        self:FireCallbacks("SingleAntiquityUpdated", antiquityData)
    else
        internalassert(false, string.format("Invalid antiquityId (%s)", tostring(antiquityId) or "nil"))
    end
end
function ZO_AntiquityDataManager:OnSingleAntiquityDigSitesUpdated(antiquityId)
    local antiquityData = self:GetAntiquityData(antiquityId)
    if antiquityData then
        antiquityData:OnDigSitesUpdated()
        self:SortTopLevelAntiquityCategories()
        self:FireCallbacks("SingleAntiquityDigSitesUpdated", antiquityData)
    else
        internalassert(false, string.format("Invalid antiquityId (%s)", tostring(antiquityId) or "nil"))
    end
end
function ZO_AntiquityDataManager:OnSingleAntiquityLeadAcquired(antiquityId)
    local antiquityData = self:GetAntiquityData(antiquityId)
    if antiquityData then
        antiquityData:OnLeadAcquired()
        self:SortTopLevelAntiquityCategories()
        self:FireCallbacks("SingleAntiquityLeadAcquired", antiquityData)
    else
        internalassert(false, string.format("Invalid antiquityId (%s)", tostring(antiquityId) or "nil"))
    end
end
function ZO_AntiquityDataManager:OnSingleAntiquityNewLeadCleared(antiquityId)
    local antiquityData = self:GetAntiquityData(antiquityId)
    if antiquityData then
        self:SortTopLevelAntiquityCategories()
        self:FireCallbacks("SingleAntiquityNewLeadCleared", antiquityData)
    else
        internalassert(false, string.format("Invalid antiquityId (%s)", tostring(antiquityId) or "nil"))
    end
end
function ZO_AntiquityDataManager:OnSkillsUpdated()
    local newHighestScryableDifficulty = GetHighestScryableDifficulty()
    if self.highestScryableDifficulty ~= newHighestScryableDifficulty then
        self:RefreshAll()
        self.highestScryableDifficulty = newHighestScryableDifficulty
    end
end
function ZO_AntiquityDataManager:OnAntiquityShowCodexEntry(antiquityId)
    if IsInGamepadPreferredMode() then
        local DONT_PUSH = false
        local antiquityData = self:GetAntiquityData(antiquityId)
        internalassert(antiquityData ~= nil)
        if antiquityData then
            ANTIQUITY_LORE_GAMEPAD:SetFromFanfare(true)
            ANTIQUITY_LORE_GAMEPAD:ShowAntiquityOrSet(antiquityData, DONT_PUSH)
        end
    else
        local categoryId = GetAntiquityCategoryId(antiquityId)
        ANTIQUITY_JOURNAL_KEYBOARD:ShowCategory(categoryId)
        ANTIQUITY_LORE_KEYBOARD:ShowAntiquity(antiquityId)
    end
end
function ZO_AntiquityDataManager:RefreshAll()
    for _, antiquityData in self:AntiquityIterator() do
        antiquityData:Refresh()
    end
    self:FireCallbacks("AntiquitiesUpdated")
end
function ZO_AntiquityDataManager:RebuildAntiquities()
    self:ClearAntiquities()
    self:ClearAntiquityCategories()
    self:ClearAntiquitySets()
    self:ClearTopLevelCategories()
    -- Load all Antiquities and their associated objects.
    local antiquityId = GetNextAntiquityId()
    while antiquityId do
        self:GetOrCreateAntiquityData(antiquityId)
        antiquityId = GetNextAntiquityId(antiquityId)
    end
    self:FireCallbacks("AntiquitiesUpdated")
end
function ZO_AntiquityDataManager:HasNewLead()
    for _, categoryData in ipairs(self.topLevelCategories) do
        if categoryData:HasNewLead() then
            return true
        end
    end
    return false
end
-- Search
function ZO_AntiquityDataManager:GetSearch()
    return self.search or ""
end
function ZO_AntiquityDataManager:GetSearchResults()
    return self.searchResults
end
function ZO_AntiquityDataManager:SetSearch(search)
    self.search = search
    StartAntiquitySearch(search)
end
function ZO_AntiquityDataManager:UpdateSearchResults()
    if #self:GetSearch() > 1 then
        local results = {}
        local numResults = GetNumAntiquitySearchResults()
        for resultIndex = 1, numResults do
            table.insert(results, GetAntiquitySearchResult(resultIndex))
        end
        self.searchResults = results
    else
        self.searchResults = nil
    end
    self:FireCallbacks("UpdateSearchResults")
end
-- Global singleton
-- The global singleton moniker is assigned by the Data Manager's constructor in order to
-- allow data objects to reference the singleton during their construction.
ZO_AntiquityDataManager:New()