@quenty/saveslot 2.7.1 → 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,19 +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
38
  -- serializable view of the slot's store; slotName/summary/timePlayed seed the imported slot's metadata
18
39
  -- so a restored slot is still recognizable and still owns the playtime behind its progress. timePlayed
19
40
  -- is absent from exports written before it was carried; those import with no accrued playtime, exactly
20
- -- as they did when they were written.
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.
21
43
  export type SaveSlotExport = {
22
44
  data: { [string]: any },
23
45
  slotName: string?,
24
46
  summary: any?,
25
47
  timePlayed: number?,
48
+ kind: SaveSlotExportKind?,
26
49
  }
27
50
 
28
51
  --[=[
@@ -56,9 +79,50 @@ function SaveSlotExportUtils.isSaveSlotExport(value: any): boolean
56
79
  if value.timePlayed ~= nil and type(value.timePlayed) ~= "number" then
57
80
  return false
58
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
59
89
  return true
60
90
  end
61
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
+
62
126
  --[=[
63
127
  Builds a [SaveSlotExport] from a slot's data table and its metadata.
64
128
 
@@ -53,6 +53,42 @@ describe("SaveSlotExportUtils.isSaveSlotExport", function()
53
53
  it("rejects a non-number timePlayed", function()
54
54
  expect(SaveSlotExportUtils.isSaveSlotExport({ data = {}, timePlayed = "600" })).toEqual(false)
55
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)
56
92
  end)
57
93
 
58
94
  describe("SaveSlotExportUtils.create", function()