@quenty/saveslot 1.7.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.
@@ -0,0 +1,142 @@
1
+ --!strict
2
+ --[[
3
+ Coverage for the "slotIndex"/"slotIndices" Cmdr types, focused on the "." shorthand (Cmdr's
4
+ Default callback) that resolves to the player's current slot -- the active slot, or the last one
5
+ they played. Cmdr itself is stubbed so the type's Default/Transform can be exercised directly.
6
+
7
+ @class SaveSlotCmdrUtils.spec.lua
8
+ ]]
9
+ local require = require(script.Parent.loader).load(script)
10
+
11
+ local Jest = require("Jest")
12
+ local SaveSlotCmdrUtils = require("SaveSlotCmdrUtils")
13
+
14
+ local describe = Jest.Globals.describe
15
+ local expect = Jest.Globals.expect
16
+ local it = Jest.Globals.it
17
+
18
+ -- A stand-in player. The fake data service keys off nothing, so any sentinel works.
19
+ local FAKE_PLAYER = newproxy(false)
20
+
21
+ -- Minimal SaveSlotDataService driven by a mutable `state`. Mirrors the real methods the type uses.
22
+ local function newFakeDataService(state)
23
+ return {
24
+ GetSlotList = function(_self, _player)
25
+ return state.slots
26
+ end,
27
+ GetLastActiveSlotId = function(_self, _player)
28
+ return state.lastActiveSlotId
29
+ end,
30
+ GetSlotMetadata = function(_self, _player, slotId)
31
+ for _, slot in state.slots do
32
+ if slot.SlotId == slotId then
33
+ return slot
34
+ end
35
+ end
36
+ return nil
37
+ end,
38
+ }
39
+ end
40
+
41
+ -- Enough of Cmdr for registerSlotIndexType: a substring fuzzy finder, a listable wrapper that copies
42
+ -- fields the way the real Util.MakeListableType does (so Default propagation is exercised), and a
43
+ -- registry that captures the registered definitions.
44
+ local function newFakeCmdr()
45
+ local registered: { [string]: any } = {}
46
+ local cmdr = {
47
+ Util = {
48
+ MakeFuzzyFinder = function(entries)
49
+ return function(text)
50
+ local matches = {}
51
+ for _, entry in entries do
52
+ if text == "" or string.find(entry, text, 1, true) then
53
+ table.insert(matches, entry)
54
+ end
55
+ end
56
+ return matches
57
+ end
58
+ end,
59
+ MakeListableType = function(singular)
60
+ return {
61
+ Listable = true,
62
+ Transform = singular.Transform,
63
+ Validate = singular.Validate,
64
+ Autocomplete = singular.Autocomplete,
65
+ Parse = singular.Parse,
66
+ Default = singular.Default,
67
+ }
68
+ end,
69
+ },
70
+ Registry = {
71
+ RegisterType = function(_self, name, definition)
72
+ registered[name] = definition
73
+ end,
74
+ },
75
+ }
76
+ return cmdr, registered
77
+ end
78
+
79
+ -- Registers the types against a fake data service backed by `state` and returns the registered types.
80
+ local function registerSlotIndex(state)
81
+ local cmdr, registered = newFakeCmdr()
82
+ SaveSlotCmdrUtils.registerSlotIndexType(cmdr :: any, newFakeDataService(state))
83
+ return registered.slotIndex, registered.slotIndices
84
+ end
85
+
86
+ describe("SaveSlotCmdrUtils.registerSlotIndexType", function()
87
+ it('resolves "." to the current slot\'s index via Default', function()
88
+ local slotIndex = registerSlotIndex({
89
+ lastActiveSlotId = "id-2",
90
+ slots = {
91
+ { SlotId = "id-1", SlotIndex = 1 },
92
+ { SlotId = "id-2", SlotIndex = 2 },
93
+ },
94
+ })
95
+
96
+ expect(slotIndex.Default(FAKE_PLAYER)).toBe("2")
97
+ end)
98
+
99
+ it("returns nil from Default when there is no current slot", function()
100
+ local slotIndex = registerSlotIndex({
101
+ lastActiveSlotId = nil,
102
+ slots = {},
103
+ })
104
+
105
+ expect(slotIndex.Default(FAKE_PLAYER)).toBeNil()
106
+ end)
107
+
108
+ it("returns nil from Default when the remembered slot no longer exists", function()
109
+ -- Last-active points at a deleted slot: GetSlotMetadata finds nothing, so "." resolves to nothing.
110
+ local slotIndex = registerSlotIndex({
111
+ lastActiveSlotId = "id-gone",
112
+ slots = {
113
+ { SlotId = "id-1", SlotIndex = 1 },
114
+ },
115
+ })
116
+
117
+ expect(slotIndex.Default(FAKE_PLAYER)).toBeNil()
118
+ end)
119
+
120
+ it("still fuzzy-finds a literal index through Transform/Parse", function()
121
+ local slotIndex = registerSlotIndex({
122
+ lastActiveSlotId = "id-1",
123
+ slots = {
124
+ { SlotId = "id-1", SlotIndex = 1 },
125
+ { SlotId = "id-2", SlotIndex = 2 },
126
+ },
127
+ })
128
+
129
+ expect(slotIndex.Parse(slotIndex.Transform("2", FAKE_PLAYER))).toBe(2)
130
+ end)
131
+
132
+ it('exposes Default on the listable type too, so "." works for delete-save-slot', function()
133
+ local _slotIndex, slotIndices = registerSlotIndex({
134
+ lastActiveSlotId = "id-1",
135
+ slots = {
136
+ { SlotId = "id-1", SlotIndex = 1 },
137
+ },
138
+ })
139
+
140
+ expect(slotIndices.Default(FAKE_PLAYER)).toBe("1")
141
+ end)
142
+ end)
@@ -7,19 +7,39 @@ local require = require(script.Parent.loader).load(script)
7
7
 
