@quenty/saveslot 1.8.0 → 1.9.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 +12 -0
- package/package.json +6 -4
- package/src/Server/Binders/HasSaveSlots.lua +329 -32
- package/src/Server/Binders/HasSaveSlots.spec.lua +784 -0
- package/src/Server/Cmdr/SaveSlotCmdrService.lua +162 -23
- package/src/Server/SaveSlotOverflow.spec.lua +128 -0
- package/src/Server/SaveSlotService.lua +43 -18
- package/src/Server/SaveSlotService.spec.lua +53 -18
- package/src/Shared/Cmdr/SaveSlotCmdrUtils.lua +15 -0
- package/src/Shared/Cmdr/SaveSlotCmdrUtils.spec.lua +142 -0
- package/src/Shared/Data/SaveSlotData.lua +27 -2
- package/src/Shared/Data/SaveSlotData.spec.lua +68 -0
- package/src/Shared/SaveSlotDataService.lua +14 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
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.9.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@1.8.0...@quenty/saveslot@1.9.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 a method to reset active slots ([f353b3e](https://github.com/Quenty/NevermoreEngine/commit/f353b3e579651bd6dcabf158f5d2c8943725ccc5))
|
|
15
|
+
- Add duplication to save slots package ([27288e4](https://github.com/Quenty/NevermoreEngine/commit/27288e4b8cd6d29a93c2807c364f13ea03ad5fee))
|
|
16
|
+
- Add save slot tracking data ([34ce611](https://github.com/Quenty/NevermoreEngine/commit/34ce61104410a96be2d23b21db6485b0ea428548))
|
|
17
|
+
|
|
6
18
|
# [1.8.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@1.7.0...@quenty/saveslot@1.8.0) (2026-07-20)
|
|
7
19
|
|
|
8
20
|
**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.
|
|
3
|
+
"version": "1.9.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.
|
|
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.
|
|
51
|
+
"@quenty/teleportserviceutils": "9.35.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": "
|
|
59
|
+
"gitHead": "b7e59984e586064ea3cec6176e79b3f7451ecdc5"
|
|
58
60
|
}
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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.
|
|
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:
|
|
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,10 +565,20 @@ function HasSaveSlots.PromiseSelectNewSaveSlot(self: HasSaveSlots): Promise.Prom
|
|
|
411
565
|
end
|
|
412
566
|
|
|
413
567
|
--[=[
|
|
414
|
-
|
|
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.
|
|
417
|
-
|
|
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
|
|
@@ -489,7 +653,9 @@ function HasSaveSlots._buildSlot(
|
|
|
489
653
|
end
|
|
490
654
|
|
|
491
655
|
-- Store mutable metadata on change
|
|
492
|
-
for _, key in
|
|
656
|
+
for _, key in
|
|
657
|
+
{ "SlotName", "CreatedTime", "LastPlayedTime", "Summary", "TimePlayed", "PlayCount", "LastSessionLength" }
|
|
658
|
+
do
|
|
493
659
|
attributes[key].Value = data[key]
|
|
494
660
|
maid:GiveTask(metadataStore:StoreOnValueChange(key, attributes[key]))
|
|
495
661
|
|
|
@@ -522,33 +688,164 @@ function HasSaveSlots._setupSummary(self: HasSaveSlots): ()
|
|
|
522
688
|
|
|
523
689
|
local maid, slotStore = brio:ToMaidAndValue()
|
|
524
690
|
|
|
525
|
-
maid:GiveTask(self.
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
691
|
+
maid:GiveTask(self:_observeSummary(slotStore):Subscribe(function(summary: SaveSlotData.SaveSlotSummary)
|
|
692
|
+
-- An empty aggregate (no providers, or every one errored/contributed nothing) clears the
|
|
693
|
+
-- Summary rather than persisting an empty table.
|
|
694
|
+
SaveSlotData.Summary:Set(activeSlot, if next(summary) ~= nil then summary else nil)
|
|
695
|
+
end))
|
|
696
|
+
end)
|
|
697
|
+
)
|
|
698
|
+
end
|
|
532
699
|
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
700
|
+
-- Aggregates every registered summary provider's current value into one table keyed by provider name,
|
|
701
|
+
-- re-aggregating whenever a provider is registered or unregistered. Always emits a table (possibly
|
|
702
|
+
-- empty); providers that contribute nothing are simply absent from it.
|
|
703
|
+
function HasSaveSlots._observeSummary(
|
|
704
|
+
self: HasSaveSlots,
|
|
705
|
+
slotStore: DataStoreStage.DataStoreStage
|
|
706
|
+
): Observable.Observable<SaveSlotData.SaveSlotSummary>
|
|
707
|
+
return self._summaryProviders:ObserveKeyList():Pipe({
|
|
708
|
+
Rx.switchMap(function(names: { string })
|
|
709
|
+
if #names == 0 then
|
|
710
|
+
return Rx.of({}) :: any
|
|
711
|
+
end
|
|
538
712
|
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
713
|
+
local observablesByName = {}
|
|
714
|
+
for _, name in names do
|
|
715
|
+
observablesByName[name] = self:_observeProviderValue(name, slotStore)
|
|
716
|
+
end
|
|
717
|
+
|
|
718
|
+
return Rx.combineLatest(observablesByName):Pipe({
|
|
719
|
+
Rx.map(function(valuesByName: { [string]: any }): SaveSlotData.SaveSlotSummary
|
|
720
|
+
local summary = {}
|
|
721
|
+
for name, value in valuesByName do
|
|
722
|
+
if value ~= NONE then
|
|
723
|
+
summary[name] = value
|
|
724
|
+
end
|
|
546
725
|
end
|
|
726
|
+
return summary
|
|
727
|
+
end) :: any,
|
|
728
|
+
}) :: any
|
|
729
|
+
end) :: any,
|
|
730
|
+
}) :: any
|
|
731
|
+
end
|
|
547
732
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
733
|
+
-- Observes the value contributed by the provider registered under `name`, isolating it: an error when
|
|
734
|
+
-- calling the provider, a non-Observable return, or a mid-stream error all resolve to the NONE sentinel
|
|
735
|
+
-- so one bad provider neither stalls nor fails the aggregate. Tracks provider replacement at `name`.
|
|
736
|
+
function HasSaveSlots._observeProviderValue(
|
|
737
|
+
self: HasSaveSlots,
|
|
738
|
+
name: string,
|
|
739
|
+
slotStore: DataStoreStage.DataStoreStage
|
|
740
|
+
): Observable.Observable<any>
|
|
741
|
+
return self._summaryProviders:ObserveAtKey(name):Pipe({
|
|
742
|
+
Rx.switchMap(function(provider: SummaryProvider?)
|
|
743
|
+
if not provider then
|
|
744
|
+
return Rx.of(NONE) :: any
|
|
745
|
+
end
|
|
746
|
+
|
|
747
|
+
local success, observable = pcall(provider, self._obj, slotStore)
|
|
748
|
+
if not success then
|
|
749
|
+
warn(`[HasSaveSlots] Summary provider {name} errored: {observable}`)
|
|
750
|
+
return Rx.of(NONE) :: any
|
|
751
|
+
end
|
|
752
|
+
|
|
753
|
+
if not Observable.isObservable(observable) then
|
|
754
|
+
warn(`[HasSaveSlots] Summary provider {name} did not return an Observable`)
|
|
755
|
+
return Rx.of(NONE) :: any
|
|
756
|
+
end
|
|
757
|
+
|
|
758
|
+
return (observable :: any):Pipe({
|
|
759
|
+
Rx.catchError(function(err)
|
|
760
|
+
warn(`[HasSaveSlots] Summary provider {name} stream errored: {err}`)
|
|
761
|
+
return Rx.of(NONE)
|
|
762
|
+
end) :: any,
|
|
763
|
+
})
|
|
764
|
+
end) :: any,
|
|
765
|
+
}) :: any
|
|
766
|
+
end
|
|
767
|
+
|
|
768
|
+
--[=[
|
|
769
|
+
Accrues per-slot playtime automatically. A "session" spans the time a slot is the active slot:
|
|
770
|
+
selecting a slot begins one (bumping PlayCount), and deselecting, switching, or unbinding ends
|
|
771
|
+
it. Elapsed wall time is folded into the slot's TimePlayed from a datastore saving callback, so
|
|
772
|
+
it persists on exactly the cadence the data is written -- always fresh at save time, with no
|
|
773
|
+
separate timer -- and again at each session boundary.
|
|
774
|
+
]=]
|
|
775
|
+
function HasSaveSlots._setupPlaytimeTracking(self: HasSaveSlots): ()
|
|
776
|
+
self._playSessionSlotId = nil
|
|
777
|
+
self._playSessionStart = nil
|
|
778
|
+
self._playSessionLastFlush = nil
|
|
779
|
+
|
|
780
|
+
-- The active slot bounds the session: end the previous one (if any) and begin the new one.
|
|
781
|
+
self._maid:GiveTask(self.ActiveSlotId.Changed:Connect(function()
|
|
782
|
+
self:_endPlaySession()
|
|
783
|
+
|
|
784
|
+
local activeSlotId = self.ActiveSlotId.Value
|
|
785
|
+
if activeSlotId ~= nil then
|
|
786
|
+
self:_beginPlaySession(activeSlotId)
|
|
787
|
+
end
|
|
788
|
+
end))
|
|
789
|
+
|
|
790
|
+
-- Fold accrued time into TimePlayed just before every save so the written value is current. The
|
|
791
|
+
-- callback runs before the save serializes staged data (see DataStore._syncData), so the flush is
|
|
792
|
+
-- captured by that same save -- including the final save-on-leave.
|
|
793
|
+
self._maid:GivePromise(self._loadPromise):Then(function()
|
|
794
|
+
self._maid:GiveTask(self._dataStore:AddSavingCallback(function()
|
|
795
|
+
self:_flushPlaytime()
|
|
796
|
+
end))
|
|
797
|
+
end)
|
|
798
|
+
|
|
799
|
+
-- Best-effort flush on unbind for the case where the binder tears down before that final save.
|
|
800
|
+
self._maid:GiveTask(function()
|
|
801
|
+
self:_endPlaySession()
|
|
802
|
+
end)
|
|
803
|
+
end
|
|
804
|
+
|
|
805
|
+
function HasSaveSlots._beginPlaySession(self: HasSaveSlots, slotId: SaveSlotData.SlotId): ()
|
|
806
|
+
local now = os.time()
|
|
807
|
+
self._playSessionSlotId = slotId
|
|
808
|
+
self._playSessionStart = now
|
|
809
|
+
self._playSessionLastFlush = now
|
|
810
|
+
|
|
811
|
+
local slot = self._slotMap[slotId]
|
|
812
|
+
if slot then
|
|
813
|
+
SaveSlotData.PlayCount:Set(slot, (SaveSlotData.PlayCount:Get(slot) or 0) + 1)
|
|
814
|
+
end
|
|
815
|
+
end
|
|
816
|
+
|
|
817
|
+
function HasSaveSlots._flushPlaytime(self: HasSaveSlots): ()
|
|
818
|
+
local slotId = self._playSessionSlotId
|
|
819
|
+
if slotId == nil then
|
|
820
|
+
return
|
|
821
|
+
end
|
|
822
|
+
|
|
823
|
+
local slot = self._slotMap[slotId]
|
|
824
|
+
if not slot then
|
|
825
|
+
return
|
|
826
|
+
end
|
|
827
|
+
|
|
828
|
+
local now = os.time()
|
|
829
|
+
|
|
830
|
+
-- Add only the time since the last flush so repeated flushes within a session never double count.
|
|
831
|
+
local sinceFlush = now - (self._playSessionLastFlush or now)
|
|
832
|
+
if sinceFlush > 0 then
|
|
833
|
+
SaveSlotData.TimePlayed:Set(slot, (SaveSlotData.TimePlayed:Get(slot) or 0) + sinceFlush)
|
|
834
|
+
self._playSessionLastFlush = now
|
|
835
|
+
end
|
|
836
|
+
|
|
837
|
+
SaveSlotData.LastSessionLength:Set(slot, now - (self._playSessionStart or now))
|
|
838
|
+
end
|
|
839
|
+
|
|
840
|
+
function HasSaveSlots._endPlaySession(self: HasSaveSlots): ()
|
|
841
|
+
if self._playSessionSlotId == nil then
|
|
842
|
+
return
|
|
843
|
+
end
|
|
844
|
+
|
|
845
|
+
self:_flushPlaytime()
|
|
846
|
+
self._playSessionSlotId = nil
|
|
847
|
+
self._playSessionStart = nil
|
|
848
|
+
self._playSessionLastFlush = nil
|
|
552
849
|
end
|
|
553
850
|
|
|
554
851
|
function HasSaveSlots._setupRemotes(self: HasSaveSlots): ()
|