@quenty/gameproductservice 14.56.0 → 14.58.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,168 @@
1
+ --!strict
2
+ --[[
3
+ Integration coverage for GameProductDataService driven through a real ServiceBag. Only
4
+ GameProductDataService (and its dependency TieRealmService) is registered, so the heavy
5
+ binder/MarketplaceService graph is never started. This focuses on the pieces that do not
6
+ require a live Player: the exported purchase signals, the server-only prompting ValueObject,
7
+ and the prompt guard's realm behavior. The realm is forced explicitly so the test is not
8
+ sensitive to where the runner executes.
9
+
10
+ @class GameProductDataService.spec.lua
11
+ ]]
12
+ local require = require(script.Parent.loader).load(script)
13
+
14
+ local GameConfigAssetTypes = require("GameConfigAssetTypes")
15
+ local GameProductDataService = require("GameProductDataService")
16
+ local Jest = require("Jest")
17
+ local PromiseTestUtils = require("PromiseTestUtils")
18
+ local ServiceBag = require("ServiceBag")
19
+ local Signal = require("Signal")
20
+ local TieRealmService = require("TieRealmService")
21
+ local TieRealms = require("TieRealms")
22
+
23
+ local describe = Jest.Globals.describe
24
+ local expect = Jest.Globals.expect
25
+ local it = Jest.Globals.it
26
+
27
+ local function setup(tieRealm: TieRealms.TieRealm?)
28
+ local serviceBag = ServiceBag.new()
29
+ local tieRealmService = (serviceBag:GetService(TieRealmService) :: any) :: TieRealmService.TieRealmService
30
+ local gameProductDataService = (
31
+ serviceBag:GetService(GameProductDataService) :: any
32
+ ) :: GameProductDataService.GameProductDataService
33
+ serviceBag:Init()
34
+ tieRealmService:SetTieRealm(tieRealm or TieRealms.SERVER)
35
+ serviceBag:Start()
36
+
37
+ return {
38
+ serviceBag = serviceBag,
39
+ tieRealmService = tieRealmService,
40
+ gameProductDataService = gameProductDataService,
41
+ destroy = function()
42
+ serviceBag:Destroy()
43
+ end,
44
+ }
45
+ end
46
+
47
+ describe("GameProductDataService purchase signals", function()
48
+ it("should expose the six purchase signals after init", function()
49
+ local context = setup()
50
+ local service = context.gameProductDataService
51
+
52
+ for _, signalName in
53
+ {
54
+ "GamePassPurchased",
55
+ "ProductPurchased",
56
+ "AssetPurchased",
57
+ "BundlePurchased",
58
+ "SubscriptionPurchased",
59
+ "MembershipPurchased",
60
+ }
61
+ do
62
+ expect(Signal.isSignal((service :: any)[signalName])).toEqual(true)
63
+ end
64
+
65
+ context.destroy()
66
+ end)
67
+ end)
68
+
69
+ describe("GameProductDataService server-only prompting", function()
70
+ it("should default to disabled", function()
71
+ local context = setup()
72
+ expect(context.gameProductDataService:GetServerOnlyPromptingValue().Value).toEqual(false)
73
+ context.destroy()
74
+ end)
75
+
76
+ it("should enable and disable via SetServerOnlyPrompting on the server", function()
77
+ local context = setup()
78
+ local service = context.gameProductDataService
79
+
80
+ service:SetServerOnlyPrompting(true)
81
+ expect(service:GetServerOnlyPromptingValue().Value).toEqual(true)
82
+
83
+ service:SetServerOnlyPrompting(false)
84
+ expect(service:GetServerOnlyPromptingValue().Value).toEqual(false)
85
+ context.destroy()
86
+ end)
87
+
88
+ it("should replicate the change through the ValueObject observable", function()
89
+ local context = setup()
90
+ local service = context.gameProductDataService
91
+
92
+ local values = {}
93
+ local sub = service:GetServerOnlyPromptingValue():Observe():Subscribe(function(value)
94
+ table.insert(values, value)
95
+ end)
96
+
97
+ service:SetServerOnlyPrompting(true)
98
+ sub:Destroy()
99
+
100
+ expect(values[1]).toEqual(false)
101
+ expect(values[#values]).toEqual(true)
102
+ context.destroy()
103
+ end)
104
+
105
+ it("should reject a non-boolean argument", function()
106
+ local context = setup()
107
+ local service = context.gameProductDataService
108
+
109
+ expect(function()
110
+ service:SetServerOnlyPrompting("nope" :: any)
111
+ end).toThrow()
112
+ context.destroy()
113
+ end)
114
+
115
+ it("should refuse to configure server-only prompting from the client realm", function()
116
+ local context = setup(TieRealms.CLIENT)
117
+ local service = context.gameProductDataService
118
+
119
+ expect(function()
120
+ service:SetServerOnlyPrompting(true)
121
+ end).toThrow()
122
+ context.destroy()
123
+ end)
124
+ end)
125
+
126
+ describe("GameProductDataService ownership overrides", function()
127
+ it("should refuse to set an ownership override from the client realm", function()
128
+ local context = setup(TieRealms.CLIENT)
129
+ local service = context.gameProductDataService
130
+ local fakePlayer = Instance.new("Folder")
131
+
132
+ -- The realm guard is checked before anything else, so a player can never grant themselves
133
+ -- ownership from the client.
134
+ expect(function()
135
+ service:SetPlayerOwnershipOverride(fakePlayer :: any, GameConfigAssetTypes.GAME, 123, true)
136
+ end).toThrow()
137
+
138
+ fakePlayer:Destroy()
139
+ context.destroy()
140
+ end)
141
+
142
+ it("should refuse to clear an ownership override from the client realm", function()
143
+ local context = setup(TieRealms.CLIENT)
144
+ local service = context.gameProductDataService
145
+ local fakePlayer = Instance.new("Folder")
146
+
147
+ expect(function()
148
+ service:ClearPlayerOwnershipOverride(fakePlayer :: any, GameConfigAssetTypes.GAME, 123)
149
+ end).toThrow()
150
+
151
+ fakePlayer:Destroy()
152
+ context.destroy()
153
+ end)
154
+ end)
155
+
156
+ describe("GameProductDataService prompt guard", function()
157
+ it("should resolve immediately on the server realm", function()
158
+ local context = setup(TieRealms.SERVER)
159
+ local service = context.gameProductDataService
160
+
161
+ local fakePlayer = Instance.new("Folder")
162
+ local outcome = PromiseTestUtils.awaitOutcome(service:_promiseServerOnlyPromptingGuard(fakePlayer :: any), 5)
163
+ expect(outcome).toEqual("resolved")
164
+
165
+ fakePlayer:Destroy()
166
+ context.destroy()
167
+ end)
168
+ end)
@@ -1,8 +1,14 @@
1
1
  --!strict
2
2
  --[=[
3
- This tracking is relatively complicated because we want to track both
4
- ownership from attributes (can be customized locally), as well as async
5
- cloud services.
3
+ Tracks whether a player owns a given asset. Ownership resolves from an async cloud
4
+ query (see [PlayerAssetOwnershipTracker:SetQueryOwnershipCallback]) plus session
5
+ purchases, with an optional local override layered on top.
6
+
7
+ The override (see [PlayerAssetOwnershipTracker:SetOwnershipOverride]) is an authoritative layer:
8
+ when set it wins over the cloud query and session purchases, forcing ownership on (`true`) or off
9
+ (`false`). It is stored in a server-owned [JSONAttributeValue] on the player that Roblox
10
+ replicates read-only to clients, so every realm applies the same overrides but only the server
11
+ can assign them -- a player can never grant themselves ownership.
6
12
 
7
13
  @class PlayerAssetOwnershipTracker
8
14
  ]=]
@@ -13,10 +19,14 @@ local BaseObject = require("BaseObject")
13
19
  local GameConfigAssetTypeUtils = require("GameConfigAssetTypeUtils")
14
20
  local GameConfigAssetTypes = require("GameConfigAssetTypes")
15
21
  local GameConfigPicker = require("GameConfigPicker")
22
+ local JSONAttributeValue = require("JSONAttributeValue")
16
23
  local Maid = require("Maid")
17
24
  local Observable = require("Observable")
25
+ local ObservableMap = require("ObservableMap")
18
26
  local ObservableSet = require("ObservableSet")
27
+ local PlayerProductOwnershipOverrideUtils = require("PlayerProductOwnershipOverrideUtils")
19
28
  local Promise = require("Promise")
29
+ local Rx = require("Rx")
20
30
  local ValueObject = require("ValueObject")
21
31
 
22
32
  local PlayerAssetOwnershipTracker = setmetatable({}, BaseObject)
@@ -32,6 +42,14 @@ export type PlayerAssetOwnershipTracker =
32
42
  _marketTracker: any,
33
43
  _ownershipCallback: ValueObject.ValueObject<(number) -> Promise.Promise<boolean> | nil>,
34
44
  _ownedAssetIdSet: ObservableSet.ObservableSet<number>,
45
+ -- Typed `any` (not ObservableMap<number, boolean>) to keep this class's intersection
46
+ -- type small enough for the Luau solver; accessed only through `self: any` methods.
47
+ _ownershipOverrides: any,
48
+ -- Server-owned JSONAttributeValue holding the replicated override map, plus the last map
49
+ -- applied to _ownershipOverrides (for diffing on reconcile). Both `any` for the same
50
+ -- solver reason as _ownershipOverrides.
51
+ _overrideAttribute: any,
52
+ _appliedOverrideState: any,
35
53
  _assetOwnershipPromiseCache: { [number]: Promise.Promise<boolean> | boolean },
36
54
  },
37
55
  {} :: typeof({ __index = PlayerAssetOwnershipTracker })
@@ -55,6 +73,11 @@ function PlayerAssetOwnershipTracker.new(
55
73
 
56
74
  self._ownershipCallback = self._maid:Add(ValueObject.new(nil))
57
75
  self._ownedAssetIdSet = self._maid:Add(ObservableSet.new())
76
+ self._ownershipOverrides = self._maid:Add(ObservableMap.new())
77
+
78
+ self._appliedOverrideState = {}
79
+ self._overrideAttribute =
80
+ JSONAttributeValue.new(self._player, PlayerProductOwnershipOverrideUtils.attributeName(assetType), nil)
58
81
 
59
82
  self._assetOwnershipPromiseCache = {}
60
83
 
@@ -62,6 +85,13 @@ function PlayerAssetOwnershipTracker.new(
62
85
  self:SetOwnership(idOrKey, true)
63
86
  end))
64
87
 
88
+ -- Apply the replicated override map to _ownershipOverrides. Observe() emits synchronously on
89
+ -- subscribe (current value) and again on every change -- including a client receiving the
90
+ -- server's replicated write -- so this covers both the initial reconcile and later updates.
91
+ self._maid:GiveTask(self._overrideAttribute:Observe():Subscribe(function()
92
+ self:_reconcileOverrides()
93
+ end))
94
+
65
95
  return self
66
96
  end
67
97
 
@@ -143,6 +173,86 @@ function PlayerAssetOwnershipTracker:SetOwnership(idOrKey, ownsAsset: boolean):
143
173
  self._ownedAssetIdSet:Add(assetId)
144
174
  end
145
175
 
176
+ --[=[
177
+ Sets a server-authoritative override for the player's ownership of the asset. When set, the
178
+ override wins over the cloud query and any session purchase, forcing ownership on (`true`) or off
179
+ (`false`). Passing `nil` clears the override so ownership falls back to the cloud query again
180
+ (equivalent to [PlayerAssetOwnershipTracker.ClearOwnershipOverride]).
181
+
182
+ The override is published into a replicated attribute and applied on every realm, so it drives
183
+ client-side ownership-gated UI. Only the server can assign it: the client-facing service exposes
184
+ no setter and [GameProductService.SetPlayerOwnershipOverride] guards the server realm, so a player
185
+ can never grant themselves ownership. Callers should go through that service, never here directly.
186
+
187
+ @param idOrKey number | string
188
+ @param ownsAsset boolean?
189
+ ]=]
190
+ -- `self: any` here (and on ClearOwnershipOverride / the observe helpers) keeps the Luau solver
191
+ -- from renormalizing the full PlayerAssetOwnershipTracker & BaseObject intersection at every
192
+ -- `self:...` access, which otherwise trips "Code is too complex to typecheck" for this module.
193
+ function PlayerAssetOwnershipTracker.SetOwnershipOverride(self: any, idOrKey, ownsAsset: boolean?): ()
194
+ assert(type(idOrKey) == "number" or type(idOrKey) == "string", "Bad idOrKey")
195
+ assert(type(ownsAsset) == "boolean" or ownsAsset == nil, "Bad ownsAsset")
196
+
197
+ local assetId = self._configPicker:ToAssetId(self._assetType, idOrKey)
198
+ if not assetId then
199
+ warn(
200
+ string.format("[PlayerAssetOwnershipTracker.SetOwnershipOverride] - Nothing with key %q", tostring(idOrKey))
201
+ )
202
+ return
203
+ end
204
+
205
+ -- Resolve to a stable asset id so the override applies to the same key every realm observes.
206
+ local state = PlayerProductOwnershipOverrideUtils.sanitizeState(self._overrideAttribute.Value)
207
+ if ownsAsset == nil then
208
+ state[tostring(assetId)] = nil
209
+ else
210
+ state[tostring(assetId)] = ownsAsset
211
+ end
212
+ if next(state) == nil then
213
+ -- Drop the attribute entirely so "no overrides" is the absence of the attribute.
214
+ self._overrideAttribute.Value = nil
215
+ else
216
+ self._overrideAttribute.Value = state
217
+ end
218
+
219
+ -- Apply immediately rather than waiting on the (possibly deferred) attribute-changed signal.
220
+ -- _reconcileOverrides diffs against the applied state, so the later Observe callback is a no-op.
221
+ self:_reconcileOverrides()
222
+ end
223
+
224
+ -- Applies the replicated override map onto _ownershipOverrides, adding/updating present entries and
225
+ -- removing ones that are gone. Runs on every realm from the attribute observer.
226
+ function PlayerAssetOwnershipTracker._reconcileOverrides(self: any): ()
227
+ local desiredState = PlayerProductOwnershipOverrideUtils.sanitizeState(self._overrideAttribute.Value)
228
+
229
+ for key, ownsAsset in desiredState do
230
+ if self._appliedOverrideState[key] ~= ownsAsset then
231
+ self._ownershipOverrides:Set(tonumber(key) or key, ownsAsset)
232
+ end
233
+ end
234
+
235
+ for key in self._appliedOverrideState do
236
+ if desiredState[key] == nil then
237
+ self._ownershipOverrides:Remove(tonumber(key) or key)
238
+ end
239
+ end
240
+
241
+ self._appliedOverrideState = desiredState
242
+ end
243
+
244
+ --[=[
245
+ Clears any local ownership override for the asset, so ownership falls back to the cloud
246
+ query. Equivalent to `SetOwnershipOverride(idOrKey, nil)`.
247
+
248
+ @param idOrKey number | string
249
+ ]=]
250
+ function PlayerAssetOwnershipTracker.ClearOwnershipOverride(self: any, idOrKey): ()
251
+ assert(type(idOrKey) == "number" or type(idOrKey) == "string", "Bad idOrKey")
252
+
253
+ self:SetOwnershipOverride(idOrKey, nil)
254
+ end
255
+
146
256
  --[=[
147
257
  Promises true if the user owns the asset and false otherwise
148
258
 
@@ -154,6 +264,12 @@ function PlayerAssetOwnershipTracker:PromiseOwnsAsset(idOrKey)
154
264
 
155
265
  local assetId = self._configPicker:ToAssetId(self._assetType, idOrKey)
156
266
  if assetId then
267
+ -- Local override wins over the cloud query and session purchases.
268
+ local override = self._ownershipOverrides:Get(assetId)
269
+ if override ~= nil then
270
+ return Promise.resolved(override)
271
+ end
272
+
157
273
  if self._ownedAssetIdSet:Contains(assetId) then
158
274
  return Promise.resolved(true)
159
275
  end
@@ -192,34 +308,68 @@ function PlayerAssetOwnershipTracker:ObserveOwnsAsset(idOrKey): Observable.Obser
192
308
  return
193
309
  end
194
310
 
195
- -- Only fire once we find the asset
196
311
  local maid, assetId = brio:ToMaidAndValue()
197
- maid:GivePromise(self:_promiseQueryAssetId(assetId)):Then(function()
198
- maid:GiveTask(self._ownedAssetIdSet:ObserveContains(assetId):Subscribe(function(value)
199
- sub:Fire(value)
200
- end))
201
- end)
312
+ maid:GiveTask(self:_observeOwnsAssetId(assetId):Subscribe(function(value)
313
+ sub:Fire(value)
314
+ end))
202
315
  end))
203
316
 
204
317
  return topMaid
205
318
  end) :: any
