@quenty/saveslot 1.5.0 → 1.7.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 +17 -0
- package/package.json +19 -18
- package/src/Client/Binders/HasSaveSlotsClient.lua +14 -0
- package/src/Client/SaveSlotServiceClient.lua +17 -0
- package/src/Server/Binders/HasSaveSlots.lua +135 -0
- package/src/Server/Binders/HasSaveSlots.spec.lua +446 -8
- package/src/Server/Cmdr/SaveSlotCmdrService.lua +83 -16
- package/src/Server/SaveSlotService.lua +47 -12
- package/src/Server/SaveSlotService.spec.lua +109 -7
- package/src/Shared/Data/HasSaveSlotsData.lua +1 -0
- package/src/Shared/HasSaveSlotsBase.lua +54 -0
- package/src/Shared/HasSaveSlotsInterface.lua +11 -0
- package/src/Shared/SaveSlotDataService.lua +15 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
--!
|
|
1
|
+
--!strict
|
|
2
2
|
--[[
|
|
3
3
|
Integration coverage for the real HasSaveSlots binder, driven against a mocked datastore. Because
|
|
4
4
|
the load path only reads the player's UserId, a plain Folder stands in for a Player with
|
|
@@ -12,6 +12,7 @@ local require = require(script.Parent.loader).load(script)
|
|
|
12
12
|
local DataStoreMock = require("DataStoreMock")
|
|
13
13
|
local Jest = require("Jest")
|
|
14
14
|
local PlayerDataStoreManager = require("PlayerDataStoreManager")
|
|
15
|
+
local PlayerDataStoreService = require("PlayerDataStoreService")
|
|
15
16
|
local PromiseTestUtils = require("PromiseTestUtils")
|
|
16
17
|
local ServiceBag = require("ServiceBag")
|
|
17
18
|
|
|
@@ -35,17 +36,23 @@ afterEach(function()
|
|
|
35
36
|
PlayerDataStoreManager._toPlayerUserIdOrError = originalToUserId
|
|
36
37
|
end)
|
|
37
38
|
|
|
38
|
-
local function setup(mock)
|
|
39
|
+
local function setup(mock: DataStoreMock.DataStoreMock?)
|
|
39
40
|
mock = mock or DataStoreMock.new()
|
|
40
41
|
|
|
41
42
|
local serviceBag = ServiceBag.new()
|
|
42
|
-
|
|
43
|
+
serviceBag:GetService(require("TeleportDataService"))
|
|
44
|
+
-- GetService returns the required module's type; PlayerDataStoreService returns its class table, so
|
|
45
|
+
-- cast to the instance type before calling instance methods.
|
|
46
|
+
local playerDataStoreService = (
|
|
47
|
+
serviceBag:GetService(PlayerDataStoreService) :: any
|
|
48
|
+
) :: PlayerDataStoreService.PlayerDataStoreService
|
|
43
49
|
local binder = serviceBag:GetService(require("HasSaveSlots"))
|
|
44
50
|
serviceBag:Init()
|
|
45
51
|
playerDataStoreService:SetRobloxDataStore(mock)
|
|
46
52
|
serviceBag:Start()
|
|
47
53
|
|
|
48
|
-
|
|
54
|
+
-- A Folder stands in for a Player; the load path only reads the UserId, intercepted below.
|
|
55
|
+
local fakePlayer = (Instance.new("Folder") :: any) :: Player
|
|
49
56
|
fakePlayer.Name = "FakePlayer"
|
|
50
57
|
fakePlayer.Parent = Workspace
|
|
51
58
|
|
|
@@ -57,10 +64,8 @@ local function setup(mock)
|
|
|
57
64
|
return originalToUserId(self, playerOrUserId)
|
|
58
65
|
end
|
|
59
66
|
|
|
60
|
-
local hasSaveSlots = binder:Bind(fakePlayer)
|
|
61
|
-
|
|
62
|
-
hasSaveSlots.MaxSlotCount.Value = 5
|
|
63
|
-
end
|
|
67
|
+
local hasSaveSlots = assert(binder:Bind(fakePlayer), "Failed to bind HasSaveSlots")
|
|
68
|
+
hasSaveSlots.MaxSlotCount.Value = 5
|
|
64
69
|
|
|
65
70
|
local function destroy()
|
|
66
71
|
fakePlayer:Destroy()
|
|
@@ -259,6 +264,439 @@ describe("HasSaveSlots against a fake player (healthy datastore)", function()
|
|
|
259
264
|
|
|
260
265
|
context.destroy()
|
|
261
266
|
end)
|
|
267
|
+
|
|
268
|
+
it("should replicate the last active slot after selection", function()
|
|
269
|
+
local context = setup()
|
|
270
|
+
|
|
271
|
+
local createPromise = context.hasSaveSlots:PromiseCreateSlot(1)
|
|
272
|
+
if not PromiseTestUtils.awaitSettled(createPromise, 10) then
|
|
273
|
+
expect("create hung").toEqual("create settled")
|
|
274
|
+
context.destroy()
|
|
275
|
+
return
|
|
276
|
+
end
|
|
277
|
+
local _, slotId = createPromise:Yield()
|
|
278
|
+
|
|
279
|
+
local selectPromise = context.hasSaveSlots:PromiseSelectSlot(slotId)
|
|
280
|
+
if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
|
|
281
|
+
expect("select hung").toEqual("select settled")
|
|
282
|
+
context.destroy()
|
|
283
|
+
return
|
|
284
|
+
end
|
|
285
|
+
selectPromise:Yield()
|
|
286
|
+
|
|
287
|
+
expect(context.hasSaveSlots.LastActiveSlotId.Value).toEqual(slotId)
|
|
288
|
+
|
|
289
|
+
context.destroy()
|
|
290
|
+
end)
|
|
291
|
+
|
|
292
|
+
it("PromiseDeselectSlot clears the active slot but remembers the last active", function()
|
|
293
|
+
local context = setup()
|
|
294
|
+
|
|
295
|
+
local createPromise = context.hasSaveSlots:PromiseCreateSlot(1)
|
|
296
|
+
if not PromiseTestUtils.awaitSettled(createPromise, 10) then
|
|
297
|
+
expect("create hung").toEqual("create settled")
|
|
298
|
+
context.destroy()
|
|
299
|
+
return
|
|
300
|
+
end
|
|
301
|
+
local _, slotId = createPromise:Yield()
|
|
302
|
+
|
|
303
|
+
local selectPromise = context.hasSaveSlots:PromiseSelectSlot(slotId)
|
|
304
|
+
if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
|
|
305
|
+
expect("select hung").toEqual("select settled")
|
|
306
|
+
context.destroy()
|
|
307
|
+
return
|
|
308
|
+
end
|
|
309
|
+
selectPromise:Yield()
|
|
310
|
+
|
|
311
|
+
local deselectPromise = context.hasSaveSlots:PromiseDeselectSlot()
|
|
312
|
+
if not PromiseTestUtils.awaitSettled(deselectPromise, 10) then
|
|
313
|
+
expect("deselect hung").toEqual("deselect settled")
|
|
314
|
+
context.destroy()
|
|
315
|
+
return
|
|
316
|
+
end
|
|
317
|
+
expect((deselectPromise:Yield())).toEqual(true)
|
|
318
|
+
|
|
319
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toBeNil()
|
|
320
|
+
expect(context.hasSaveSlots.LastActiveSlotId.Value).toEqual(slotId)
|
|
321
|
+
|
|
322
|
+
-- The slot is deselected, not deleted: the last-active pointer still resolves it
|
|
323
|
+
local lastPromise = context.hasSaveSlots:PromiseLastActiveSlotId()
|
|
324
|
+
if not PromiseTestUtils.awaitSettled(lastPromise, 10) then
|
|
325
|
+
expect("lastActive hung").toEqual("lastActive settled")
|
|
326
|
+
context.destroy()
|
|
327
|
+
return
|
|
328
|
+
end
|
|
329
|
+
expect((lastPromise:Wait())).toEqual(slotId)
|
|
330
|
+
|
|
331
|
+
context.destroy()
|
|
332
|
+
end)
|
|
333
|
+
|
|
334
|
+
it("deleting the last-active slot clears the continue pointer", function()
|
|
335
|
+
-- Repro for the `delete-save-slot *` bug: deleting the (deselected) active slot left
|
|
336
|
+
-- LastActiveSlotId dangling, so the menu kept offering "Continue" for a slot that was gone.
|
|
337
|
+
local context = setup()
|
|
338
|
+
|
|
339
|
+
local createPromise = context.hasSaveSlots:PromiseCreateSlot(1)
|
|
340
|
+
if not PromiseTestUtils.awaitSettled(createPromise, 10) then
|
|
341
|
+
expect("create hung").toEqual("create settled")
|
|
342
|
+
context.destroy()
|
|
343
|
+
return
|
|
344
|
+
end
|
|
345
|
+
local _, slotId = createPromise:Yield()
|
|
346
|
+
|
|
347
|
+
local selectPromise = context.hasSaveSlots:PromiseSelectSlot(slotId)
|
|
348
|
+
if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
|
|
349
|
+
expect("select hung").toEqual("select settled")
|
|
350
|
+
context.destroy()
|
|
351
|
+
return
|
|
352
|
+
end
|
|
353
|
+
selectPromise:Yield()
|
|
354
|
+
|
|
355
|
+
-- Deselect first, matching how the cmdr command frees the active slot before deleting it.
|
|
356
|
+
local deselectPromise = context.hasSaveSlots:PromiseDeselectSlot()
|
|
357
|
+
if not PromiseTestUtils.awaitSettled(deselectPromise, 10) then
|
|
358
|
+
expect("deselect hung").toEqual("deselect settled")
|
|
359
|
+
context.destroy()
|
|
360
|
+
return
|
|
361
|
+
end
|
|
362
|
+
deselectPromise:Yield()
|
|
363
|
+
|
|
364
|
+
-- Deselection deliberately keeps remembering the slot for "Continue"...
|
|
365
|
+
expect(context.hasSaveSlots.LastActiveSlotId.Value).toEqual(slotId)
|
|
366
|
+
|
|
367
|
+
local deletePromise = context.hasSaveSlots:PromiseDeleteSlot(slotId)
|
|
368
|
+
if not PromiseTestUtils.awaitSettled(deletePromise, 10) then
|
|
369
|
+
expect("delete hung").toEqual("delete settled")
|
|
370
|
+
context.destroy()
|
|
371
|
+
return
|
|
372
|
+
end
|
|
373
|
+
deletePromise:Yield()
|
|
374
|
+
|
|
375
|
+
-- ...but once that slot is deleted, there is nothing to continue on.
|
|
376
|
+
expect(context.hasSaveSlots.LastActiveSlotId.Value).toBeNil()
|
|
377
|
+
|
|
378
|
+
local lastPromise = context.hasSaveSlots:PromiseLastActiveSlotId()
|
|
379
|
+
if not PromiseTestUtils.awaitSettled(lastPromise, 10) then
|
|
380
|
+
expect("lastActive hung").toEqual("lastActive settled")
|
|
381
|
+
context.destroy()
|
|
382
|
+
return
|
|
383
|
+
end
|
|
384
|
+
expect((lastPromise:Wait())).toBeNil()
|
|
385
|
+
|
|
386
|
+
context.destroy()
|
|
387
|
+
end)
|
|
388
|
+
|
|
389
|
+
it("deleting a non-last-active slot leaves the continue pointer intact", function()
|
|
390
|
+
-- The clear must be scoped to the resumable slot: deleting some other slot must not wipe it.
|
|
391
|
+
local context = setup()
|
|
392
|
+
|
|
393
|
+
local firstPromise = context.hasSaveSlots:PromiseCreateSlot(1)
|
|
394
|
+
if not PromiseTestUtils.awaitSettled(firstPromise, 10) then
|
|
395
|
+
expect("first create hung").toEqual("first create settled")
|
|
396
|
+
context.destroy()
|
|
397
|
+
return
|
|
398
|
+
end
|
|
399
|
+
local _, firstSlotId = firstPromise:Yield()
|
|
400
|
+
|
|
401
|
+
local secondPromise = context.hasSaveSlots:PromiseCreateSlot(2)
|
|
402
|
+
if not PromiseTestUtils.awaitSettled(secondPromise, 10) then
|
|
403
|
+
expect("second create hung").toEqual("second create settled")
|
|
404
|
+
context.destroy()
|
|
405
|
+
return
|
|
406
|
+
end
|
|
407
|
+
local _, secondSlotId = secondPromise:Yield()
|
|
408
|
+
|
|
409
|
+
-- Slot 1 is the last-active; slot 2 is just another slot, and stays deletable while 1 is deselected.
|
|
410
|
+
local selectPromise = context.hasSaveSlots:PromiseSelectSlot(firstSlotId)
|
|
411
|
+
if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
|
|
412
|
+
expect("select hung").toEqual("select settled")
|
|
413
|
+
context.destroy()
|
|
414
|
+
return
|
|
415
|
+
end
|
|
416
|
+
selectPromise:Yield()
|
|
417
|
+
|
|
418
|
+
local deselectPromise = context.hasSaveSlots:PromiseDeselectSlot()
|
|
419
|
+
if not PromiseTestUtils.awaitSettled(deselectPromise, 10) then
|
|
420
|
+
expect("deselect hung").toEqual("deselect settled")
|
|
421
|
+
context.destroy()
|
|
422
|
+
return
|
|
423
|
+
end
|
|
424
|
+
deselectPromise:Yield()
|
|
425
|
+
|
|
426
|
+
local deletePromise = context.hasSaveSlots:PromiseDeleteSlot(secondSlotId)
|
|
427
|
+
if not PromiseTestUtils.awaitSettled(deletePromise, 10) then
|
|
428
|
+
expect("delete hung").toEqual("delete settled")
|
|
429
|
+
context.destroy()
|
|
430
|
+
return
|
|
431
|
+
end
|
|
432
|
+
deletePromise:Yield()
|
|
433
|
+
|
|
434
|
+
expect(context.hasSaveSlots.LastActiveSlotId.Value).toEqual(firstSlotId)
|
|
435
|
+
|
|
436
|
+
context.destroy()
|
|
437
|
+
end)
|
|
438
|
+
|
|
439
|
+
it("PromiseDeselectSlot is a no-op when no slot is active", function()
|
|
440
|
+
local context = setup()
|
|
441
|
+
|
|
442
|
+
local deselectPromise = context.hasSaveSlots:PromiseDeselectSlot()
|
|
443
|
+
if not PromiseTestUtils.awaitSettled(deselectPromise, 10) then
|
|
444
|
+
expect("deselect hung").toEqual("deselect settled")
|
|
445
|
+
context.destroy()
|
|
446
|
+
return
|
|
447
|
+
end
|
|
448
|
+
expect((deselectPromise:Yield())).toEqual(true)
|
|
449
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toBeNil()
|
|
450
|
+
|
|
451
|
+
context.destroy()
|
|
452
|
+
end)
|
|
453
|
+
|
|
454
|
+
it("PromiseDeselectSlot then PromiseSelectLastSaveSlot round-trips back into the slot", function()
|
|
455
|
+
local context = setup()
|
|
456
|
+
|
|
457
|
+
local createPromise = context.hasSaveSlots:PromiseCreateSlot(1)
|
|
458
|
+
if not PromiseTestUtils.awaitSettled(createPromise, 10) then
|
|
459
|
+
expect("create hung").toEqual("create settled")
|
|
460
|
+
context.destroy()
|
|
461
|
+
return
|
|
462
|
+
end
|
|
463
|
+
local _, slotId = createPromise:Yield()
|
|
464
|
+
|
|
465
|
+
local selectPromise = context.hasSaveSlots:PromiseSelectSlot(slotId)
|
|
466
|
+
if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
|
|
467
|
+
expect("select hung").toEqual("select settled")
|
|
468
|
+
context.destroy()
|
|
469
|
+
return
|
|
470
|
+
end
|
|
471
|
+
selectPromise:Yield()
|
|
472
|
+
|
|
473
|
+
local deselectPromise = context.hasSaveSlots:PromiseDeselectSlot()
|
|
474
|
+
if not PromiseTestUtils.awaitSettled(deselectPromise, 10) then
|
|
475
|
+
expect("deselect hung").toEqual("deselect settled")
|
|
476
|
+
context.destroy()
|
|
477
|
+
return
|
|
478
|
+
end
|
|
479
|
+
deselectPromise:Yield()
|
|
480
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toBeNil()
|
|
481
|
+
|
|
482
|
+
local continuePromise = context.hasSaveSlots:PromiseSelectLastSaveSlot()
|
|
483
|
+
if not PromiseTestUtils.awaitSettled(continuePromise, 10) then
|
|
484
|
+
expect("continue hung").toEqual("continue settled")
|
|
485
|
+
context.destroy()
|
|
486
|
+
return
|
|
487
|
+
end
|
|
488
|
+
expect((continuePromise:Wait())).toEqual(slotId)
|
|
489
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(slotId)
|
|
490
|
+
|
|
491
|
+
context.destroy()
|
|
492
|
+
end)
|
|
493
|
+
|
|
494
|
+
it("PromiseSelectLastSaveSlot re-selects the last active slot after the active slot is cleared", function()
|
|
495
|
+
local context = setup()
|
|
496
|
+
|
|
497
|
+
local createPromise = context.hasSaveSlots:PromiseCreateSlot(1)
|
|
498
|
+
if not PromiseTestUtils.awaitSettled(createPromise, 10) then
|
|
499
|
+
expect("create hung").toEqual("create settled")
|
|
500
|
+
context.destroy()
|
|
501
|
+
return
|
|
502
|
+
end
|
|
503
|
+
local _, slotId = createPromise:Yield()
|
|
504
|
+
|
|
505
|
+
local selectPromise = context.hasSaveSlots:PromiseSelectSlot(slotId)
|
|
506
|
+
if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
|
|
507
|
+
expect("select hung").toEqual("select settled")
|
|
508
|
+
context.destroy()
|
|
509
|
+
return
|
|
510
|
+
end
|
|
511
|
+
selectPromise:Yield()
|
|
512
|
+
|
|
513
|
+
-- Simulate returning to the menu: no active slot, but the last active is remembered
|
|
514
|
+
context.hasSaveSlots.ActiveSlotId.Value = nil
|
|
515
|
+
expect(context.hasSaveSlots.LastActiveSlotId.Value).toEqual(slotId)
|
|
516
|
+
|
|
517
|
+
local continuePromise = context.hasSaveSlots:PromiseSelectLastSaveSlot()
|
|
518
|
+
if not PromiseTestUtils.awaitSettled(continuePromise, 10) then
|
|
519
|
+
expect("continue hung").toEqual("continue settled")
|
|
520
|
+
context.destroy()
|
|
521
|
+
return
|
|
522
|
+
end
|
|
523
|
+
expect((continuePromise:Wait())).toEqual(slotId)
|
|
524
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(slotId)
|
|
525
|
+
|
|
526
|
+
context.destroy()
|
|
527
|
+
end)
|
|
528
|
+
|
|
529
|
+
it("PromiseSelectLastSaveSlot resolves nil when there is nothing to continue", function()
|
|
530
|
+
local context = setup()
|
|
531
|
+
|
|
532
|
+
local continuePromise = context.hasSaveSlots:PromiseSelectLastSaveSlot()
|
|
533
|
+
if not PromiseTestUtils.awaitSettled(continuePromise, 10) then
|
|
534
|
+
expect("continue hung").toEqual("continue settled")
|
|
535
|
+
context.destroy()
|
|
536
|
+
return
|
|
537
|
+
end
|
|
538
|
+
expect((continuePromise:Wait())).toBeNil()
|
|
539
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toBeNil()
|
|
540
|
+
|
|
541
|
+
context.destroy()
|
|
542
|
+
end)
|
|
543
|
+
|
|
544
|
+
it("PromiseSelectNewSaveSlot creates and selects a fresh slot each time", function()
|
|
545
|
+
local context = setup()
|
|
546
|
+
|
|
547
|
+
local firstPromise = context.hasSaveSlots:PromiseSelectNewSaveSlot()
|
|
548
|
+
if not PromiseTestUtils.awaitSettled(firstPromise, 10) then
|
|
549
|
+
expect("new game hung").toEqual("new game settled")
|
|
550
|
+
context.destroy()
|
|
551
|
+
return
|
|
552
|
+
end
|
|
553
|
+
local firstSlotId = firstPromise:Wait()
|
|
554
|
+
expect(type(firstSlotId)).toEqual("string")
|
|
555
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(firstSlotId)
|
|
556
|
+
|
|
557
|
+
local secondPromise = context.hasSaveSlots:PromiseSelectNewSaveSlot()
|
|
558
|
+
if not PromiseTestUtils.awaitSettled(secondPromise, 10) then
|
|
559
|
+
expect("second new game hung").toEqual("second new game settled")
|
|
560
|
+
context.destroy()
|
|
561
|
+
return
|
|
562
|
+
end
|
|
563
|
+
local secondSlotId = secondPromise:Wait()
|
|
564
|
+
expect(type(secondSlotId)).toEqual("string")
|
|
565
|
+
expect(secondSlotId ~= firstSlotId).toEqual(true)
|
|
566
|
+
|
|
567
|
+
context.destroy()
|
|
568
|
+
end)
|
|
569
|
+
|
|
570
|
+
it("PromiseDeleteAllSlots wipes every slot and clears the selection", function()
|
|
571
|
+
local context = setup()
|
|
572
|
+
|
|
573
|
+
local firstPromise = context.hasSaveSlots:PromiseCreateSlot(1)
|
|
574
|
+
if not PromiseTestUtils.awaitSettled(firstPromise, 10) then
|
|
575
|
+
expect("first create hung").toEqual("first create settled")
|
|
576
|
+
context.destroy()
|
|
577
|
+
return
|
|
578
|
+
end
|
|
579
|
+
local _, firstSlotId = firstPromise:Yield()
|
|
580
|
+
|
|
581
|
+
local secondPromise = context.hasSaveSlots:PromiseCreateSlot(2)
|
|
582
|
+
if not PromiseTestUtils.awaitSettled(secondPromise, 10) then
|
|
583
|
+
expect("second create hung").toEqual("second create settled")
|
|
584
|
+
context.destroy()
|
|
585
|
+
return
|
|
586
|
+
end
|
|
587
|
+
secondPromise:Yield()
|
|
588
|
+
|
|
589
|
+
local selectPromise = context.hasSaveSlots:PromiseSelectSlot(firstSlotId)
|
|
590
|
+
if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
|
|
591
|
+
expect("select hung").toEqual("select settled")
|
|
592
|
+
context.destroy()
|
|
593
|
+
return
|
|
594
|
+
end
|
|
595
|
+
selectPromise:Yield()
|
|
596
|
+
|
|
597
|
+
local wipePromise = context.hasSaveSlots:PromiseDeleteAllSlots()
|
|
598
|
+
if not PromiseTestUtils.awaitSettled(wipePromise, 10) then
|
|
599
|
+
expect("wipe hung").toEqual("wipe settled")
|
|
600
|
+
context.destroy()
|
|
601
|
+
return
|
|
602
|
+
end
|
|
603
|
+
wipePromise:Yield()
|
|
604
|
+
|
|
605
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toBeNil()
|
|
606
|
+
expect(context.hasSaveSlots.LastActiveSlotId.Value).toBeNil()
|
|
607
|
+
|
|
608
|
+
local hasPromise = context.hasSaveSlots:PromiseHasSlot(firstSlotId)
|
|
609
|
+
if not PromiseTestUtils.awaitSettled(hasPromise, 10) then
|
|
610
|
+
expect("hasSlot hung").toEqual("hasSlot settled")
|
|
611
|
+
context.destroy()
|
|
612
|
+
return
|
|
613
|
+
end
|
|
614
|
+
expect((hasPromise:Wait())).toEqual(false)
|
|
615
|
+
|
|
616
|
+
context.destroy()
|
|
617
|
+
end)
|
|
618
|
+
|
|
619
|
+
it("PromiseSelectNewSaveSlot reuses the lowest free index after a deletion", function()
|
|
620
|
+
local context = setup()
|
|
621
|
+
|
|
622
|
+
-- Fill indices 1, 2, 3 (nothing is selected, so all remain deletable)
|
|
623
|
+
local slotIdsByIndex = {}
|
|
624
|
+
for index = 1, 3 do
|
|
625
|
+
local createPromise = context.hasSaveSlots:PromiseCreateSlot(index)
|
|
626
|
+
if not PromiseTestUtils.awaitSettled(createPromise, 10) then
|
|
627
|
+
expect("create hung").toEqual("create settled")
|
|
628
|
+
context.destroy()
|
|
629
|
+
return
|
|
630
|
+
end
|
|
631
|
+
local _, slotId = createPromise:Yield()
|
|
632
|
+
slotIdsByIndex[index] = slotId
|
|
633
|
+
end
|
|
634
|
+
|
|
635
|
+
-- Open a gap at index 2
|
|
636
|
+
local deletePromise = context.hasSaveSlots:PromiseDeleteSlot(slotIdsByIndex[2])
|
|
637
|
+
if not PromiseTestUtils.awaitSettled(deletePromise, 10) then
|
|
638
|
+
expect("delete hung").toEqual("delete settled")
|
|
639
|
+
context.destroy()
|
|
640
|
+
return
|
|
641
|
+
end
|
|
642
|
+
deletePromise:Yield()
|
|
643
|
+
|
|
644
|
+
-- The next new slot fills the gap at 2 rather than appending at 4
|
|
645
|
+
local newPromise = context.hasSaveSlots:PromiseSelectNewSaveSlot()
|
|
646
|
+
if not PromiseTestUtils.awaitSettled(newPromise, 10) then
|
|
647
|
+
expect("new game hung").toEqual("new game settled")
|
|
648
|
+
context.destroy()
|
|
649
|
+
return
|
|
650
|
+
end
|
|
651
|
+
local newSlotId = newPromise:Wait()
|
|
652
|
+
expect(type(newSlotId)).toEqual("string")
|
|
653
|
+
|
|
654
|
+
local metadataPromise = context.hasSaveSlots:PromiseGetSlotMetadata(newSlotId)
|
|
655
|
+
if not PromiseTestUtils.awaitSettled(metadataPromise, 10) then
|
|
656
|
+
expect("metadata hung").toEqual("metadata settled")
|
|
657
|
+
context.destroy()
|
|
658
|
+
return
|
|
659
|
+
end
|
|
660
|
+
expect((metadataPromise:Wait()).SlotIndex).toEqual(2)
|
|
661
|
+
|
|
662
|
+
context.destroy()
|
|
663
|
+
end)
|
|
664
|
+
|
|
665
|
+
it("allows creating a slot past a finite cap when the count is unbounded", function()
|
|
666
|
+
local context = setup()
|
|
667
|
+
context.hasSaveSlots.MaxSlotCount.Value = math.huge
|
|
668
|
+
|
|
669
|
+
-- Index 6 is beyond the setup's finite cap of 5; unbounded must accept it
|
|
670
|
+
local createPromise = context.hasSaveSlots:PromiseCreateSlot(6)
|
|
671
|
+
if not PromiseTestUtils.awaitSettled(createPromise, 10) then
|
|
672
|
+
expect("create hung").toEqual("create settled")
|
|
673
|
+
context.destroy()
|
|
674
|
+
return
|
|
675
|
+
end
|
|
676
|
+
local ok, slotId = createPromise:Yield()
|
|
677
|
+
expect(ok).toEqual(true)
|
|
678
|
+
expect(type(slotId)).toEqual("string")
|
|
679
|
+
|
|
680
|
+
context.destroy()
|
|
681
|
+
end)
|
|
682
|
+
|
|
683
|
+
it("PromiseSelectNewSaveSlot never runs out of slots when unbounded", function()
|
|
684
|
+
local context = setup()
|
|
685
|
+
context.hasSaveSlots.MaxSlotCount.Value = math.huge
|
|
686
|
+
|
|
687
|
+
-- Allocate past the finite cap of 5; every call still yields a fresh slot
|
|
688
|
+
for _ = 1, 7 do
|
|
689
|
+
local newPromise = context.hasSaveSlots:PromiseSelectNewSaveSlot()
|
|
690
|
+
if not PromiseTestUtils.awaitSettled(newPromise, 10) then
|
|
691
|
+
expect("new game hung").toEqual("new game settled")
|
|
692
|
+
context.destroy()
|
|
693
|
+
return
|
|
694
|
+
end
|
|
695
|
+
expect(type(newPromise:Wait())).toEqual("string")
|
|
696
|
+
end
|
|
697
|
+
|
|
698
|
+
context.destroy()
|
|
699
|
+
end)
|
|
262
700
|
end)
|
|
263
701
|
|
|
264
702
|
describe("HasSaveSlots against a fake player (datastore down)", function()
|
|
@@ -8,6 +8,7 @@ local require = require(script.Parent.loader).load(script)
|
|
|
8
8
|
local CmdrService = require("CmdrService")
|
|
9
9
|
local HasSaveSlots = require("HasSaveSlots")
|
|
10
10
|
local Maid = require("Maid")
|
|
11
|
+
local Promise = require("Promise")
|
|
11
12
|
local SaveSlotCmdrUtils = require("SaveSlotCmdrUtils")
|
|
12
13
|
local SaveSlotDataService = require("SaveSlotDataService")
|
|
13
14
|
local ServiceBag = require("ServiceBag")
|
|
@@ -112,20 +113,59 @@ function SaveSlotCmdrService._registerCommands(self: SaveSlotCmdrService): ()
|
|
|
112
113
|
return `Switched to slot {slotIndex}.`
|
|
113
114
|
end)
|
|
114
115
|
|
|
116
|
+
self._cmdrService:RegisterCommand({
|
|
117
|
+
Name = "deselect-save-slot",
|
|
118
|
+
Description = "Clears the active save slot, returning to a no-slot state.",
|
|
119
|
+
Group = "SaveSlots",
|
|
120
|
+
Args = {},
|
|
121
|
+
}, function(context)
|
|
122
|
+
if not self._saveSlotDataService:GetActiveSlotId(context.Executor) then
|
|
123
|
+
return "No active slot."
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
self._maid
|
|
127
|
+
:GivePromise(self._hasSaveSlotsBinder:Promise(context.Executor))
|
|
128
|
+
:Then(function(hasSaveSlots)
|
|
129
|
+
return hasSaveSlots:PromiseDeselectSlot()
|
|
130
|
+
end)
|
|
131
|
+
:Wait()
|
|
132
|
+
|
|
133
|
+
return "Deselected active slot."
|
|
134
|
+
end)
|
|
135
|
+
|
|
115
136
|
self._cmdrService:RegisterCommand({
|
|
116
137
|
Name = "create-save-slot",
|
|
117
|
-
Description = "Creates a save slot
|
|
138
|
+
Description = "Creates a save slot. Defaults to the lowest free index when none is given.",
|
|
118
139
|
Group = "SaveSlots",
|
|
119
140
|
Args = {
|
|
120
141
|
{
|
|
121
142
|
Name = "Slot",
|
|
122
143
|
Type = "number",
|
|
123
|
-
Description = "Slot index to create.",
|
|
144
|
+
Description = "Slot index to create. Omit to use the lowest free index.",
|
|
145
|
+
Optional = true,
|
|
124
146
|
},
|
|
125
147
|
},
|
|
126
|
-
}, function(context, slotIndex: number)
|
|
148
|
+
}, function(context, slotIndex: number?)
|
|
127
149
|
local maxSlotCount = context.Executor:GetAttribute("MaxSlotCount")
|
|
128
|
-
|
|
150
|
+
|
|
151
|
+
if slotIndex == nil then
|
|
152
|
+
-- Default to the lowest free index, filling gaps left by deletions.
|
|
153
|
+
local used = {}
|
|
154
|
+
for _, metadata in self._saveSlotDataService:GetSlotList(context.Executor) do
|
|
155
|
+
used[metadata.SlotIndex] = true
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
local freeIndex = 1
|
|
159
|
+
while used[freeIndex] do
|
|
160
|
+
freeIndex += 1
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
if freeIndex > maxSlotCount then
|
|
164
|
+
return "All slots are already in use."
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
slotIndex = freeIndex
|
|
168
|
+
elseif (slotIndex < 1) or (slotIndex > maxSlotCount) then
|
|
129
169
|
return `Index must be in range [1, {maxSlotCount}].`
|
|
130
170
|
end
|
|
131
171
|
|
|
@@ -146,33 +186,60 @@ function SaveSlotCmdrService._registerCommands(self: SaveSlotCmdrService): ()
|
|
|
146
186
|
|
|
147
187
|
self._cmdrService:RegisterCommand({
|
|
148
188
|
Name = "delete-save-slot",
|
|
149
|
-
Description = "Deletes the given save
|
|
189
|
+
Description = "Deletes the given save slots.",
|
|
150
190
|
Group = "SaveSlots",
|
|
151
191
|
Args = {
|
|
152
192
|
{
|
|
153
|
-
Name = "
|
|
154
|
-
Type = "
|
|
155
|
-
Description = "Slot
|
|
193
|
+
Name = "Slots",
|
|
194
|
+
Type = "slotIndices",
|
|
195
|
+
Description = "Slot indices to delete (e.g. 1,2 or * for all).",
|
|
156
196
|
},
|
|
157
197
|
},
|
|
158
|
-
}, function(context,
|
|
159
|
-
local
|
|
160
|
-
|
|
161
|
-
|
|
198
|
+
}, function(context, slotIndices: { number })
|
|
199
|
+
local activeSlotId = self._saveSlotDataService:GetActiveSlotId(context.Executor)
|
|
200
|
+
|
|
201
|
+
-- Resolve indices to ids up front; `*` can include the active slot and duplicates.
|
|
202
|
+
local seen = {}
|
|
203
|
+
local toDelete = {}
|
|
204
|
+
for _, slotIndex in slotIndices do
|
|
205
|
+
local slotId = self._saveSlotDataService:GetSlotIdFromIndex(context.Executor, slotIndex)
|
|
206
|
+
if slotId and not seen[slotId] then
|
|
207
|
+
seen[slotId] = true
|
|
208
|
+
table.insert(toDelete, { slotIndex = slotIndex, slotId = slotId })
|
|
209
|
+
end
|
|
162
210
|
end
|
|
163
211
|
|
|
164
|
-
if
|
|
165
|
-
return "
|
|
212
|
+
if #toDelete == 0 then
|
|
213
|
+
return "No matching slots to delete."
|
|
166
214
|
end
|
|
167
215
|
|
|
168
216
|
self._maid
|
|
169
217
|
:GivePromise(self._hasSaveSlotsBinder:Promise(context.Executor))
|
|
170
218
|
:Then(function(hasSaveSlots)
|
|
171
|
-
|
|
219
|
+
-- Delete sequentially to avoid concurrent datastore saves. The active slot can't be
|
|
220
|
+
-- deleted while selected, so deselect it first (flushing its progress) when reached.
|
|
221
|
+
local promise = Promise.resolved()
|
|
222
|
+
for _, entry in toDelete do
|
|
223
|
+
promise = promise:Then(function()
|
|
224
|
+
if entry.slotId == activeSlotId then
|
|
225
|
+
return hasSaveSlots:PromiseDeselectSlot():Then(function()
|
|
226
|
+
return hasSaveSlots:PromiseDeleteSlot(entry.slotId)
|
|
227
|
+
end)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
return hasSaveSlots:PromiseDeleteSlot(entry.slotId)
|
|
231
|
+
end)
|
|
232
|
+
end
|
|
233
|
+
return promise
|
|
172
234
|
end)
|
|
173
235
|
:Wait()
|
|
174
236
|
|
|
175
|
-
|
|
237
|
+
local deletedIndices = {}
|
|
238
|
+
for _, entry in toDelete do
|
|
239
|
+
table.insert(deletedIndices, entry.slotIndex)
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
return `Deleted slot(s) {table.concat(deletedIndices, ", ")}.`
|
|
176
243
|
end)
|
|
177
244
|
end
|
|
178
245
|
|