-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
GameConfigAssetBase.lua
265 lines (228 loc) · 7.16 KB
/
GameConfigAssetBase.lua
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
--[=[
@class GameConfigAssetBase
]=]
local require = require(script.Parent.loader).load(script)
local BaseObject = require("BaseObject")
local Rx = require("Rx")
local RxAttributeUtils = require("RxAttributeUtils")
local RxInstanceUtils = require("RxInstanceUtils")
local GameConfigAssetConstants = require("GameConfigAssetConstants")
local Promise = require("Promise")
local GameConfigAssetUtils = require("GameConfigAssetUtils")
local AttributeValue = require("AttributeValue")
local GameConfigTranslator = require("GameConfigTranslator")
local GameConfigAssetBase = setmetatable({}, BaseObject)
GameConfigAssetBase.ClassName = "GameConfigAssetBase"
GameConfigAssetBase.__index = GameConfigAssetBase
--[=[
Constructs a new GameConfigAssetBase. Should be done via binder. This is a base class.
@param obj Folder
@param serviceBag ServiceBag
@return GameConfigAssetBase
]=]
function GameConfigAssetBase.new(obj, serviceBag)
local self = setmetatable(BaseObject.new(obj), GameConfigAssetBase)
self._serviceBag = assert(serviceBag, "No serviceBag")
self._nameTranslationKey = AttributeValue.new(self._obj, "NameTranslationKey", "assets.name.unknown")
self._descriptionTranslationKey = AttributeValue.new(self._obj, "DescriptionTranslationKey", "assets.description.unknown")
self._configTranslator = self._serviceBag:GetService(GameConfigTranslator)
return self
end
--[=[
Observes the translated name
@return Observable<string>
]=]
function GameConfigAssetBase:ObserveTranslatedName()
return self:ObserveNameTranslationKey():Pipe({
Rx.switchMap(function(key)
return self._configTranslator:ObserveFormatByKey(key)
end)
})
end
--[=[
Observes the translated description
@return Observable<string>
]=]
function GameConfigAssetBase:ObserveTranslatedDescription()
return self:ObserveDescriptionTranslationKey():Pipe({
Rx.switchMap(function(key)
return self._configTranslator:ObserveFormatByKey(key)
end)
})
end
function GameConfigAssetBase:SetNameTranslationKey(nameTranslationKey)
assert(type(nameTranslationKey) == "string" or nameTranslationKey == nil, "Bad nameTranslationKey")
self._nameTranslationKey.Value = nameTranslationKey or "assets.name.unknown"
end
function GameConfigAssetBase:SetDescriptionTranslationKey(descriptionTranslationKey)
assert(type(descriptionTranslationKey) == "string" or descriptionTranslationKey == nil, "Bad descriptionTranslationKey")
self._descriptionTranslationKey.Value = descriptionTranslationKey or "assets.description.unknown"
end
--[=[
Gets the asset id
@return number
]=]
function GameConfigAssetBase:GetAssetId()
return self._obj:GetAttribute(GameConfigAssetConstants.ASSET_ID_ATTRIBUTE)
end
--[=[
Observes the assetId
@return Observable<number>
]=]
function GameConfigAssetBase:ObserveAssetId()
return RxAttributeUtils.observeAttribute(self._obj, GameConfigAssetConstants.ASSET_ID_ATTRIBUTE, nil)
end
--[=[
Gets the asset type
@return string?
]=]
function GameConfigAssetBase:GetAssetType()
return self._obj:GetAttribute(GameConfigAssetConstants.ASSET_TYPE_ATTRIBUTE)
end
--[=[
Observes the asset type
@return Observable<string?>
]=]
function GameConfigAssetBase:ObserveAssetType()
return RxAttributeUtils.observeAttribute(self._obj, GameConfigAssetConstants.ASSET_TYPE_ATTRIBUTE, nil)
end
--[=[
Observes the asset key
@return Observable<string>
]=]
function GameConfigAssetBase:ObserveAssetKey()
return RxInstanceUtils.observeProperty(self._obj, "Name")
end
--[=[
Gets the asset key
@return string
]=]
function GameConfigAssetBase:GetAssetKey()
return self._obj.Name
end
--[=[
Observes the asset state
@return any
]=]
function GameConfigAssetBase:ObserveState()
return Rx.combineLatest({
assetId = self:ObserveAssetId();
assetKey = self:ObserveAssetKey();
assetType = self:ObserveAssetType();
})
end
--[=[
Promises the cloud price in Robux
@param cancelToken CancelToken
@return Promise<string?>
]=]
function GameConfigAssetBase:PromiseCloudPriceInRobux(cancelToken)
return Rx.toPromise(self:ObserveCloudPriceInRobux(), cancelToken)
end
--[=[
Promises the cloud price in Robux
@param cancelToken CancelToken
@return Promise<string?>
]=]
function GameConfigAssetBase:PromiseCloudName(cancelToken)
return Rx.toPromise(self:ObserveCloudName(), cancelToken)
end
--[=[
Promises the color of the game asset (for dialog and other systems)
@param _cancelToken CancelToken
@return Promise<Color3>
]=]
function GameConfigAssetBase:PromiseColor(_cancelToken)
return Promise.resolved(Color3.fromRGB(66, 158, 166))
end
--[=[
Promises the name translation key
@param cancelToken CancelToken
@return Promise<string?>
]=]
function GameConfigAssetBase:PromiseNameTranslationKey(cancelToken)
return Rx.toPromise(self:ObserveNameTranslationKey(), cancelToken)
end
--[=[
Observes the name translation key.
@return Observable<string?>
]=]
function GameConfigAssetBase:ObserveNameTranslationKey()
return self._nameTranslationKey:Observe()
end
--[=[
Observes the description translation key.
@return Observable<string?>
]=]
function GameConfigAssetBase:ObserveDescriptionTranslationKey()
return self._descriptionTranslationKey:Observe()
end
--[=[
Observes the cloud name. See [GameConfigAssetBase.ObserveNameTranslationKey] for
translation keys.
@return Observable<string?>
]=]
function GameConfigAssetBase:ObserveCloudName()
return self:_observeCloudProperty({ "Name" }, "string")
end
--[=[
Observes the cloud name. See [GameConfigAssetBase.ObserveDescriptionTranslationKey] for
translation keys.
@return Observable<string?>
]=]
function GameConfigAssetBase:ObserveCloudDescription()
return self:_observeCloudProperty({ "Description" }, "string")
end
--[=[
Observes the cost in Robux.
@return Observable<number?>
]=]
function GameConfigAssetBase:ObserveCloudPriceInRobux()
return self:_observeCloudProperty({ "PriceInRobux" }, "number")
end
--[=[
@return Observable<number?>
]=]
function GameConfigAssetBase:ObserveCloudIconImageAssetId()
return self:_observeCloudProperty({ "IconImageAssetId", "IconImageId" }, "number")
end
function GameConfigAssetBase:_observeCloudProperty(propertyNameList, expectedType)
assert(type(propertyNameList) == "table", "Bad propertyNameList")
assert(type(expectedType) == "string", "Bad expectedType")
return self:_observeCloudDataFromState():Pipe({
Rx.map(function(data)
if type(data) == "table" then
for _, propertyName in pairs(propertyNameList) do
local result = data[propertyName]
if type(result) == expectedType then
return result
end
end
return nil
else
return nil
end
end)
})
end
function GameConfigAssetBase:_observeCloudDataFromState()
if self._cloudDataObservable then
return self._cloudDataObservable
end
self._cloudDataObservable = self:ObserveState():Pipe({
Rx.switchMap(function(state)
if type(state.assetId) == "number" and type(state.assetType) == "string" and type(state.assetKey) == "string" then
return Rx.fromPromise(self:_promiseCloudDataForState(state))
else
return Rx.of(nil)
end
end);
Rx.distinct();
Rx.shareReplay(1);
})
return self._cloudDataObservable
end
function GameConfigAssetBase:_promiseCloudDataForState(state)
return GameConfigAssetUtils.promiseCloudDataForAssetType(state.assetType, state.assetId)
end
return GameConfigAssetBase