@quenty/saveslot 1.8.0 → 1.10.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,22 @@
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
+ # [1.10.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@1.9.0...@quenty/saveslot@1.10.0) (2026-07-21)
7
+
8
+ **Note:** Version bump only for package @quenty/saveslot
9
+
10
+ # [1.9.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@1.8.0...@quenty/saveslot@1.9.0) (2026-07-21)
11
+
12
+ ### Bug Fixes
13
+
14
+ - Resolve luau-lsp and moonwave lint failures ([286e352](https://github.com/Quenty/NevermoreEngine/commit/286e3527b52a86639fc0485359141d7805b3fefa))
15
+
16
+ ### Features
17
+
18
+ - Add a method to reset active slots ([f353b3e](https://github.com/Quenty/NevermoreEngine/commit/f353b3e579651bd6dcabf158f5d2c8943725ccc5))
19
+ - Add duplication to save slots package ([27288e4](https://github.com/Quenty/NevermoreEngine/commit/27288e4b8cd6d29a93c2807c364f13ea03ad5fee))
20
+ - Add save slot tracking data ([34ce611](https://github.com/Quenty/NevermoreEngine/commit/34ce61104410a96be2d23b21db6485b0ea428548))
21
+
6
22
  # [1.8.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@1.7.0...@quenty/saveslot@1.8.0) (2026-07-20)
7
23
 
8
24
  **Note:** Version bump only for package @quenty/saveslot
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/saveslot",
3
- "version": "1.8.0",
3
+ "version": "1.10.0",
4
4
  "description": "PlayerDataStoreService wrapper for save slots",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -29,15 +29,17 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "@quenty/adorneedata": "7.37.0",
32
+ "@quenty/attributeutils": "14.32.0",
32
33
  "@quenty/baseobject": "10.14.0",
33
34
  "@quenty/binder": "14.39.0",
34
35
  "@quenty/brio": "14.32.0",
35
36
  "@quenty/cmdrservice": "13.49.0",
36
- "@quenty/datastore": "13.43.0",
37
+ "@quenty/datastore": "13.44.0",
37
38
  "@quenty/instanceutils": "13.32.0",
38
39
  "@quenty/loader": "10.11.0",
39
40
  "@quenty/maid": "3.10.0",
40
41
  "@quenty/nevermore-test-runner": "1.4.0",
42
+ "@quenty/observablecollection": "12.41.0",
41
43
  "@quenty/playerbinder": "14.39.0",
42
44
  "@quenty/promise": "10.20.0",
43
45
  "@quenty/propertyvalue": "7.32.0",
@@ -46,7 +48,7 @@
46
48
  "@quenty/servicebag": "11.19.0",
47
49
  "@quenty/signal": "7.13.1",
48
50
  "@quenty/table": "3.9.2",
49
- "@quenty/teleportserviceutils": "9.34.0",
51
+ "@quenty/teleportserviceutils": "9.36.0",
50
52
  "@quenty/tie": "10.43.0",
51
53
  "@quenty/valueobject": "13.33.0",
52
54
  "@quentystudios/jest-lua": "3.10.0-quenty.2"
@@ -54,5 +56,5 @@
54
56
  "publishConfig": {
55
57
  "access": "public"
56
58
  },
57
- "gitHead": "d2d7ae433f3fa756e038522986144b4f5c616756"
59
+ "gitHead": "8bd905a5c7e7f1fd4e178b54beeadef94a615d1d"
58
60
  }
@@ -129,14 +129,18 @@ function HasSaveSlotsClient.PromiseSlotIdFromIndex(
129
129
  return self._remoting.PromiseSlotIdFromIndex:PromiseInvokeServer(slotIndex)
130
130
  end
131
131
 
132
- -- Client realm hook for HasSaveSlotsBase: the incoming slot id is whatever the local player
133
- -- teleported in with, read from their local teleport data via TeleportDataServiceClient.
134
- function HasSaveSlotsClient._getIncomingSlotId(self: HasSaveSlotsClient): SaveSlotData.SlotId?
135
- local slotId = self._teleportDataServiceClient:GetArrivedValue(SaveSlotConstants.TELEPORT_DATA_SLOT_KEY)
136
- if type(slotId) == "string" then
137
- return slotId
138
- end
139
- return nil
132
+ -- Client realm hook for HasSaveSlotsBase: the incoming slot id is whatever the local player teleported
133
+ -- in with, read from the unified TeleportDataServiceClient view (a promise for symmetry with the
134
+ -- server; on the client it resolves immediately from local teleport data).
135
+ function HasSaveSlotsClient.PromiseIncomingSlotId(self: HasSaveSlotsClient): Promise.Promise<SaveSlotData.SlotId?>
136
+ return self._teleportDataServiceClient
137
+ :PromiseArrivedValue(SaveSlotConstants.TELEPORT_DATA_SLOT_KEY)
138
+ :Then(function(slotId): SaveSlotData.SlotId?
139
+ if type(slotId) == "string" then
140
+ return slotId
141
+ end
142
+ return nil
143
+ end)
140
144
  end
141
145
 
142
146
  return Binder.new("HasSaveSlots", HasSaveSlotsClient :: any) :: Binder.Binder<HasSaveSlotsClient>
@@ -7,6 +7,7 @@ local require = require(script.Parent.loader).load(script)
7
7
 
8
8
  local ReplicatedStorage = game:GetService("ReplicatedStorage")
9
9
 
10
+ local HasSaveSlotsData = require("HasSaveSlotsData")
10
11
  local Maid = require("Maid")
11
12
  local Remoting = require("Remoting")
12
13
  local SaveSlotConstants = require("SaveSlotConstants")
@@ -44,15 +45,33 @@ function SaveSlotServiceClient.Init(self: SaveSlotServiceClient, serviceBag: Ser
44
45
  self._remoting = self._maid:Add(Remoting.Client.new(ReplicatedStorage, "SaveSlotService"))
45
46
  end
46
47
 
48
+ function SaveSlotServiceClient.Start(self: SaveSlotServiceClient)
49
+ -- Symmetric mirror of the server's provider ([SaveSlotService.Start]): a teleport the client builds
50
+ -- carries the local player's active slot id, so a client-initiated teleport resumes on the same slot
51
+ -- without any menu re-attaching it by hand.
52
+ self._maid:GiveTask(
53
+ self._teleportDataServiceClient:RegisterPerPlayerTeleportDataProvider(
54
+ function(player: Player): { [string]: any }?
55
+ local slotId = HasSaveSlotsData.ActiveSlotId:Get(player)
56
+ if type(slotId) == "string" then
57
+ return { [SaveSlotConstants.TELEPORT_DATA_SLOT_KEY] = slotId }
58
+ end
59
+ return nil
60
+ end
61
+ )
62
+ )
63
+ end
64
+
47
65
  --[=[
48
- Returns whether the local player teleported in carrying a save-slot id -- i.e. arrived via an
49
- internal slot teleport rather than a fresh join. Sync, presence-only (mirrors the server's
50
- [SaveSlotService.IsInternalTeleport]).
66
+ Resolves whether the local player teleported in carrying a save-slot id -- i.e. arrived via an
67
+ internal slot teleport rather than a fresh join. Presence-only. A promise for symmetry with the
68
+ server's [SaveSlotService.PromiseIsInternalTeleport]; on the client it resolves immediately from
69
+ local teleport data.
51
70
 
52
- @return boolean
71
+ @return Promise<boolean>
53
72
  ]=]
54
- function SaveSlotServiceClient.IsInternalTeleport(self: SaveSlotServiceClient): boolean
55
- return self._teleportDataServiceClient:HasArrivedValue(SaveSlotConstants.TELEPORT_DATA_SLOT_KEY)
73
+ function SaveSlotServiceClient.PromiseIsInternalTeleport(self: SaveSlotServiceClient): any
74
+ return self._teleportDataServiceClient:PromiseHasArrivedValue(SaveSlotConstants.TELEPORT_DATA_SLOT_KEY)
56
75
  end
57
76
 
58
77
  --[=[
@@ -14,6 +14,7 @@ local HasSaveSlotsBase = require("HasSaveSlotsBase")
14
14
  local HasSaveSlotsInterface = require("HasSaveSlotsInterface")
15
15
  local Maid = require("Maid")
16
16
  local Observable = require("Observable")
17
+ local ObservableMap = require("ObservableMap")
17
18
  local PlayerBinder = require("PlayerBinder")
18
19
  local PlayerDataStoreService = require("PlayerDataStoreService")
19
20
  local Promise = require("Promise")
@@ -24,9 +25,23 @@ local SaveSlotConstants = require("SaveSlotConstants")
24
25
  local SaveSlotData = require("SaveSlotData")
25
26
  local ServiceBag = require("ServiceBag")
26
27
  local TeleportDataService = require("TeleportDataService")
27
- local ValueObject = require("ValueObject")
28
28
 
29
- export type SaveSlotSummaryProvider = (Player, any) -> Observable.Observable<string>
29
+ -- A summary provider contributes one named piece of a slot's Summary. It is called with the player and
30
+ -- the slot's DataStoreStage and returns an Observable of a JSON-serializable value. Providers are
31
+ -- aggregated into a table keyed by their registered name; see RegisterSummaryProvider and _setupSummary.
32
+ export type SummaryProvider = (Player, DataStoreStage.DataStoreStage) -> Observable.Observable<any>
33
+
34
+ -- A provider that errors (when called, or mid-stream) contributes this sentinel rather than stalling or
35
+ -- failing the whole aggregate; _setupSummary strips it back out before writing the Summary. Compared by
36
+ -- identity, so it never collides with a real value (even an empty table) a provider might emit.
37
+ local NONE = {}
38
+
39
+ -- The caller-supplied fields for a new slot. SlotId and SlotIndex are assigned by PromiseCreateSlot
40
+ -- itself (from a fresh GUID and the slotIndex argument), so they are never taken from here.
41
+ export type SaveSlotCreateMetadata = {
42
+ SlotName: string?,
43
+ Summary: SaveSlotData.SaveSlotSummary?,
44
+ }
30
45
 
31
46
  local HasSaveSlots = setmetatable({}, HasSaveSlotsBase)
32
47
  HasSaveSlots.ClassName = "HasSaveSlots"
@@ -45,9 +60,12 @@ export type HasSaveSlots =
45
60
  _dataStore: any,
46
61
  _systemStore: any,
47
62
  _metadataStore: any,
48
- _summaryProvider: ValueObject.ValueObject<SaveSlotSummaryProvider?>,
63
+ _summaryProviders: ObservableMap.ObservableMap<string, SummaryProvider>,
49
64
  _lastActiveSlotId: SaveSlotData.SlotId?,
50
65
  _teleportDataService: any,
66
+ _playSessionSlotId: SaveSlotData.SlotId?,
67
+ _playSessionStart: number?,
68
+ _playSessionLastFlush: number?,
51
69
  },
52
70
  {} :: typeof({ __index = HasSaveSlots })
53
71
  ))
@@ -67,13 +85,14 @@ function HasSaveSlots.new(player: Player, serviceBag: ServiceBag.ServiceBag): Ha
67
85
 
68
86
  self._slotMap = {}
69
87
 
70
- self._summaryProvider = self._maid:Add(ValueObject.new(nil))
88
+ self._summaryProviders = self._maid:Add(ObservableMap.new())
71
89
 
72
90
  self._loadPromise = self._maid:GivePromise(self:_promiseLoadSlots())
73
91
 
74
92
  self._remoting = self._maid:Add(Remoting.Server.new(self._obj, "HasSaveSlots"))
75
93
 
76
94
  self:_setupSummary()
95
+ self:_setupPlaytimeTracking()
77
96
  self:_setupRemotes()
78
97
 
79
98
  self._maid:GiveTask(HasSaveSlotsInterface.Server:Implement(self._obj, self))
@@ -185,7 +204,7 @@ end
185
204
  function HasSaveSlots.PromiseCreateSlot(
186
205
  self: HasSaveSlots,
187
206
  slotIndex: number,
188
- metadata: SaveSlotData.SaveSlotMetadata?
207
+ metadata: SaveSlotCreateMetadata?
189
208
  ): Promise.Promise<SaveSlotData.SlotId>
190
209
  return (self._loadPromise :: any):Then(function()
191
210
  if (slotIndex < 1) or (slotIndex > self.MaxSlotCount.Value) then
@@ -212,6 +231,67 @@ function HasSaveSlots.PromiseCreateSlot(
212
231
  end)
213
232
  end
214
233
 
234
+ --[=[
235
+ Duplicates the slot with the given ID into a new slot at the lowest free index,
236
+ copying its saved data. Resolves to the new slot's id. The copy is not selected,
237
+ its metadata (playtime, timestamps) starts fresh, and its name is suffixed with
238
+ " (Copy)". Rejects when the source slot is missing or every index is in use.
239
+ ]=]
240
+ function HasSaveSlots.PromiseDuplicateSlot(
241
+ self: HasSaveSlots,
242
+ slotId: SaveSlotData.SlotId
243
+ ): Promise.Promise<SaveSlotData.SlotId>
244
+ return (self._loadPromise :: any):Then(function()
245
+ local sourceSlot = self._slotMap[slotId]
246
+ if not sourceSlot then
247
+ return (Promise :: any).rejected(`Slot \{{slotId}\} not found`)
248
+ end
249
+
250
+ -- Lowest free positive index, filling gaps left by deletions (mirrors PromiseSelectNewSaveSlot).
251
+ local usedIndices = {}
252
+ for _, slot in self._slotMap do
253
+ usedIndices[SaveSlotData.SlotIndex:Get(slot)] = true
254
+ end
255
+ local freeIndex = 1
256
+ while usedIndices[freeIndex] do
257
+ freeIndex += 1
258
+ end
259
+ if freeIndex > self.MaxSlotCount.Value then
260
+ return (Promise :: any).rejected("All slots are already in use")
261
+ end
262
+
263
+ local sourceMetadata = SaveSlotData:Get(sourceSlot)
264
+
265
+ -- Read the source's saved data before creating the copy so a read failure leaves no orphan slot.
266
+ return self:_getSlotStore(slotId):LoadAll({}):Then(function(sourceData)
267
+ -- The default slot shares the player's root store with the SaveSlots system data. Never carry
268
+ -- that system key across into the copy's saved data.
269
+ local slotData = if type(sourceData) == "table" then table.clone(sourceData) else {}
270
+ slotData[SaveSlotConstants.SYSTEM_STORE_KEY] = nil
271
+
272
+ return self:PromiseCreateSlot(freeIndex, {
273
+ SlotName = `{sourceMetadata.SlotName} (Copy)`,
274
+ Summary = sourceMetadata.Summary,
275
+ }):Then(function(newSlotId: SaveSlotData.SlotId)
276
+ local destStore = self:_getSlotStore(newSlotId)
277
+
278
+ if destStore == self._dataStore then
279
+ -- The copy is the default slot, whose store is the shared root. Merge so the SaveSlots
280
+ -- system store living alongside it survives (a plain Overwrite would wipe it).
281
+ destStore:OverwriteMerge(slotData)
282
+ else
283
+ destStore:Overwrite(slotData)
284
+ end
285
+
286
+ -- Flush so the duplicated data survives a crash before the next autosave.
287
+ return self._dataStore:Save():Then(function()
288
+ return newSlotId
289
+ end)
290
+ end)
291
+ end)
292
+ end)
293
+ end
294
+
215
295
  --[=[
216
296
  Deletes the slot with the given ID
217
297
  ]=]
@@ -291,6 +371,80 @@ function HasSaveSlots.PromiseDeleteAllSlots(self: HasSaveSlots): Promise.Promise
291
371
  end)
292
372
  end
293
373
 
374
+ --[=[
375
+ Resets the slot with the given id to a fresh empty one -- equivalent to deleting the slot
376
+ and creating a new one at the same index. The slot keeps its index and name; its saved data
377
+ and metadata (timestamps) start fresh. Resolves to the new slot id.
378
+
379
+ When the reset slot is the active slot, the selection clears and then reselects the fresh
380
+ slot: everything bound to [HasSaveSlots.ObserveActiveSlotStoreBrio] tears down as the
381
+ selection clears and rebuilds against the empty store on reselect, so consumers reset
382
+ reactively without wiping their own state. A non-active slot is left unselected, and its
383
+ "Continue" pointer (when it was the last-active slot) is carried across to the fresh id so
384
+ the reset slot stays resumable. Rejects when the slot is missing.
385
+ ]=]
386
+ function HasSaveSlots.PromiseResetSlot(
387
+ self: HasSaveSlots,
388
+ slotId: SaveSlotData.SlotId
389
+ ): Promise.Promise<SaveSlotData.SlotId>
390
+ return (self._loadPromise :: any):Then(function(): any
391
+ local slot = self._slotMap[slotId]
392
+ if not slot then
393
+ return (Promise :: any).rejected(`Slot \{{slotId}\} not found`)
394
+ end
395
+
396
+ local metadata = SaveSlotData:Get(slot)
397
+ local wasActive = (slotId == self.ActiveSlotId.Value)
398
+ -- PromiseDeleteSlot clears the continue pointer when it targets the last-active slot; capture it
399
+ -- now so the non-active path can move it onto the fresh slot id below.
400
+ local wasLastActive = (slotId == self._lastActiveSlotId)
401
+
402
+ if wasActive then
403
+ -- Clear the selection so the slot is deletable, skipping the deselect flush that would
404
+ -- persist progress we are about to delete.
405
+ self.ActiveSlotId.Value = nil
406
+ end
407
+
408
+ return self:PromiseDeleteSlot(slotId)
409
+ :Then(function()
410
+ return self:PromiseCreateSlot(metadata.SlotIndex, { SlotName = metadata.SlotName })
411
+ end)
412
+ :Then(function(newSlotId: SaveSlotData.SlotId)
413
+ if wasActive then
414
+ -- Reselecting restores the continue pointer via the ActiveSlotId hook.
415
+ return self:PromiseSelectSlot(newSlotId):Then(function()
416
+ return newSlotId
417
+ end)
418
+ end
419
+
420
+ -- Non-active reset never reselects, so carry the resume pointer onto the fresh id
421
+ -- ourselves when this slot was the one "Continue" would resume.
422
+ if wasLastActive then
423
+ self._lastActiveSlotId = newSlotId
424
+ self.LastActiveSlotId.Value = newSlotId
425
+ end
426
+
427
+ return newSlotId
428
+ end)
429
+ end)
430
+ end
431
+
432
+ --[=[
433
+ Resets the active slot to a fresh empty one -- see [HasSaveSlots.PromiseResetSlot]. The slot
434
+ keeps its index and name; its saved data and metadata (timestamps) start fresh, and the fresh
435
+ slot stays selected. Resolves to the new slot id, or nil when no slot is active.
436
+ ]=]
437
+ function HasSaveSlots.PromiseResetActiveSlot(self: HasSaveSlots): Promise.Promise<SaveSlotData.SlotId?>
438
+ return (self._loadPromise :: any):Then(function(): any
439
+ local slotId = self.ActiveSlotId.Value
440
+ if not slotId then
441
+ return nil -- Nothing selected to reset
442
+ end
443
+
444
+ return self:PromiseResetSlot(slotId)
445
+ end)
446
+ end
447
+
294
448
  --[=[
295
449
  Sets the metadata for the slot with the given ID
296
450
  ]=]
@@ -411,20 +565,36 @@ function HasSaveSlots.PromiseSelectNewSaveSlot(self: HasSaveSlots): Promise.Prom
411
565
  end
412
566
 
413
567
  --[=[
414
- Sets the summary provider
568
+ Registers a named summary provider. Every registered provider's current value is aggregated into
569
+ the active slot's Summary, keyed by `name` (see [HasSaveSlots.PromiseGetSlotMetadata]). Registering
570
+ the same name again replaces the previous provider. Returns a function that unregisters the provider
571
+ (also give it to a [Maid]).
572
+
573
+ @param name string
574
+ @param provider SummaryProvider
575
+ @return () -> ()
415
576
  ]=]
416
- function HasSaveSlots.SetSummaryProvider(self: HasSaveSlots, provider: SaveSlotSummaryProvider?): ()
417
- self._summaryProvider.Value = provider
577
+ function HasSaveSlots.RegisterSummaryProvider(self: HasSaveSlots, name: string, provider: SummaryProvider): () -> ()
578
+ assert(type(name) == "string", "Bad name")
579
+ assert(type(provider) == "function", "Bad provider")
580
+
581
+ return self._summaryProviders:Set(name, provider :: any)
418
582
  end
419
583
 
420
584
  -- Server realm hook for HasSaveSlotsBase: the incoming slot id is whatever the player teleported in
421
- -- with, read from their join data via TeleportDataService.
422
- function HasSaveSlots._getIncomingSlotId(self: HasSaveSlots): SaveSlotData.SlotId?
423
- local slotId = self._teleportDataService:GetArrivedValue(self._obj, SaveSlotConstants.TELEPORT_DATA_SLOT_KEY)
424
- if type(slotId) == "string" then
425
- return slotId
426
- end
427
- return nil
585
+ -- with, read from the unified TeleportDataService view. It is a promise because a client-initiated
586
+ -- teleport only reaches the server once the client replicates its arrived data. The slot id is a
587
+ -- client *request* -- fine here, because PromiseLoadSaveSlotFromTeleport re-validates ownership via
588
+ -- PromiseHasSlot before selecting it.
589
+ function HasSaveSlots.PromiseIncomingSlotId(self: HasSaveSlots): Promise.Promise<SaveSlotData.SlotId?>
590
+ return self._teleportDataService
591
+ :PromiseArrivedValue(self._obj, SaveSlotConstants.TELEPORT_DATA_SLOT_KEY)
592
+ :Then(function(slotId): SaveSlotData.SlotId?
593
+ if type(slotId) == "string" then
594
+ return slotId
595
+ end
596
+ return nil
597
+ end)
428
598
  end
429
599
 
430
600
  function HasSaveSlots._promiseLoadSlots(self: HasSaveSlots): Promise.Promise<{}>
@@ -489,7 +659,9 @@ function HasSaveSlots._buildSlot(
489
659
  end
490
660
 
491
661
  -- Store mutable metadata on change
492
- for _, key in { "SlotName", "CreatedTime", "LastPlayedTime", "Summary" } do
662
+ for _, key in
663
+ { "SlotName", "CreatedTime", "LastPlayedTime", "Summary", "TimePlayed", "PlayCount", "LastSessionLength" }
664
+ do
493
665
  attributes[key].Value = data[key]
494
666
  maid:GiveTask(metadataStore:StoreOnValueChange(key, attributes[key]))
495
667
 
@@ -522,33 +694,164 @@ function HasSaveSlots._setupSummary(self: HasSaveSlots): ()
522
694
 
523
695
  local maid, slotStore = brio:ToMaidAndValue()
524
696
 
525
- maid:GiveTask(self._summaryProvider
526
- :Observe()
527
- :Pipe({
528
- Rx.switchMap(function(provider: SaveSlotSummaryProvider?)
529
- if not provider then
530
- return Rx.of("") :: any
531
- end
697
+ maid:GiveTask(self:_observeSummary(slotStore):Subscribe(function(summary: SaveSlotData.SaveSlotSummary)
698
+ -- An empty aggregate (no providers, or every one errored/contributed nothing) clears the
699
+ -- Summary rather than persisting an empty table.
700
+ SaveSlotData.Summary:Set(activeSlot, if next(summary) ~= nil then summary else nil)
701
+ end))
702
+ end)
703
+ )
704
+ end
532
705
 
533
- local success, observable = pcall(provider, self._obj, slotStore)
534
- if not success then
535
- warn(`[HasSaveSlots] Summary provider errored: {observable}`)
536
- return Rx.of("") :: any
537
- end
706
+ -- Aggregates every registered summary provider's current value into one table keyed by provider name,
707
+ -- re-aggregating whenever a provider is registered or unregistered. Always emits a table (possibly
708
+ -- empty); providers that contribute nothing are simply absent from it.
709
+ function HasSaveSlots._observeSummary(
710
+ self: HasSaveSlots,
711
+ slotStore: DataStoreStage.DataStoreStage
712
+ ): Observable.Observable<SaveSlotData.SaveSlotSummary>
713
+ return self._summaryProviders:ObserveKeyList():Pipe({
714
+ Rx.switchMap(function(names: { string })
715
+ if #names == 0 then
716
+ return Rx.of({}) :: any
717
+ end
538
718
 
539
- return observable
540
- end) :: any,
541
- })
542
- :Subscribe(function(summary: string)
543
- if type(summary) ~= "string" then
544
- warn(`[HasSaveSlots] Summary provider emitted non-string ({typeof(summary)})`)
545
- return
719
+ local observablesByName = {}
720
+ for _, name in names do
721
+ observablesByName[name] = self:_observeProviderValue(name, slotStore)
722
+ end
723
+
724
+ return Rx.combineLatest(observablesByName):Pipe({
725
+ Rx.map(function(valuesByName: { [string]: any }): SaveSlotData.SaveSlotSummary
726
+ local summary = {}
727
+ for name, value in valuesByName do
728
+ if value ~= NONE then
729
+ summary[name] = value
730
+ end
546
731
  end
732
+ return summary
733
+ end) :: any,
734
+ }) :: any
735
+ end) :: any,
736
+ }) :: any
737
+ end
547
738
 
548
- SaveSlotData.Summary:Set(activeSlot, summary)
549
- end))
550
- end)
551
- )
739
+ -- Observes the value contributed by the provider registered under `name`, isolating it: an error when
740
+ -- calling the provider, a non-Observable return, or a mid-stream error all resolve to the NONE sentinel
741
+ -- so one bad provider neither stalls nor fails the aggregate. Tracks provider replacement at `name`.
742
+ function HasSaveSlots._observeProviderValue(
743
+ self: HasSaveSlots,
744
+ name: string,
745
+ slotStore: DataStoreStage.DataStoreStage
746
+ ): Observable.Observable<any>
747
+ return self._summaryProviders:ObserveAtKey(name):Pipe({
748
+ Rx.switchMap(function(provider: SummaryProvider?)
749
+ if not provider then
750
+ return Rx.of(NONE) :: any
751
+ end
752
+
753
+ local success, observable = pcall(provider, self._obj, slotStore)
754
+ if not success then
755
+ warn(`[HasSaveSlots] Summary provider {name} errored: {observable}`)
756
+ return Rx.of(NONE) :: any
757
+ end
758
+
759
+ if not Observable.isObservable(observable) then
760
+ warn(`[HasSaveSlots] Summary provider {name} did not return an Observable`)
761
+ return Rx.of(NONE) :: any
762
+ end
763
+
764
+ return (observable :: any):Pipe({
765
+ Rx.catchError(function(err)
766
+ warn(`[HasSaveSlots] Summary provider {name} stream errored: {err}`)
767
+ return Rx.of(NONE)
768
+ end) :: any,
769
+ })
770
+ end) :: any,
771
+ }) :: any
772
+ end
773
+
774
+ --[=[
775
+ Accrues per-slot playtime automatically. A "session" spans the time a slot is the active slot:
776
+ selecting a slot begins one (bumping PlayCount), and deselecting, switching, or unbinding ends
777
+ it. Elapsed wall time is folded into the slot's TimePlayed from a datastore saving callback, so
778
+ it persists on exactly the cadence the data is written -- always fresh at save time, with no
779
+ separate timer -- and again at each session boundary.
780
+ ]=]
781
+ function HasSaveSlots._setupPlaytimeTracking(self: HasSaveSlots): ()
782
+ self._playSessionSlotId = nil
783
+ self._playSessionStart = nil
784
+ self._playSessionLastFlush = nil
785
+
786
+ -- The active slot bounds the session: end the previous one (if any) and begin the new one.
787
+ self._maid:GiveTask(self.ActiveSlotId.Changed:Connect(function()
788
+ self:_endPlaySession()
789
+
790
+ local activeSlotId = self.ActiveSlotId.Value
791
+ if activeSlotId ~= nil then
792
+ self:_beginPlaySession(activeSlotId)
793
+ end
794
+ end))
795
+
796
+ -- Fold accrued time into TimePlayed just before every save so the written value is current. The
797
+ -- callback runs before the save serializes staged data (see DataStore._syncData), so the flush is
798
+ -- captured by that same save -- including the final save-on-leave.
799
+ self._maid:GivePromise(self._loadPromise):Then(function()
800
+ self._maid:GiveTask(self._dataStore:AddSavingCallback(function()
801
+ self:_flushPlaytime()
802
+ end))
803
+ end)
804
+
805
+ -- Best-effort flush on unbind for the case where the binder tears down before that final save.
806
+ self._maid:GiveTask(function()
807
+ self:_endPlaySession()
808
+ end)
809
+ end
810
+
811
+ function HasSaveSlots._beginPlaySession(self: HasSaveSlots, slotId: SaveSlotData.SlotId): ()
812
+ local now = os.time()
813
+ self._playSessionSlotId = slotId
814
+ self._playSessionStart = now
815
+ self._playSessionLastFlush = now
816
+
817
+ local slot = self._slotMap[slotId]
818
+ if slot then
819
+ SaveSlotData.PlayCount:Set(slot, (SaveSlotData.PlayCount:Get(slot) or 0) + 1)
820
+ end
821
+ end
822
+
823
+ function HasSaveSlots._flushPlaytime(self: HasSaveSlots): ()
824
+ local slotId = self._playSessionSlotId
825
+ if slotId == nil then
826
+ return
827
+ end
828
+
829
+ local slot = self._slotMap[slotId]
830
+ if not slot then
831
+ return
832
+ end
833
+
834
+ local now = os.time()
835
+
836
+ -- Add only the time since the last flush so repeated flushes within a session never double count.
837
+ local sinceFlush = now - (self._playSessionLastFlush or now)
838
+ if sinceFlush > 0 then
839
+ SaveSlotData.TimePlayed:Set(slot, (SaveSlotData.TimePlayed:Get(slot) or 0) + sinceFlush)
840
+ self._playSessionLastFlush = now
841
+ end
842
+
843
+ SaveSlotData.LastSessionLength:Set(slot, now - (self._playSessionStart or now))
844
+ end
845
+
846
+ function HasSaveSlots._endPlaySession(self: HasSaveSlots): ()
847
+ if self._playSessionSlotId == nil then
848
+ return
849
+ end
850
+
851
+ self:_flushPlaytime()
852
+ self._playSessionSlotId = nil
853
+ self._playSessionStart = nil
854
+ self._playSessionLastFlush = nil
552
855
  end
553
856
 
554
857
  function HasSaveSlots._setupRemotes(self: HasSaveSlots): ()