예제 #1
0
        public Player CreatePlayer(CreatePlayerRequest createPlayerRequest, ApplicationUser applicationUser, bool linkCurrentUserToThisPlayer = false)
        {
            if (createPlayerRequest == null)
            {
                throw new ArgumentNullException(nameof(createPlayerRequest));
            }
            ValidatePlayerNameIsNotNullOrWhiteSpace(createPlayerRequest.Name);
            if (!applicationUser.CurrentGamingGroupId.HasValue)
            {
                throw new UserHasNoGamingGroupException(applicationUser.Id);
            }
            int gamingGroupId = createPlayerRequest.GamingGroupId ?? applicationUser.CurrentGamingGroupId.Value;

            ThrowPlayerAlreadyExistsExceptionIfPlayerExistsWithThisName(createPlayerRequest.Name, gamingGroupId);

            var newPlayer = new Player
            {
                Name              = createPlayerRequest.Name,
                Active            = true,
                ApplicationUserId = linkCurrentUserToThisPlayer ? applicationUser.Id : null,
                GamingGroupId     = gamingGroupId
            };

            newPlayer = _dataContext.Save(newPlayer, applicationUser);
            _dataContext.CommitAllChanges();

            new Task(() => _eventTracker.TrackPlayerCreation(applicationUser)).Start();

            return(newPlayer);
        }
예제 #2
0
        public virtual Player Save(Player player, ApplicationUser currentUser)
        {
            ValidatePlayerIsNotNull(player);
            ValidatePlayerNameIsNotNullOrWhiteSpace(player.Name);
            ValidatePlayerWithThisNameDoesntAlreadyExist(player, currentUser);
            bool alreadyInDatabase = player.AlreadyInDatabase();

            var newPlayer = dataContext.Save(player, currentUser);

            dataContext.CommitAllChanges();

            if (!alreadyInDatabase)
            {
                new Task(() => eventTracker.TrackPlayerCreation(currentUser)).Start();
            }
            else
            {
                if (!player.Active)
                {
                    this.RecalculateNemeses(player, currentUser);
                }
            }

            return(newPlayer);
        }
예제 #3
0
        public Player CreatePlayer(CreatePlayerRequest createPlayerRequest, ApplicationUser applicationUser, bool linkCurrentUserToThisPlayer = false)
        {
            ValidateRequestIsNotNull(createPlayerRequest);
            ValidatePlayerNameIsNotNullOrWhiteSpace(createPlayerRequest.Name);
            ValidateRequestedEmailIsntSetAtTheSameTimeAsAttemptingToLinktoCurrentPlayer(createPlayerRequest,
                                                                                        linkCurrentUserToThisPlayer);
            var gamingGroupId = ValidateAndGetGamingGroupId(createPlayerRequest.GamingGroupId, applicationUser);

            ValidatePlayerDoesntExistWithThisName(createPlayerRequest.Name, gamingGroupId);

            ValidateUserNotAlreadyRegisteredWithThisEmail(
                gamingGroupId, createPlayerRequest.PlayerEmailAddress);

            var newPlayer = new Player
            {
                Name              = createPlayerRequest.Name,
                Active            = true,
                ApplicationUserId = linkCurrentUserToThisPlayer ? applicationUser.Id : null,
                GamingGroupId     = gamingGroupId
            };

            newPlayer = _dataContext.Save(newPlayer, applicationUser);

            if (!string.IsNullOrWhiteSpace(createPlayerRequest.PlayerEmailAddress))
            {
                var playerInvitation = new PlayerInvitation
                {
                    EmailSubject       = $"NemeStats Invitation from {applicationUser.UserName}",
                    InvitedPlayerEmail = createPlayerRequest.PlayerEmailAddress,
                    InvitedPlayerId    = newPlayer.Id,
                    GamingGroupId      = gamingGroupId
                };
                _playerInviter.InvitePlayer(playerInvitation, applicationUser);
            }

            _dataContext.CommitAllChanges();

            new Task(() => _eventTracker.TrackPlayerCreation(applicationUser)).Start();

            return(newPlayer);
        }