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 TournamentGroup(Tournament tournament, int number)
            : this()
        {
            Require.NotNull(tournament, nameof(tournament));

            this.Id = Guid.NewGuid();

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

            if (number <= 0)
            {
                throw new ArgumentException(nameof(number));
            }

            this.Number = number;
        }
        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 void CheckOpenShouldStart()
        {
            // Arrange
            var mockUnitOfWork = TestUtils.GetUnitOfWorkMock();
            var tournamentRepository = new MockTournamentRepository();
            mockUnitOfWork.SetupGet(x => x.Tournaments).Returns(tournamentRepository);
            var unitOfWork = mockUnitOfWork.Object;
            var gameServiceMock = new Mock<IGameService>();
            var service = new TournamentService(unitOfWork, gameServiceMock.Object, TestUtils.MockMapTemplateProvider());

            var openTournament = new Tournament(
                "Tournament",
                8,
                3,
                3,
                3,
                DateTime.UtcNow.AddDays(-1),
                DateTime.UtcNow,
                new Domain.Games.GameOptions
                {
                    NumberOfPlayersPerTeam = 1
                });
            tournamentRepository.Add(openTournament);

            for (int i = 0; i < 8; ++i)
            {
                openTournament.AddUser(TestUtils.CreateUser($"User{i}"));
            }

            // Act
            var started = service.CheckOpenTournaments();

            // Assert
            Assert.IsTrue(started);
            Assert.AreEqual(TournamentState.Groups, openTournament.State);
            Assert.AreEqual(12, openTournament.Pairings.Count());
        }
            public void Success()
            {
                var tournament = new Tournament("Tournament", 16, 3, 3, 3, DateTime.UtcNow, DateTime.UtcNow, this.options);

                Assert.AreNotEqual(Guid.Empty, tournament.Id);

                Assert.AreEqual(16, tournament.NumberOfTeams);

                Assert.AreEqual(3, tournament.NumberOfGroupGames);
                Assert.AreEqual(3, tournament.NumberOfKnockoutGames);
                Assert.AreEqual(3, tournament.NumberOfFinalGames);

                Assert.AreEqual(this.options.Id, tournament.OptionsId);
                Assert.AreEqual(this.options, tournament.Options);
            }
 public void CreateGamesForPairings(Tournament tournament)
 {
     foreach(var pairing in tournament.Pairings.Where(
         x => x.State == PairingState.None && x.Games.Count() != x.NumberOfGames))
     {
         this.CreateGamesForPairing(pairing);
     }
 }
        public void SynchronizeGamesToPairings(Tournament tournament)
        {
            foreach(var pairing in tournament.Pairings.Where(x => x.State == PairingState.Active))
            {
                // Synchronize number of won games
                pairing.TeamAWon = this.CountWonGamesForTeam(pairing.Games, pairing.TeamA);
                pairing.TeamBWon = this.CountWonGamesForTeam(pairing.Games, pairing.TeamB);

                if (pairing.CanWinnerBeDetermined)
                {
                    pairing.State = PairingState.Done;
                    pairing.Loser.State = TournamentTeamState.InActive;

                    // TODO: Generate domain event for winner
                    // TODO: Generate domain event for loser
                }
            }
        }
 public TournamentTeam(Tournament tournament)
     : this()
 {
     this.Tournament = tournament;
     this.TournamentId = tournament.Id;
 }