8
8
  local AdorneeData = require("AdorneeData")
9
9
  local AdorneeDataEntry = require("AdorneeDataEntry")
10
+ local JSONAttributeValue = require("JSONAttributeValue")
10
11
  local PropertyValue = require("PropertyValue")
11
12
 
12
13
  export type SlotId = string
13
14
 
15
+ -- A slot's Summary is the structured preview shown in a slot-selection UI, keyed by summary-provider
16
+ -- name (see HasSaveSlots.RegisterSummaryProvider). It is JSON-encoded into a single attribute, so the
17
+ -- values must be JSON-serializable.
18
+ export type SaveSlotSummary = { [string]: any }
19
+
14
20
  export type SaveSlotMetadata = {
15
21
  SlotId: SlotId,
16
22
  SlotIndex: number,
17
23
  SlotName: string?,
18
24
  CreatedTime: number?,
19
25
  LastPlayedTime: number?,
20
- Summary: string?,
26
+ Summary: SaveSlotSummary?,
27
+ -- Accrued automatically by HasSaveSlots while the slot is the active slot; see _setupPlaytimeTracking.
28
+ TimePlayed: number?, -- total seconds the slot has been actively played, across every session
29
+ PlayCount: number?, -- number of sessions (incremented each time the slot is selected)
30
+ LastSessionLength: number?, -- seconds of the current/most-recent session
21
31
  }
22
32
 
