Example #1
0
        public Task <bool> Handle(AuthorizeNewAccountRequest request, CancellationToken cancellationToken)
        {
            using (_threadContextSessionProvider.CreateSessionScope())
            {
                var user = _botUserRepository.ListBySpecification(new UndeletedEntities <BotUser>())
                           .FirstOrDefault(x =>
                                           !string.IsNullOrEmpty(x.PasswordHash) &&
                                           BCrypt.Net.BCrypt.Verify(request.Password, x.PasswordHash));
                if (user == null)
                {
                    return(Task.FromResult(false));
                }

                if (user.Id == request.CurrentUserId)
                {
                    return(Task.FromResult(true));
                }
                var currentUser = _botUserRepository.Get(request.CurrentUserId);
                foreach (var currentUserUserAccount in currentUser.UserAccounts)
                {
                    currentUserUserAccount.User = user;
                    user.UserAccounts.Add(currentUserUserAccount);
                }

                _botUserRepository.Save(user);
                currentUser.UserAccounts.Clear();
                _botUserRepository.Save(currentUser);
                _botUserRepository.Delete(currentUser);
            }
            return(Task.FromResult(true));
        }
Example #2
0
        public Task <Unit> Handle(ScheduledEventRequest <DistributionByTeamsEventMetadata> message,
                                  CancellationToken cancellationToken)
        {
            using (_threadContextSessionProvider.CreateSessionScope())
            {
                var game           = _gameRepository.Get(message.MetaData.GameId);
                var administrators = _botUserRepository.ListBySpecification(new UndeletedEntities <BotUser>())
                                     .Where(x => x.Role >= EUserRole.Moderator).ToList();

                if (game.AcceptedPlayers.Count != _serviceConfiguration.PlayersPerTeam * 2)
                {
                    NotifyAboutUnsuccessfulCollecting(game, administrators);
                }
                else
                {
                    DistributePlayersPerTeams(game);
                    NotifyAboutSuccessfulCollecting(game, game.DistributedPlayers, administrators);
                    NotifyPlayers(game.DistributedPlayers, game);
                }
                _scheduler.DeleteEvent <IGameScheduledEventMetadata>(x => x.GameId == game.Id && !(x is PlayerGameAcceptanceTimeoutEventMetadata));

                _gameRepository.Save(game);
            }

            return(Task.FromResult(Unit.Value));
        }
Example #3
0
 public Task <bool> Handle(CheckUniquePassword request, CancellationToken cancellationToken)
 {
     using (_threadContextSessionProvider.CreateSessionScope())
     {
         return(Task.FromResult(_botUserRepository.ListBySpecification(new UndeletedEntities <BotUser>())
                                .Any(x => !string.IsNullOrEmpty(x.PasswordHash) && BCrypt.Net.BCrypt.Verify(request.Password, x.PasswordHash))));
     }
 }
 public Task <Unit> Handle(CancelGameRequest request, CancellationToken cancellationToken)
 {
     using (_threadContextSessionProvider.CreateSessionScope())
     {
         var game           = _gameRepository.Get(request.GameId);
         var administrators = _botUserRepository.ListBySpecification(new UndeletedEntities <BotUser>())
                              .Where(x => x.Role >= EUserRole.Moderator).ToList();
         NotifyAboutUnsuccessfulCollecting(game, administrators);
         NotifyPlayers(game);
         _scheduler.DeleteEvent <IGameScheduledEventMetadata>(x => x.GameId == game.Id);
         game.IsActive = false;
         _gameRepository.Save(game);
     }
     return(Task.FromResult(Unit.Value));
 }
        public Task <BotUser> Handle(AuthorizeRequest request, CancellationToken cancellationToken)
        {
            using (_threadContextSessionProvider.CreateSessionScope())
            {
                var users = _botUserRepository.ListBySpecification(new UndeletedEntities <BotUser>());
                var user  = GetUserByCommunicator(request.Communicator,
                                                  _botUserRepository.ListBySpecification(new UndeletedEntities <BotUser>()));

                if (user == null)
                {
                    user = new BotUser()
                    {
                        Id   = Guid.NewGuid(),
                        Role = EUserRole.UnregisteredUser
                    };
                    user.UserAccounts.Add(GetAccount(request.Communicator, user));

                    _logger.Info("User added");
                    _botUserRepository.Save(user);
                }

                return(Task.FromResult(user));
            }
        }
Example #6
0
        public Task <Unit> Handle(RegisterRequest request, CancellationToken cancellationToken)
        {
            using (_threadContextSessionProvider.CreateSessionScope())
            {
                var users = _botUserRepository.ListBySpecification(new UndeletedEntities <BotUser>()).ToList();
                if (users.Count() > MaxUsersCount)
                {
                    throw new ArgumentOutOfRangeException("—оздано максимальное количество пользователей");
                }

                var user = GetUserByCommunicator(request.Communicator, users);

                if (user == null)
                {
                    user = new BotUser()
                    {
                        Id           = Guid.NewGuid(),
                        FirstName    = request.FirstName,
                        LastName     = request.LastName,
                        Role         = EUserRole.User,
                        UserAccounts = new List <BaseAccount>(),
                        PasswordHash = BCrypt.Net.BCrypt.HashPassword(request.Password)
                    };
                    user.UserAccounts.Add(GetAccount(request.Communicator, user));

                    _logger.Info("User added");
                }
                else
                {
                    user.FirstName = request.FirstName;
                    user.LastName  = request.LastName;
                    if (user.Role == EUserRole.UnregisteredUser)
                    {
                        user.Role = EUserRole.User;
                    }
                    user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(request.Password);
                    _logger.Info("User updated");
                }

                _botUserRepository.Save(user);
            }
            return(Unit.Task);
        }