ESO Lua File v100010

internalingame/market/market_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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
ZO_MARKET_NAME = "Market"
ZO_BUY_CROWNS_URL = {url = GetString(SI_MARKET_INSUFFICIENT_FUNDS_LINK)}
ZO_BUY_CROWNS_FRONT_FACING_ADDRESS = {mainTextParams = {GetString(SI_MARKET_INSUFFICIENT_FUNDS_LINK_TEXT)}}
--
--[[ Market Singleton ]]--
--
local Market_Singleton = ZO_Object:Subclass()
function Market_Singleton:New(...)
    local market = ZO_Object.New(self)
    market:Initialize(...)
    return market
end
function Market_Singleton:Initialize()
    local function OnMarketStateUpdated(...)
        SYSTEMS:GetObject(ZO_MARKET_NAME):OnMarketStateUpdated(...)
    end
    local function OnMarketCurrencyUpdated(...)
        SYSTEMS:GetObject(ZO_MARKET_NAME):OnMarketCurrencyUpdated(...)
    end
    local function OnMarketPurchaseResult(...)
        SYSTEMS:GetObject(ZO_MARKET_NAME):OnMarketPurchaseResult(...)
    end
    local function OnMarketSearchResultsReady()
        SYSTEMS:GetObject(ZO_MARKET_NAME):OnMarketSearchResultsReady()
    end
    local function OnMarketCollectibleUpdated(_, justUnlocked)
        SYSTEMS:GetObject(ZO_MARKET_NAME):OnCollectibleUpdated(justUnlocked)
    end
    EVENT_MANAGER:RegisterForEvent(ZO_MARKET_NAME, EVENT_MARKET_STATE_UPDATED, function(eventId, ...) OnMarketStateUpdated(...) end)
    EVENT_MANAGER:RegisterForEvent(ZO_MARKET_NAME, EVENT_MARKET_CURRENCY_UPDATE, function(eventId, ...) OnMarketCurrencyUpdated(...) end)
    EVENT_MANAGER:RegisterForEvent(ZO_MARKET_NAME, EVENT_MARKET_PURCHASE_RESULT, function(eventId, ...) OnMarketPurchaseResult(...) end)
    EVENT_MANAGER:RegisterForEvent(ZO_MARKET_NAME, EVENT_MARKET_PRODUCT_SEARCH_RESULTS_READY, function() OnMarketSearchResultsReady() end)
    EVENT_MANAGER:RegisterForEvent(ZO_MARKET_NAME, EVENT_COLLECTIBLE_UPDATED, function(eventId, ...) OnMarketCollectibleUpdated(...) end)
end
ZO_MARKET_SINGLETON = Market_Singleton:New()
--
--[[ Market Shared ]]--
--
ZO_Market_Shared = ZO_Object:Subclass()
function ZO_Market_Shared:New(...)
    local market = ZO_Object.New(self)
    market:Initialize(...)
    return market
end
function ZO_Market_Shared:Initialize()
    -- clean up any preview that may have been left over
    self.searchResults = {}
    self.searchString = ""
    self:UpdateMarket()
end
do
    -- Search stride is the number of elements between products.
    -- This is because C++ returns products as category​Index, subcategory​Index, productIndex
    local SEARCH_DATA_STRIDE = 3
    function ZO_Market_Shared:UpdateSearchResults(...)
        ZO_ClearTable(self.searchResults)
        for i = 1, select("#", ...), SEARCH_DATA_STRIDE do
            local categoryIndex, subcategoryIndex, productIndex = select(i, ...)
            if not self.searchResults[categoryIndex] then
                self.searchResults[categoryIndex] = {}
            end
            local effectiveSubCategory = subcategoryIndex or "root"
            if not self.searchResults[categoryIndex][effectiveSubCategory] then
                self.searchResults[categoryIndex][effectiveSubCategory] = {}
            end
            self.searchResults[categoryIndex][effectiveSubCategory][productIndex] = true
        end
    end
end
function ZO_Market_Shared:OnInitialInteraction()
        OpenMarket()
        SetSecureRenderModeEnabled(true)
        -- ensure that we are in the correct state
        if self.marketState ~= GetMarketState() then
            self:UpdateMarket()
        elseif self.marketState == MARKET_STATE_OPEN then
            self:UpdateCurrentCategory()
        end
end
function ZO_Market_Shared:OnEndInteraction()
end
function ZO_Market_Shared:OnStateChanged(oldState, newState)
    if newState == SCENE_SHOWING then
        self:OnInitialInteraction()
        self:OnShowing()
    elseif newState == SCENE_SHOWN then
        self:OnShown()
    elseif newState == SCENE_HIDING then
        self:OnHiding()
    elseif newState == SCENE_HIDDEN then
        self:OnHidden()
        self:OnEndInteraction()
    end
end
function ZO_Market_Shared:RegisterSceneStateChangeCallback()
    self.marketScene:RegisterCallback("StateChange", function(...) self:OnStateChanged(...) end)
end
function ZO_Market_Shared:ResetCategoryData()
    self.currentCategoryData = nil
end
function ZO_Market_Shared:OnMarketStateUpdated(marketState)
    self:UpdateMarket()
end
-- difference is the difference in crowns for this update. For instance, new crowns purchased will show a positive difference.
-- TO DO: this will be used once live updates to crowns are supported during the game.
function ZO_Market_Shared:OnMarketCurrencyUpdated(currentCurrency, difference)
    self:UpdateCurrencyBalance(currentCurrency)
end
function ZO_Market_Shared:OnMarketPurchaseResult()
end
function ZO_Market_Shared:OnMarketSearchResultsReady()
    self:UpdateMarket()
