private async Task <PlayerPreferences> GetOrCreatePreferencesAsync(NetUserId userId)
        {
            var prefs = await _db.GetPlayerPreferencesAsync(userId);

            if (prefs is null)
            {
                return(await _db.InitPrefsAsync(userId, HumanoidCharacterProfile.Default()));
            }

            return(prefs);
        }
Пример #2
0
        /// <summary>
        /// Retrieves preferences for the given username from storage.
        /// Creates and saves default preferences if they are not found, then returns them.
        /// </summary>
        public PlayerPreferences GetPreferences(string username)
        {
            var prefs = GetFromSql(username);

            if (prefs is null)
            {
                _preferencesDb.SaveSelectedCharacterIndex(username, 0);
                _preferencesDb.SaveCharacterSlot(username, HumanoidCharacterProfile.Default(), 0);
                prefs = GetFromSql(username);
            }

            return(prefs);
        }
        /// <summary>
        /// Retrieves preferences for the given username from storage.
        /// Creates and saves default preferences if they are not found, then returns them.
        /// </summary>
        public async Task <PlayerPreferences> GetPreferencesAsync(string username)
        {
            var prefs = await GetFromSql(username);

            if (prefs is null)
            {
                await _preferencesDb.SaveSelectedCharacterIndexAsync(username, 0);

                await _preferencesDb.SaveCharacterSlotAsync(username, HumanoidCharacterProfile.Default(), 0);

                prefs = await GetFromSql(username);
            }

            return(prefs);
        }
        public async Task TestDeleteCharacter()
        {
            var db       = GetDb();
            var username = new NetUserId(new Guid("640bd619-fc8d-4fe2-bf3c-4a5fb17d6ddd"));
            await db.InitPrefsAsync(username, HumanoidCharacterProfile.Default());

            await db.SaveCharacterSlotAsync(username, CharlieCharlieson(), 1);

            await db.SaveSelectedCharacterIndexAsync(username, 1);

            await db.SaveCharacterSlotAsync(username, null, 1);

            var prefs = await db.GetPlayerPreferencesAsync(username);

            Assert.That(!prefs.Characters.Any(p => p.Key != 0));
        }
        public async Task TestDeleteCharacter()
        {
            var db       = GetDb();
            var username = new NetUserId(new Guid("640bd619-fc8d-4fe2-bf3c-4a5fb17d6ddd"));

            IoCManager.Resolve <ISerializationManager>().Initialize();
            IoCManager.Resolve <IPrototypeManager>().LoadFromStream(new StringReader(Prototypes));
            await db.InitPrefsAsync(username, HumanoidCharacterProfile.Default());

            await db.SaveCharacterSlotAsync(username, CharlieCharlieson(), 1);

            await db.SaveSelectedCharacterIndexAsync(username, 1);

            await db.SaveCharacterSlotAsync(username, null, 1);

            var prefs = await db.GetPlayerPreferencesAsync(username);

            Assert.That(!prefs.Characters.Any(p => p.Key != 0));
        }
        public async void OnClientConnected(IPlayerSession session)
        {
            if (!ShouldStorePrefs(session.ConnectedClient.AuthType))
            {
                // Don't store data for guests.
                var prefsData = new PlayerPrefData
                {
                    PrefsLoaded = Task.CompletedTask,
                    Prefs       = new PlayerPreferences(
                        new[] { new KeyValuePair <int, ICharacterProfile>(0, HumanoidCharacterProfile.Default()) },
                        0)
                };

                _cachedPlayerPrefs[session.UserId] = prefsData;
            }
            else
            {
                var prefsData = new PlayerPrefData();
                var loadTask  = LoadPrefs();
                prefsData.PrefsLoaded = loadTask;
                _cachedPlayerPrefs[session.UserId] = prefsData;

                await loadTask;

                async Task LoadPrefs()
                {
                    var prefs = await GetOrCreatePreferencesAsync(session.UserId);

                    prefsData.Prefs = prefs;

                    var msg = _netManager.CreateNetMessage <MsgPreferencesAndSettings>();

                    msg.Preferences = prefs;
                    msg.Settings    = new GameSettings
                    {
                        MaxCharacterSlots = MaxCharacterSlots
                    };
                    _netManager.ServerSendMessage(msg, session.ConnectedClient);
                }
            }
        }