Back to Home

ESO Lua File v101044

ingame/housingeditor/housingpreview_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
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
ZO_HousingPreview_Manager = ZO_CallbackObject:Subclass()
function ZO_HousingPreview_Manager:New(...)
    local singleton = ZO_CallbackObject.New(self)
    singleton:Initialize(...)
    return singleton
end
function ZO_HousingPreview_Manager:Initialize()
    self.houseStoreData = {}
    self.houseMarketData = {}
    self.fullDataKeyed = {}
    self.fullDataSorted = {}
    self.displayInfo = {}
end
function ZO_HousingPreview_Manager:RegisterForEvents()
    local function OnPlayerActivated()
        local zoneHouseId = GetCurrentZoneHouseId()
        if zoneHouseId > 0 then
            local collectibleId = GetCollectibleIdForHouse(zoneHouseId)
            local collectibleData = ZO_CollectibleData_Base.Acquire(collectibleId)
            local displayInfo = self.displayInfo
            displayInfo.houseName = collectibleData:GetFormattedName()
            local foundInZoneId = GetHouseFoundInZoneId(zoneHouseId)
            displayInfo.houseFoundInLocation = GetZoneNameById(foundInZoneId)
            local houseCategory = GetHouseCategoryType(zoneHouseId)
            displayInfo.houseCategory = GetString("SI_HOUSECATEGORYTYPE", houseCategory)
            displayInfo.backgroundImage = GetHousePreviewBackgroundImage(zoneHouseId)
            collectibleData:ReleaseObject()
            self:FireCallbacks("OnPlayerActivated")
        else
            ZO_ClearTable(self.displayInfo)
        end
    end
    local function OnOpenHouseStore()
        if SYSTEMS:GetObject("HOUSING_PREVIEW"):IsShowing() then
            self:UpdateHouseStoreData()
            EndInteraction(INTERACTION_VENDOR)
        end
    end
    local function OnMarketStateUpdated(eventCode, displayGroup, marketState)
        if displayGroup == MARKET_DISPLAY_GROUP_HOUSE_PREVIEW then
            self:UpdateHouseMarketData(marketState)
        end
    end
    EVENT_MANAGER:RegisterForEvent("ZO_HousingPreview_Manager", EVENT_PLAYER_ACTIVATED, OnPlayerActivated)
    EVENT_MANAGER:RegisterForEvent("ZO_HousingPreview_Manager", EVENT_OPEN_HOUSE_STORE, OnOpenHouseStore)
    EVENT_MANAGER:RegisterForEvent("ZO_HousingPreview_Manager", EVENT_MARKET_STATE_UPDATED, OnMarketStateUpdated)
    EVENT_MANAGER:RegisterForEvent("ZO_HousingPreview_Manager", EVENT_CLOSE_STORE, function(eventCode) self:FireCallbacks("HouseStoreInteractEnd") end)
end
function ZO_HousingPreview_Manager:UpdateHouseStoreData()
    ZO_ClearNumericallyIndexedTable(self.houseStoreData)
    for entryIndex = 1, GetNumStoreItems() do
        local _, name, _, price, _, meetsRequirementsToBuy, _, _, _, _, _, _, _, entryType, buyStoreFailure, buyErrorStringId = GetStoreEntryInfo(entryIndex)
        if entryType == STORE_ENTRY_TYPE_HOUSE_WITH_TEMPLATE then
            local houseTemplateId = GetStoreEntryHouseTemplateId(entryIndex)
            local houseTemplateData =
            {
                goldStoreEntryIndex = entryIndex,
                houseTemplateId = houseTemplateId,
                name = name,
                goldPrice = price,
                requiredToBuyErrorText = not meetsRequirementsToBuy and ZO_StoreManager_GetRequiredToBuyErrorText(buyStoreFailure, buyErrorStringId) or nil,
            }
            table.insert(self.houseStoreData, houseTemplateData)
        end
    end
