@quenty/saveslot 2.7.0 → 2.7.1

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,12 @@
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
+ ## [2.7.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@2.7.0...@quenty/saveslot@2.7.1) (2026-08-10)
7
+
8
+ ### Bug Fixes
9
+
10
+ - Carry accrued playtime onto a copied slot ([90be930](https://github.com/Quenty/NevermoreEngine/commit/90be9301629ee967f52be7ad947f3f1568b48f29))
11
+
6
12
  # [2.7.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@2.6.3...@quenty/saveslot@2.7.0) (2026-08-10)
7
13
 
8
14
  ### Bug Fixes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/saveslot",
3
- "version": "2.7.0",
3
+ "version": "2.7.1",
4
4
  "description": "PlayerDataStoreService wrapper for save slots",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -57,5 +57,5 @@
57
57
  "publishConfig": {
58
58
  "access": "public"
59
59
  },
60
- "gitHead": "445e0146b4f5fdc58d1e9b8f4e1249bd6a230249"
60
+ "gitHead": "4bd956dcbbcd2b4d50e8517343d2f9a1e2d45f8a"
61
61
  }
@@ -154,6 +154,58 @@ describe("HasSaveSlots.PromiseExportSlot / PromiseImportSlot", function()
154
154
  end)
155
155
  end)
156
156
 
