Back to Home

ESO Lua File v100031

ingame/map/worldmapkeepsummary_shared.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
local UPDATE_RATE = 1
ZO_MapKeepSummary_Shared = ZO_Object:Subclass()
function ZO_MapKeepSummary_Shared:New(...)
    local object = ZO_Object.New(self)
    object:Initialize(...)
    return object
end
function ZO_MapKeepSummary_Shared:Initialize(control)
    self.control = control
    control:SetHandler("OnUpdate", function(_, time)
        if(self.nextUpdate == nil or time > self.nextUpdate) then
            self:RefreshTimeDependentControls()
            self.nextUpdate = time + UPDATE_RATE
        end
    end)
    self.fragment = ZO_FadeSceneFragment:New(control)
    self.fragment:RegisterCallback("StateChange", function(oldState, newState)
        if(newState == SCENE_FRAGMENT_SHOWING) then
            self:RefreshAll()
        elseif(newState == SCENE_FRAGMENT_HIDDEN) then
            self.keepUpgradeObject = nil
        end
    end)
    local function IfShowing(f)
        if(self.fragment:IsShowing()) then
            f(self)
        end
    end
    local function IfShowingKeep(keepId, bgQueryType, f)
        if(self.keepUpgradeObject and keepId == self.keepUpgradeObject:GetKeep() and DoBattlegroundContextsIntersect(bgQueryType, self.keepUpgradeObject:GetBGQueryType())) then
            f(self)
        end
    end
    CALLBACK_MANAGER:RegisterCallback("OnWorldMapKeepChanged", function()
        IfShowing(self.RefreshAll)
    end)
    control:RegisterForEvent(EVENT_GUILD_NAME_AVAILABLE, function()
        IfShowing(self.RefreshGuildOwner)
    end)
    control:RegisterForEvent(EVENT_KEEP_ALLIANCE_OWNER_CHANGED, function(_, keepId, bgContext)
        IfShowingKeep(keepId, bgContext, self.RefreshAll)
    end)
    control:RegisterForEvent(EVENT_KEEP_GUILD_CLAIM_UPDATE, function(_, keepId, bgContext)
        IfShowingKeep(keepId, bgContext, self.RefreshGuildOwner)
    end)
    control:RegisterForEvent(EVENT_KEEP_INITIALIZED, function(_, keepId, bgContext)
        IfShowingKeep(keepId, bgContext, self.RefreshAll)
    end)
    control:RegisterForEvent(EVENT_KEEPS_INITIALIZED, function()
        IfShowing(self.RefreshAll)
    end)
    self.rowPool = ZO_ControlPool:New(self.rowLayout, control, "UpgradeRow")
    self.rowPool:SetCustomFactoryBehavior(function(control)
        control.allianceTexture = control:GetNamedChild("Alliance")
    end)
end
function ZO_MapKeepSummary_Shared:InitializeRows()
    self.rowPool:ReleaseAllObjects()
    local lastRow
    local keepId = self.keepUpgradeObject:GetKeep()
    local keepType = GetKeepType(keepId)
    for i = 1, self.keepUpgradeObject:GetNumUpgradeTypes() do
        local row = self.rowPool:AcquireObject(i)
        if(keepType == KEEPTYPE_KEEP) then
            self.keepUpgradeObject:SetResourceType(i)
        else
            self.keepUpgradeObject:SetUpgradePath(i)
        end  
        row:GetNamedChild("ResourceName"):SetText(self.keepUpgradeObject:GetUpgradeTypeName())
        if(lastRow) then
            row:SetAnchor(TOPLEFT, lastRow, BOTTOMLEFT, 0, 20)
        else
            row:SetAnchor(TOPLEFT, self.control:GetNamedChild("Alliance"), BOTTOMLEFT, 0, 37)
        end
        lastRow = row
    end
end
function ZO_MapKeepSummary_Shared:RefreshAll()
    self:RefreshData()
end
function ZO_MapKeepSummary_Shared:GetFragment()
    return self.fragment
end
function ZO_MapKeepSummary_Shared:GetKeepUpgradeObject()
    -- stub
end
function ZO_MapKeepSummary_Shared:RefreshData()
    self.keepUpgradeObject = self:GetKeepUpgradeObject()
    self:InitializeRows()
end
function ZO_MapKeepSummary_Shared:RefreshAlliance()
    local alliance = self.keepUpgradeObject:GetAlliance()
    local allianceControl = self.control:GetNamedChild("Alliance")
    allianceControl.alliance = alliance
    allianceControl:SetTexture(GetLargeAllianceSymbolIcon(alliance))
end
function ZO_MapKeepSummary_Shared:RefreshGuildOwner()
    local guildName = self.keepUpgradeObject:GetGuildOwner()
    if(guildName == "") then
        guildName = GetString(SI_KEEP_UNCLAIMED)
    end
    self.control:GetNamedChild("GuildOwner"):SetText(guildName)
end
function ZO_MapKeepSummary_Shared:GenerateRemainingTimeLabel(current, forNextLevel, resourceRate, level)
    if self.keepUpgradeObject:IsInputEnemyControlled() then
        return GetString(SI_KEEP_UPGRADE_ENEMY_CONTROLLED)
    elseif level >= GetKeepMaxUpgradeLevel(self.keepUpgradeObject:GetKeep()) then
        return GetString(SI_KEEP_UPGRADE_AT_MAX)
    elseif forNextLevel <= 0 or resourceRate <= 0 or current > forNextLevel then
        return GetString(SI_KEEP_UPGRADE_TIME_TO_NEXT_LEVEL_INVALID)
    else        
        local timeRemaining = ((forNextLevel - current) / resourceRate) * 60
        local timeText = ZO_FormatCountdownTimer(timeRemaining)
        return zo_strformat(SI_KEEP_UPGRADE_TIME_TO_NEXT_LEVEL, timeText)
    end
end
function ZO_MapKeepSummary_Shared:RefreshTimeDependentControls()
    local keepId = self.keepUpgradeObject:GetKeep()
    local keepType = GetKeepType(keepId)
    for i, row in ipairs(self.rowPool:GetActiveObjects()) do
        if(keepType == KEEPTYPE_KEEP) then
            self.keepUpgradeObject:SetResourceType(i)
        else
            self.keepUpgradeObject:SetUpgradePath(i)
        end  
        if(keepType == KEEPTYPE_KEEP) then
            local resourceKeepId = GetResourceKeepForKeep(keepId, i)
            local alliance = GetKeepAlliance(resourceKeepId, self.keepUpgradeObject:GetBGQueryType())
            row.allianceTexture:SetHidden(false)
            row.allianceTexture.alliance = alliance
            row.allianceTexture:SetTexture(GetLargeAllianceSymbolIcon(alliance))
        else
            row.allianceTexture:SetHidden(true)
        end
        local level = self.keepUpgradeObject:GetUpgradeLevel()
        row:GetNamedChild("Level"):SetText(level)
        local current, forNextLevel = self.keepUpgradeObject:GetUpgradeLevelProgress(level)  
        local resourceRate = self.keepUpgradeObject:GetRate()
        local remainingTimeText = self:GenerateRemainingTimeLabel(current, forNextLevel, resourceRate, level)
        row:GetNamedChild("TimeUntilNextLevel"):SetText(remainingTimeText)
    end
end