33
+ -- The Summary is a structured table JSON-encoded into one attribute. A string is also accepted so a
34
+ -- legacy plain-string Summary (from before this was structured) loads without error -- it surfaces as
35
+ -- a string until the active slot's providers regenerate it as a table.
36
+ local function isSummaryValue(value: any): (boolean, string?)
37
+ if value == nil or type(value) == "table" or type(value) == "string" then
38
+ return true
39
+ end
40
+ return false, `Summary must be a table, string, or nil, got {typeof(value)}`
41
+ end
42
+
23
43
  return AdorneeData.new({
24
44
  SlotId = AdorneeDataEntry.new("string", function(folder: Folder)
25
45
  return PropertyValue.new(folder, "Name")
@@ -28,5 +48,10 @@ return AdorneeData.new({
28
48
  SlotName = AdorneeDataEntry.optionalAttribute("string", "SlotName"),
29
49
  CreatedTime = AdorneeDataEntry.optionalAttribute("number", "CreatedTime"),
30
50
  LastPlayedTime = AdorneeDataEntry.optionalAttribute("number", "LastPlayedTime"),
31
- Summary = AdorneeDataEntry.optionalAttribute("string", "Summary"),
51
+ Summary = AdorneeDataEntry.new(isSummaryValue, function(folder: Folder)
52
+ return JSONAttributeValue.new(folder, "Summary", nil)
53
+ end),
54
+ TimePlayed = AdorneeDataEntry.optionalAttribute("number", "TimePlayed"),
55
+ PlayCount = AdorneeDataEntry.optionalAttribute("number", "PlayCount"),
56
+ LastSessionLength = AdorneeDataEntry.optionalAttribute("number", "LastSessionLength"),
32
57
  })
@@ -0,0 +1,68 @@
1
+ --!nonstrict
2
+ --[[
3
+ Covers the Summary metadata field, which is JSON-encoded into a single attribute so a structured,
4
+ provider-keyed summary survives the attribute round-trip (attributes cannot hold tables directly).
5
+
6
+ @class SaveSlotData.spec.lua
7
+ ]]
8
+ local require = require(script.Parent.loader).load(script)
9
+
10
+ local Jest = require("Jest")
11
+ local SaveSlotData = require("SaveSlotData")
12
+
13
+ local describe = Jest.Globals.describe
14
+ local expect = Jest.Globals.expect
15
+ local it = Jest.Globals.it
16
+
17
+ describe("SaveSlotData.Summary", function()
18
+ it("round-trips a structured Summary table through the attribute", function()
19
+ local folder = Instance.new("Folder")
20
+
21
+ SaveSlotData.Summary:Set(folder, { coins = 100, world = 3 })
22
+ local value = SaveSlotData.Summary:Get(folder)
23
+
24
+ expect(value.coins).toEqual(100)
25
+ expect(value.world).toEqual(3)
26
+
27
+ folder:Destroy()
28
+ end)
29
+
30
+ it("clears the Summary when set to nil", function()
31
+ local folder = Instance.new("Folder")
32
+
33
+ SaveSlotData.Summary:Set(folder, { coins = 1 })
34
+ SaveSlotData.Summary:Set(folder, nil)
35
+
36
+ expect(SaveSlotData.Summary:Get(folder)).toBeNil()
37
+
38
+ folder:Destroy()
39
+ end)
40
+
41
+ it("tolerates a legacy plain-string Summary without erroring", function()
42
+ -- Before the summary was structured it was a single string. A slot saved then still loads: the
43
+ -- value survives (as a string) until the active slot's providers regenerate it as a table.
44
+ local folder = Instance.new("Folder")
45
+
46
+ expect(function()
47
+ SaveSlotData.Summary:Set(folder, "Chapters: 1")
48
+ end).never.toThrow()
49
+
50
+ local value
51
+ expect(function()
52
+ value = SaveSlotData.Summary:Get(folder)
53
+ end).never.toThrow()
54
+ expect(value).toEqual("Chapters: 1")
55
+
56
+ folder:Destroy()
57
+ end)
58
+
59
+ it("rejects a non-serializable Summary value", function()
60
+ local folder = Instance.new("Folder")
61
+
62
+ expect(function()
63
+ SaveSlotData.Summary:Set(folder, 42 :: any)
64
+ end).toThrow()
65
+
66
+ folder:Destroy()
67
+ end)
68
+ end)
@@ -76,6 +76,20 @@ function SaveSlotDataService.GetActiveSlotId(self: SaveSlotDataService, player:
76
76
  return hasSaveSlots and hasSaveSlots.ActiveSlotId.Value
77
77
  end
78
78
 
79
+ --[=[
80
+ Returns the player's last active slot ID -- the active slot when one is selected, otherwise the
81
+ slot they last played and can continue on. This is the slot the "." shorthand resolves to in the
82
+ save-slot Cmdr commands. The synchronous counterpart to
83
+ [SaveSlotDataService.ObserveLastActiveSlotId].
84
+ ]=]
85
+ function SaveSlotDataService.GetLastActiveSlotId(self: SaveSlotDataService, player: Player): SaveSlotData.SlotId?
86
+ local hasSaveSlots = HasSaveSlotsInterface:Find(player, self._realm)
87
+ if not hasSaveSlots then
88
+ return nil
89
+ end
90
+ return hasSaveSlots.ActiveSlotId.Value or hasSaveSlots.LastActiveSlotId.Value
91
+ end
92
+
79
93
  --[=[
80
94
  Observes the player's active slot list
81
95
  ]=]