public void SynchronizeGames()
        {
            // Arrange
            var mockUnitOfWork = TestUtils.GetUnitOfWorkMock();
            mockUnitOfWork.SetupGet(x => x.Tournaments).Returns(new MockTournamentRepository());
            mockUnitOfWork.SetupGet(x => x.Games).Returns(new MockGamesRepository());
            var unitOfWork = mockUnitOfWork.Object;

            var gameServiceMock = new Mock<IGameService>();
            var service = new TournamentService(unitOfWork, gameServiceMock.Object, TestUtils.MockMapTemplateProvider());

            var tournament = new Tournament("T", 2, 0, 1, 1, DateTime.UtcNow, DateTime.UtcNow, new GameOptions { NumberOfPlayersPerTeam = 1 });

            var user1 = TestUtils.CreateUser("1");
            var user2 = TestUtils.CreateUser("2");

            var teamA = new TournamentTeam(tournament);
            teamA.AddUser(user1);

            var teamB = new TournamentTeam(tournament);
            teamB.AddUser(user2);

            var pairing = new TournamentPairing(tournament, 1, 1, teamA, teamB, 1);
            pairing.State = PairingState.Active;
            tournament.Pairings = new[] { pairing };

            tournament.Teams.Add(teamA);
            tournament.Teams.Add(teamB);

            var game = new Game(null, Enums.GameType.Tournament, "T", "WorldDeluxe", new GameOptions());

            game.State = Enums.GameState.Ended;

            var team1 = new Team(game);
            team1.Players.Add(new Player(user1, team1)
            {
                Outcome = Enums.PlayerOutcome.Won
            });
            game.Teams.Add(team1);

            var team2 = new Team(game);
            team2.Players.Add(new Player(user2, team2)
            {
                Outcome = Enums.PlayerOutcome.Defeated
            });
            game.Teams.Add(team2);

            pairing.Games = new[] { game };

            // Act
            service.SynchronizeGamesToPairings(tournament);

            // Assert
            Assert.IsTrue(tournament.Pairings.First().CanWinnerBeDetermined);
            Assert.AreEqual(PairingState.Done, pairing.State);
            Assert.AreEqual(TournamentTeamState.InActive, teamB.State);
            Assert.AreEqual(TournamentTeamState.Active, teamA.State);
        }
        public TournamentParticipant(TournamentTeam team, User user)
            : this()
        {
            this.TeamId = team.Id;
            this.Team = team;

            this.UserId = user.Id;
            this.User = user;
        }