end
local PRODUCT_LISTINGS_STRIDE = 2
function ZO_HousingPreview_Manager:UpdateHouseMarketData(marketState)
    if not marketState then
        marketState = GetMarketState(MARKET_DISPLAY_GROUP_HOUSE_PREVIEW)
    end
    ZO_ClearNumericallyIndexedTable(self.houseMarketData)
    if marketState == MARKET_STATE_OPEN then
        local zoneHouseId = GetCurrentZoneHouseId()
        if zoneHouseId > 0 then
            for index = 1, GetNumHouseTemplatesForHouse(zoneHouseId) do
                local houseTemplateId = GetHouseTemplateIdByIndexForHouse(zoneHouseId, index)
                local marketProductListings = { GetActiveMarketProductListingsForHouseTemplate(houseTemplateId, MARKET_DISPLAY_GROUP_HOUSE_PREVIEW) }
                if #marketProductListings > 0 then
                    local houseTemplateData =
                    {
                        houseTemplateId = houseTemplateId,
                        marketPurchaseOptions = { }
                    }
                    --There could be multiple listings per template, one for each currency type.
                    for i = 1, #marketProductListings, PRODUCT_LISTINGS_STRIDE do
                        local marketProductId = marketProductListings[i]
                        local presentationIndex = marketProductListings[i + 1]
                        local currencyType, cost, costAfterDiscount, discountPercent, esoPlusCost = GetMarketProductPricingByPresentation(marketProductId, presentationIndex)
                        --Don't allow the same currency twice. This is a nonsense scenario but technically possible.
                        if not houseTemplateData.marketPurchaseOptions[currencyType] then
                            local marketPurchaseData =
                            {
                                marketProductId = marketProductId,
                                presentationIndex = presentationIndex,
                                cost = cost,
                                costAfterDiscount = costAfterDiscount,
                                discountPercent = discountPercent,
                            }
                            houseTemplateData.marketPurchaseOptions[currencyType] = marketPurchaseData
                            houseTemplateData.name = houseTemplateData.name or GetMarketProductDisplayName(marketProductId)
                        end
                    end
                    table.insert(self.houseMarketData, houseTemplateData)
                end
            end
        end
    end
end
function ZO_HousingPreview_Manager:AddTemplateDataToFullLists(templateData)
    local copiedTemplateData = ZO_ShallowTableCopy(templateData)
    self.fullDataKeyed[templateData.houseTemplateId] = copiedTemplateData
    table.insert(self.fullDataSorted, copiedTemplateData)
end
function ZO_HousingPreview_Manager:RefreshFullHouseTemplateData()
    ZO_ClearTable(self.fullDataKeyed)
    ZO_ClearNumericallyIndexedTable(self.fullDataSorted)
    for i, templateData in ipairs(self.houseStoreData) do
        self:AddTemplateDataToFullLists(templateData)
    end
    for i, templateData in ipairs(self.houseMarketData) do
        local existingData = self.fullDataKeyed[templateData.houseTemplateId]
        if existingData then
            existingData.marketPurchaseOptions = templateData.marketPurchaseOptions
        else
            self:AddTemplateDataToFullLists(templateData)
        end
    end
    self:FireCallbacks("OnHouseTemplateDataUpdated")
end
function ZO_HousingPreview_Manager:GetDisplayInfo()
    return self.displayInfo
end
function ZO_HousingPreview_Manager:GetFullHouseTemplateData()
    return self.fullDataSorted
end
function ZO_HousingPreview_Manager:RequestOpenMarket()
    OpenMarket(MARKET_DISPLAY_GROUP_HOUSE_PREVIEW)
    local marketState = GetMarketState(MARKET_DISPLAY_GROUP_HOUSE_PREVIEW)
    if marketState == MARKET_STATE_OPEN then
        self:UpdateHouseMarketData(marketState)
    end
end
ZO_HOUSE_PREVIEW_MANAGER = ZO_HousingPreview_Manager:New()