157
+ it("carries accrued playtime through the export", function()
158
+ runWithContext(function(context)
159
+ local hasSaveSlots = context.hasSaveSlots
160
+
161
+ local sourceSlotId = awaitValueOf(hasSaveSlots:PromiseCreateSlot(2, { TimePlayed = 3600 }))
162
+
163
+ local export = awaitValueOf(hasSaveSlots:PromiseExportSlot(sourceSlotId))
164
+ expect(export.timePlayed).toEqual(3600)
165
+
166
+ local newSlotId = awaitValueOf(hasSaveSlots:PromiseImportSlot(export))
167
+ local metadata = awaitValueOf(hasSaveSlots:PromiseGetSlotMetadata(newSlotId))
168
+ expect(metadata.TimePlayed).toEqual(3600)
169
+ end)
170
+ end)
171
+
172
+ it("exports the live session's playtime, not just what was last saved", function()
173
+ runWithContext(function(context)
174
+ local hasSaveSlots = context.hasSaveSlots
175
+
176
+ local sourceSlotId = createSelectAndWrite(hasSaveSlots, 2)
177
+
178
+ -- Rewind the live session's clock so the export observes ~120s of unflushed play.
179
+ local tracker: any = hasSaveSlots
180
+ tracker._playSessionStart = os.time() - 120
181
+ tracker._playSessionLastFlush = os.time() - 120
182
+
183
+ local export = awaitValueOf(hasSaveSlots:PromiseExportSlot(sourceSlotId))
184
+ expect(export.timePlayed ~= nil and export.timePlayed >= 120).toEqual(true)
185
+ end)
186
+ end)
187
+
188
+ it("imports an export written before playtime was carried", function()
189
+ runWithContext(function(context)
190
+ local hasSaveSlots = context.hasSaveSlots
191
+
192
+ local newSlotId = awaitValueOf(hasSaveSlots:PromiseImportSlot({ data = { Coins = 1 }, slotName = "Hero" }))
193
+
194
+ local metadata = awaitValueOf(hasSaveSlots:PromiseGetSlotMetadata(newSlotId))
195
+ expect(metadata.SlotName).toEqual("Hero")
196
+ expect(metadata.TimePlayed).toBeNil()
197
+ end)
198
+ end)
199
+
200
+ it("refuses to import an export whose timePlayed is not a number", function()
201
+ runWithContext(function(context)
202
+ local hasSaveSlots = context.hasSaveSlots
203
+ expect(awaitResolved(hasSaveSlots:PromiseImportSlot(({ data = {}, timePlayed = "600" }) :: any))).toEqual(
204
+ false
205
+ )
206
+ end)
207
+ end)
208
+
157
209
  it("refuses to export the main slot", function()
158
210
  runWithContext(function(context)
159
211
  local hasSaveSlots = context.hasSaveSlots
@@ -42,9 +42,12 @@ local NONE = {}
42
42
 
43
43
  -- The caller-supplied fields for a new slot. SlotId and SlotIndex are assigned by PromiseCreateSlot
44
44
  -- itself (from a fresh GUID and the slotIndex argument), so they are never taken from here.
45
+ -- TimePlayed seeds the new slot's accrued playtime, which is what lets a copy of a slot (duplicate or
46
+ -- export/import) carry the playtime that belongs to the progress being copied.
45
47
  export type SaveSlotCreateMetadata = {
46
48
  SlotName: string?,
47
49
  Summary: SaveSlotData.SaveSlotSummary?,
50
+ TimePlayed: number?,
48
51
  }
49
52
 
50
53
  local HasSaveSlots = setmetatable({}, HasSaveSlotsBase)
@@ -283,6 +286,7 @@ function HasSaveSlots.PromiseCreateSlot(
283
286
  SlotName = (metadata and metadata.SlotName) or `Slot {slotIndex}`,
284
287
  CreatedTime = os.time(),
285
288
  Summary = metadata and metadata.Summary,
289
+ TimePlayed = metadata and metadata.TimePlayed,
286
290
  }
287
291
 
288
292
  self:_buildSlot(slotId, data, true)
@@ -322,6 +326,10 @@ function HasSaveSlots.PromiseExportSlot(
322
326
  return (Promise :: any).rejected("Cannot export the main slot")
323
327
  end
324
328
 
329
+ -- As in PromiseDuplicateSlot: bring the live session's accrued time into TimePlayed before it is
330
+ -- read, so an export of the active slot carries its playtime up to this moment.
331
+ self:_flushPlaytime()
332
+
325
333
  local metadata = SaveSlotData:Get(slot)
326
334
  return self:_getSlotStore(slotId):LoadAll({}):Then(function(sourceData)
327
335
  local data = if type(sourceData) == "table" then table.clone(sourceData) else {}
@@ -334,7 +342,7 @@ function HasSaveSlots.PromiseExportSlot(
334
342
  data[SaveSlotConstants.SYSTEM_STORE_KEY] = nil
335
343
  end
336
344
 
337
- return SaveSlotExportUtils.create(data, metadata.SlotName, metadata.Summary)
345
+ return SaveSlotExportUtils.create(data, metadata.SlotName, metadata.Summary, metadata.TimePlayed)
338
346
  end)
339
347
  end)
340
348
  end
@@ -376,6 +384,8 @@ function HasSaveSlots.PromiseImportSlot(
376
384
  return self:PromiseCreateSlot(freeIndex, {
377
385
  SlotName = export.slotName,
378
386
  Summary = export.summary,
387
+ -- Absent on exports taken before playtime was carried; those import as before, with no playtime.
388
+ TimePlayed = export.timePlayed,
379
389
  }):Then(function(newSlotId: SaveSlotData.SlotId)
380
390
  -- freeIndex is always > DEFAULT_SLOT_INDEX, so this store is an isolated substore
381
391
  -- (never the shared root); a plain Overwrite cannot touch system or global data.
@@ -644,8 +654,8 @@ end
644
654
 
645
655
  --[=[
646
656
  Duplicates the slot with the given ID into a new slot at the lowest free index,
647
- copying its saved data. Resolves to the new slot's id. The copy is not selected,
648
- its metadata (playtime, timestamps) starts fresh, and its name is suffixed with
657
+ copying its saved data and accrued playtime. Resolves to the new slot's id. The copy is not
658
+ selected, its timestamps and session counters start fresh, and its name is suffixed with
649
659
  " (Copy)". Rejects when the source slot is missing or every index is in use.
650
660
 
651
661
  An ephemeral slot may be duplicated: the copy is a real, persisted slot seeded with the ephemeral
@@ -666,6 +676,10 @@ function HasSaveSlots.PromiseDuplicateSlot(
666
676
 
667
677
  local sourceIsEphemeral = self:_isEphemeral(slotId)
668
678
 
679
+ -- The source is usually the live active slot, whose current session has not landed in TimePlayed
680
+ -- yet; fold it in first so the copy carries the playtime the player is actually looking at.
681
+ self:_flushPlaytime()
682
+
669
683
  -- Lowest free positive index, filling gaps left by deletions (mirrors PromiseSelectNewSaveSlot).
670
684
  local usedIndices = {}
671
685
  for existingSlotId, slot in self._slotMap do
@@ -696,6 +710,9 @@ function HasSaveSlots.PromiseDuplicateSlot(
696
710
  then sourceMetadata.SlotName
697
711
  else `{sourceMetadata.SlotName} (Copy)`,
698
712
  Summary = sourceMetadata.Summary,
713
+ -- Playtime describes the progress being copied, not the slot it happens to live in, so
714
+ -- it travels with that progress rather than resetting the copy to a never-played slot.
715
+ TimePlayed = sourceMetadata.TimePlayed,
699
716
  })
700
717
  :Then(function(newSlotId: SaveSlotData.SlotId)
701
718
  local destStore = self:_getSlotStore(newSlotId)
@@ -1273,6 +1273,53 @@ describe("HasSaveSlots playtime tracking", function()
1273
1273
 
1274
1274
  context.destroy()
1275
1275
  end)
1276
+
1277
+ it("carries the source slot's playtime onto a duplicate, live session included", function()
1278
+ local context = setup()
1279
+
1280
+ local slotId = createAndSelect(context, 1)
1281
+
1282
+ -- Rewind the live session's clock so the duplicate observes ~120s that no flush has written yet.
1283
+ local tracker: any = context.hasSaveSlots
1284
+ tracker._playSessionStart = os.time() - 120
1285
+ tracker._playSessionLastFlush = os.time() - 120
1286
+
1287
+ local duplicatePromise = context.hasSaveSlots:PromiseDuplicateSlot(slotId)
1288
+ if not PromiseTestUtils.awaitSettled(duplicatePromise, 10) then
1289
+ expect("duplicate hung").toEqual("duplicate settled")
1290
+ context.destroy()
1291
+ return
1292
+ end
1293
+ local newSlotId = duplicatePromise:Wait()
1294
+
1295
+ local copied = getMetadata(context, newSlotId).TimePlayed
1296
+ expect(copied ~= nil and copied >= 120).toEqual(true)
1297
+
1298
+ context.destroy()
1299
+ end)
1300
+
1301
+ it("leaves a duplicate of a never-played slot without playtime", function()
1302
+ local context = setup()
1303
+
1304
+ local createPromise = context.hasSaveSlots:PromiseCreateSlot(1)
1305
+ if not PromiseTestUtils.awaitSettled(createPromise, 10) then
1306
+ expect("create hung").toEqual("create settled")
1307
+ context.destroy()
1308
+ return
1309
+ end
1310
+ local _, slotId = createPromise:Yield()
1311
+
1312
+ local duplicatePromise = context.hasSaveSlots:PromiseDuplicateSlot(slotId)
1313
+ if not PromiseTestUtils.awaitSettled(duplicatePromise, 10) then
1314
+ expect("duplicate hung").toEqual("duplicate settled")
1315
+ context.destroy()
1316
+ return
1317
+ end
1318
+
1319
+ expect(getMetadata(context, duplicatePromise:Wait()).TimePlayed).toBeNil()
1320
+
1321
+ context.destroy()
1322
+ end)
1276
1323
  end)
1277
1324
 
1278
1325
  describe("HasSaveSlots summary providers", function()
@@ -14,12 +14,15 @@ local SaveSlotConstants = require("SaveSlotConstants")
14
14
  local SaveSlotExportUtils = {}
15
15
 
16
16
  -- A slot's saved data plus the presentation metadata worth carrying with it. `data` is the merged,
17
- -- serializable view of the slot's store; slotName/summary seed the imported slot's metadata so a
18
- -- restored slot is still recognizable.
17
+ -- serializable view of the slot's store; slotName/summary/timePlayed seed the imported slot's metadata
18
+ -- so a restored slot is still recognizable and still owns the playtime behind its progress. timePlayed
19
+ -- is absent from exports written before it was carried; those import with no accrued playtime, exactly
20
+ -- as they did when they were written.
19
21
  export type SaveSlotExport = {
20
22
  data: { [string]: any },
21
23
  slotName: string?,
22
24
  summary: any?,
25
+ timePlayed: number?,
23
26
  }
24
27
 
25
28
  --[=[
@@ -50,6 +53,9 @@ function SaveSlotExportUtils.isSaveSlotExport(value: any): boolean
50
53
  if value.slotName ~= nil and type(value.slotName) ~= "string" then
51
54
  return false
52
55
  end
56
+ if value.timePlayed ~= nil and type(value.timePlayed) ~= "number" then
57
+ return false
58
+ end
53
59
  return true
54
60
  end
55
61
 
@@ -59,13 +65,20 @@ end
59
65
  @param data { [string]: any }
60
66
  @param slotName string?
61
67
  @param summary any?
68
+ @param timePlayed number? -- the source slot's accrued playtime, in seconds
62
69
  @return SaveSlotExport
63
70
  ]=]
64
- function SaveSlotExportUtils.create(data: { [string]: any }, slotName: string?, summary: any?): SaveSlotExport
71
+ function SaveSlotExportUtils.create(
72
+ data: { [string]: any },
73
+ slotName: string?,
74
+ summary: any?,
75
+ timePlayed: number?
76
+ ): SaveSlotExport
65
77
  return {
66
78
  data = data,
67
79
  slotName = slotName,
68
80
  summary = summary,
81
+ timePlayed = timePlayed,
69
82
  }
70
83
  end
71
84
 
@@ -45,14 +45,23 @@ describe("SaveSlotExportUtils.isSaveSlotExport", function()
45
45
  it("rejects a non-string slotName", function()
46
46
  expect(SaveSlotExportUtils.isSaveSlotExport({ data = {}, slotName = 5 })).toEqual(false)
47
47
  end)
48
+
49
+ it("accepts an export without timePlayed, as written before playtime was carried", function()
50
+ expect(SaveSlotExportUtils.isSaveSlotExport({ data = {}, slotName = "Hero" })).toEqual(true)
51
+ end)
52
+
53
+ it("rejects a non-number timePlayed", function()
54
+ expect(SaveSlotExportUtils.isSaveSlotExport({ data = {}, timePlayed = "600" })).toEqual(false)
55
+ end)
48
56
  end)
49
57
 
50
58
  describe("SaveSlotExportUtils.create", function()
51
59
  it("carries data and metadata through and is a valid export", function()
52
- local export = SaveSlotExportUtils.create({ Coins = 3 }, "Hero", { pct = 50 })
60
+ local export = SaveSlotExportUtils.create({ Coins = 3 }, "Hero", { pct = 50 }, 600)
53
61
  expect(export.data.Coins).toEqual(3)
54
62
  expect(export.slotName).toEqual("Hero")
55
63
  expect(export.summary.pct).toEqual(50)
64
+ expect(export.timePlayed).toEqual(600)
56
65
  expect(SaveSlotExportUtils.isSaveSlotExport(export)).toEqual(true)
57
66
  end)
58
67
 
@@ -60,6 +69,7 @@ describe("SaveSlotExportUtils.create", function()
60
69
  local export = SaveSlotExportUtils.create({})
61
70
  expect(export.slotName).toBeNil()
62
71
  expect(export.summary).toBeNil()
72
+ expect(export.timePlayed).toBeNil()
63
73
  expect(SaveSlotExportUtils.isSaveSlotExport(export)).toEqual(true)
64
74
  end)
65
75
  end)