@quenty/saveslot 2.7.0 → 2.8.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.
@@ -176,7 +176,7 @@ describe("SaveSlotCmdrUtils.registerSlotIndexType", function()
176
176
  expect((slotIndex.Validate(slotIndex.Transform("-2", FAKE_PLAYER)))).toBe(false)
177
177
  end)
178
178
 
179
- it('exposes Default on the listable type too, so "." works for delete-save-slot', function()
179
+ it('exposes Default on the listable type too, so "." works for saveslot-delete', function()
180
180
  local _slotIndex, slotIndices = registerSlotIndex({
181
181
  lastActiveSlotId = "id-1",
182
182
  slots = {
@@ -187,3 +187,70 @@ describe("SaveSlotCmdrUtils.registerSlotIndexType", function()
187
187
  expect(slotIndices.Default(FAKE_PLAYER)).toBe("1")
188
188
  end)
189
189
  end)
190
+
191
+ describe("SaveSlotCmdrUtils.formatValue", function()
192
+ it("prints whole numbers without the float tail datastores round-trip them with", function()
193
+ expect(SaveSlotCmdrUtils.formatValue(42.0)).toBe("42")
194
+ expect(SaveSlotCmdrUtils.formatValue(1.5)).toBe("1.50")
195
+ end)
196
+
197
+ it("prints a map with sorted keys, so repeated listings read the same", function()
198
+ expect(SaveSlotCmdrUtils.formatValue({ world = 3, coins = 100 })).toBe("{coins = 100, world = 3}")
199
+ end)
200
+
201
+ it("prints an array as a list", function()
202
+ expect(SaveSlotCmdrUtils.formatValue({ "a", "b" })).toBe("[a, b]")
203
+ end)
204
+
205
+ it("collapses past the depth limit rather than unfolding forever", function()
206
+ expect(SaveSlotCmdrUtils.formatValue({ a = { b = { c = { d = { e = 1 } } } } })).toBe("{a = {b = {c = ...}}}")
207
+ end)
208
+ end)
209
+
210
+ describe("SaveSlotCmdrUtils.formatSummaryLines", function()
211
+ it("prints one line per provider, unwrapping the provider's own table", function()
212
+ expect(SaveSlotCmdrUtils.formatSummaryLines({
213
+ progress = { chapter = 3, percent = 42 },
214
+ currency = 1200,
215
+ })).toEqual({
216
+ "currency: 1200",
217
+ "progress: chapter = 3, percent = 42",
218
+ })
219
+ end)
220
+
221
+ it("passes a legacy plain-string summary straight through", function()
222
+ expect(SaveSlotCmdrUtils.formatSummaryLines("Chapters: 1")).toEqual({ "Chapters: 1" })
223
+ end)
224
+
225
+ it("has nothing to say about a missing summary", function()
226
+ expect(SaveSlotCmdrUtils.formatSummaryLines(nil)).toEqual({})
227
+ end)
228
+ end)
229
+
230
+ describe("SaveSlotCmdrUtils.formatSlotBlock", function()
231
+ local NOW = 1_754_000_000
232
+
233
+ it("prints the summary, playtime and timestamps a listing is read for", function()
234
+ local block = SaveSlotCmdrUtils.formatSlotBlock({
235
+ SlotId = "id-1",
236
+ SlotIndex = 1,
237
+ SlotName = "Alpha",
238
+ CreatedTime = NOW - 86400 * 3,
239
+ LastPlayedTime = NOW - 3600 * 2,
240
+ TimePlayed = 5000,
241
+ PlayCount = 4,
242
+ Summary = { progress = { chapter = 3 } },
243
+ }, "Active", NOW)
244
+
245
+ expect(block).toBe(table.concat({
246
+ '"Alpha" (1) — Active',
247
+ " played 1h 23m, 4 session(s), last played 2025-07-31 20:13 UTC (2h ago)",
248
+ " created 2025-07-28 22:13 UTC (3d ago)",
249
+ " progress: chapter = 3",
250
+ }, "\n"))
251
+ end)
252
+
253
+ it("prints a never-played slot as its header alone, rather than empty detail lines", function()
254
+ expect(SaveSlotCmdrUtils.formatSlotBlock({ SlotId = "id-2", SlotIndex = 2 }, nil, NOW)).toBe('"Slot 2" (2)')
255
+ end)
256
+ end)
@@ -10,16 +10,42 @@
10
10
  local require = require(script.Parent.loader).load(script)
11
11
 
12
12
  local SaveSlotConstants = require("SaveSlotConstants")
13
+ local Table = require("Table")
13
14
 
14
15
  local SaveSlotExportUtils = {}
15
16
 
17
+ --[=[
18
+ What an entry in the shared store may be loaded as.
19
+
20
+ The two exist because one of the shared store's readers takes its key from the client: a teleport
21
+ arrives carrying the key of the transferable ephemeral slot it is resuming, and the arrival path
22
+ loads whatever is behind it. A share code handed to a friend must not be loadable that way, so the
23
+ writer records which of the two it wrote and each reader accepts only its own kind.
24
+
25
+ @interface SaveSlotExportKind
26
+ .CODE "code" -- a share code, redeemable into a slot the owner keeps
27
+ .TRANSFER "transfer" -- a live snapshot carried across a teleport, loadable from a client-presented key
28
+ @within SaveSlotExportUtils
29
+ ]=]
30
+ export type SaveSlotExportKind = "code" | "transfer"
31
+
32
+ SaveSlotExportUtils.Kind = Table.readonly({
33
+ CODE = "code" :: SaveSlotExportKind,
34
+ TRANSFER = "transfer" :: SaveSlotExportKind,
35
+ })
36
+
16
37
  -- 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.
38
+ -- serializable view of the slot's store; slotName/summary/timePlayed seed the imported slot's metadata
39
+ -- so a restored slot is still recognizable and still owns the playtime behind its progress. timePlayed
40
+ -- is absent from exports written before it was carried; those import with no accrued playtime, exactly
41
+ -- as they did when they were written. `kind` is absent on entries written before kinds existed; see
42
+ -- [SaveSlotExportUtils.canLoadAs] for how those are read.
19
43
  export type SaveSlotExport = {
20
44
  data: { [string]: any },
21
45
  slotName: string?,
22
46
  summary: any?,
47
+ timePlayed: number?,
48
+ kind: SaveSlotExportKind?,
23
49
  }
24
50
 
25
51
  --[=[
@@ -50,22 +76,73 @@ function SaveSlotExportUtils.isSaveSlotExport(value: any): boolean
50
76
  if value.slotName ~= nil and type(value.slotName) ~= "string" then
51
77
  return false
52
78
  end
79
+ if value.timePlayed ~= nil and type(value.timePlayed) ~= "number" then
80
+ return false
81
+ end
82
+ if
83
+ value.kind ~= nil
84
+ and value.kind ~= SaveSlotExportUtils.Kind.CODE
85
+ and value.kind ~= SaveSlotExportUtils.Kind.TRANSFER
86
+ then
87
+ return false
88
+ end
53
89
  return true
54
90
  end
55
91
 
92
+ --[=[
93
+ Returns whether an entry read from the shared store may be loaded as the given kind.
94
+
95
+ Entries written before kinds existed carry none, and are treated as share codes: back then the
96
+ transfer path re-saved under the very key the code was minted as, so an untagged entry is
97
+ indistinguishable from the code it started life as. Old codes therefore keep working, while the
98
+ arrival path -- the one reading a client-presented key -- accepts nothing that was not explicitly
99
+ written as a transfer.
100
+
101
+ @param export SaveSlotExport
102
+ @param kind SaveSlotExportKind
103
+ @return boolean
104
+ ]=]
105
+ function SaveSlotExportUtils.canLoadAs(export: SaveSlotExport, kind: SaveSlotExportKind): boolean
106
+ if export.kind == nil then
107
+ return kind == SaveSlotExportUtils.Kind.CODE
108
+ end
109
+
110
+ return export.kind == kind
111
+ end
112
+
113
+ --[=[
114
+ Returns a copy of the export tagged as the given kind, for writing to the shared store.
115
+
116
+ @param export SaveSlotExport
117
+ @param kind SaveSlotExportKind
118
+ @return SaveSlotExport
119
+ ]=]
120
+ function SaveSlotExportUtils.withKind(export: SaveSlotExport, kind: SaveSlotExportKind): SaveSlotExport
121
+ local copy = table.clone(export)
122
+ copy.kind = kind
123
+ return copy
124
+ end
125
+
56
126
  --[=[
57
127
  Builds a [SaveSlotExport] from a slot's data table and its metadata.
58
128
 
59
129
  @param data { [string]: any }
60
130
  @param slotName string?
61
131
  @param summary any?
132
+ @param timePlayed number? -- the source slot's accrued playtime, in seconds
62
133
  @return SaveSlotExport
63
134
  ]=]
64
- function SaveSlotExportUtils.create(data: { [string]: any }, slotName: string?, summary: any?): SaveSlotExport
135
+ function SaveSlotExportUtils.create(
136
+ data: { [string]: any },
137
+ slotName: string?,
138
+ summary: any?,
139
+ timePlayed: number?
140
+ ): SaveSlotExport
65
141
  return {
66
142
  data = data,
67
143
  slotName = slotName,
68
144
  summary = summary,
145
+ timePlayed = timePlayed,
69
146
  }
70
147
  end
71
148
 
@@ -45,14 +45,59 @@ 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)
56
+
57
+ it("rejects a kind that is neither of the two", function()
58
+ expect(SaveSlotExportUtils.isSaveSlotExport({ data = {}, kind = "whatever" })).toEqual(false)
59
+ expect(SaveSlotExportUtils.isSaveSlotExport({ data = {}, kind = 5 })).toEqual(false)
60
+ end)
61
+ end)
62
+
63
+ describe("SaveSlotExportUtils.canLoadAs", function()
64
+ it("matches the kind it was written as", function()
65
+ local code = SaveSlotExportUtils.withKind({ data = {} }, SaveSlotExportUtils.Kind.CODE)
66
+ expect(SaveSlotExportUtils.canLoadAs(code, SaveSlotExportUtils.Kind.CODE)).toEqual(true)
67
+ expect(SaveSlotExportUtils.canLoadAs(code, SaveSlotExportUtils.Kind.TRANSFER)).toEqual(false)
68
+
69
+ local transfer = SaveSlotExportUtils.withKind({ data = {} }, SaveSlotExportUtils.Kind.TRANSFER)
70
+ expect(SaveSlotExportUtils.canLoadAs(transfer, SaveSlotExportUtils.Kind.TRANSFER)).toEqual(true)
71
+ expect(SaveSlotExportUtils.canLoadAs(transfer, SaveSlotExportUtils.Kind.CODE)).toEqual(false)
72
+ end)
73
+
74
+ it("reads an untagged entry as a code, so old codes keep working and none become transfers", function()
75
+ local legacy = { data = {} }
76
+ expect(SaveSlotExportUtils.canLoadAs(legacy, SaveSlotExportUtils.Kind.CODE)).toEqual(true)
77
+ expect(SaveSlotExportUtils.canLoadAs(legacy, SaveSlotExportUtils.Kind.TRANSFER)).toEqual(false)
78
+ end)
79
+ end)
80
+
81
+ describe("SaveSlotExportUtils.withKind", function()
82
+ it("tags a copy, leaving the export it was given alone", function()
83
+ local export = SaveSlotExportUtils.create({ Coins = 3 }, "Hero")
84
+ local tagged = SaveSlotExportUtils.withKind(export, SaveSlotExportUtils.Kind.TRANSFER)
85
+
86
+ expect(tagged.kind).toEqual(SaveSlotExportUtils.Kind.TRANSFER)
87
+ expect(tagged.data.Coins).toEqual(3)
88
+ expect(tagged.slotName).toEqual("Hero")
89
+ expect(export.kind).toBeNil()
90
+ expect(SaveSlotExportUtils.isSaveSlotExport(tagged)).toEqual(true)
91
+ end)
48
92
  end)
49
93
 
50
94
  describe("SaveSlotExportUtils.create", function()
51
95
  it("carries data and metadata through and is a valid export", function()
52
- local export = SaveSlotExportUtils.create({ Coins = 3 }, "Hero", { pct = 50 })
96
+ local export = SaveSlotExportUtils.create({ Coins = 3 }, "Hero", { pct = 50 }, 600)
53
97
  expect(export.data.Coins).toEqual(3)
54
98
  expect(export.slotName).toEqual("Hero")
55
99
  expect(export.summary.pct).toEqual(50)
100
+ expect(export.timePlayed).toEqual(600)
56
101
  expect(SaveSlotExportUtils.isSaveSlotExport(export)).toEqual(true)
57
102
  end)
58
103
 
@@ -60,6 +105,7 @@ describe("SaveSlotExportUtils.create", function()
60
105
  local export = SaveSlotExportUtils.create({})
61
106
  expect(export.slotName).toBeNil()
62
107
  expect(export.summary).toBeNil()
108
+ expect(export.timePlayed).toBeNil()
63
109
  expect(SaveSlotExportUtils.isSaveSlotExport(export)).toEqual(true)
64
110
  end)
65
111
  end)