@quenty/saveslot 1.1.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 +32 -0
- package/LICENSE.md +21 -0
- package/README.md +23 -0
- package/default.project.json +14 -0
- package/package.json +55 -0
- package/src/Client/Binders/HasSaveSlotsClient.lua +128 -0
- package/src/Client/Cmdr/SaveSlotCmdrServiceClient.lua +49 -0
- package/src/Client/SaveSlotServiceClient.lua +55 -0
- package/src/Server/Binders/HasSaveSlots.lua +479 -0
- package/src/Server/Cmdr/SaveSlotCmdrService.lua +183 -0
- package/src/Server/SaveSlotService.lua +229 -0
- package/src/Shared/Cmdr/SaveSlotCmdrUtils.lua +33 -0
- package/src/Shared/Data/HasSaveSlotsData.lua +14 -0
- package/src/Shared/Data/SaveSlotData.lua +32 -0
- package/src/Shared/HasSaveSlotsBase.lua +50 -0
- package/src/Shared/HasSaveSlotsInterface.lua +29 -0
- package/src/Shared/SaveSlotConstants.lua +16 -0
- package/src/Shared/SaveSlotDataService.lua +157 -0
- package/src/node_modules.project.json +7 -0
- package/test/default.project.json +29 -0
- package/test/scripts/Client/ClientMain.client.lua +12 -0
- package/test/scripts/Server/ServerMain.server.lua +14 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
@class SaveSlotService
|
|
4
|
+
]=]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
9
|
+
|
|
10
|
+
local Brio = require("Brio")
|
|
11
|
+
local DataStoreStage = require("DataStoreStage")
|
|
12
|
+
local HasSaveSlots = require("HasSaveSlots")
|
|
13
|
+
local Maid = require("Maid")
|
|
14
|
+
local Observable = require("Observable")
|
|
15
|
+
local Promise = require("Promise")
|
|
16
|
+
local Remoting = require("Remoting")
|
|
17
|
+
local RxBrioUtils = require("RxBrioUtils")
|
|
18
|
+
local SaveSlotConstants = require("SaveSlotConstants")
|
|
19
|
+
local SaveSlotData = require("SaveSlotData")
|
|
20
|
+
local ServiceBag = require("ServiceBag")
|
|
21
|
+
|
|
22
|
+
local SaveSlotService = {}
|
|
23
|
+
SaveSlotService.ServiceName = "SaveSlotService"
|
|
24
|
+
|
|
25
|
+
export type SaveSlotService = typeof(setmetatable(
|
|
26
|
+
{} :: {
|
|
27
|
+
_serviceBag: ServiceBag.ServiceBag,
|
|
28
|
+
_maid: Maid.Maid,
|
|
29
|
+
_hasSaveSlotsBinder: any,
|
|
30
|
+
_selectionRequired: boolean,
|
|
31
|
+
_maxSlotCount: number,
|
|
32
|
+
_defaultSummaryProvider: HasSaveSlots.SaveSlotSummaryProvider?,
|
|
33
|
+
_remoting: any,
|
|
34
|
+
},
|
|
35
|
+
{} :: typeof({ __index = SaveSlotService })
|
|
36
|
+
))
|
|
37
|
+
|
|
38
|
+
function SaveSlotService.Init(self: SaveSlotService, serviceBag: ServiceBag.ServiceBag)
|
|
39
|
+
assert(not (self :: any)._serviceBag, "Already initialized")
|
|
40
|
+
self._serviceBag = assert(serviceBag, "No serviceBag")
|
|
41
|
+
self._maid = Maid.new()
|
|
42
|
+
|
|
43
|
+
-- External
|
|
44
|
+
self._serviceBag:GetService(require("PlayerDataStoreService"))
|
|
45
|
+
|
|
46
|
+
-- Internal
|
|
47
|
+
self._serviceBag:GetService(require("SaveSlotCmdrService"))
|
|
48
|
+
self._serviceBag:GetService(require("SaveSlotDataService"))
|
|
49
|
+
|
|
50
|
+
-- Binders
|
|
51
|
+
self._hasSaveSlotsBinder = self._serviceBag:GetService(HasSaveSlots)
|
|
52
|
+
|
|
53
|
+
self._selectionRequired = false
|
|
54
|
+
self._maxSlotCount = 1
|
|
55
|
+
|
|
56
|
+
self._remoting = self._maid:Add(Remoting.Server.new(ReplicatedStorage, "SaveSlotService"))
|
|
57
|
+
|
|
58
|
+
self._maid:GiveTask(self._remoting.GetExplicitSelectionRequired:Bind(function()
|
|
59
|
+
return self:GetExplicitSelectionRequired()
|
|
60
|
+
end))
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
function SaveSlotService.Start(self: SaveSlotService)
|
|
64
|
+
self._maid:GiveTask(self._hasSaveSlotsBinder:ObserveAllBrio():Subscribe(function(brio)
|
|
65
|
+
if brio:IsDead() then
|
|
66
|
+
return
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
local maid, hasSaveSlots = brio:ToMaidAndValue()
|
|
70
|
+
|
|
71
|
+
-- Pass consumer-specified configs
|
|
72
|
+
hasSaveSlots.MaxSlotCount.Value = self._maxSlotCount
|
|
73
|
+
if self._defaultSummaryProvider then
|
|
74
|
+
hasSaveSlots:SetSummaryProvider(self._defaultSummaryProvider)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
maid:GivePromise(hasSaveSlots:PromiseSlotsLoaded()):Then(function()
|
|
78
|
+
if self._selectionRequired then
|
|
79
|
+
return -- Consumer handles selection
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
-- Select last active slot
|
|
83
|
+
return hasSaveSlots:PromiseLastActiveSlotId():Then(function(lastActiveSlotId: SaveSlotData.SlotId?)
|
|
84
|
+
return hasSaveSlots:PromiseHasSlot(lastActiveSlotId):Then(function(hasLastSlot: boolean)
|
|
85
|
+
if hasLastSlot then
|
|
86
|
+
return hasSaveSlots:PromiseSelectSlot(lastActiveSlotId)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
-- Or create and select default slot
|
|
90
|
+
return hasSaveSlots
|
|
91
|
+
:PromiseSlotIdFromIndex(SaveSlotConstants.DEFAULT_SLOT_INDEX)
|
|
92
|
+
:Then(function(defaultSlotId: SaveSlotData.SlotId?)
|
|
93
|
+
if defaultSlotId then
|
|
94
|
+
return defaultSlotId
|
|
95
|
+
else
|
|
96
|
+
return hasSaveSlots:PromiseCreateSlot(SaveSlotConstants.DEFAULT_SLOT_INDEX)
|
|
97
|
+
end
|
|
98
|
+
end)
|
|
99
|
+
:Then(function(slotId: SaveSlotData.SlotId)
|
|
100
|
+
return hasSaveSlots:PromiseSelectSlot(slotId)
|
|
101
|
+
end)
|
|
102
|
+
end)
|
|
103
|
+
end)
|
|
104
|
+
end)
|
|
105
|
+
end))
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
--[=[
|
|
109
|
+
Requires explicit slot selection
|
|
110
|
+
]=]
|
|
111
|
+
function SaveSlotService.RequireExplicitSelection(self: SaveSlotService): ()
|
|
112
|
+
assert(not self._serviceBag:IsStarted(), "RequireExplicitSelection must be called before Start")
|
|
113
|
+
self._selectionRequired = true
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
--[=[
|
|
117
|
+
Returns whether explicit slot selection is required
|
|
118
|
+
]=]
|
|
119
|
+
function SaveSlotService.GetExplicitSelectionRequired(self: SaveSlotService): boolean
|
|
120
|
+
return self._selectionRequired
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
--[=[
|
|
124
|
+
Sets the max slot count
|
|
125
|
+
]=]
|
|
126
|
+
function SaveSlotService.SetMaxSlotCount(self: SaveSlotService, maxSlotCount: number): ()
|
|
127
|
+
assert(not self._serviceBag:IsStarted(), "SetMaxSlotCount must be called before Start")
|
|
128
|
+
assert(maxSlotCount >= 1, "Bad maxSlotCount")
|
|
129
|
+
self._maxSlotCount = maxSlotCount
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
--[=[
|
|
133
|
+
Sets the default slot summary provider
|
|
134
|
+
]=]
|
|
135
|
+
function SaveSlotService.SetDefaultSummaryProvider(
|
|
136
|
+
self: SaveSlotService,
|
|
137
|
+
provider: HasSaveSlots.SaveSlotSummaryProvider
|
|
138
|
+
): ()
|
|
139
|
+
assert(type(provider) == "function", "Bad provider")
|
|
140
|
+
self._defaultSummaryProvider = provider
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
--[=[
|
|
144
|
+
Observes the [DataStoreStage] for the player's active slot as a [Brio]
|
|
145
|
+
]=]
|
|
146
|
+
function SaveSlotService.ObserveActiveSlotStoreBrio(
|
|
147
|
+
self: SaveSlotService,
|
|
148
|
+
player: Player
|
|
149
|
+
): Observable.Observable<Brio.Brio<DataStoreStage.DataStoreStage>>
|
|
150
|
+
return self._hasSaveSlotsBinder:ObserveBrio(player):Pipe({
|
|
151
|
+
RxBrioUtils.switchMapBrio(function(hasSaveSlots)
|
|
152
|
+
return hasSaveSlots:ObserveActiveSlotStoreBrio()
|
|
153
|
+
end),
|
|
154
|
+
})
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
--[=[
|
|
158
|
+
Returns the [DataStoreStage] for the player's active slot
|
|
159
|
+
]=]
|
|
160
|
+
function SaveSlotService.PromiseActiveSlotStore(
|
|
161
|
+
self: SaveSlotService,
|
|
162
|
+
player: Player
|
|
163
|
+
): Promise.Promise<DataStoreStage.DataStoreStage?>
|
|
164
|
+
return self._hasSaveSlotsBinder:Promise(player):Then(function(hasSaveSlots)
|
|
165
|
+
return hasSaveSlots:PromiseActiveSlotStore()
|
|
166
|
+
end)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
--[=[
|
|
170
|
+
Returns whether the player has a slot with the given ID
|
|
171
|
+
]=]
|
|
172
|
+
function SaveSlotService.PromiseHasSlot(
|
|
173
|
+
self: SaveSlotService,
|
|
174
|
+
player: Player,
|
|
175
|
+
slotId: SaveSlotData.SlotId
|
|
176
|
+
): Promise.Promise<boolean>
|
|
177
|
+
return self._hasSaveSlotsBinder:Promise(player):Then(function(hasSaveSlots)
|
|
178
|
+
return hasSaveSlots:PromiseHasSlot(slotId)
|
|
179
|
+
end)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
--[=[
|
|
183
|
+
Selects the slot with the given ID for the player
|
|
184
|
+
]=]
|
|
185
|
+
function SaveSlotService.PromiseSelectSlot(
|
|
186
|
+
self: SaveSlotService,
|
|
187
|
+
player: Player,
|
|
188
|
+
slotId: SaveSlotData.SlotId
|
|
189
|
+
): Promise.Promise<DataStoreStage.DataStoreStage>
|
|
190
|
+
return self._hasSaveSlotsBinder:Promise(player):Then(function(hasSaveSlots)
|
|
191
|
+
return hasSaveSlots:PromiseSelectSlot(slotId)
|
|
192
|
+
end)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
--[=[
|
|
196
|
+
Creates a slot for the player at the given index
|
|
197
|
+
]=]
|
|
198
|
+
function SaveSlotService.PromiseCreateSlot(
|
|
199
|
+
self: SaveSlotService,
|
|
200
|
+
player: Player,
|
|
201
|
+
slotIndex: number,
|
|
202
|
+
metadata: SaveSlotData.SaveSlotMetadata?
|
|
203
|
+
): Promise.Promise<any>
|
|
204
|
+
return self._hasSaveSlotsBinder:Promise(player):Then(function(hasSaveSlots)
|
|
205
|
+
return hasSaveSlots:PromiseCreateSlot(slotIndex, metadata)
|
|
206
|
+
end)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
--[=[
|
|
210
|
+
Deletes the slot with the given ID for the player
|
|
211
|
+
]=]
|
|
212
|
+
function SaveSlotService.PromiseDeleteSlot(
|
|
213
|
+
self: SaveSlotService,
|
|
214
|
+
player: Player,
|
|
215
|
+
slotId: SaveSlotData.SlotId
|
|
216
|
+
): Promise.Promise<any>
|
|
217
|
+
return self._hasSaveSlotsBinder:Promise(player):Then(function(hasSaveSlots)
|
|
218
|
+
return hasSaveSlots:PromiseDeleteSlot(slotId)
|
|
219
|
+
end)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
--[=[
|
|
223
|
+
Destroys the service
|
|
224
|
+
]=]
|
|
225
|
+
function SaveSlotService.Destroy(self: SaveSlotService): ()
|
|
226
|
+
self._maid:Destroy()
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
return SaveSlotService
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
@class SaveSlotCmdrUtils
|
|
4
|
+
]=]
|
|
5
|
+
|
|
6
|
+
local SaveSlotCmdrUtils = {}
|
|
7
|
+
|
|
8
|
+
function SaveSlotCmdrUtils.registerSlotIndexType(cmdr, saveSlotDataService)
|
|
9
|
+
local slotIndex = {
|
|
10
|
+
Transform = function(text: string, player: Player)
|
|
11
|
+
local slots = saveSlotDataService:GetSlotList(player)
|
|
12
|
+
local slotIndices = {}
|
|
13
|
+
for _, metadata in slots do
|
|
14
|
+
table.insert(slotIndices, tostring(metadata.SlotIndex))
|
|
15
|
+
end
|
|
16
|
+
return cmdr.Util.MakeFuzzyFinder(slotIndices)(text)
|
|
17
|
+
end,
|
|
18
|
+
Validate = function(keys)
|
|
19
|
+
return #keys > 0, "No matching slot."
|
|
20
|
+
end,
|
|
21
|
+
Autocomplete = function(keys)
|
|
22
|
+
return keys
|
|
23
|
+
end,
|
|
24
|
+
Parse = function(keys)
|
|
25
|
+
return tonumber(keys[1])
|
|
26
|
+
end,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
cmdr.Registry:RegisterType("slotIndex", slotIndex)
|
|
30
|
+
cmdr.Registry:RegisterType("slotIndices", cmdr.Util.MakeListableType(slotIndex))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
return SaveSlotCmdrUtils
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
@class HasSaveSlotsData
|
|
4
|
+
]=]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local AdorneeData = require("AdorneeData")
|
|
9
|
+
local AdorneeDataEntry = require("AdorneeDataEntry")
|
|
10
|
+
|
|
11
|
+
return AdorneeData.new({
|
|
12
|
+
ActiveSlotId = AdorneeDataEntry.optionalAttribute("string", "ActiveSlotId"),
|
|
13
|
+
MaxSlotCount = math.huge,
|
|
14
|
+
})
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
@class SaveSlotData
|
|
4
|
+
]=]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local AdorneeData = require("AdorneeData")
|
|
9
|
+
local AdorneeDataEntry = require("AdorneeDataEntry")
|
|
10
|
+
local PropertyValue = require("PropertyValue")
|
|
11
|
+
|
|
12
|
+
export type SlotId = string
|
|
13
|
+
|
|
14
|
+
export type SaveSlotMetadata = {
|
|
15
|
+
SlotId: SlotId,
|
|
16
|
+
SlotIndex: number,
|
|
17
|
+
SlotName: string?,
|
|
18
|
+
CreatedTime: number?,
|
|
19
|
+
LastPlayedTime: number?,
|
|
20
|
+
Summary: string?,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return AdorneeData.new({
|
|
24
|
+
SlotId = AdorneeDataEntry.new("string", function(folder: Folder)
|
|
25
|
+
return PropertyValue.new(folder, "Name")
|
|
26
|
+
end),
|
|
27
|
+
SlotIndex = 0,
|
|
28
|
+
SlotName = AdorneeDataEntry.optionalAttribute("string", "SlotName"),
|
|
29
|
+
CreatedTime = AdorneeDataEntry.optionalAttribute("number", "CreatedTime"),
|
|
30
|
+
LastPlayedTime = AdorneeDataEntry.optionalAttribute("number", "LastPlayedTime"),
|
|
31
|
+
Summary = AdorneeDataEntry.optionalAttribute("string", "Summary"),
|
|
32
|
+
})
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
@class HasSaveSlotsBase
|
|
4
|
+
]=]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local BaseObject = require("BaseObject")
|
|
9
|
+
local HasSaveSlotsData = require("HasSaveSlotsData")
|
|
10
|
+
local SaveSlotData = require("SaveSlotData")
|
|
11
|
+
local ServiceBag = require("ServiceBag")
|
|
12
|
+
local Signal = require("Signal")
|
|
13
|
+
local ValueObject = require("ValueObject")
|
|
14
|
+
|
|
15
|
+
local HasSaveSlotsBase = setmetatable({}, BaseObject)
|
|
16
|
+
HasSaveSlotsBase.ClassName = "HasSaveSlotsBase"
|
|
17
|
+
HasSaveSlotsBase.__index = HasSaveSlotsBase
|
|
18
|
+
|
|
19
|
+
export type HasSaveSlotsBase =
|
|
20
|
+
typeof(setmetatable(
|
|
21
|
+
{} :: {
|
|
22
|
+
_obj: Player,
|
|
23
|
+
_serviceBag: ServiceBag.ServiceBag,
|
|
24
|
+
_attributes: any,
|
|
25
|
+
|
|
26
|
+
ActiveSlotId: ValueObject.ValueObject<SaveSlotData.SlotId?>,
|
|
27
|
+
MaxSlotCount: ValueObject.ValueObject<number>,
|
|
28
|
+
|
|
29
|
+
SlotChanged: Signal.Signal<SaveSlotData.SlotId>,
|
|
30
|
+
},
|
|
31
|
+
{} :: typeof({ __index = HasSaveSlotsBase })
|
|
32
|
+
))
|
|
33
|
+
& BaseObject.BaseObject
|
|
34
|
+
|
|
35
|
+
function HasSaveSlotsBase.new(player: Player, serviceBag: ServiceBag.ServiceBag): HasSaveSlotsBase
|
|
36
|
+
local self: HasSaveSlotsBase = setmetatable(BaseObject.new(player) :: any, HasSaveSlotsBase)
|
|
37
|
+
|
|
38
|
+
self._serviceBag = assert(serviceBag, "No serviceBag")
|
|
39
|
+
|
|
40
|
+
self._attributes = HasSaveSlotsData:Create(self._obj)
|
|
41
|
+
|
|
42
|
+
self.ActiveSlotId = self._attributes.ActiveSlotId
|
|
43
|
+
self.MaxSlotCount = self._attributes.MaxSlotCount
|
|
44
|
+
|
|
45
|
+
self.SlotChanged = self.ActiveSlotId.Changed
|
|
46
|
+
|
|
47
|
+
return self
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
return HasSaveSlotsBase
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
@class HasSaveSlotsInterface
|
|
4
|
+
]=]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local TieDefinition = require("TieDefinition")
|
|
9
|
+
|
|
10
|
+
return TieDefinition.new("HasSaveSlots", {
|
|
11
|
+
ActiveSlotId = TieDefinition.Types.PROPERTY,
|
|
12
|
+
MaxSlotCount = TieDefinition.Types.PROPERTY,
|
|
13
|
+
|
|
14
|
+
PromiseHasSlot = TieDefinition.Types.METHOD,
|
|
15
|
+
PromiseSelectSlot = TieDefinition.Types.METHOD,
|
|
16
|
+
PromiseCreateSlot = TieDefinition.Types.METHOD,
|
|
17
|
+
PromiseDeleteSlot = TieDefinition.Types.METHOD,
|
|
18
|
+
PromiseSetSlotMetadata = TieDefinition.Types.METHOD,
|
|
19
|
+
PromiseGetSlotMetadata = TieDefinition.Types.METHOD,
|
|
20
|
+
PromiseSlotIdFromIndex = TieDefinition.Types.METHOD,
|
|
21
|
+
PromiseLastActiveSlotId = TieDefinition.Types.METHOD,
|
|
22
|
+
|
|
23
|
+
SlotChanged = TieDefinition.Types.SIGNAL,
|
|
24
|
+
|
|
25
|
+
[TieDefinition.Realms.SERVER] = {
|
|
26
|
+
ObserveActiveSlotStoreBrio = TieDefinition.Types.METHOD,
|
|
27
|
+
PromiseActiveSlotStore = TieDefinition.Types.METHOD,
|
|
28
|
+
},
|
|
29
|
+
})
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
@class SaveSlotConstants
|
|
4
|
+
]=]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local Table = require("Table")
|
|
9
|
+
|
|
10
|
+
return Table.readonly({
|
|
11
|
+
SYSTEM_STORE_KEY = "SaveSlots",
|
|
12
|
+
SLOT_STORE_KEY = "slots",
|
|
13
|
+
METADATA_STORE_KEY = "slotMetadata",
|
|
14
|
+
METADATA_CONTAINER_NAME = "SaveSlots",
|
|
15
|
+
DEFAULT_SLOT_INDEX = 1,
|
|
16
|
+
})
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
@class SaveSlotDataService
|
|
4
|
+
]=]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local HasSaveSlotsInterface = require("HasSaveSlotsInterface")
|
|
9
|
+
local Observable = require("Observable")
|
|
10
|
+
local Rx = require("Rx")
|
|
11
|
+
local RxBrioUtils = require("RxBrioUtils")
|
|
12
|
+
local RxInstanceUtils = require("RxInstanceUtils")
|
|
13
|
+
local SaveSlotConstants = require("SaveSlotConstants")
|
|
14
|
+
local SaveSlotData = require("SaveSlotData")
|
|
15
|
+
local ServiceBag = require("ServiceBag")
|
|
16
|
+
local TieRealmService = require("TieRealmService")
|
|
17
|
+
local TieRealms = require("TieRealms")
|
|
18
|
+
|
|
19
|
+
local SaveSlotDataService = {}
|
|
20
|
+
SaveSlotDataService.ServiceName = "SaveSlotDataService"
|
|
21
|
+
|
|
22
|
+
export type SaveSlotDataService = typeof(setmetatable(
|
|
23
|
+
{} :: {
|
|
24
|
+
_serviceBag: ServiceBag.ServiceBag,
|
|
25
|
+
_tieRealmService: TieRealmService.TieRealmService,
|
|
26
|
+
_realm: TieRealms.TieRealm,
|
|
27
|
+
},
|
|
28
|
+
{} :: typeof({ __index = SaveSlotDataService })
|
|
29
|
+
))
|
|
30
|
+
|
|
31
|
+
function SaveSlotDataService.Init(self: SaveSlotDataService, serviceBag: ServiceBag.ServiceBag)
|
|
32
|
+
assert(not (self :: any)._serviceBag, "Already initialized")
|
|
33
|
+
self._serviceBag = assert(serviceBag, "No serviceBag")
|
|
34
|
+
|
|
35
|
+
-- External
|
|
36
|
+
self._tieRealmService = self._serviceBag:GetService(TieRealmService) :: any
|
|
37
|
+
|
|
38
|
+
self._realm = self._tieRealmService:GetTieRealm()
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
--[=[
|
|
42
|
+
Observes the player's active slot ID
|
|
43
|
+
]=]
|
|
44
|
+
function SaveSlotDataService.ObserveActiveSlotId(
|
|
45
|
+
self: SaveSlotDataService,
|
|
46
|
+
player: Player
|
|
47
|
+
): Observable.Observable<SaveSlotData.SlotId?>
|
|
48
|
+
return (HasSaveSlotsInterface:ObserveBrio(player, self._realm) :: any):Pipe({
|
|
49
|
+
RxBrioUtils.switchMapBrio(function(hasSaveSlots)
|
|
50
|
+
return hasSaveSlots.ActiveSlotId:Observe()
|
|
51
|
+
end),
|
|
52
|
+
RxBrioUtils.emitOnDeath(nil),
|
|
53
|
+
})
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
--[=[
|
|
57
|
+
Returns the player's active slot ID
|
|
58
|
+
]=]
|
|
59
|
+
function SaveSlotDataService.GetActiveSlotId(self: SaveSlotDataService, player: Player): SaveSlotData.SlotId?
|
|
60
|
+
local hasSaveSlots = HasSaveSlotsInterface:Find(player, self._realm)
|
|
61
|
+
return hasSaveSlots and hasSaveSlots.ActiveSlotId.Value
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
--[=[
|
|
65
|
+
Observes the player's active slot list
|
|
66
|
+
]=]
|
|
67
|
+
function SaveSlotDataService.ObserveSlotList(
|
|
68
|
+
_self: SaveSlotDataService,
|
|
69
|
+
player: Player
|
|
70
|
+
): Observable.Observable<{ SaveSlotData.SaveSlotMetadata }?>
|
|
71
|
+
return (
|
|
72
|
+
RxInstanceUtils.observeLastNamedChildBrio(player, "Folder", SaveSlotConstants.METADATA_CONTAINER_NAME) :: any
|
|
73
|
+
):Pipe({
|
|
74
|
+
RxBrioUtils.switchMapBrio(function(slotContainer: Folder)
|
|
75
|
+
return RxInstanceUtils.observeChildrenBrio(slotContainer):Pipe({
|
|
76
|
+
RxBrioUtils.flatMapBrio(function(slotFolder)
|
|
77
|
+
return SaveSlotData:Observe(slotFolder)
|
|
78
|
+
end) :: any,
|
|
79
|
+
RxBrioUtils.reduceToAliveList() :: any,
|
|
80
|
+
})
|
|
81
|
+
end),
|
|
82
|
+
RxBrioUtils.emitOnDeath(nil),
|
|
83
|
+
})
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
--[=[
|
|
87
|
+
Returns the player's slot list
|
|
88
|
+
]=]
|
|
89
|
+
function SaveSlotDataService.GetSlotList(_self: SaveSlotDataService, player: Player): { SaveSlotData.SaveSlotMetadata }
|
|
90
|
+
local slotList = {}
|
|
91
|
+
|
|
92
|
+
local slotContainer = player:FindFirstChild(SaveSlotConstants.METADATA_CONTAINER_NAME)
|
|
93
|
+
if slotContainer then
|
|
94
|
+
for _, slot in slotContainer:GetChildren() do
|
|
95
|
+
table.insert(slotList, SaveSlotData:Get(slot))
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
return slotList
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
--[=[
|
|
103
|
+
Observes the slot metadata with the given ID for the player
|
|
104
|
+
]=]
|
|
105
|
+
function SaveSlotDataService.ObserveSlotMetadata(
|
|
106
|
+
_self: SaveSlotDataService,
|
|
107
|
+
player: Player,
|
|
108
|
+
slotId: SaveSlotData.SlotId
|
|
109
|
+
): Observable.Observable<SaveSlotData.SaveSlotMetadata?>
|
|
110
|
+
return (
|
|
111
|
+
RxInstanceUtils.observeLastNamedChildBrio(player, "Folder", SaveSlotConstants.METADATA_CONTAINER_NAME) :: any
|
|
112
|
+
):Pipe({
|
|
113
|
+
RxBrioUtils.switchMapBrio(function(slotContainer: Folder)
|
|
114
|
+
return RxInstanceUtils.observeLastNamedChildBrio(slotContainer, "Folder", slotId)
|
|
115
|
+
end),
|
|
116
|
+
RxBrioUtils.emitOnDeath(nil),
|
|
117
|
+
Rx.switchMap(function(slot: Folder?)
|
|
118
|
+
return slot and SaveSlotData:Observe(slot) or Rx.EMPTY
|
|
119
|
+
end),
|
|
120
|
+
})
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
--[=[
|
|
124
|
+
Returns the slot metadata with the given ID for the player
|
|
125
|
+
]=]
|
|
126
|
+
function SaveSlotDataService.GetSlotMetadata(
|
|
127
|
+
_self: SaveSlotDataService,
|
|
128
|
+
player: Player,
|
|
129
|
+
slotId: SaveSlotData.SlotId
|
|
130
|
+
): SaveSlotData.SaveSlotMetadata?
|
|
131
|
+
local slotContainer = player:FindFirstChild(SaveSlotConstants.METADATA_CONTAINER_NAME)
|
|
132
|
+
local slot = slotContainer and slotContainer:FindFirstChild(slotId)
|
|
133
|
+
|
|
134
|
+
if slot then
|
|
135
|
+
return SaveSlotData:Get(slot)
|
|
136
|
+
else
|
|
137
|
+
return nil
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
--[=[
|
|
142
|
+
Returns the ID for the slot at the given index
|
|
143
|
+
]=]
|
|
144
|
+
function SaveSlotDataService.GetSlotIdFromIndex(
|
|
145
|
+
self: SaveSlotDataService,
|
|
146
|
+
player: Player,
|
|
147
|
+
slotIndex: number
|
|
148
|
+
): SaveSlotData.SlotId?
|
|
149
|
+
for _, slot in self:GetSlotList(player) do
|
|
150
|
+
if slotIndex == slot.SlotIndex then
|
|
151
|
+
return slot.SlotId
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
return nil
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
return SaveSlotDataService
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "SaveSlotTest",
|
|
3
|
+
"globIgnorePaths": [
|
|
4
|
+
"**/.package-lock.json",
|
|
5
|
+
"**/.pnpm",
|
|
6
|
+
"**/.pnpm-workspace-state-v1.json",
|
|
7
|
+
"**/.modules.yaml",
|
|
8
|
+
"**/.ignored",
|
|
9
|
+
"**/.ignored_*"
|
|
10
|
+
],
|
|
11
|
+
"tree": {
|
|
12
|
+
"$className": "DataModel",
|
|
13
|
+
"ServerScriptService": {
|
|
14
|
+
"saveslot": {
|
|
15
|
+
"$path": ".."
|
|
16
|
+
},
|
|
17
|
+
"Script": {
|
|
18
|
+
"$path": "scripts/Server"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"StarterPlayer": {
|
|
22
|
+
"StarterPlayerScripts": {
|
|
23
|
+
"Main": {
|
|
24
|
+
"$path": "scripts/Client"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
--!nonstrict
|
|
2
|
+
--[[
|
|
3
|
+
@class ClientMain
|
|
4
|
+
]]
|
|
5
|
+
|
|
6
|
+
local loader = game:GetService("ReplicatedStorage"):WaitForChild("saveslot"):WaitForChild("loader")
|
|
7
|
+
local require = require(loader).bootstrapGame(loader.Parent)
|
|
8
|
+
|
|
9
|
+
local serviceBag = require("ServiceBag").new()
|
|
10
|
+
serviceBag:GetService(require("SaveSlotServiceClient"))
|
|
11
|
+
serviceBag:Init()
|
|
12
|
+
serviceBag:Start()
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
--!nonstrict
|
|
2
|
+
--[[
|
|
3
|
+
@class ServerMain
|
|
4
|
+
]]
|
|
5
|
+
|
|
6
|
+
local ServerScriptService = game:GetService("ServerScriptService")
|
|
7
|
+
|
|
8
|
+
local loader = ServerScriptService:FindFirstChild("LoaderUtils", true).Parent
|
|
9
|
+
local require = require(loader).bootstrapGame(ServerScriptService.saveslot)
|
|
10
|
+
|
|
11
|
+
local serviceBag = require("ServiceBag").new()
|
|
12
|
+
serviceBag:GetService(require("SaveSlotService"))
|
|
13
|
+
serviceBag:Init()
|
|
14
|
+
serviceBag:Start()
|