Back to Home

ESO Lua File v101043

ingame/achievements/achievements_manager.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
ZO_ACHIEVEMENTS_ROOT_SUBCATEGORY = "root"
ZO_ACHIEVEMENTS_COMPLETION_STATUS = 
{
    NOT_APPLICABLE = 1,
    INCOMPLETE = 2,
    IN_PROGRESS = 3,
    COMPLETE = 4,
}
local Achievements_Manager = ZO_CallbackObject:Subclass()
function Achievements_Manager:New(...)
    local manager = ZO_CallbackObject.New(self)
    manager:Initialize(...)
    return manager
end
function Achievements_Manager:Initialize()
    self.searchString = ""
    self.searchResults = {}
    -- ESO-747009: Ensure that the last search string stored in C++ is reset when we reinitialize UI
    local function OnAchievementsUpdated()
        -- When the data is getting rebuilt, the indicies can change so our old search results are no longer any good
        if self:GetSearchResults() then
            local currentSearch = self.searchString
            ZO_ClearTable(self.searchResults)
            local FORCE_REFRESH = true
            self:SetSearchString(currentSearch, FORCE_REFRESH)
        end
    end
    EVENT_MANAGER:RegisterForEvent("Achievements_Manager", EVENT_ACHIEVEMENTS_UPDATED, OnAchievementsUpdated)
    EVENT_MANAGER:RegisterForEvent("Achievements_Manager", EVENT_ACHIEVEMENT_AWARDED, OnAchievementsUpdated)
    EVENT_MANAGER:RegisterForEvent("Achievements_Manager", EVENT_ACHIEVEMENTS_SEARCH_RESULTS_READY, function() self:UpdateSearchResults() end)
end
function Achievements_Manager:ClearSearch(requiresImmediateRefresh)
    if self.searchString ~= "" then
        self:SetSearchString("")
    end
    if requiresImmediateRefresh then
        -- If we're relying on results being cleared this frame, do this now so any checks will work, and let the "search" run in the background
        -- so that C stays in sync. This will result in a double update, but sometimes it can't be avoided
        ZO_ClearTable(self.searchResults)
        self:FireCallbacks("UpdateSearchResults")
    end
end
function Achievements_Manager:SetSearchString(searchString, forceRefresh)
    if forceRefresh or (self.searchString ~= (searchString or "")) then
        self.requestingSearchResults = true
        self.searchString = searchString or ""
        StartAchievementSearch(self.searchString, forceRefresh)
    end
end
function Achievements_Manager:IsRequestingSearchResults()
    return self.requestingSearchResults
end
function Achievements_Manager:UpdateSearchResults()
    self.requestingSearchResults = false
    ZO_ClearTable(self.searchResults)
    local searchResults = self.searchResults
    for i = 1, GetNumAchievementsSearchResults() do
        local categoryIndex, subcategoryIndex, achievementIndex = GetAchievementsSearchResult(i)
        local categoryData = searchResults[categoryIndex]
        if not categoryData then
            categoryData = {}
            searchResults[categoryIndex] = categoryData
        end
        local effectiveSubcategoryIndex = subcategoryIndex or ZO_ACHIEVEMENTS_ROOT_SUBCATEGORY
        local effectiveSubcategoryData = categoryData[effectiveSubcategoryIndex]
        if not effectiveSubcategoryData then
            effectiveSubcategoryData = {}
            categoryData[effectiveSubcategoryIndex] = effectiveSubcategoryData
        end
        effectiveSubcategoryData[achievementIndex] = true
    end
    self:FireCallbacks("UpdateSearchResults")
end
function Achievements_Manager:GetSearchResults()
    if zo_strlen(self.searchString) > 1 then
        return self.searchResults
    end
    return nil
end
function Achievements_Manager:IsInSearchResults(categoryIndex, subcategoryIndex, achievementIndex)
    local searchResults = self:GetSearchResults()
    if searchResults then
        local effectiveSubcategoryIndex = subcategoryIndex or ZO_ACHIEVEMENTS_ROOT_SUBCATEGORY
        return searchResults[categoryIndex] and searchResults[categoryIndex][effectiveSubcategoryIndex] and searchResults[categoryIndex][effectiveSubcategoryIndex][achievementIndex]
    else
        return true
    end
end
function Achievements_Manager:GetAchievementStatus(achievementId)
    local completed = 0
    local total = 0
    local numCriteria = GetAchievementNumCriteria(achievementId)
    for criterionIndex = 1, numCriteria do
        local _, numCompleted, numRequired = GetAchievementCriterion(achievementId, criterionIndex)
        completed = completed + numCompleted
        total = total + numRequired
    end
    if total > 0 then
        if completed > 0 then
            if completed == total then
                return ZO_ACHIEVEMENTS_COMPLETION_STATUS.COMPLETE
            else
                return ZO_ACHIEVEMENTS_COMPLETION_STATUS.IN_PROGRESS
            end
        else
            return ZO_ACHIEVEMENTS_COMPLETION_STATUS.INCOMPLETE
        end
    end
    return ZO_ACHIEVEMENTS_COMPLETION_STATUS.NOT_APPLICABLE
end
ACHIEVEMENTS_MANAGER = Achievements_Manager:New()