@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.
@@ -26,6 +26,7 @@ export type PlayerAssetMarketTracker =
26
26
  typeof(setmetatable(
27
27
  {} :: {
28
28
  _assetType: GameConfigAssetTypes.GameConfigAssetType,
29
+ _promptable: boolean,
29
30
  _convertIds: (string | number) -> number?,
30
31
  _observeIdsBrio: (string | number) -> Observable.Observable<Brio.Brio<number>>,
31
32
  _pendingPurchasePromises: { Promise.Promise<boolean> },
@@ -51,18 +52,22 @@ export type PlayerAssetMarketTracker =
51
52
  @param assetType GameConfigAssetTypes
52
53
  @param convertIds function
53
54
  @param observeIdsBrio function
55
+ @param promptable boolean? -- Defaults to true. When false, [PlayerAssetMarketTracker.PromisePromptPurchase] rejects instead of prompting. Used for asset types like games that cannot be prompted in-experience.
54
56
  @return PlayerAssetMarketTracker
55
57
  ]=]
56
58
  function PlayerAssetMarketTracker.new(
57
59
  assetType: GameConfigAssetTypes.GameConfigAssetType,
58
60
  convertIds,
59
- observeIdsBrio
61
+ observeIdsBrio,
62
+ promptable: boolean?
60
63
  ): PlayerAssetMarketTracker
61
64
  assert(GameConfigAssetTypeUtils.isAssetType(assetType), "Bad assetType")
65
+ assert(type(promptable) == "boolean" or promptable == nil, "Bad promptable")
62
66
 
63
67
  local self: PlayerAssetMarketTracker = setmetatable(BaseObject.new() :: any, PlayerAssetMarketTracker)
64
68
 
65
69
  self._assetType = assert(assetType, "No assetType")
70
+ self._promptable = promptable ~= false
66
71
  self._convertIds = assert(convertIds, "No convertIds")
67
72
  self._observeIdsBrio = assert(observeIdsBrio, "No observeIdsBrio")
68
73
 
@@ -187,6 +192,15 @@ function PlayerAssetMarketTracker.PromisePromptPurchase(
187
192
  ): Promise.Promise<boolean>
188
193
  assert(type(idOrKey) == "number" or type(idOrKey) == "string", "Bad idOrKey")
189
194
 
195
+ if not self._promptable then
196
+ return Promise.rejected(
197
+ string.format(
198
+ "[PlayerAssetMarketTracker.PromisePromptPurchase] - The %s asset type cannot be prompted for purchase in-experience. Use ownership checks instead.",
199
+ tostring(self._assetType)
200
+ )
201
+ )
202
+ end
203
+
190
204
  local id = self._convertIds(idOrKey)
191
205
  if not id then
192
206
  return Promise.rejected(
@@ -294,6 +308,16 @@ function PlayerAssetMarketTracker.GetAssetType(self: PlayerAssetMarketTracker):
294
308
  return self._assetType
295
309
  end
296
310
 
311
+ --[=[
312
+ Returns whether this asset type can be prompted for purchase in-experience. When
313
+ false, [PlayerAssetMarketTracker.PromisePromptPurchase] rejects.
314
+
315
+ @return boolean
316
+ ]=]
317
+ function PlayerAssetMarketTracker.IsPromptable(self: PlayerAssetMarketTracker): boolean
318
+ return self._promptable
319
+ end
320
+
297
321
  --[=[
298
322
  Returns true if item has been purchased this session
299
323
 
@@ -0,0 +1,366 @@
1
+ --!strict
2
+ --[[
3
+ Unit coverage for PlayerAssetMarketTracker. The tracker is constructed directly with a fake
4
+ id-conversion table and a synchronous brio observable, so no ServiceBag, GameConfig, or real
5
+ MarketplaceService is involved. Each test builds a fresh tracker and destroys it so nothing
6
+ leaks into the shared test place.
7
+
8
+ @class PlayerAssetMarketTracker.spec.lua
9
+ ]]
10
+ local require = require(script.Parent.loader).load(script)
11
+
12
+ local Brio = require("Brio")
13
+ local GameConfigAssetTypes = require("GameConfigAssetTypes")
14
+ local Jest = require("Jest")
15
+ local Observable = require("Observable")
16
+ local PlayerAssetMarketTracker = require("PlayerAssetMarketTracker")
17
+ local PromiseTestUtils = require("PromiseTestUtils")
18
+
19
+ local describe = Jest.Globals.describe
20
+ local expect = Jest.Globals.expect
21
+ local it = Jest.Globals.it
22
+
23
+ -- Maps asset keys to numeric ids. Any key not present converts to nil, which exercises the
24
+ -- "no asset with key" reject paths.
25
+ local KEY_TO_ID = {
26
+ swordKey = 111,
27
+ shieldKey = 222,
28
+ }
29
+
30
+ local function convertIds(idOrKey)
31
+ if type(idOrKey) == "number" then
32
+ return idOrKey
33
+ end
34
+ return KEY_TO_ID[idOrKey]
35
+ end
36
+
37
+ -- Emits a single live brio wrapping the resolved id, synchronously on subscribe, then completes
38
+ -- for unknown keys. Synchronous emission lets ObserveAssetPurchased register the known id before
39
+ -- the test fires Purchased.
40
+ local function observeIdsBrio(idOrKey)
41
+ return Observable.new(function(sub)
42
+ local id = convertIds(idOrKey)
43
+ if not id then
44
+ sub:Complete()
45
+ return nil
46
+ end
47
+
48
+ local brio = Brio.new(id)
49
+ sub:Fire(brio)
50
+
51
+ return function()
52
+ brio:Destroy()
53
+ end
54
+ end)
55
+ end
56
+
57
+ local function setup()
58
+ local tracker = PlayerAssetMarketTracker.new(GameConfigAssetTypes.PRODUCT, convertIds, observeIdsBrio)
59
+
60
+ return {
61
+ tracker = tracker,
62
+ destroy = function()
63
+ tracker:Destroy()
64
+ end,
65
+ }
66
+ end
67
+
68
+ -- Builds a non-promptable tracker (as used for the game asset type), which should reject
69
+ -- prompts instead of surfacing them.
70
+ local function setupNonPromptable()
71
+ local tracker = PlayerAssetMarketTracker.new(GameConfigAssetTypes.GAME, convertIds, observeIdsBrio, false)
72
+
73
+ return {
74
+ tracker = tracker,
75
+ destroy = function()
76
+ tracker:Destroy()
77
+ end,
78
+ }
79
+ end
80
+
81
+ -- Drives a full prompt: fires PromisePromptPurchase, waits for ShowPromptRequested to surface the
82
+ -- resolved id, then feeds the purchase result back in. Returns the tracking promise and the id.
83
+ local function promptAndCapture(tracker, idOrKey)
84
+ local capturedId
85
+ local conn = tracker.ShowPromptRequested:Connect(function(id)
86
+ capturedId = id
87
+ end)
88
+
89
+ local promise = tracker:PromisePromptPurchase(idOrKey)
90
+ PromiseTestUtils.awaitValue(function()
91
+ return capturedId ~= nil
92
+ end, 5)
93
+ conn:Disconnect()
94
+
95
+ return promise, capturedId
96
+ end
97
+
98
+ describe("PlayerAssetMarketTracker basics", function()
99
+ it("should report its asset type", function()
100
+ local context = setup()
101
+ expect(context.tracker:GetAssetType()).toEqual(GameConfigAssetTypes.PRODUCT)
102
+ context.destroy()
103
+ end)
104
+
105
+ it("should start with no prompt open", function()
106
+ local context = setup()
107
+ expect(context.tracker:IsPromptOpen()).toEqual(false)
108
+ context.destroy()
109
+ end)
110
+
111
+ it("should hold and return an ownership tracker", function()
112
+ local context = setup()
113
+ expect(context.tracker:GetOwnershipTracker()).toBeNil()
114
+
115
+ local fakeOwnershipTracker = {}
116
+ context.tracker:SetOwnershipTracker(fakeOwnershipTracker :: any)
117
+ expect(context.tracker:GetOwnershipTracker()).toEqual(fakeOwnershipTracker)
118
+
119
+ context.tracker:SetOwnershipTracker(nil)
120
+ expect(context.tracker:GetOwnershipTracker()).toBeNil()
121
+ context.destroy()
122
+ end)
123
+
124
+ it("should be promptable by default", function()
125
+ local context = setup()
126
+ expect(context.tracker:IsPromptable()).toEqual(true)
127
+ context.destroy()
128
+ end)
129
+ end)
130
+
131
+ describe("PlayerAssetMarketTracker non-promptable asset types", function()
132
+ it("should report IsPromptable() as false", function()
133
+ local context = setupNonPromptable()
134
+ expect(context.tracker:IsPromptable()).toEqual(false)
135
+ context.destroy()
136
+ end)
137
+
138
+ it("should reject PromisePromptPurchase for a known id without opening a prompt", function()
139
+ local context = setupNonPromptable()
140
+
141
+ local promptRequested = false
142
+ local conn = context.tracker.ShowPromptRequested:Connect(function()
143
+ promptRequested = true
144
+ end)
145
+
146
+ local outcome = PromiseTestUtils.awaitOutcome(context.tracker:PromisePromptPurchase("swordKey"), 5)
147
+ conn:Disconnect()
148
+
149
+ expect(outcome).toEqual("rejected")
150
+ expect(promptRequested).toEqual(false)
151
+ expect(context.tracker:IsPromptOpen()).toEqual(false)
152
+ context.destroy()
153
+ end)
154
+
155
+ it("should reject PromisePromptPurchase for an unknown key too", function()
156
+ local context = setupNonPromptable()
157
+
158
+ local outcome = PromiseTestUtils.awaitOutcome(context.tracker:PromisePromptPurchase("doesNotExist"), 5)
159
+ expect(outcome).toEqual("rejected")
160
+ context.destroy()
161
+ end)
162
+ end)
163
+
164
+ describe("PlayerAssetMarketTracker:HasPurchasedThisSession()", function()
165
+ it("should return false before anything is purchased", function()
166
+ local context = setup()
167
+ expect(context.tracker:HasPurchasedThisSession("swordKey")).toEqual(false)
168
+ context.destroy()
169
+ end)
170
+
171
+ it("should return true after a purchase event for that id", function()
172
+ local context = setup()
173
+ context.tracker:HandlePurchaseEvent(KEY_TO_ID.swordKey, true)
174
+ expect(context.tracker:HasPurchasedThisSession("swordKey")).toEqual(true)
175
+ context.destroy()
176
+ end)
177
+
178
+ it("should stay false for a different id than the one purchased", function()
179
+ local context = setup()
180
+ context.tracker:HandlePurchaseEvent(KEY_TO_ID.swordKey, true)
181
+ expect(context.tracker:HasPurchasedThisSession("shieldKey")).toEqual(false)
182
+ context.destroy()
183
+ end)
184
+
185
+ it("should return false for an unknown key", function()
186
+ local context = setup()
187
+ expect(context.tracker:HasPurchasedThisSession("doesNotExist")).toEqual(false)
188
+ context.destroy()
189
+ end)
190
+
191
+ it("should not mark a failed purchase as purchased", function()
192
+ local context = setup()
193
+ context.tracker:HandlePurchaseEvent(KEY_TO_ID.swordKey, false)
194
+ expect(context.tracker:HasPurchasedThisSession("swordKey")).toEqual(false)
195
+ context.destroy()
196
+ end)
197
+ end)
198
+
199
+ describe("PlayerAssetMarketTracker:HandlePurchaseEvent()", function()
200
+ it("should fire the Purchased signal with the id on a successful purchase", function()
201
+ local context = setup()
202
+
203
+ local fired = {}
204
+ local conn = context.tracker.Purchased:Connect(function(id)
205
+ table.insert(fired, id)
206
+ end)
207
+
208
+ context.tracker:HandlePurchaseEvent(KEY_TO_ID.swordKey, true)
209
+ conn:Disconnect()
210
+
211
+ expect(#fired).toEqual(1)
212
+ expect(fired[1]).toEqual(KEY_TO_ID.swordKey)
213
+ context.destroy()
214
+ end)
215
+
216
+ it("should not fire Purchased on a failed purchase", function()
217
+ local context = setup()
218
+
219
+ local firedCount = 0
220
+ local conn = context.tracker.Purchased:Connect(function()
221
+ firedCount += 1
222
+ end)
223
+
224
+ context.tracker:HandlePurchaseEvent(KEY_TO_ID.swordKey, false)
225
+ conn:Disconnect()
226
+
227
+ expect(firedCount).toEqual(0)
228
+ context.destroy()
229
+ end)
230
+ end)
231
+
232
+ describe("PlayerAssetMarketTracker:PromisePromptPurchase()", function()
233
+ it("should reject for an unknown key", function()
234
+ local context = setup()
235
+
236
+ local outcome = PromiseTestUtils.awaitOutcome(context.tracker:PromisePromptPurchase("doesNotExist"), 5)
237
+ expect(outcome).toEqual("rejected")
238
+ context.destroy()
239
+ end)
240
+
241
+ it("should request a prompt and resolve true once the purchase succeeds", function()
242
+ local context = setup()
243
+
244
+ local promise, capturedId = promptAndCapture(context.tracker, "swordKey")
245
+ expect(capturedId).toEqual(KEY_TO_ID.swordKey)
246
+ expect(context.tracker:IsPromptOpen()).toEqual(true)
247
+
248
+ context.tracker:HandlePurchaseEvent(capturedId, true)
249
+ context.tracker:HandlePromptClosedEvent(capturedId)
250
+
251
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
252
+ local ok, purchased = promise:Yield()
253
+ expect(ok).toEqual(true)
254
+ expect(purchased).toEqual(true)
255
+ context.destroy()
256
+ end)
257
+
258
+ it("should resolve false when the purchase is declined", function()
259
+ local context = setup()
260
+
261
+ local promise, capturedId = promptAndCapture(context.tracker, "swordKey")
262
+ context.tracker:HandlePurchaseEvent(capturedId, false)
263
+ context.tracker:HandlePromptClosedEvent(capturedId)
264
+
265
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
266
+ local ok, purchased = promise:Yield()
267
+ expect(ok).toEqual(true)
268
+ expect(purchased).toEqual(false)
269
+ context.destroy()
270
+ end)
271
+
272
+ it("should reject a second prompt while one is already open", function()
273
+ local context = setup()
274
+
275
+ local firstPromise, firstId = promptAndCapture(context.tracker, "swordKey")
276
+ expect(context.tracker:IsPromptOpen()).toEqual(true)
277
+
278
+ local secondOutcome = PromiseTestUtils.awaitOutcome(context.tracker:PromisePromptPurchase("shieldKey"), 5)
279
+ expect(secondOutcome).toEqual("rejected")
280
+
281
+ -- Close out the first prompt so nothing leaks.
282
+ context.tracker:HandlePurchaseEvent(firstId, true)
283
+ context.tracker:HandlePromptClosedEvent(firstId)
284
+ PromiseTestUtils.awaitSettled(firstPromise, 5)
285
+ context.destroy()
286
+ end)
287
+ end)
288
+
289
+ describe("PlayerAssetMarketTracker prompt open counting", function()
290
+ it("should report the prompt closed after HandlePromptClosedEvent", function()
291
+ local context = setup()
292
+
293
+ local promise, capturedId = promptAndCapture(context.tracker, "swordKey")
294
+ expect(context.tracker:IsPromptOpen()).toEqual(true)
295
+
296
+ context.tracker:HandlePurchaseEvent(capturedId, true)
297
+ context.tracker:HandlePromptClosedEvent(capturedId)
298
+ PromiseTestUtils.awaitSettled(promise, 5)
299
+
300
+ expect(PromiseTestUtils.awaitValue(function()
301
+ return not context.tracker:IsPromptOpen()
302
+ end, 5)).toEqual(true)
303
+ context.destroy()
304
+ end)
305
+
306
+ it("should surface the open count through ObservePromptOpenCount", function()
307
+ local context = setup()
308
+
309
+ local counts = {}
310
+ local sub = context.tracker:ObservePromptOpenCount():Subscribe(function(value)
311
+ table.insert(counts, value)
312
+ end)
313
+
314
+ local promise, capturedId = promptAndCapture(context.tracker, "swordKey")
315
+ context.tracker:HandlePurchaseEvent(capturedId, true)
316
+ context.tracker:HandlePromptClosedEvent(capturedId)
317
+ PromiseTestUtils.awaitSettled(promise, 5)
318
+ sub:Destroy()
319
+
320
+ -- Should have observed the initial 0 and at least one increment to 1.
321
+ expect(counts[1]).toEqual(0)
322
+ local sawOpen = false
323
+ for _, value in counts do
324
+ if value >= 1 then
325
+ sawOpen = true
326
+ end
327
+ end
328
+ expect(sawOpen).toEqual(true)
329
+ context.destroy()
330
+ end)
331
+ end)
332
+
333
+ describe("PlayerAssetMarketTracker:ObserveAssetPurchased()", function()
334
+ it("should fire when the tracked id is purchased", function()
335
+ local context = setup()
336
+
337
+ local fireCount = 0
338
+ local sub = context.tracker:ObserveAssetPurchased("swordKey"):Subscribe(function()
339
+ fireCount += 1
340
+ end)
341
+
342
+ context.tracker:HandlePurchaseEvent(KEY_TO_ID.swordKey, true)
343
+
344
+ expect(PromiseTestUtils.awaitValue(function()
345
+ return fireCount > 0
346
+ end, 5)).toEqual(true)
347
+ sub:Destroy()
348
+ context.destroy()
349
+ end)
350
+
351
+ it("should not fire when a different id is purchased", function()
352
+ local context = setup()
353
+
354
+ local fireCount = 0
355
+ local sub = context.tracker:ObserveAssetPurchased("swordKey"):Subscribe(function()
356
+ fireCount += 1
357
+ end)
358
+
359
+ context.tracker:HandlePurchaseEvent(KEY_TO_ID.shieldKey, true)
360
+ task.wait()
361
+ sub:Destroy()
362
+
363
+ expect(fireCount).toEqual(0)
364
+ context.destroy()
365
+ end)
366
+ end)
@@ -78,12 +78,17 @@ function PlayerProductManagerBase.new(player: Player, serviceBag: ServiceBag.Ser
78
78
  local subscription = self:_addAssetTracker(GameConfigAssetTypes.SUBSCRIPTION)
79
79
  local membership = self:_addAssetTracker(GameConfigAssetTypes.MEMBERSHIP)
80
80
 
81
+ -- Paid-access games can be owned but cannot be prompted in-experience, so the market
82
+ -- tracker is created non-promptable. There's no purchase signal to listen for either.
83
+ self:_addAssetTracker(GameConfigAssetTypes.GAME, false)
84
+
81
85
  -- Some assets can be owned and thus, are reflected here
82
86
  local passOwnership = self:_addOwnershipTracker(GameConfigAssetTypes.PASS)
83
87
  local assetOwnership = self:_addOwnershipTracker(GameConfigAssetTypes.ASSET)
84
88
  local bundleOwnership = self:_addOwnershipTracker(GameConfigAssetTypes.BUNDLE)
85
89
  local subscriptionOwnership = self:_addOwnershipTracker(GameConfigAssetTypes.SUBSCRIPTION)
86
90
  local membershipOwnership = self:_addOwnershipTracker(GameConfigAssetTypes.MEMBERSHIP)
91
+ local gameOwnership = self:_addOwnershipTracker(GameConfigAssetTypes.GAME)
87
92
 
88
93
  -- Prompt
89
94
  self._maid:GiveTask(asset.ShowPromptRequested:Connect(function(assetId)
@@ -143,6 +148,11 @@ function PlayerProductManagerBase.new(player: Player, serviceBag: ServiceBag.Ser
143
148
  return Promise.resolved(self._player.MembershipType == membershipType)
144
149
  end)
145
150
 
151
+ -- Paid-access game ownership is queried through PlayerOwnsAssetAsync
152
+ gameOwnership:SetQueryOwnershipCallback(function(assetId)
153
+ return MarketplaceUtils.promisePlayerOwnsAssetAsync(self._player, assetId)
154
+ end)
155
+
146
156
  return self
147
157
  end
148
158
 
@@ -292,7 +302,8 @@ end
292
302
 
293
303
  function PlayerProductManagerBase._addAssetTracker(
294
304
  self: PlayerProductManagerBase,
295
- assetType: GameConfigAssetTypes.GameConfigAssetType
305
+ assetType: GameConfigAssetTypes.GameConfigAssetType,
306
+ promptable: boolean?
296
307
  ): PlayerAssetMarketTracker.PlayerAssetMarketTracker
297
308
  assert(GameConfigAssetTypeUtils.isAssetType(assetType), "Bad assetType")
298
309
  assert(not self._assetMarketTrackers[assetType], "Already have tracker")
@@ -305,7 +316,7 @@ function PlayerProductManagerBase._addAssetTracker(
305
316
  assert(type(idOrKey) == "number" or type(idOrKey) == "string", "Bad idOrKey")
306
317
 
307
318
  return self._configPicker:ObserveToAssetIdBrio(assetType, idOrKey)
308
- end))
319
+ end, promptable))
309
320
 
310
321
  self._assetMarketTrackers[assetType] = assetMarketTracker
311
322