@quenty/gameproductservice 14.57.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [14.58.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/gameproductservice@14.57.0...@quenty/gameproductservice@14.58.0) (2026-07-21)
7
+
8
+ ### Bug Fixes
9
+
10
+ - Resolve luau-lsp and moonwave lint failures ([286e352](https://github.com/Quenty/NevermoreEngine/commit/286e3527b52a86639fc0485359141d7805b3fefa))
11
+
12
+ ### Features
13
+
14
+ - Add kinda risky ownership override method + cmdr commands ([b0aaa68](https://github.com/Quenty/NevermoreEngine/commit/b0aaa68ebfab7f34da05b57bca77b35bf7890c3b))
15
+ - Game ownership works ([c09bdc3](https://github.com/Quenty/NevermoreEngine/commit/c09bdc32174fc17cfac7aeb6c7849b556be9cec4))
16
+
6
17
  # [14.57.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/gameproductservice@14.56.0...@quenty/gameproductservice@14.57.0) (2026-07-20)
7
18
 
8
19
  ### Bug Fixes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/gameproductservice",
3
- "version": "14.57.0",
3
+ "version": "14.58.0",
4
4
  "description": "Generalized monetization system for handling products and purchases correctly.",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -61,5 +61,5 @@
61
61
  "@quenty/valueobject": "13.33.0",
62
62
  "@quentystudios/jest-lua": "3.10.0-quenty.2"
63
63
  },
64
- "gitHead": "2c903e91ae2ea1702ae1ce94af20af83af81e4fe"
64
+ "gitHead": "b7e59984e586064ea3cec6176e79b3f7451ecdc5"
65
65
  }
@@ -0,0 +1,50 @@
1
+ --!strict
2
+ --[=[
3
+ Registers the game product cmdr types on the client so cmdr can autocomplete the
4
+ `set-ownership` command's arguments. The commands themselves are registered on the server by
5
+ [GameProductCmdrService]; only the shared types need to exist on the client for autocomplete.
6
+
7
+ @client
8
+ @class GameProductCmdrServiceClient
9
+ ]=]
10
+
11
+ local require = require(script.Parent.loader).load(script)
12
+
13
+ local GameProductCmdrTypeUtils = require("GameProductCmdrTypeUtils")
14
+ local Maid = require("Maid")
15
+ local ServiceBag = require("ServiceBag")
16
+
17
+ local GameProductCmdrServiceClient = {}
18
+ GameProductCmdrServiceClient.ServiceName = "GameProductCmdrServiceClient"
19
+
20
+ export type GameProductCmdrServiceClient = typeof(setmetatable(
21
+ {} :: {
22
+ _serviceBag: ServiceBag.ServiceBag,
23
+ _maid: Maid.Maid,
24
+ _cmdrService: any,
25
+ _gameConfigServiceClient: any,
26
+ },
27
+ {} :: typeof({ __index = GameProductCmdrServiceClient })
28
+ ))
29
+
30
+ function GameProductCmdrServiceClient.Init(self: GameProductCmdrServiceClient, serviceBag: ServiceBag.ServiceBag): ()
31
+ assert(not (self :: any)._serviceBag, "Already initialized")
32
+ self._serviceBag = assert(serviceBag, "No serviceBag")
33
+ self._maid = Maid.new()
34
+
35
+ self._cmdrService = self._serviceBag:GetService(require("CmdrServiceClient"))
36
+ self._gameConfigServiceClient = self._serviceBag:GetService(require("GameConfigServiceClient"))
37
+ end
38
+
39
+ function GameProductCmdrServiceClient.Start(self: GameProductCmdrServiceClient): ()
40
+ local configPicker = self._gameConfigServiceClient:GetConfigPicker()
41
+ self._maid:GivePromise(self._cmdrService:PromiseCmdr()):Then(function(cmdr)
42
+ GameProductCmdrTypeUtils.registerTypes(cmdr, configPicker)
43
+ end)
44
+ end
45
+
46
+ function GameProductCmdrServiceClient.Destroy(self: GameProductCmdrServiceClient): ()
47
+ self._maid:DoCleaning()
48
+ end
49
+
50
+ return GameProductCmdrServiceClient
@@ -60,6 +60,7 @@ function GameProductServiceClient.Init(self: GameProductServiceClient, serviceBa
60
60
  -- Internal
61
61
  self._gameProductDataService = self._serviceBag:GetService(require("GameProductDataService"))
62
62
  self._serviceBag:GetService(require("PlayerProductManagerClient"))
63
+ self._serviceBag:GetService((require :: any)("GameProductCmdrServiceClient"))
63
64
 
64
65
  -- Additional API for ergonomics
65
66
  self.GamePassPurchased = self._maid:Add(Signal.new() :: any) -- :Fire(gamePassId)
@@ -200,6 +201,12 @@ function GameProductServiceClient.PromisePlayerOwnership(
200
201
  return self._gameProductDataService:PromisePlayerOwnership(player, assetType, idOrKey)
201
202
  end
202
203
 
204
+ --[=[
205
+ Ownership overrides are intentionally not settable from the client. They are server-authoritative
206
+ and replicated read-only (see [GameProductService.SetPlayerOwnershipOverride]), so a player can
207
+ never grant themselves ownership. Read ownership with [GameProductServiceClient.ObservePlayerOwnership].
208
+ ]=]
209
+
203
210
  --[=[
204
211
  Observes if the player owns this cloud asset or not
205
212
 
@@ -5,18 +5,32 @@
5
5
 
6
6
  local require = require(script.Parent.loader).load(script)
7
7
 
8
+ local GameConfigAssetTypeUtils = require("GameConfigAssetTypeUtils")
8
9
  local GameConfigAssetTypes = require("GameConfigAssetTypes")
10
+ local GameProductCmdrTypeUtils = require("GameProductCmdrTypeUtils")
9
11
  local PlayerUtils = require("PlayerUtils")
10
12
  local ServiceBag = require("ServiceBag")
11
13
 
12
14
  local GameProductCmdrService = {}
13
15
  GameProductCmdrService.ServiceName = "GameProductCmdrService"
14
16
 
17
+ -- Asset types that have an ownership tracker (see PlayerProductManagerBase). Only these can
18
+ -- have their ownership overridden.
19
+ local OWNABLE_ASSET_TYPES: { [string]: boolean } = {
20
+ [GameConfigAssetTypes.PASS] = true,
21
+ [GameConfigAssetTypes.ASSET] = true,
22
+ [GameConfigAssetTypes.BUNDLE] = true,
23
+ [GameConfigAssetTypes.SUBSCRIPTION] = true,
24
+ [GameConfigAssetTypes.MEMBERSHIP] = true,
25
+ [GameConfigAssetTypes.GAME] = true,
26
+ }
27
+
15
28
  export type GameProductCmdrService = typeof(setmetatable(
16
29
  {} :: {
17
30
  _serviceBag: ServiceBag.ServiceBag,
18
31
  _cmdrService: any,
19
32
  _gameProductService: any,
33
+ _gameConfigDataService: any,
20
34
  },
21
35
  {} :: typeof({ __index = GameProductCmdrService })
22
36
  ))
@@ -27,9 +41,17 @@ function GameProductCmdrService.Init(self: GameProductCmdrService, serviceBag: S
27
41
 
28
42
  self._cmdrService = self._serviceBag:GetService(require("CmdrService"))
29
43
  self._gameProductService = self._serviceBag:GetService(require("GameProductService"))
44
+ self._gameConfigDataService = self._serviceBag:GetService(require("GameConfigDataService"))
30
45
  end
31
46
 
32
47
  function GameProductCmdrService.Start(self: GameProductCmdrService): ()
48
+ -- Register the shared cmdr types before the commands that reference them. The client
49
+ -- registers the same types via GameProductCmdrServiceClient so cmdr can autocomplete.
50
+ local configPicker = self._gameConfigDataService:GetConfigPicker()
51
+ self._cmdrService:PromiseCmdr():Then(function(cmdr)
52
+ GameProductCmdrTypeUtils.registerTypes(cmdr, configPicker)
53
+ end)
54
+
33
55
  self:_registerCommands()
34
56
  end
35
57
 
@@ -190,6 +212,88 @@ function GameProductCmdrService._registerCommands(self: GameProductCmdrService):
190
212
 
191
213
  return string.format("Prompted: %s", table.concat(givenTo, ", "))
192
214
  end)
215
+
216
+ self._cmdrService:RegisterCommand({
217
+ Name = "set-ownership",
218
+ Description = "Overrides a player's local ownership of an asset. Useful for testing ownership-gated "
219
+ .. "behavior, including paid-access games which cannot be prompted in-experience.",
220
+ Group = "GameConfig",
221
+ Args = {
222
+ {
223
+ Name = "Player",
224
+ Type = "players",
225
+ Description = "The player(s) to override ownership for.",
226
+ },
227
+ {
228
+ Name = "AssetType",
229
+ Type = GameProductCmdrTypeUtils.OwnableAssetTypeName,
230
+ Description = "Ownable asset type: game, pass, asset, bundle, subscription, or membership.",
231
+ },
232
+ {
233
+ Name = "AssetIdOrKey",
234
+ Type = GameProductCmdrTypeUtils.OwnableAssetIdOrKeyName,
235
+ Description = "The asset id (number) or GameConfig asset key.",
236
+ },
237
+ {
238
+ Name = "State",
239
+ Type = GameProductCmdrTypeUtils.OwnershipStateName,
240
+ Description = "own (force owned), disown (force not owned), or clear (remove the override).",
241
+ },
242
+ },
243
+ }, function(_context, players, assetType, assetIdOrKey, state)
244
+ assetType = string.lower(assetType)
245
+ if not GameConfigAssetTypeUtils.isAssetType(assetType) then
246
+ return string.format("Unknown asset type %q", tostring(assetType))
247
+ end
248
+
249
+ if not OWNABLE_ASSET_TYPES[assetType] then
250
+ return string.format("Asset type %q is not ownable", tostring(assetType))
251
+ end
252
+
253
+ state = string.lower(state)
254
+ local ownsAsset: boolean?
255
+ if state == "own" then
256
+ ownsAsset = true
257
+ elseif state == "disown" then
258
+ ownsAsset = false
259
+ elseif state == "clear" then
260
+ ownsAsset = nil
261
+ else
262
+ return string.format("Unknown state %q (expected own, disown, or clear)", tostring(state))
263
+ end
264
+
265
+ -- Numeric ids arrive as strings from cmdr; coerce so raw asset ids resolve directly,
266
+ -- while GameConfig keys pass through as strings.
267
+ local idOrKey = tonumber(assetIdOrKey) or assetIdOrKey
268
+
269
+ local ownableAssetType = assetType :: GameConfigAssetTypes.GameConfigAssetType
270
+
271
+ local appliedTo = {}
272
+ for _, player in players do
273
+ self._gameProductService
274
+ :SetPlayerOwnershipOverride(player, ownableAssetType, idOrKey, ownsAsset)
275
+ :Catch(function(err)
276
+ warn(
277
+ string.format(
278
+ "[set-ownership] - Failed for %s: %s",
279
+ PlayerUtils.formatName(player),
280
+ tostring(err)
281
+ )
282
+ )
283
+ end)
284
+
285
+ table.insert(appliedTo, PlayerUtils.formatName(player))
286
+ end
287
+
288
+ local verb = if ownsAsset == nil then "Cleared" elseif ownsAsset then "Granted" else "Revoked"
289
+ return string.format(
290
+ "%s %s ownership of %s for %s",
291
+ verb,
292
+ assetType,
293
+ tostring(idOrKey),
294
+ table.concat(appliedTo, ", ")
295
+ )
296
+ end)
193
297
  end
194
298
 
195
299
  return GameProductCmdrService
@@ -204,6 +204,60 @@ function GameProductService.PromisePlayerOwnership(
204
204
  return self._gameProductDataService:PromisePlayerOwnership(player, assetType, idOrKey)
205
205
  end
206
206
 
207
+ --[=[
208
+ Sets a server-authoritative override for the player's ownership of the asset. When set, the
209
+ override wins over the cloud query and any session purchase, forcing ownership on (`true`) or off
210
+ (`false`). Passing `nil` clears the override.
211
+
212
+ The override is replicated read-only to the client and applied to every realm's ownership
213
+ tracker, so it drives client-side ownership-gated UI. Useful for testing ownership-gated behavior
214
+ (including paid-access games, which cannot be prompted in-experience) or granting access from a
215
+ non-Roblox condition. Only the server may set it; the client has no setter, so a player can never
216
+ grant themselves ownership.
217
+
218
+ @param player Player
219
+ @param assetType GameConfigAssetType
220
+ @param idOrKey string | number
221
+ @param ownsAsset boolean?
222
+ @return Promise
223
+ ]=]
224
+ function GameProductService.SetPlayerOwnershipOverride(
225
+ self: GameProductService,
226
+ player: Player,
227
+ assetType: GameConfigAssetTypes.GameConfigAssetType,
228
+ idOrKey: string | number,
229
+ ownsAsset: boolean?
230
+ ): Promise.Promise<()>
231
+ assert(typeof(player) == "Instance" and player:IsA("Player"), "Bad player")
232
+ assert(GameConfigAssetTypeUtils.isAssetType(assetType), "Bad assetType")
233
+ assert(type(idOrKey) == "number" or type(idOrKey) == "string", "Bad idOrKey")
234
+ assert(type(ownsAsset) == "boolean" or ownsAsset == nil, "Bad ownsAsset")
235
+
236
+ return self._gameProductDataService:SetPlayerOwnershipOverride(player, assetType, idOrKey, ownsAsset)
237
+ end
238
+
239
+ --[=[
240
+ Clears any local ownership override for the asset, so ownership falls back to the cloud
241
+ query. Equivalent to `SetPlayerOwnershipOverride(player, assetType, idOrKey, nil)`.
242
+
243
+ @param player Player
244
+ @param assetType GameConfigAssetType
245
+ @param idOrKey string | number
246
+ @return Promise
247
+ ]=]
248
+ function GameProductService.ClearPlayerOwnershipOverride(
249
+ self: GameProductService,
250
+ player: Player,
251
+ assetType: GameConfigAssetTypes.GameConfigAssetType,
252
+ idOrKey: string | number
253
+ ): Promise.Promise<()>
254
+ assert(typeof(player) == "Instance" and player:IsA("Player"), "Bad player")
255
+ assert(GameConfigAssetTypeUtils.isAssetType(assetType), "Bad assetType")
256
+ assert(type(idOrKey) == "number" or type(idOrKey) == "string", "Bad idOrKey")
257
+
258
+ return self._gameProductDataService:ClearPlayerOwnershipOverride(player, assetType, idOrKey)
259
+ end
260
+
207
261
  --[=[
208
262
  Checks if the asset is ownable and if it is, checks player ownership. Otherwise, it checks if the asset
209
263
  has been purchased this session. If the asset has not been purchased this session it prompts the user to
@@ -0,0 +1,131 @@
1
+ --!strict
2
+ --[=[
3
+ Registers the shared cmdr enum types used by the game product commands. These must be
4
+ registered on both the server (so commands validate and parse) and the client (so cmdr can
5
+ autocomplete them), mirroring how [GameConfigCmdrUtils] registers the asset id types.
6
+
7
+ @class GameProductCmdrTypeUtils
8
+ ]=]
9
+
10
+ local require = require(script.Parent.loader).load(script)
11
+
12
+ local GameConfigAssetTypes = require("GameConfigAssetTypes")
13
+
14
+ local GameProductCmdrTypeUtils = {}
15
+
16
+ --[=[
17
+ The name of the ownable-asset-type enum cmdr type.
18
+ @prop OwnableAssetTypeName "ownableAssetType"
19
+ @within GameProductCmdrTypeUtils
20
+ ]=]
21
+ GameProductCmdrTypeUtils.OwnableAssetTypeName = "ownableAssetType"
22
+
23
+ --[=[
24
+ The name of the ownership-state enum cmdr type.
25
+ @prop OwnershipStateName "ownershipState"
26
+ @within GameProductCmdrTypeUtils
27
+ ]=]
28
+ GameProductCmdrTypeUtils.OwnershipStateName = "ownershipState"
29
+
30
+ --[=[
31
+ The name of the ownable-asset id/key cmdr type. Autocompletes asset keys across all ownable
32
+ asset types (a union -- it is not filtered by the command's chosen AssetType, since cmdr
33
+ types cannot read sibling arguments) and also accepts raw numeric ids.
34
+ @prop OwnableAssetIdOrKeyName "ownableAssetIdOrKey"
35
+ @within GameProductCmdrTypeUtils
36
+ ]=]
37
+ GameProductCmdrTypeUtils.OwnableAssetIdOrKeyName = "ownableAssetIdOrKey"
38
+
39
+ -- Asset types that have an ownership tracker (see PlayerProductManagerBase), and so can have
40
+ -- their ownership overridden.
41
+ local OWNABLE_ASSET_TYPES: { GameConfigAssetTypes.GameConfigAssetType } = {
42
+ GameConfigAssetTypes.GAME,
43
+ GameConfigAssetTypes.PASS,
44
+ GameConfigAssetTypes.ASSET,
45
+ GameConfigAssetTypes.BUNDLE,
46
+ GameConfigAssetTypes.SUBSCRIPTION,
47
+ GameConfigAssetTypes.MEMBERSHIP,
48
+ }
49
+
50
+ --[=[
51
+ Ownership override states accepted by the `set-ownership` command.
52
+ @prop OwnershipStates { "own", "disown", "clear" }
53
+ @within GameProductCmdrTypeUtils
54
+ ]=]
55
+ GameProductCmdrTypeUtils.OwnershipStates = { "own", "disown", "clear" }
56
+
57
+ -- Builds the ownable-asset id/key type. Autocompletes keys across every ownable asset type
58
+ -- (a union) and accepts raw numbers. Parse returns the raw key string or number; the command
59
+ -- resolves it against its chosen AssetType via the config picker.
60
+ local function makeOwnableAssetIdOrKeyType(cmdr: any, configPicker: any)
61
+ --stylua: ignore
62
+ return {
63
+ Transform = function(text)
64
+ local keys = {}
65
+ local seen = {}
66
+
67
+ for _, assetType in OWNABLE_ASSET_TYPES do
68
+ for _, assetConfig in configPicker:GetAllActiveAssetsOfType(assetType) do
69
+ local key = assetConfig:GetAssetKey()
70
+ if not seen[key] then
71
+ seen[key] = true
72
+ table.insert(keys, key)
73
+ end
74
+ end
75
+ end
76
+
77
+ local find = cmdr.Util.MakeFuzzyFinder(keys)
78
+ local found = find(text)
79
+
80
+ -- Allow raw numeric ids too
81
+ if tonumber(text) then
82
+ table.insert(found, tonumber(text))
83
+ end
84
+
85
+ return found
86
+ end;
87
+ Validate = function(found)
88
+ return #found > 0, "No ownable asset with that key or id could be found."
89
+ end;
90
+ Autocomplete = function(found)
91
+ local strings = {}
92
+ for _, item in found do
93
+ table.insert(strings, tostring(item))
94
+ end
95
+ return strings
96
+ end;
97
+ Parse = function(found)
98
+ return found[1]
99
+ end;
100
+ }
101
+ end
102
+
103
+ --[=[
104
+ Registers the game product cmdr types. Safe to call once per cmdr registry (server and each
105
+ client). Registering a duplicate type name throws, so this should only be called from a
106
+ single service on each realm.
107
+
108
+ @param cmdr Cmdr
109
+ @param configPicker GameConfigPicker
110
+ ]=]
111
+ function GameProductCmdrTypeUtils.registerTypes(cmdr: any, configPicker: any): ()
112
+ assert(cmdr, "Bad cmdr")
113
+ assert(configPicker, "Bad configPicker")
114
+
115
+ cmdr.Registry:RegisterType(
116
+ GameProductCmdrTypeUtils.OwnableAssetTypeName,
117
+ cmdr.Util.MakeEnumType(GameProductCmdrTypeUtils.OwnableAssetTypeName, OWNABLE_ASSET_TYPES)
118
+ )
119
+
120
+ cmdr.Registry:RegisterType(
121
+ GameProductCmdrTypeUtils.OwnershipStateName,
122
+ cmdr.Util.MakeEnumType(GameProductCmdrTypeUtils.OwnershipStateName, GameProductCmdrTypeUtils.OwnershipStates)
123
+ )
124
+
125
+ cmdr.Registry:RegisterType(
126
+ GameProductCmdrTypeUtils.OwnableAssetIdOrKeyName,
127
+ makeOwnableAssetIdOrKeyType(cmdr, configPicker)
128
+ )
129
+ end
130
+
131
+ return GameProductCmdrTypeUtils
@@ -277,6 +277,67 @@ function GameProductDataService.PromisePlayerOwnership(
277
277
  end)
278
278
  end
279
279
 
280
+ --[=[
281
+ Sets a server-authoritative override for the player's ownership of the asset. When set, the
282
+ override wins over the cloud query and any session purchase, forcing ownership on (`true`) or off
283
+ (`false`). Passing `nil` clears the override.
284
+
285
+ The override is stored in a replicated attribute and applied to every realm's ownership tracker,
286
+ so it drives client-side ownership-gated UI. It can only be set from the server: this rejects on
287
+ the client realm, and [GameProductServiceClient] deliberately exposes no setter, so a player can
288
+ never grant themselves ownership.
289
+
290
+ @server
291
+ @param player Player
292
+ @param assetType GameConfigAssetType
293
+ @param idOrKey string | number
294
+ @param ownsAsset boolean?
295
+ @return Promise
296
+ ]=]
297
+ function GameProductDataService.SetPlayerOwnershipOverride(
298
+ self: GameProductDataService,
299
+ player: Player,
300
+ assetType: GameConfigAssetTypes.GameConfigAssetType,
301
+ idOrKey: string | number,
302
+ ownsAsset: boolean?
303
+ ): Promise.Promise<()>
304
+ -- Server authority: ownership overrides are never assignable from a client (otherwise a player
305
+ -- could grant themselves ownership of paid assets).
306
+ assert(
307
+ self._tieRealmService:GetTieRealm() ~= TieRealms.CLIENT,
308
+ "[GameProductDataService] - Ownership overrides can only be set from the server"
309
+ )
310
+ assert(typeof(player) == "Instance" and player:IsA("Player"), "Bad player")
311
+ assert(GameConfigAssetTypeUtils.isAssetType(assetType), "Bad assetType")
312
+ assert(type(idOrKey) == "number" or type(idOrKey) == "string", "Bad idOrKey")
313
+ assert(type(ownsAsset) == "boolean" or ownsAsset == nil, "Bad ownsAsset")
314
+
315
+ return self:_promisePlayerProductManager(player):Then(function(playerProductManager)
316
+ local ownershipTracker = playerProductManager:GetOwnershipTrackerOrError(assetType)
317
+ ownershipTracker:SetOwnershipOverride(idOrKey, ownsAsset)
318
+ end)
319
+ end
320
+
321
+ --[=[
322
+ Clears any ownership override for the asset, so ownership falls back to the cloud query.
323
+ Equivalent to `SetPlayerOwnershipOverride(player, assetType, idOrKey, nil)`, and likewise
324
+ server-only.
325
+
326
+ @server
327
+ @param player Player
328
+ @param assetType GameConfigAssetType
329
+ @param idOrKey string | number
330
+ @return Promise
331
+ ]=]
332
+ function GameProductDataService.ClearPlayerOwnershipOverride(
333
+ self: GameProductDataService,
334
+ player: Player,
335
+ assetType: GameConfigAssetTypes.GameConfigAssetType,
336
+ idOrKey: string | number
337
+ ): Promise.Promise<()>
338
+ return self:SetPlayerOwnershipOverride(player, assetType, idOrKey, nil)
339
+ end
340
+
280
341
  --[=[
281
342
  Returns true if item has been purchased this session
282
343
 
@@ -11,6 +11,7 @@
11
11
  ]]
12
12
  local require = require(script.Parent.loader).load(script)
13
13
 
14
+ local GameConfigAssetTypes = require("GameConfigAssetTypes")
14
15
  local GameProductDataService = require("GameProductDataService")
15
16
  local Jest = require("Jest")
16
17
  local PromiseTestUtils = require("PromiseTestUtils")
@@ -122,6 +123,36 @@ describe("GameProductDataService server-only prompting", function()
122
123
  end)
123
124
  end)
124
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
+
125
156
  describe("GameProductDataService prompt guard", function()
126
157
  it("should resolve immediately on the server realm", function()
127
158
  local context = setup(TieRealms.SERVER)
@@ -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
@@ -17,6 +17,7 @@ local GameConfigPicker = require("GameConfigPicker")
17
17
  local Jest = require("Jest")
18
18
  local Observable = require("Observable")
19
19
  local PlayerAssetOwnershipTracker = require("PlayerAssetOwnershipTracker")
20
+ local PlayerProductOwnershipOverrideUtils = require("PlayerProductOwnershipOverrideUtils")
20
21
  local Promise = require("Promise")
21
22
  local PromiseTestUtils = require("PromiseTestUtils")
22
23
  local Signal = require("Signal")
@@ -240,3 +241,336 @@ describe("PlayerAssetOwnershipTracker:ObserveOwnsAsset()", function()
240
241
  context.destroy()
241
242
  end)
242
243
  end)
244
+
245
+ describe("PlayerAssetOwnershipTracker:SetOwnershipOverride() ownership wins", function()
246
+ it("should resolve true for a true override even when the callback reports no ownership", function()
247
+ local context = setup()
248
+ context.tracker:SetQueryOwnershipCallback(function()
249
+ return Promise.resolved(false)
250
+ end)
251
+
252
+ context.tracker:SetOwnershipOverride("swordKey", true)
253
+
254
+ local promise = context.tracker:PromiseOwnsAsset("swordKey")
255
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
256
+ local _, owns = promise:Yield()
257
+ expect(owns).toEqual(true)
258
+ context.destroy()
259
+ end)
260
+
261
+ it("should resolve false for a false override even when the callback reports ownership", function()
262
+ local context = setup()
263
+ context.tracker:SetQueryOwnershipCallback(function()
264
+ return Promise.resolved(true)
265
+ end)
266
+
267
+ context.tracker:SetOwnershipOverride("swordKey", false)
268
+
269
+ local promise = context.tracker:PromiseOwnsAsset("swordKey")
270
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
271
+ local _, owns = promise:Yield()
272
+ expect(owns).toEqual(false)
273
+ context.destroy()
274
+ end)
275
+
276
+ it("should resolve true for a true override even with no callback set", function()
277
+ local context = setup()
278
+
279
+ context.tracker:SetOwnershipOverride("swordKey", true)
280
+
281
+ local promise = context.tracker:PromiseOwnsAsset("swordKey")
282
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
283
+ local _, owns = promise:Yield()
284
+ expect(owns).toEqual(true)
285
+ context.destroy()
286
+ end)
287
+
288
+ it("should let a false override revoke an asset already in the owned set", function()
289
+ local context = setup()
290
+ context.tracker:SetOwnership(KEY_TO_ID.swordKey, true)
291
+
292
+ context.tracker:SetOwnershipOverride("swordKey", false)
293
+
294
+ local promise = context.tracker:PromiseOwnsAsset("swordKey")
295
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
296
+ local _, owns = promise:Yield()
297
+ expect(owns).toEqual(false)
298
+ context.destroy()
299
+ end)
300
+
301
+ it("should let a false override revoke an asset after a purchase fires", function()
302
+ local context = setup()
303
+ context.marketTracker.Purchased:Fire(KEY_TO_ID.swordKey)
304
+
305
+ context.tracker:SetOwnershipOverride(KEY_TO_ID.swordKey, false)
306
+
307
+ local promise = context.tracker:PromiseOwnsAsset("swordKey")
308
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
309
+ local _, owns = promise:Yield()
310
+ expect(owns).toEqual(false)
311
+ context.destroy()
312
+ end)
313
+
314
+ it("should not invoke the cloud callback while an override is set", function()
315
+ local context = setup()
316
+
317
+ local callCount = 0
318
+ context.tracker:SetQueryOwnershipCallback(function()
319
+ callCount += 1
320
+ return Promise.resolved(true)
321
+ end)
322
+
323
+ context.tracker:SetOwnershipOverride("swordKey", true)
324
+ local promise = context.tracker:PromiseOwnsAsset("swordKey")
325
+ PromiseTestUtils.awaitSettled(promise, 5)
326
+
327
+ expect(callCount).toEqual(0)
328
+ context.destroy()
329
+ end)
330
+ end)
331
+
332
+ describe("PlayerAssetOwnershipTracker:SetOwnershipOverride() lifecycle", function()
333
+ it("should fall back to the cloud query once the override is cleared", function()
334
+ local context = setup()
335
+ context.tracker:SetQueryOwnershipCallback(function()
336
+ return Promise.resolved(false)
337
+ end)
338
+
339
+ context.tracker:SetOwnershipOverride("swordKey", true)
340
+ local owned = context.tracker:PromiseOwnsAsset("swordKey")
341
+ expect(PromiseTestUtils.awaitSettled(owned, 5)).toEqual(true)
342
+ local _, ownsWhileOverridden = owned:Yield()
343
+ expect(ownsWhileOverridden).toEqual(true)
344
+
345
+ context.tracker:ClearOwnershipOverride("swordKey")
346
+ local afterClear = context.tracker:PromiseOwnsAsset("swordKey")
347
+ expect(PromiseTestUtils.awaitSettled(afterClear, 5)).toEqual(true)
348
+ local _, ownsAfterClear = afterClear:Yield()
349
+ expect(ownsAfterClear).toEqual(false)
350
+ context.destroy()
351
+ end)
352
+
353
+ it("should treat SetOwnershipOverride(nil) as clearing the override", function()
354
+ local context = setup()
355
+ context.tracker:SetQueryOwnershipCallback(function()
356
+ return Promise.resolved(false)
357
+ end)
358
+
359
+ context.tracker:SetOwnershipOverride("swordKey", true)
360
+ context.tracker:SetOwnershipOverride("swordKey", nil)
361
+
362
+ local promise = context.tracker:PromiseOwnsAsset("swordKey")
363
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
364
+ local _, owns = promise:Yield()
365
+ expect(owns).toEqual(false)
366
+ context.destroy()
367
+ end)
368
+
369
+ it("should flip when the override is re-set", function()
370
+ local context = setup()
371
+
372
+ context.tracker:SetOwnershipOverride("swordKey", true)
373
+ local first = context.tracker:PromiseOwnsAsset("swordKey")
374
+ PromiseTestUtils.awaitSettled(first, 5)
375
+ local _, firstOwns = first:Yield()
376
+ expect(firstOwns).toEqual(true)
377
+
378
+ context.tracker:SetOwnershipOverride("swordKey", false)
379
+ local second = context.tracker:PromiseOwnsAsset("swordKey")
380
+ PromiseTestUtils.awaitSettled(second, 5)
381
+ local _, secondOwns = second:Yield()
382
+ expect(secondOwns).toEqual(false)
383
+ context.destroy()
384
+ end)
385
+ end)
386
+
387
+ describe("PlayerAssetOwnershipTracker:SetOwnershipOverride() combined keys", function()
388
+ it("should honor an override set by key when queried by id", function()
389
+ local context = setup()
390
+
391
+ context.tracker:SetOwnershipOverride("swordKey", true)
392
+
393
+ local promise = context.tracker:PromiseOwnsAsset(KEY_TO_ID.swordKey)
394
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
395
+ local _, owns = promise:Yield()
396
+ expect(owns).toEqual(true)
397
+ context.destroy()
398
+ end)
399
+
400
+ it("should honor an override set by id when queried by key", function()
401
+ local context = setup()
402
+ context.tracker:SetQueryOwnershipCallback(function()
403
+ return Promise.resolved(true)
404
+ end)
405
+
406
+ context.tracker:SetOwnershipOverride(KEY_TO_ID.swordKey, false)
407
+
408
+ local promise = context.tracker:PromiseOwnsAsset("swordKey")
409
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
410
+ local _, owns = promise:Yield()
411
+ expect(owns).toEqual(false)
412
+ context.destroy()
413
+ end)
414
+
415
+ it("should not affect a different asset", function()
416
+ local context = setup()
417
+ context.tracker:SetQueryOwnershipCallback(function()
418
+ return Promise.resolved(false)
419
+ end)
420
+
421
+ context.tracker:SetOwnershipOverride("swordKey", true)
422
+
423
+ local promise = context.tracker:PromiseOwnsAsset("shieldKey")
424
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
425
+ local _, owns = promise:Yield()
426
+ expect(owns).toEqual(false)
427
+ context.destroy()
428
+ end)
429
+
430
+ it("should ignore an override for an unknown key without erroring", function()
431
+ local context = setup()
432
+ -- Should warn and no-op rather than throw.
433
+ context.tracker:SetOwnershipOverride("doesNotExist", true)
434
+
435
+ local outcome = PromiseTestUtils.awaitOutcome(context.tracker:PromiseOwnsAsset("doesNotExist"), 5)
436
+ expect(outcome).toEqual("rejected")
437
+ context.destroy()
438
+ end)
439
+ end)
440
+
441
+ describe("PlayerAssetOwnershipTracker:ObserveOwnsAsset() overrides", function()
442
+ it("should emit the override value and revert to the cloud query when cleared", function()
443
+ local context = setup()
444
+ context.tracker:SetQueryOwnershipCallback(function()
445
+ return Promise.resolved(false)
446
+ end)
447
+
448
+ local values = {}
449
+ local sub = context.tracker:ObserveOwnsAsset(KEY_TO_ID.swordKey):Subscribe(function(value)
450
+ table.insert(values, value)
451
+ end)
452
+
453
+ expect(PromiseTestUtils.awaitValue(function()
454
+ return values[#values] == false
455
+ end, 5)).toEqual(true)
456
+
457
+ context.tracker:SetOwnershipOverride(KEY_TO_ID.swordKey, true)
458
+ expect(PromiseTestUtils.awaitValue(function()
459
+ return values[#values] == true
460
+ end, 5)).toEqual(true)
461
+
462
+ context.tracker:ClearOwnershipOverride(KEY_TO_ID.swordKey)
463
+ expect(PromiseTestUtils.awaitValue(function()
464
+ return values[#values] == false
465
+ end, 5)).toEqual(true)
466
+
467
+ sub:Destroy()
468
+ context.destroy()
469
+ end)
470
+
471
+ it("should emit false for a false override even when the cloud query reports ownership", function()
472
+ local context = setup()
473
+ context.tracker:SetQueryOwnershipCallback(function()
474
+ return Promise.resolved(true)
475
+ end)
476
+
477
+ context.tracker:SetOwnershipOverride(KEY_TO_ID.swordKey, false)
478
+
479
+ local values = {}
480
+ local sub = context.tracker:ObserveOwnsAsset(KEY_TO_ID.swordKey):Subscribe(function(value)
481
+ table.insert(values, value)
482
+ end)
483
+
484
+ expect(PromiseTestUtils.awaitValue(function()
485
+ return values[#values] == false
486
+ end, 5)).toEqual(true)
487
+
488
+ sub:Destroy()
489
+ context.destroy()
490
+ end)
491
+ end)
492
+
493
+ describe("PlayerAssetOwnershipTracker override replication", function()
494
+ -- Two trackers over the SAME player share the replicated attribute. Roblox attribute
495
+ -- replication means a client sees exactly the attribute the server wrote, so a second tracker
496
+ -- binding to the same player is the replication-receive path.
497
+ local ATTRIBUTE_NAME = PlayerProductOwnershipOverrideUtils.attributeName(GameConfigAssetTypes.PASS)
498
+
499
+ local function makeTracker(player: Instance)
500
+ local purchased = Signal.new()
501
+ local tracker = PlayerAssetOwnershipTracker.new(
502
+ (player :: any) :: Player,
503
+ (makeConfigPicker() :: any) :: GameConfigPicker.GameConfigPicker,
504
+ GameConfigAssetTypes.PASS,
505
+ { Purchased = purchased }
506
+ )
507
+ return tracker, function()
508
+ tracker:Destroy()
509
+ purchased:Destroy()
510
+ end
511
+ end
512
+
513
+ it("should apply an override already present when a new tracker binds to the same player", function()
514
+ local player = Instance.new("Folder")
515
+
516
+ -- The "server" tracker authors the override, writing the replicated attribute.
517
+ local serverTracker, destroyServer = makeTracker(player)
518
+ serverTracker:SetOwnershipOverride("swordKey", true)
519
+
520
+ -- The "client" tracker binds later over the same (replicated) player attribute.
521
+ local clientTracker, destroyClient = makeTracker(player)
522
+ local promise = clientTracker:PromiseOwnsAsset("swordKey")
523
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
524
+ local _, owns = promise:Yield()
525
+ expect(owns).toEqual(true)
526
+
527
+ destroyClient()
528
+ destroyServer()
529
+ player:Destroy()
530
+ end)
531
+
532
+ it("should apply a false override present at bind time even when the cloud query owns", function()
533
+ local player = Instance.new("Folder")
534
+
535
+ local serverTracker, destroyServer = makeTracker(player)
536
+ serverTracker:SetOwnershipOverride("swordKey", false)
537
+
538
+ local clientTracker, destroyClient = makeTracker(player)
539
+ clientTracker:SetQueryOwnershipCallback(function()
540
+ return Promise.resolved(true)
541
+ end)
542
+ local promise = clientTracker:PromiseOwnsAsset("swordKey")
543
+ expect(PromiseTestUtils.awaitSettled(promise, 5)).toEqual(true)
544
+ local _, owns = promise:Yield()
545
+ expect(owns).toEqual(false)
546
+
547
+ destroyClient()
548
+ destroyServer()
549
+ player:Destroy()
550
+ end)
551
+
552
+ it("should not write the attribute for an unknown key", function()
553
+ local player = Instance.new("Folder")
554
+ local tracker, destroy = makeTracker(player)
555
+
556
+ tracker:SetOwnershipOverride("doesNotExist", true)
557
+ expect(player:GetAttribute(ATTRIBUTE_NAME)).toEqual(nil)
558
+
559
+ destroy()
560
+ player:Destroy()
561
+ end)
562
+
563
+ it("should remove the attribute once the only override is cleared", function()
564
+ local player = Instance.new("Folder")
565
+ local tracker, destroy = makeTracker(player)
566
+
567
+ tracker:SetOwnershipOverride("swordKey", true)
568
+ expect(player:GetAttribute(ATTRIBUTE_NAME) == nil).toEqual(false)
569
+
570
+ tracker:ClearOwnershipOverride("swordKey")
571
+ expect(player:GetAttribute(ATTRIBUTE_NAME)).toEqual(nil)
572
+
573
+ destroy()
574
+ player:Destroy()
575
+ end)
576
+ end)
@@ -0,0 +1,56 @@
1
+ --!strict
2
+ --[=[
3
+ Helpers for the replicated ownership-override map that [PlayerAssetOwnershipTracker] maintains.
4
+
5
+ Each ownership tracker owns one [JSONAttributeValue] on the player, named per asset type, holding
6
+ a map of `{ [assetIdString] = ownsAsset }`. The server writes it (authoritative) and Roblox
7
+ replicates it read-only to clients, where every realm's tracker applies it. Storing the asset id
8
+ as the (string) map key lets non-numeric ids (subscriptions) survive the JSON round-trip.
9
+
10
+ @class PlayerProductOwnershipOverrideUtils
11
+ ]=]
12
+
13
+ local PlayerProductOwnershipOverrideUtils = {}
14
+
15
+ -- Prefix for the per-asset-type override attribute on the player. Server-owned; clients may read it
16
+ -- but never assign it, so a player can never grant themselves ownership.
17
+ PlayerProductOwnershipOverrideUtils.ATTRIBUTE_PREFIX = "GameProductOwnershipOverride"
18
+
19
+ export type OwnershipOverrideState = { [string]: boolean }
20
+
21
+ --[=[
22
+ The attribute name that carries a given asset type's override map.
23
+
24
+ @param assetType string
25
+ @return string
26
+ ]=]
27
+ function PlayerProductOwnershipOverrideUtils.attributeName(assetType: string): string
28
+ assert(type(assetType) == "string", "Bad assetType")
29
+
30
+ return string.format("%s_%s", PlayerProductOwnershipOverrideUtils.ATTRIBUTE_PREFIX, assetType)
31
+ end
32
+
33
+ --[=[
34
+ Coerces an arbitrary decoded attribute value into a well-formed override map, dropping any
35
+ malformed entries. Returns a fresh mutable table (so callers may edit it), and treats nil (no
36
+ attribute yet) as an empty map.
37
+
38
+ @param value any
39
+ @return OwnershipOverrideState
40
+ ]=]
41
+ function PlayerProductOwnershipOverrideUtils.sanitizeState(value: any): OwnershipOverrideState
42
+ local state: OwnershipOverrideState = {}
43
+ if type(value) ~= "table" then
44
+ return state
45
+ end
46
+
47
+ for key, ownsAsset in value do
48
+ if type(key) == "string" and type(ownsAsset) == "boolean" then
49
+ state[key] = ownsAsset
50
+ end
51
+ end
52
+
53
+ return state
54
+ end
55
+
56
+ return PlayerProductOwnershipOverrideUtils
@@ -0,0 +1,65 @@
1
+ --!strict
2
+ --[[
3
+ Unit coverage for PlayerProductOwnershipOverrideUtils -- the pure helpers behind the replicated
4
+ ownership-override map. No instances, ServiceBag, or attributes are involved.
5
+
6
+ @class PlayerProductOwnershipOverrideUtils.spec.lua
7
+ ]]
8
+ local require = require(script.Parent.loader).load(script)
9
+
10
+ local Jest = require("Jest")
11
+ local PlayerProductOwnershipOverrideUtils = require("PlayerProductOwnershipOverrideUtils")
12
+
13
+ local describe = Jest.Globals.describe
14
+ local expect = Jest.Globals.expect
15
+ local it = Jest.Globals.it
16
+
17
+ describe("PlayerProductOwnershipOverrideUtils.attributeName", function()
18
+ it("should namespace the attribute per asset type", function()
19
+ expect(PlayerProductOwnershipOverrideUtils.attributeName("game")).toEqual("GameProductOwnershipOverride_game")
20
+ expect(PlayerProductOwnershipOverrideUtils.attributeName("pass")).toEqual("GameProductOwnershipOverride_pass")
21
+ end)
22
+
23
+ it("should give different asset types different attributes", function()
24
+ expect(PlayerProductOwnershipOverrideUtils.attributeName("game")).never.toEqual(
25
+ PlayerProductOwnershipOverrideUtils.attributeName("subscription")
26
+ )
27
+ end)
28
+ end)
29
+
30
+ describe("PlayerProductOwnershipOverrideUtils.sanitizeState", function()
31
+ it("should treat nil as an empty map", function()
32
+ expect(next(PlayerProductOwnershipOverrideUtils.sanitizeState(nil))).toEqual(nil)
33
+ end)
34
+
35
+ it("should treat a non-table as an empty map", function()
36
+ expect(next(PlayerProductOwnershipOverrideUtils.sanitizeState("garbage"))).toEqual(nil)
37
+ end)
38
+
39
+ it("should keep boolean entries with string keys", function()
40
+ local state = PlayerProductOwnershipOverrideUtils.sanitizeState({
41
+ ["9705252209"] = true,
42
+ ["123"] = false,
43
+ })
44
+ expect(state["9705252209"]).toEqual(true)
45
+ expect(state["123"]).toEqual(false)
46
+ end)
47
+
48
+ it("should drop entries whose value is not a boolean", function()
49
+ local state = PlayerProductOwnershipOverrideUtils.sanitizeState({
50
+ ["1"] = true,
51
+ ["2"] = "yes",
52
+ ["3"] = 5,
53
+ })
54
+ expect(state["1"]).toEqual(true)
55
+ expect(state["2"]).toEqual(nil)
56
+ expect(state["3"]).toEqual(nil)
57
+ end)
58
+
59
+ it("should return a fresh table the caller can mutate", function()
60
+ local source = { ["1"] = true }
61
+ local state = PlayerProductOwnershipOverrideUtils.sanitizeState(source)
62
+ state["2"] = false
63
+ expect(source["2"]).toEqual(nil)
64
+ end)
65
+ end)