Esempio n. 1
0
        private async void HandleUpdateCharacterMessage(MsgUpdateCharacter message)
        {
            var slot    = message.Slot;
            var profile = message.Profile;
            var userId  = message.MsgChannel.UserId;

            if (!_cachedPlayerPrefs.TryGetValue(userId, out var prefsData) || !prefsData.PrefsLoaded.IsCompleted)
            {
                Logger.WarningS("prefs", $"User {userId} tried to modify preferences before they loaded.");
                return;
            }

            if (slot < 0 || slot >= MaxCharacterSlots)
            {
                return;
            }

            var curPrefs = prefsData.Prefs !;

            var arr = new ICharacterProfile[MaxCharacterSlots];

            curPrefs.Characters.ToList().CopyTo(arr, 0);

            arr[slot] = HumanoidCharacterProfile.EnsureValid((HumanoidCharacterProfile)profile, _protos);

            prefsData.Prefs = new PlayerPreferences(arr, slot);

            if (ShouldStorePrefs(message.MsgChannel.AuthType))
            {
                await _db.SaveCharacterSlotAsync(message.MsgChannel.UserId, message.Profile, message.Slot);
            }
        }
        private async void HandleUpdateCharacterMessage(MsgUpdateCharacter message)
        {
            var slot    = message.Slot;
            var profile = message.Profile;
            var userId  = message.MsgChannel.UserId;

            if (profile == null)
            {
                Logger.WarningS("prefs",
                                $"User {userId} sent a {nameof(MsgUpdateCharacter)} with a null profile in slot {slot}.");
                return;
            }

            if (!_cachedPlayerPrefs.TryGetValue(userId, out var prefsData) || !prefsData.PrefsLoaded.IsCompleted)
            {
                Logger.WarningS("prefs", $"User {userId} tried to modify preferences before they loaded.");
                return;
            }

            if (slot < 0 || slot >= MaxCharacterSlots)
            {
                return;
            }

            var curPrefs = prefsData.Prefs !;

            var profiles = new Dictionary <int, ICharacterProfile>(curPrefs.Characters)
            {
                [slot] = HumanoidCharacterProfile.EnsureValid((HumanoidCharacterProfile)profile, _protos)
            };

            prefsData.Prefs = new PlayerPreferences(profiles, slot);

            if (ShouldStorePrefs(message.MsgChannel.AuthType))
            {
                await _db.SaveCharacterSlotAsync(message.MsgChannel.UserId, message.Profile, message.Slot);
            }
        }