コード例 #1
0
        /// <summary>
        /// Saving user statistics both in the storage and in the json file database
        /// if user is identified by his/her authorization <paramref name="token"/>.
        /// </summary>
        /// <param name="token">User's authorization token.</param>
        /// <param name="outcome">Game outcome. One of <see cref="GameOutcome"/></param>
        /// <param name="move">Chosen move option. One of <see cref="MoveOptions"/></param>
        /// <returns>Returns true if user was found by token, else returns false.</returns>
        public async Task <bool> SaveAsync(string token, GameOutcome outcome, MoveOptions move)
        {
            // get user id
            var userWithToken = (await _tokens.GetAllAsync()).Where(tk => tk.Item == token).FirstOrDefault();

            // if user was not found
            if (userWithToken == null)
            {
                // only one logger message here so as not to litter the file with messages
                // about saving statistics, since this method will be called very often
                _logger.LogInformation($"{nameof(StatisticsService)}: User was not identified for saving statistics. The authorization token did not exist or expired.");
                return(false);
            }

            // get user id
            var userId = userWithToken.Id;

            // add user statistics to the storage if it doesn't exist
            await _statistics.AddAsync(new UserStatistics(userId), userId, new StatisticsEqualityComparer());

            // change state
            var record = await _statistics.GetAsync(userId);

            record.AddRoundInfo(DateTime.Now, outcome, move);

            // map with StatisticsDb
            var statisticsToSave = ModelsMapper.ToStatisticsDb(record);

            //save to file
            await _jsonDataService.WriteAsync(_options.StatisticsPath + $"{userId}.json", statisticsToSave);

            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Saving user both in the storage and in the json file database.
        /// </summary>
        /// <param name="login">User login.</param>
        /// <param name="password">User password.</param>
        /// <returns>Returns whether the user was saved.
        /// True if user was saved.
        /// False if user was not saved (because already exists).
        /// </returns>
        public async Task <bool> SaveAsync(string login, string password)
        {
            _logger.LogInformation($"{nameof(UsersService)}: Saving a new user.");

            // add user to the user storage
            var userId = await _users.AddAsync(new User(login, password), null, new UserEqualityComparer());

            // if user was not added because already exists
            if (userId == null)
            {
                _logger.LogInformation($"{nameof(UsersService)}: User was not added because the login {login} already exists.");
                return(false);
            }

            _logger.LogInformation($"{nameof(UsersService)}: New user with login {login} was added to the storage.");

            // create empty statistics for the new user in memory storage
            var userStatistics = new UserStatistics((int)userId);
            await _statistics.AddAsync(userStatistics, (int)userId);

            _logger.LogInformation($"{nameof(UsersService)}: Empty statistics for the new user with login {login} was added to the storage.");

            // save empty statistics for the new user to the json-file database
            var statisticsToSave = ModelsMapper.ToStatisticsDb(userStatistics);
            await _statDataService.WriteAsync($"{_options.StatisticsPath}{userId}.json", statisticsToSave);

            _logger.LogInformation($"{nameof(UsersService)}: Empty statistics for the new user with login {login} was saved to the file {_options.StatisticsPath}{userId}.json.");

            // get all users
            var users = await _users.GetAllAsync();

            // map with UserDb
            var usersToSave = users.Select(user => ModelsMapper.ToUserDb(user.Id, user.Item));

            // save to file
            await _userDataService.WriteAsync(_options.UsersPath, usersToSave);

            _logger.LogInformation($"{nameof(UsersService)}: All users were rewritten in {_options.UsersPath}.");

            return(true);
        }