Don't allow deletion of the selected character slot.

This commit is contained in:
Pieter-Jan Briers
2020-10-07 18:02:10 +02:00
parent a984076574
commit 882ed619fd
3 changed files with 56 additions and 14 deletions

View File

@@ -131,14 +131,36 @@ namespace Content.Server.Preferences
var curPrefs = prefsData.Prefs!;
// If they try to delete the slot they have selected then we switch to another one.
// Of course, that's only if they HAVE another slot.
int? nextSlot = null;
if (curPrefs.SelectedCharacterIndex == slot)
{
var (ns, profile) = curPrefs.Characters.FirstOrDefault(p => p.Key != message.Slot);
if (profile == null)
{
// Only slot left, can't delete.
return;
}
nextSlot = ns;
}
var arr = new Dictionary<int, ICharacterProfile>(curPrefs.Characters);
arr.Remove(slot);
prefsData.Prefs = new PlayerPreferences(arr, slot);
prefsData.Prefs = new PlayerPreferences(arr, nextSlot ?? curPrefs.SelectedCharacterIndex);
if (ShouldStorePrefs(message.MsgChannel.AuthType))
{
await _db.SaveCharacterSlotAsync(message.MsgChannel.UserId, null, message.Slot);
if (nextSlot != null)
{
await _db.DeleteSlotAndSetSelectedIndex(userId, slot, nextSlot.Value);
}
else
{
await _db.SaveCharacterSlotAsync(userId, null, slot);
}
}
}