@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,479 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
@class HasSaveSlots
|
|
4
|
+
]=]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local HttpService = game:GetService("HttpService")
|
|
9
|
+
|
|
10
|
+
local Binder = require("Binder")
|
|
11
|
+
local Brio = require("Brio")
|
|
12
|
+
local DataStoreStage = require("DataStoreStage")
|
|
13
|
+
local HasSaveSlotsBase = require("HasSaveSlotsBase")
|
|
14
|
+
local HasSaveSlotsInterface = require("HasSaveSlotsInterface")
|
|
15
|
+
local Maid = require("Maid")
|
|
16
|
+
local Observable = require("Observable")
|
|
17
|
+
local PlayerBinder = require("PlayerBinder")
|
|
18
|
+
local PlayerDataStoreService = require("PlayerDataStoreService")
|
|
19
|
+
local Promise = require("Promise")
|
|
20
|
+
local Remoting = require("Remoting")
|
|
21
|
+
local Rx = require("Rx")
|
|
22
|
+
local RxBrioUtils = require("RxBrioUtils")
|
|
23
|
+
local SaveSlotConstants = require("SaveSlotConstants")
|
|
24
|
+
local SaveSlotData = require("SaveSlotData")
|
|
25
|
+
local ServiceBag = require("ServiceBag")
|
|
26
|
+
local ValueObject = require("ValueObject")
|
|
27
|
+
|
|
28
|
+
export type SaveSlotSummaryProvider = (Player, any) -> Observable.Observable<string>
|
|
29
|
+
|
|
30
|
+
local HasSaveSlots = setmetatable({}, HasSaveSlotsBase)
|
|
31
|
+
HasSaveSlots.ClassName = "HasSaveSlots"
|
|
32
|
+
HasSaveSlots.__index = HasSaveSlots
|
|
33
|
+
|
|
34
|
+
export type HasSaveSlots =
|
|
35
|
+
typeof(setmetatable(
|
|
36
|
+
{} :: {
|
|
37
|
+
_obj: Player,
|
|
38
|
+
_serviceBag: ServiceBag.ServiceBag,
|
|
39
|
+
_playerDataStoreService: any,
|
|
40
|
+
_slotContainer: Folder,
|
|
41
|
+
_slotMap: { [SaveSlotData.SlotId]: Folder },
|
|
42
|
+
_loadPromise: Promise.Promise<{}>,
|
|
43
|
+
_remoting: any,
|
|
44
|
+
_dataStore: any,
|
|
45
|
+
_systemStore: any,
|
|
46
|
+
_metadataStore: any,
|
|
47
|
+
_summaryProvider: ValueObject.ValueObject<SaveSlotSummaryProvider?>,
|
|
48
|
+
_lastActiveSlotId: SaveSlotData.SlotId?,
|
|
49
|
+
},
|
|
50
|
+
{} :: typeof({ __index = HasSaveSlots })
|
|
51
|
+
))
|
|
52
|
+
& HasSaveSlotsBase.HasSaveSlotsBase
|
|
53
|
+
|
|
54
|
+
function HasSaveSlots.new(player: Player, serviceBag: ServiceBag.ServiceBag): HasSaveSlots
|
|
55
|
+
local self: HasSaveSlots = setmetatable(HasSaveSlotsBase.new(player, serviceBag) :: any, HasSaveSlots)
|
|
56
|
+
|
|
57
|
+
self._serviceBag = assert(serviceBag, "No serviceBag")
|
|
58
|
+
self._playerDataStoreService = self._serviceBag:GetService(PlayerDataStoreService)
|
|
59
|
+
|
|
60
|
+
self._slotContainer = self._maid:Add(Instance.new("Folder"))
|
|
61
|
+
self._slotContainer.Name = SaveSlotConstants.METADATA_CONTAINER_NAME
|
|
62
|
+
self._slotContainer.Archivable = false
|
|
63
|
+
self._slotContainer.Parent = self._obj
|
|
64
|
+
|
|
65
|
+
self._slotMap = {}
|
|
66
|
+
|
|
67
|
+
self._summaryProvider = self._maid:Add(ValueObject.new(nil))
|
|
68
|
+
|
|
69
|
+
self._loadPromise = self._maid:GivePromise(self:_promiseLoadSlots())
|
|
70
|
+
|
|
71
|
+
self._remoting = self._maid:Add(Remoting.Server.new(self._obj, "HasSaveSlots"))
|
|
72
|
+
|
|
73
|
+
self:_setupSummary()
|
|
74
|
+
self:_setupRemotes()
|
|
75
|
+
|
|
76
|
+
self._maid:GiveTask(HasSaveSlotsInterface.Server:Implement(self._obj, self))
|
|
77
|
+
|
|
78
|
+
return self
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
--[=[
|
|
82
|
+
Observes the [DataStoreStage] for the active slot as a [Brio]
|
|
83
|
+
]=]
|
|
84
|
+
function HasSaveSlots.ObserveActiveSlotStoreBrio(
|
|
85
|
+
self: HasSaveSlots
|
|
86
|
+
): Observable.Observable<Brio.Brio<DataStoreStage.DataStoreStage>>
|
|
87
|
+
return Rx.fromPromise(self._loadPromise):Pipe({
|
|
88
|
+
Rx.switchMap(function()
|
|
89
|
+
return self.ActiveSlotId
|
|
90
|
+
:ObserveBrio(function(slotId: SaveSlotData.SlotId?)
|
|
91
|
+
return (slotId ~= nil)
|
|
92
|
+
end)
|
|
93
|
+
:Pipe({
|
|
94
|
+
RxBrioUtils.map(function(slotId: SaveSlotData.SlotId)
|
|
95
|
+
return self:_getSlotStore(slotId)
|
|
96
|
+
end) :: any,
|
|
97
|
+
}) :: any
|
|
98
|
+
end) :: any,
|
|
99
|
+
}) :: any
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
--[=[
|
|
103
|
+
Returns the [DataStoreStage] for the active slot
|
|
104
|
+
]=]
|
|
105
|
+
function HasSaveSlots.PromiseActiveSlotStore(self: HasSaveSlots): Promise.Promise<DataStoreStage.DataStoreStage?>
|
|
106
|
+
return (self._loadPromise :: any):Then(function()
|
|
107
|
+
print(self.ActiveSlotId.Value)
|
|
108
|
+
if not self.ActiveSlotId.Value then
|
|
109
|
+
return (Promise :: any).resolved(nil)
|
|
110
|
+
end
|
|
111
|
+
return self:_getSlotStore(self.ActiveSlotId.Value)
|
|
112
|
+
end)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
--[=[
|
|
116
|
+
Promises that all slots have loaded
|
|
117
|
+
]=]
|
|
118
|
+
function HasSaveSlots.PromiseSlotsLoaded(self: HasSaveSlots): Promise.Promise<any>
|
|
119
|
+
return self._loadPromise
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
--[=[
|
|
123
|
+
Returns whether the slot with the given ID exists
|
|
124
|
+
]=]
|
|
125
|
+
function HasSaveSlots.PromiseHasSlot(self: HasSaveSlots, slotId: SaveSlotData.SlotId?): Promise.Promise<boolean>
|
|
126
|
+
return (self._loadPromise :: any):Then(function()
|
|
127
|
+
return slotId and ((self._slotMap[slotId] :: Folder?) ~= nil)
|
|
128
|
+
end)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
--[=[
|
|
132
|
+
Selects the slot with the given ID
|
|
133
|
+
]=]
|
|
134
|
+
function HasSaveSlots.PromiseSelectSlot(self: HasSaveSlots, slotId: SaveSlotData.SlotId): Promise.Promise<any>
|
|
135
|
+
return (self._loadPromise :: any):Then(function()
|
|
136
|
+
if slotId == self.ActiveSlotId.Value then
|
|
137
|
+
return -- Already set
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
local slot = self._slotMap[slotId]
|
|
141
|
+
if not slot then
|
|
142
|
+
return (Promise :: any).rejected(`Slot \{{slotId}\} not found`)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
local function setSlot()
|
|
146
|
+
self.ActiveSlotId.Value = slotId
|
|
147
|
+
SaveSlotData.LastPlayedTime:Set(slot, os.time())
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
-- Initialize or save and switch
|
|
151
|
+
if self.ActiveSlotId.Value == nil then
|
|
152
|
+
setSlot()
|
|
153
|
+
return
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
return self._dataStore:Save():Then(setSlot)
|
|
157
|
+
end)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
--[=[
|
|
161
|
+
Creates a slot at the given index
|
|
162
|
+
]=]
|
|
163
|
+
function HasSaveSlots.PromiseCreateSlot(
|
|
164
|
+
self: HasSaveSlots,
|
|
165
|
+
slotIndex: number,
|
|
166
|
+
metadata: SaveSlotData.SaveSlotMetadata?
|
|
167
|
+
): Promise.Promise<SaveSlotData.SlotId>
|
|
168
|
+
return (self._loadPromise :: any):Then(function()
|
|
169
|
+
if (slotIndex < 1) or (slotIndex > self.MaxSlotCount.Value) then
|
|
170
|
+
return (Promise :: any).rejected(`Index must be in range [1, {self.MaxSlotCount.Value}]`)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
for _, slot in self._slotMap do
|
|
174
|
+
if slotIndex == SaveSlotData.SlotIndex:Get(slot) then
|
|
175
|
+
return (Promise :: any).rejected(`Slot {slotIndex} already exists`)
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
local slotId = HttpService:GenerateGUID(false)
|
|
180
|
+
local data = {
|
|
181
|
+
SlotId = slotId,
|
|
182
|
+
SlotIndex = slotIndex,
|
|
183
|
+
SlotName = (metadata and metadata.SlotName) or `Slot {slotIndex}`,
|
|
184
|
+
CreatedTime = os.time(),
|
|
185
|
+
Summary = metadata and metadata.Summary,
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
self:_buildSlot(slotId, data, true)
|
|
189
|
+
return slotId
|
|
190
|
+
end)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
--[=[
|
|
194
|
+
Deletes the slot with the given ID
|
|
195
|
+
]=]
|
|
196
|
+
function HasSaveSlots.PromiseDeleteSlot(self: HasSaveSlots, slotId: SaveSlotData.SlotId): Promise.Promise<any>
|
|
197
|
+
return (self._loadPromise :: any):Then(function()
|
|
198
|
+
if slotId == self.ActiveSlotId.Value then
|
|
199
|
+
return (Promise :: any).rejected("Cannot delete active slot")
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
local slot = self._slotMap[slotId]
|
|
203
|
+
if not slot then
|
|
204
|
+
return (Promise :: any).rejected(`Slot \{{slotId}\} not found`)
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
self._maid[slotId] = nil
|
|
208
|
+
|
|
209
|
+
-- Wipe default slot
|
|
210
|
+
local slotIndex = SaveSlotData.SlotIndex:Get(slot)
|
|
211
|
+
|
|
212
|
+
if slotIndex == SaveSlotConstants.DEFAULT_SLOT_INDEX then
|
|
213
|
+
return self._dataStore:PromiseKeyList():Then(function(keys)
|
|
214
|
+
for _, key in keys do
|
|
215
|
+
if key ~= SaveSlotConstants.SYSTEM_STORE_KEY then
|
|
216
|
+
self._dataStore:Delete(key)
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
self._metadataStore:Delete(slotId)
|
|
220
|
+
end)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
-- Or delete slot from substore
|
|
224
|
+
self._systemStore:GetSubStore(SaveSlotConstants.SLOT_STORE_KEY):Delete(slotId)
|
|
225
|
+
self._metadataStore:Delete(slotId)
|
|
226
|
+
end)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
--[=[
|
|
230
|
+
Sets the metadata for the slot with the given ID
|
|
231
|
+
]=]
|
|
232
|
+
function HasSaveSlots.PromiseSetSlotMetadata(
|
|
233
|
+
self: HasSaveSlots,
|
|
234
|
+
slotId: SaveSlotData.SlotId,
|
|
235
|
+
data: SaveSlotData.SaveSlotMetadata
|
|
236
|
+
): Promise.Promise<any>
|
|
237
|
+
if data.SlotId and (data.SlotId ~= slotId) then
|
|
238
|
+
return (Promise :: any).rejected("SlotId is locked")
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
return (self._loadPromise :: any):Then(function()
|
|
242
|
+
local slot = self._slotMap[slotId]
|
|
243
|
+
|
|
244
|
+
-- Routing depends on immutable indices to distinguish the default slot
|
|
245
|
+
if data.SlotIndex and (data.SlotIndex ~= SaveSlotData.SlotIndex:Get(slot)) then
|
|
246
|
+
return (Promise :: any).rejected("SlotIndex is locked")
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
SaveSlotData:Set(slot, data)
|
|
250
|
+
end)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
--[=[
|
|
254
|
+
Gets the metadata for the slot with the given ID
|
|
255
|
+
]=]
|
|
256
|
+
function HasSaveSlots.PromiseGetSlotMetadata(
|
|
257
|
+
self: HasSaveSlots,
|
|
258
|
+
slotId: SaveSlotData.SlotId
|
|
259
|
+
): Promise.Promise<SaveSlotData.SaveSlotMetadata?>
|
|
260
|
+
return (self._loadPromise :: any):Then(function()
|
|
261
|
+
local slot = self._slotMap[slotId]
|
|
262
|
+
return (Promise :: any).resolved(slot and SaveSlotData:Get(slot))
|
|
263
|
+
end)
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
--[=[
|
|
267
|
+
Returns the slot ID from the given index
|
|
268
|
+
]=]
|
|
269
|
+
function HasSaveSlots.PromiseSlotIdFromIndex(
|
|
270
|
+
self: HasSaveSlots,
|
|
271
|
+
slotIndex: number
|
|
272
|
+
): Promise.Promise<SaveSlotData.SlotId?>
|
|
273
|
+
return (self._loadPromise :: any):Then(function()
|
|
274
|
+
for slotId, slot in self._slotMap do
|
|
275
|
+
if slotIndex == SaveSlotData.SlotIndex:Get(slot) then
|
|
276
|
+
return (Promise :: any).resolved(slotId)
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
return (Promise :: any).resolved(nil)
|
|
280
|
+
end)
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
--[=[
|
|
284
|
+
Gets the last active slot ID
|
|
285
|
+
]=]
|
|
286
|
+
function HasSaveSlots.PromiseLastActiveSlotId(self: HasSaveSlots): Promise.Promise<SaveSlotData.SlotId?>
|
|
287
|
+
return (self._loadPromise :: any):Then(function()
|
|
288
|
+
return self.ActiveSlotId.Value or self._lastActiveSlotId
|
|
289
|
+
end)
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
--[=[
|
|
293
|
+
Sets the summary provider
|
|
294
|
+
]=]
|
|
295
|
+
function HasSaveSlots.SetSummaryProvider(self: HasSaveSlots, provider: SaveSlotSummaryProvider?): ()
|
|
296
|
+
self._summaryProvider.Value = provider
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
function HasSaveSlots._promiseLoadSlots(self: HasSaveSlots): Promise.Promise<{}>
|
|
300
|
+
return self._playerDataStoreService:PromiseDataStore(self._obj):Then(function(dataStore)
|
|
301
|
+
self._dataStore = dataStore
|
|
302
|
+
self._systemStore = dataStore:GetSubStore(SaveSlotConstants.SYSTEM_STORE_KEY)
|
|
303
|
+
self._metadataStore = self._systemStore:GetSubStore(SaveSlotConstants.METADATA_STORE_KEY)
|
|
304
|
+
|
|
305
|
+
return self._metadataStore:LoadAll({}):Then(function(metadata)
|
|
306
|
+
for slotId, data in metadata do
|
|
307
|
+
self:_buildSlot(slotId, data)
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
return self._systemStore:Load("activeSlotId"):Then(function(activeId: SaveSlotData.SlotId?)
|
|
311
|
+
self._lastActiveSlotId = activeId
|
|
312
|
+
self._maid:GiveTask(self._systemStore:StoreOnValueChange("activeSlotId", self.ActiveSlotId))
|
|
313
|
+
end)
|
|
314
|
+
end)
|
|
315
|
+
end)
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
function HasSaveSlots._getSlotStore(self: HasSaveSlots, slotId: SaveSlotData.SlotId): DataStoreStage.DataStoreStage
|
|
319
|
+
local slot = self._slotMap[slotId]
|
|
320
|
+
if slot and (SaveSlotData.SlotIndex:Get(slot) == SaveSlotConstants.DEFAULT_SLOT_INDEX) then
|
|
321
|
+
return self._dataStore
|
|
322
|
+
end
|
|
323
|
+
return self._systemStore:GetSubStore(SaveSlotConstants.SLOT_STORE_KEY):GetSubStore(slotId)
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
function HasSaveSlots._buildSlot(
|
|
327
|
+
self: HasSaveSlots,
|
|
328
|
+
slotId: SaveSlotData.SlotId,
|
|
329
|
+
data: SaveSlotData.SaveSlotMetadata,
|
|
330
|
+
isNew: boolean?
|
|
331
|
+
): ()
|
|
332
|
+
local maid = Maid.new()
|
|
333
|
+
self._maid[slotId] = maid
|
|
334
|
+
|
|
335
|
+
local slot = maid:Add(Instance.new("Folder"))
|
|
336
|
+
slot.Name = slotId
|
|
337
|
+
slot.Archivable = false
|
|
338
|
+
|
|
339
|
+
local metadataStore = self._metadataStore:GetSubStore(slotId)
|
|
340
|
+
|
|
341
|
+
local attributes = SaveSlotData:Create(slot)
|
|
342
|
+
attributes.SlotId.Value = slotId
|
|
343
|
+
attributes.SlotIndex.Value = data.SlotIndex
|
|
344
|
+
|
|
345
|
+
-- Store immutable SlotIndex once on creation
|
|
346
|
+
if isNew then
|
|
347
|
+
metadataStore:Store("SlotIndex", data.SlotIndex)
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
-- Store mutable metadata on change
|
|
351
|
+
for _, key in { "SlotName", "CreatedTime", "LastPlayedTime", "Summary" } do
|
|
352
|
+
attributes[key].Value = data[key]
|
|
353
|
+
maid:GiveTask(metadataStore:StoreOnValueChange(key, attributes[key]))
|
|
354
|
+
|
|
355
|
+
if isNew then
|
|
356
|
+
metadataStore:Store(key, attributes[key].Value)
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
slot.Parent = self._slotContainer
|
|
361
|
+
|
|
362
|
+
self._slotMap[slotId] = slot
|
|
363
|
+
|
|
364
|
+
maid:GiveTask(function()
|
|
365
|
+
self._slotMap[slotId] = nil
|
|
366
|
+
end)
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
function HasSaveSlots._setupSummary(self: HasSaveSlots): ()
|
|
370
|
+
self._maid:GiveTask(
|
|
371
|
+
self:ObserveActiveSlotStoreBrio():Subscribe(function(brio: Brio.Brio<DataStoreStage.DataStoreStage>)
|
|
372
|
+
if brio:IsDead() then
|
|
373
|
+
return
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
local activeSlotId = self.ActiveSlotId.Value
|
|
377
|
+
local activeSlot = activeSlotId and self._slotMap[activeSlotId]
|
|
378
|
+
if not activeSlot then
|
|
379
|
+
return
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
local maid, slotStore = brio:ToMaidAndValue()
|
|
383
|
+
|
|
384
|
+
maid:GiveTask(self._summaryProvider
|
|
385
|
+
:Observe()
|
|
386
|
+
:Pipe({
|
|
387
|
+
Rx.switchMap(function(provider: SaveSlotSummaryProvider?)
|
|
388
|
+
if not provider then
|
|
389
|
+
return Rx.of("") :: any
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
local success, observable = pcall(provider, self._obj, slotStore)
|
|
393
|
+
if not success then
|
|
394
|
+
warn(`[HasSaveSlots] Summary provider errored: {observable}`)
|
|
395
|
+
return Rx.of("") :: any
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
return observable
|
|
399
|
+
end) :: any,
|
|
400
|
+
})
|
|
401
|
+
:Subscribe(function(summary: string)
|
|
402
|
+
if type(summary) ~= "string" then
|
|
403
|
+
warn(`[HasSaveSlots] Summary provider emitted non-string ({typeof(summary)})`)
|
|
404
|
+
return
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
SaveSlotData.Summary:Set(activeSlot, summary)
|
|
408
|
+
end))
|
|
409
|
+
end)
|
|
410
|
+
)
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
function HasSaveSlots._setupRemotes(self: HasSaveSlots): ()
|
|
414
|
+
self._maid:GiveTask(self._remoting.PromiseHasSlot:Bind(function(remotePlayer, ...)
|
|
415
|
+
if remotePlayer == self._obj then
|
|
416
|
+
return self:PromiseHasSlot(...)
|
|
417
|
+
else
|
|
418
|
+
return (Promise :: any).rejected("Bad player")
|
|
419
|
+
end
|
|
420
|
+
end))
|
|
421
|
+
|
|
422
|
+
self._maid:GiveTask(self._remoting.PromiseSelectSlot:Bind(function(remotePlayer, ...)
|
|
423
|
+
if remotePlayer == self._obj then
|
|
424
|
+
return self:PromiseSelectSlot(...)
|
|
425
|
+
else
|
|
426
|
+
return (Promise :: any).rejected("Bad player")
|
|
427
|
+
end
|
|
428
|
+
end))
|
|
429
|
+
|
|
430
|
+
self._maid:GiveTask(self._remoting.PromiseCreateSlot:Bind(function(remotePlayer, ...)
|
|
431
|
+
if remotePlayer == self._obj then
|
|
432
|
+
return self:PromiseCreateSlot(...)
|
|
433
|
+
else
|
|
434
|
+
return (Promise :: any).rejected("Bad player")
|
|
435
|
+
end
|
|
436
|
+
end))
|
|
437
|
+
|
|
438
|
+
self._maid:GiveTask(self._remoting.PromiseDeleteSlot:Bind(function(remotePlayer, ...)
|
|
439
|
+
if remotePlayer == self._obj then
|
|
440
|
+
return self:PromiseDeleteSlot(...)
|
|
441
|
+
else
|
|
442
|
+
return (Promise :: any).rejected("Bad player")
|
|
443
|
+
end
|
|
444
|
+
end))
|
|
445
|
+
|
|
446
|
+
self._maid:GiveTask(self._remoting.PromiseSetSlotMetadata:Bind(function(remotePlayer, ...)
|
|
447
|
+
if remotePlayer == self._obj then
|
|
448
|
+
return self:PromiseSetSlotMetadata(...)
|
|
449
|
+
else
|
|
450
|
+
return (Promise :: any).rejected("Bad player")
|
|
451
|
+
end
|
|
452
|
+
end))
|
|
453
|
+
|
|
454
|
+
self._maid:GiveTask(self._remoting.PromiseGetSlotMetadata:Bind(function(remotePlayer, ...)
|
|
455
|
+
if remotePlayer == self._obj then
|
|
456
|
+
return self:PromiseGetSlotMetadata(...)
|
|
457
|
+
else
|
|
458
|
+
return (Promise :: any).rejected("Bad player")
|
|
459
|
+
end
|
|
460
|
+
end))
|
|
461
|
+
|
|
462
|
+
self._maid:GiveTask(self._remoting.PromiseSlotIdFromIndex:Bind(function(remotePlayer, ...)
|
|
463
|
+
if remotePlayer == self._obj then
|
|
464
|
+
return self:PromiseSlotIdFromIndex(...)
|
|
465
|
+
else
|
|
466
|
+
return (Promise :: any).rejected("Bad player")
|
|
467
|
+
end
|
|
468
|
+
end))
|
|
469
|
+
|
|
470
|
+
self._maid:GiveTask(self._remoting.PromiseLastActiveSlotId:Bind(function(remotePlayer)
|
|
471
|
+
if remotePlayer == self._obj then
|
|
472
|
+
return self:PromiseLastActiveSlotId()
|
|
473
|
+
else
|
|
474
|
+
return (Promise :: any).rejected("Bad player")
|
|
475
|
+
end
|
|
476
|
+
end))
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
return PlayerBinder.new("HasSaveSlots", HasSaveSlots :: any) :: Binder.Binder<HasSaveSlots>
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
@class SaveSlotCmdrService
|
|
4
|
+
]=]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local CmdrService = require("CmdrService")
|
|
9
|
+
local HasSaveSlots = require("HasSaveSlots")
|
|
10
|
+
local Maid = require("Maid")
|
|
11
|
+
local SaveSlotCmdrUtils = require("SaveSlotCmdrUtils")
|
|
12
|
+
local SaveSlotDataService = require("SaveSlotDataService")
|
|
13
|
+
local ServiceBag = require("ServiceBag")
|
|
14
|
+
|
|
15
|
+
local SaveSlotCmdrService = {}
|
|
16
|
+
SaveSlotCmdrService.ServiceName = "SaveSlotCmdrService"
|
|
17
|
+
|
|
18
|
+
export type SaveSlotCmdrService = typeof(setmetatable(
|
|
19
|
+
{} :: {
|
|
20
|
+
_serviceBag: ServiceBag.ServiceBag,
|
|
21
|
+
_maid: Maid.Maid,
|
|
22
|
+
_cmdrService: any,
|
|
23
|
+
_hasSaveSlotsBinder: any,
|
|
24
|
+
_saveSlotDataService: any,
|
|
25
|
+
},
|
|
26
|
+
{} :: typeof({ __index = SaveSlotCmdrService })
|
|
27
|
+
))
|
|
28
|
+
|
|
29
|
+
function SaveSlotCmdrService.Init(self: SaveSlotCmdrService, serviceBag: ServiceBag.ServiceBag)
|
|
30
|
+
assert(not (self :: any)._serviceBag, "Already initialized")
|
|
31
|
+
self._serviceBag = assert(serviceBag, "No serviceBag")
|
|
32
|
+
self._maid = Maid.new()
|
|
33
|
+
|
|
34
|
+
-- External
|
|
35
|
+
self._cmdrService = self._serviceBag:GetService(CmdrService)
|
|
36
|
+
|
|
37
|
+
-- Internal
|
|
38
|
+
self._hasSaveSlotsBinder = self._serviceBag:GetService(HasSaveSlots)
|
|
39
|
+
self._saveSlotDataService = self._serviceBag:GetService(SaveSlotDataService)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
function SaveSlotCmdrService.Start(self: SaveSlotCmdrService)
|
|
43
|
+
self._maid:GivePromise(self._cmdrService:PromiseCmdr()):Then(function(cmdr)
|
|
44
|
+
SaveSlotCmdrUtils.registerSlotIndexType(cmdr, self._saveSlotDataService)
|
|
45
|
+
self:_registerCommands()
|
|
46
|
+
end)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
function SaveSlotCmdrService._registerCommands(self: SaveSlotCmdrService): ()
|
|
50
|
+
self._cmdrService:RegisterCommand({
|
|
51
|
+
Name = "list-save-slots",
|
|
52
|
+
Description = "Lists all save slots.",
|
|
53
|
+
Group = "SaveSlots",
|
|
54
|
+
Args = {},
|
|
55
|
+
}, function(context)
|
|
56
|
+
local activeSlotId = self._saveSlotDataService:GetActiveSlotId(context.Executor)
|
|
57
|
+
local slotList = self._saveSlotDataService:GetSlotList(context.Executor)
|
|
58
|
+
local listString = ""
|
|
59
|
+
|
|
60
|
+
for _, slot in slotList do
|
|
61
|
+
local isActive = (slot.SlotId == activeSlotId)
|
|
62
|
+
listString ..= `\n"{slot.SlotName}" ({slot.SlotIndex}){isActive and " — Active" or ""}\n{slot.Summary}\n`
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
return listString
|
|
66
|
+
end)
|
|
67
|
+
|
|
68
|
+
self._cmdrService:RegisterCommand({
|
|
69
|
+
Name = "get-active-save-slot",
|
|
70
|
+
Description = "Returns the active save slot.",
|
|
71
|
+
Group = "SaveSlots",
|
|
72
|
+
Args = {},
|
|
73
|
+
}, function(context)
|
|
74
|
+
local activeSlotId = self._saveSlotDataService:GetActiveSlotId(context.Executor)
|
|
75
|
+
if not activeSlotId then
|
|
76
|
+
return "No active slot."
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
local slotData = self._saveSlotDataService:GetSlotMetadata(context.Executor, activeSlotId)
|
|
80
|
+
|
|
81
|
+
return `Currently using slot {slotData.SlotIndex} ("{slotData.SlotName}").`
|
|
82
|
+
end)
|
|
83
|
+
|
|
84
|
+
self._cmdrService:RegisterCommand({
|
|
85
|
+
Name = "set-save-slot",
|
|
86
|
+
Description = "Switches to the given save slot.",
|
|
87
|
+
Group = "SaveSlots",
|
|
88
|
+
Args = {
|
|
89
|
+
{
|
|
90
|
+
Name = "Slot",
|
|
91
|
+
Type = "slotIndex",
|
|
92
|
+
Description = "Slot index to switch to.",
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
}, function(context, slotIndex: number)
|
|
96
|
+
local slotId = self._saveSlotDataService:GetSlotIdFromIndex(context.Executor, slotIndex)
|
|
97
|
+
if not slotId then
|
|
98
|
+
return `No slot with index {slotIndex}.`
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
if slotId == self._saveSlotDataService:GetActiveSlotId(context.Executor) then
|
|
102
|
+
return "Slot is already active."
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
self._maid
|
|
106
|
+
:GivePromise(self._hasSaveSlotsBinder:Promise(context.Executor))
|
|
107
|
+
:Then(function(hasSaveSlots)
|
|
108
|
+
return hasSaveSlots:PromiseSelectSlot(slotId)
|
|
109
|
+
end)
|
|
110
|
+
:Wait()
|
|
111
|
+
|
|
112
|
+
return `Switched to slot {slotIndex}.`
|
|
113
|
+
end)
|
|
114
|
+
|
|
115
|
+
self._cmdrService:RegisterCommand({
|
|
116
|
+
Name = "create-save-slot",
|
|
117
|
+
Description = "Creates a save slot at the given index.",
|
|
118
|
+
Group = "SaveSlots",
|
|
119
|
+
Args = {
|
|
120
|
+
{
|
|
121
|
+
Name = "Slot",
|
|
122
|
+
Type = "number",
|
|
123
|
+
Description = "Slot index to create.",
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
}, function(context, slotIndex: number)
|
|
127
|
+
local maxSlotCount = context.Executor:GetAttribute("MaxSlotCount")
|
|
128
|
+
if (slotIndex < 1) or (slotIndex > maxSlotCount) then
|
|
129
|
+
return `Index must be in range [1, {maxSlotCount}].`
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
local slotId = self._saveSlotDataService:GetSlotIdFromIndex(context.Executor, slotIndex)
|
|
133
|
+
if slotId then
|
|
134
|
+
return "Slot already exists."
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
self._maid
|
|
138
|
+
:GivePromise(self._hasSaveSlotsBinder:Promise(context.Executor))
|
|
139
|
+
:Then(function(hasSaveSlots)
|
|
140
|
+
return hasSaveSlots:PromiseCreateSlot(slotIndex)
|
|
141
|
+
end)
|
|
142
|
+
:Wait()
|
|
143
|
+
|
|
144
|
+
return `Created slot {slotIndex}.`
|
|
145
|
+
end)
|
|
146
|
+
|
|
147
|
+
self._cmdrService:RegisterCommand({
|
|
148
|
+
Name = "delete-save-slot",
|
|
149
|
+
Description = "Deletes the given save slot.",
|
|
150
|
+
Group = "SaveSlots",
|
|
151
|
+
Args = {
|
|
152
|
+
{
|
|
153
|
+
Name = "Slot",
|
|
154
|
+
Type = "slotIndex",
|
|
155
|
+
Description = "Slot index to delete.",
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
}, function(context, slotIndex: number)
|
|
159
|
+
local slotId = self._saveSlotDataService:GetSlotIdFromIndex(context.Executor, slotIndex)
|
|
160
|
+
if not slotId then
|
|
161
|
+
return `No slot with index {slotIndex}.`
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
if slotId == self._saveSlotDataService:GetActiveSlotId(context.Executor) then
|
|
165
|
+
return "Cannot delete active slot."
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
self._maid
|
|
169
|
+
:GivePromise(self._hasSaveSlotsBinder:Promise(context.Executor))
|
|
170
|
+
:Then(function(hasSaveSlots)
|
|
171
|
+
return hasSaveSlots:PromiseDeleteSlot(slotId)
|
|
172
|
+
end)
|
|
173
|
+
:Wait()
|
|
174
|
+
|
|
175
|
+
return `Deleted slot {slotIndex}.`
|
|
176
|
+
end)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
function SaveSlotCmdrService.Destroy(self: SaveSlotCmdrService): ()
|
|
180
|
+
self._maid:Destroy()
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
return SaveSlotCmdrService
|