Exemple #3
0
        public void DeleteTeam(User user, TournamentTeam team)
        {
            Require.NotNull(user, nameof(user));
            Require.NotNull(team, nameof(team));

            if (!this.CanChangeTeams)
            {
                throw new DomainException(ErrorCode.TournamentCannotJoinLeave, "Cannot delete team");
            }

            if (user.Id != team.CreatedById)
            {
                throw new DomainException(ErrorCode.TournamentTeamDeleteNoPermission, "User does not have permission to delete team");
            }

            this.Teams.Remove(team);

            // TODO: Generate domain events for all players in team
        }
        public TournamentPairing(Tournament tournament, int phase, int order, TournamentTeam teamA, TournamentTeam teamB, int numberOfGames)
            : this()
        {
            Require.NotNull(teamA, nameof(teamA));
            Require.NotNull(teamB, nameof(teamB));

            Debug.Assert(numberOfGames % 2 != 0, "NumberOfGames has to be odd");

            this.Tournament   = tournament;
            this.TournamentId = tournament.Id;

            this.Phase = phase;
            this.Order = order;

            this.TeamA   = teamA;
            this.TeamAId = teamA.Id;

            this.TeamB   = teamB;
            this.TeamBId = teamB.Id;

            this.NumberOfGames = numberOfGames;
        }
        public TournamentPairing(Tournament tournament, int phase, int order, TournamentTeam teamA, TournamentTeam teamB, int numberOfGames)
            : this()
        {
            Require.NotNull(teamA, nameof(teamA));
            Require.NotNull(teamB, nameof(teamB));

            Debug.Assert(numberOfGames % 2 != 0, "NumberOfGames has to be odd");

            this.Tournament = tournament;
            this.TournamentId = tournament.Id;

            this.Phase = phase;
            this.Order = order;

            this.TeamA = teamA;
            this.TeamAId = teamA.Id;

            this.TeamB = teamB;
            this.TeamBId = teamB.Id;

            this.NumberOfGames = numberOfGames;
        }
 private TournamentPairing CreatePairing(int order, TournamentTeam teamA, TournamentTeam teamB, int numberOfGames)
 {
     var pairing = new TournamentPairing(this, this.Phase, order, teamA, teamB, numberOfGames);
     this.Pairings.Add(pairing);
     return pairing;
 }
        public void DeleteTeam(User user, TournamentTeam team)
        {
            Require.NotNull(user, nameof(user));
            Require.NotNull(team, nameof(team));

            if (!this.CanChangeTeams)
            {
                throw new DomainException(ErrorCode.TournamentCannotJoinLeave, "Cannot delete team");
            }

            if (user.Id != team.CreatedById)
            {
                throw new DomainException(ErrorCode.TournamentTeamDeleteNoPermission, "User does not have permission to delete team");
            }

            this.Teams.Remove(team);

            // TODO: Generate domain events for all players in team
        }
        /// <summary>
        /// Create new team in tournament
        /// </summary>
        /// <param name="user">User creating team</param>
        /// <param name="name">Name of new team</param>
        /// <param name="password">Optional password to protect team membership</param>
        public TournamentTeam CreateTeam(User user, string name, string password = null)
        {
            Require.NotNull(user, nameof(user));
            Require.NotNullOrEmpty(name, nameof(name));

            if (!this.CanChangeTeams)
            {
                throw new DomainException(ErrorCode.TournamentCannotJoinLeave, "Cannot create team");
            }

            if (this.Teams.Count() >= this.NumberOfTeams)
            {
                throw new DomainException(ErrorCode.TournamentTooManyTeams, "Too many teams already");
            }

            if (this.Teams.Any(t => string.Equals(t.Name, name, StringComparison.OrdinalIgnoreCase)))
            {
                throw new DomainException(ErrorCode.TournamentDuplicateTeamName, "Team with name already exists");
            }

            var team = new TournamentTeam(this)
            {
                CreatedBy = user,
                CreatedById = user.Id,

                Name = name,
                Password = password
            };

            team.AddUser(user);

            this.Teams.Add(team);

            return team;
        }
        /// <summary>
        /// Add user to tournament and given team
        /// </summary>
        /// <param name="user">User to join</param>
        /// <param name="team">Team to join</param>
        /// <param name="password">Optional password if team is protected</param>
        /// <returns>Team the user was added to</returns>
        public TournamentTeam AddUser(User user, TournamentTeam team, string password = null)
        {
            Require.NotNull(user, nameof(user));

            if (!this.CanChangeTeams)
            {
                throw new DomainException(ErrorCode.TournamentCannotJoinLeave, "Cannot leave team");
            }

            if (team == null)
            {
                if (this.Teams.Count() >= this.NumberOfTeams)
                {
                    throw new DomainException(ErrorCode.TournamentTooManyTeams, "Too many teams already");
                }

                team = new TournamentTeam(this);
                team.Name = user.UserName;
                team.CreatedBy = user;
                team.CreatedById = user.Id;

                this.Teams.Add(team);
            }
            else
            {
                if (!team.PasswordMatch(password))
                {
                    throw new DomainException(ErrorCode.TournamentIncorrectPassword, "Cannot join team, password incorrect");
                }
            }

            team.AddUser(user);

            return team;
        }
 private int CountWonGamesForTeam(IEnumerable<Game> games, TournamentTeam team)
 {
     // Return number of games that
     // - have ended
     // - a winning team contains
     //  - the first player of the given team (enough to check)
     return games.Count(x => x.State == Enums.GameState.Ended
             && x.Teams.Any(t =>
                 t.Outcome == Enums.PlayerOutcome.Won
                 && t.Players.Select(p => p.UserId).Contains(team.Participants.First().UserId)));
 }