public async Task <bool> SavePlayerSkillsAsync(PlayerController player)
        {
            var state = player.BuildPlayerState();

            if (state == null || string.IsNullOrEmpty(state.UserId))
            {
                return(false);
            }

            return(await Task.Run(() =>
            {
                if (client.Desynchronized)
                {
                    return false;
                }
                var characterUpdate = new CharacterSkillUpdate
                {
                    Level = state.Level,
                    Experience = state.Experience,
                    UserId = state.UserId,
                    CharacterId = state.CharacterId
                };

                if (lastSavedSkills.TryGetValue(player.UserId, out var lastUpdate))
                {
                    if (!RequiresUpdate(lastUpdate, characterUpdate))
                    {
                        return true; // return true so we dont get a red name in the player list just because the exp hasnt changed.
                    }
                }

                connection.SendNoAwait("update_character_skills", characterUpdate);
                //if (response != null && response.TryGetValue<bool>(out var result) && result)
                //{
                //    lastSavedSkills[player.UserId] = characterUpdate;
                //    lastSavedSkillsTime[player.UserId] = DateTime.UtcNow;
                //    return true;
                //}
                lastSavedSkills[player.UserId] = characterUpdate;
                lastSavedSkillsTime[player.UserId] = DateTime.UtcNow;
                return true;
            }));
        }
        private bool RequiresUpdate(CharacterSkillUpdate oldState, CharacterSkillUpdate newState)
        {
            if (!lastSavedSkillsTime.TryGetValue(oldState.UserId, out var date))
            {
                return(true);
            }

            if (DateTime.UtcNow - date < TimeSpan.FromSeconds(ForceSaveInterval))
            {
                return(false); // don't save yet or we will be saving on each update.
            }
            for (var i = 0; i < oldState.Experience.Length; ++i)
            {
                var oldExp = oldState.Experience[i];
                var newExp = newState.Experience[i];
                if (oldExp != newExp)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #3
0
        public async Task <bool> SavePlayerSkillsAsync(PlayerController player)
        {
            if (IntegrityCheck.IsCompromised)
            {
                return(false);
            }

            var state = player.BuildPlayerState();

            var characterUpdate = new CharacterSkillUpdate
            {
                Experience = state.Experience,
                UserId     = state.UserId
            };

            var response = await connection.SendAsync("update_character_skills", characterUpdate);

            if (response != null && response.TryGetValue <bool>(out var result))
            {
                return(result);
            }

            return(false);
        }