end
function ZO_Market_Shared:UpdateCurrentCategory()
    if self.currentCategoryData then
        self:RefreshProducts()
    end
end
function ZO_Market_Shared:OnCollectibleUpdated(justUnlocked)
    if justUnlocked then
        self:UpdateCurrentCategory()
    end
end
function ZO_Market_Shared:UpdateMarket()
    self.marketState = GetMarketState()
    if self.marketState == MARKET_STATE_OPEN then
        self:OnMarketOpen()
    else -- MARKET_STATE_LOCKED
        self:OnMarketLocked()
    end
    self:OnMarketUpdate()
end
function ZO_Market_Shared:GetState()
    return self.marketState
end
function ZO_Market_Shared:SetMarketScene(scene)
    self.marketScene = scene
end
function ZO_Market_Shared:BeginPreview()
    self.selectedMarketProduct:Preview()
end 
function ZO_Market_Shared:EndCurrentPreview()
    self:RefreshActions()
end
function ZO_Market_Shared:OnMarketOpen()
    self:ShowMarket(true)
end
function ZO_Market_Shared:OnCategorySelected(data)
    
    if data.featured then
    else
        self:BuildMarketProductList(data)
    end
    self.currentCategoryData = data
    self:RefreshActions()
end
function ZO_Market_Shared:AddKeybinds()
    KEYBIND_STRIP:AddKeybindButtonGroup(self.keybindStripDescriptors)
end
function ZO_Market_Shared:RemoveKeybinds()
    KEYBIND_STRIP:RemoveKeybindButtonGroup(self.keybindStripDescriptors)
end
function ZO_Market_Shared:RefreshKeybinds()
    KEYBIND_STRIP:UpdateKeybindButtonGroup(self.keybindStripDescriptors)
end
do
    local function GetFeaturedProductIds(index, ...)
        if index >= 1 then
            local id = GetFeaturedMarketProductId(index)
            index = index - 1
            return GetFeaturedProductIds(index, id, ...)
        end
        return ...
    end
    function ZO_Market_Shared:BuildFeaturedMarketProductList()
        local numFeaturedMarketProducts = GetNumFeaturedMarketProducts()
        
        self:LayoutMarketProducts(GetFeaturedProductIds(numFeaturedMarketProducts))
    end
end
function ZO_Market_Shared:GetCategoryIndices(data, parentData)
    if parentData then
        return parentData.categoryIndex, data.categoryIndex
    end
        
    return data.categoryIndex
end
function ZO_Market_Shared:BuildMarketProductList(data)
    local parentData = data.parentData
    local categoryIndex, subCategoryIndex = self:GetCategoryIndices(data, parentData)
        
    local numMarketProducts
    if subCategoryIndex then
        numMarketProducts = select(2, GetMarketProductSubCategoryInfo(categoryIndex, subCategoryIndex))
    else
        numMarketProducts = select(3, GetMarketProductCategoryInfo(categoryIndex))
    end
        
    self:LayoutMarketProducts(self:GetMarketProductIds(categoryIndex, subCategoryIndex, numMarketProducts))
end
function ZO_Market_Shared:GetPreviewState()
    local isPreviewing = IsCurrentlyPreviewing()
    local canPreview = false
    local isActivePreview = false
    local marketProduct = self.selectedMarketProduct
    if marketProduct ~= nil then -- User is hovering over a MarketProduct
        canPreview = marketProduct:HasPreview() and IsCharacterPreviewingAvailable()
        if isPreviewing and marketProduct:IsPreviewingActiveCollectible() then
            isActivePreview = true
        end
    end
    return isPreviewing, canPreview, isActivePreview
end
function ZO_Market_Shared:IsReadyToPreview()
    local _, canPreview, isActivePreview = self:GetPreviewState()
    return canPreview and not isActivePreview
end
function ZO_Market_Shared:ShouldAddMarketProduct(filterType, id)
    if(filterType == MARKET_FILTER_VIEW_ALL) then return true end
    local purchased = IsMarketProductPurchased(id)
    if(purchased) then
        return filterType == MARKET_FILTER_VIEW_PURCHASED
    else
        return filterType == MARKET_FILTER_VIEW_NOT_PURCHASED
    end
end
--[[ Functions to be overridden ]]--
function ZO_Market_Shared:OnShowing()
end
function ZO_Market_Shared:OnShown()
end
function ZO_Market_Shared:OnHiding()
end
function ZO_Market_Shared:OnHidden()
end
function ZO_Market_Shared:UpdateCurrencyBalance(currency)
end
function ZO_Market_Shared:InitializeKeybindDescriptors()
end
function ZO_Market_Shared:InitializeCategories()
end
function ZO_Market_Shared:InitializeMarketList()
end
function ZO_Market_Shared:InitializeFilters()
end
function ZO_Market_Shared:CreateMarketScene()
end
function ZO_Market_Shared:BuildCategories()
end
function ZO_Market_Shared:ShowMarket(show)
end
function ZO_Market_Shared:OnMarketUpdate()
end
function ZO_Market_Shared:OnMarketLocked()
end
function ZO_Market_Shared:RefreshVisibleCategoryFilter()
end
function ZO_Market_Shared:RefreshProducts()
end
function ZO_Market_Shared:GetMarketProductIds(categoryIndex, subCategoryIndex, index, ...)
    assert(true) -- must be overridden
end
function ZO_Market_Shared:RefreshActions()
    self:RefreshKeybinds() -- default behavior
end
--[[Search]]--
--------------
function ZO_Market_Shared:SearchStart(searchString)
    self.searchString = searchString
    StartMarketProductSearch(searchString)
end