206
319
  elseif type(idOrKey) == "number" then
207
- return Observable.new(function(sub)
208
- local maid = Maid.new()
209
-
210
- maid:GivePromise(self:_promiseQueryAssetId(idOrKey)):Then(function()
211
- -- Only fire once we find ownership status
212
-
213
- maid:GiveTask(self._ownedAssetIdSet:ObserveContains(idOrKey):Subscribe(function(value)
214
- sub:Fire(value)
215
- end))
216
- end)
217
-
218
- return maid
219
- end) :: any
320
+ return self:_observeOwnsAssetId(idOrKey)
220
321
  else
221
322
  error("Bad idOrKey")
222
323
  end
223
324
  end
224
325
 
326
+ --[=[
327
+ Observes ownership of a resolved numeric asset id, layering the local override on top of
328
+ the cloud query. When an override is present it wins and short-circuits the query; when it
329
+ is cleared, ownership falls back to the cloud query and owned-asset set.
330
+
331
+ @param assetId number
332
+ @return Observable<boolean>
333
+ ]=]
334
+ -- Private helpers take `self: any` on purpose: typing `self` as the full
335
+ -- PlayerAssetOwnershipTracker & BaseObject intersection makes the Luau solver renormalize it
336
+ -- at every `self:...` access here, which trips "Code is too complex to typecheck".
337
+ function PlayerAssetOwnershipTracker._observeOwnsAssetId(self: any, assetId: number): Observable.Observable<boolean>
338
+ assert(type(assetId) == "number", "Bad assetId")
339
+
340
+ return self._ownershipOverrides:ObserveAtKey(assetId):Pipe({
341
+ Rx.switchMap(function(override)
342
+ if override ~= nil then
343
+ return Rx.of(override) :: any
344
+ end
345
+
346
+ return self:_observeBaseOwnsAssetId(assetId)
347
+ end) :: any,
348
+ }) :: any
349
+ end
350
+
351
+ --[=[
352
+ Observes ownership of a numeric asset id from the cloud query and owned-asset set only,
353
+ without applying any override. Only fires once ownership status has been resolved.
354
+
355
+ @param assetId number
356
+ @return Observable<boolean>
357
+ ]=]
358
+ function PlayerAssetOwnershipTracker._observeBaseOwnsAssetId(self: any, assetId: number): Observable.Observable<boolean>
359
+ assert(type(assetId) == "number", "Bad assetId")
360
+
361
+ return Observable.new(function(sub)
362
+ local maid = Maid.new()
363
+
364
+ maid:GivePromise(self:_promiseQueryAssetId(assetId)):Then(function()
365
+ -- Only fire once we find ownership status
366
+ maid:GiveTask(self._ownedAssetIdSet:ObserveContains(assetId):Subscribe(function(value)
367
+ sub:Fire(value)
368
+ end))
369
+ end)
370
+
371
+ return maid
372
+ end) :: any
373
+ end
374
+
225
375
  return PlayerAssetOwnershipTracker