Exemple #1
0
 public GameInstance(Game game, Competition comp, TournamentRound round, TournamentPairing pair)
 {
     Game         = game;
     Competition  = comp;
     CurrentRound = round;
     Pairing      = pair;
 }
        private void CreateGamesForPairing(TournamentPairing pairing, IRandomGen random)
        {
            Debug.Assert(pairing.State == PairingState.None, "Pairing state is not correct");

            var systemUser = this.unitOfWork.Users.FindByName("System");

            for (int i = 0; i < pairing.NumberOfGames; ++i)
            {
                var game = gameService.Create(
                    Enums.GameType.Tournament,
                    systemUser,
                    pairing.GenerateGameName(i),
                    pairing.Tournament.GetMapTemplateForGame(random),
                    pairing.Tournament.Options);

                game.TournamentPairingId = pairing.Id;
                pairing.Games.Add(game);

                var teamA = game.AddTeam();
                foreach (var participant in pairing.TeamA.Participants)
                {
                    teamA.AddPlayer(participant.User);
                }

                var teamB = game.AddTeam();
                foreach (var participant in pairing.TeamB.Participants)
                {
                    teamB.AddPlayer(participant.User);
                }

                game.Start(this.mapTemplateProvider.GetTemplate(game.MapTemplateName), random);
            }

            pairing.State = PairingState.Active;
        }
Exemple #3
0
        public override bool ApplyPairing(TournamentPairing pairing)
        {
            if (pairing == null)
            {
                throw new ArgumentNullException("pairing");
            }

            if (this.Locked)
            {
                // If we (and, all of our decentants) are decided, return false, indicating that no node below us is in a state that needs a score.
                return false;
            }

            if (pairing.TeamScores.Count != 2 || pairing.TeamScores[0] == null || pairing.TeamScores[1] == null || pairing.TeamScores[0].Team == null || pairing.TeamScores[1].Team == null)
            {
                // If the pairing did not contain exactly two teams, or if either of the teams passed was null.
                throw new ArgumentException("A bye was passed as a pairing.", "pairing");
            }

            if (this.previousWinnerNode.IsDecided && this.stayNode.IsDecided && !(this.previousWinnerNode.Score != null || this.stayNode.Score != null))
            {
                // If our component nodes have played out, but we haven't
                var teamA = pairing.TeamScores[0].Team;
                var scoreA = pairing.TeamScores[0].Score;
                var teamB = pairing.TeamScores[1].Team;
                var scoreB = pairing.TeamScores[1].Score;

                if (this.previousWinnerNode.Team.TeamId == teamB.TeamId && this.stayNode.Team.TeamId == teamA.TeamId)
                {
                    // If the order of the pairing is reversed, we will normalize the pairing to us.
                    var teamSwap = teamA;
                    teamB = teamA;
                    teamA = teamSwap;

                    var scoreSwap = scoreA;
                    scoreB = scoreA;
                    scoreA = scoreSwap;
                }

                if (this.previousWinnerNode.Team.TeamId == teamA.TeamId && this.stayNode.Team.TeamId == teamB.TeamId)
                {
                    // If we are a match, assign the scores.
                    this.previousWinnerNode.Score = scoreA;
                    this.stayNode.Score = scoreB;
                    this.Lock();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return (!this.previousWinnerNode.IsDecided && this.previousWinnerNode.ApplyPairing(pairing)) || (!this.stayNode.IsDecided && this.stayNode.ApplyPairing(pairing));
            }
        }
Exemple #4
0
        /// <inheritdoc />
        public override bool ApplyPairing(TournamentPairing pairing)
        {
            if (pairing == null)
            {
                throw new ArgumentNullException(nameof(pairing));
            }

            return(false);
        }
Exemple #5
0
        public override bool ApplyPairing(TournamentPairing pairing)
        {
            if (pairing == null)
            {
                throw new ArgumentNullException("pairing");
            }

            return false;
        }
Exemple #6
0
        /// <inheritdoc />
        public override bool ApplyPairing(TournamentPairing pairing)
        {
            if (pairing == null)
            {
                throw new ArgumentNullException(nameof(pairing));
            }

            if (this.Locked)
            {
                // If we (and, all of our decentants) are decided, return false, indicating that no node below us is in a state that needs a score.
                return(false);
            }

            if (pairing.TeamScores.Count != 2 || pairing.TeamScores[0] == null || pairing.TeamScores[1] == null || pairing.TeamScores[0].Team == null || pairing.TeamScores[1].Team == null)
            {
                // If the pairing did not contain exactly two teams, or if either of the teams passed was null.
                throw new ArgumentException("A bye was passed as a pairing.", nameof(pairing));
            }

            if (this.PreviousWinnerNode.IsDecided && this.StayNode.IsDecided && !(this.PreviousWinnerNode.Score != null || this.StayNode.Score != null))
            {
                // If our component nodes have played out, but we haven't
                var teamA  = pairing.TeamScores[0].Team;
                var scoreA = pairing.TeamScores[0].Score;
                var teamB  = pairing.TeamScores[1].Team;
                var scoreB = pairing.TeamScores[1].Score;

                if (this.PreviousWinnerNode.Team.TeamId == teamB.TeamId && this.StayNode.Team.TeamId == teamA.TeamId)
                {
                    // If the order of the pairing is reversed, we will normalize the pairing to us.
                    var teamSwap = teamA;
                    teamB = teamA;
                    teamA = teamSwap;

                    var scoreSwap = scoreA;
                    scoreB = scoreA;
                    scoreA = scoreSwap;
                }

                if (this.PreviousWinnerNode.Team.TeamId == teamA.TeamId && this.StayNode.Team.TeamId == teamB.TeamId)
                {
                    // If we are a match, assign the scores.
                    this.PreviousWinnerNode.Score = scoreA;
                    this.StayNode.Score           = scoreB;
                    this.Lock();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return((!this.PreviousWinnerNode.IsDecided && this.PreviousWinnerNode.ApplyPairing(pairing)) || (!this.StayNode.IsDecided && this.StayNode.ApplyPairing(pairing)));
            }
        }
Exemple #7
0
        private void CreateGamesForPairing(ILogger log, TournamentPairing pairing, IRandomGen random)
        {
            Debug.Assert(pairing.State == PairingState.None, "Pairing state is not correct");

            var systemUser = this.unitOfWork.Users.FindByName("System");

            for (int i = 0; i < pairing.NumberOfGames; ++i)
            {
                log.Log(LogLevel.Info, "Creating game {0}", i);

                string gameName = pairing.GenerateGameName(i);
                Game   game;

                try
                {
                    game = gameService.Create(
                        Enums.GameType.Tournament,
                        systemUser,
                        gameName,
                        null,
                        pairing.Tournament.GetMapTemplateForGame(random),
                        pairing.Tournament.Options);
                }
                catch (DomainException exception) when(exception.ErrorCode == ErrorCode.NameAlreadyTaken)
                {
                    // Try again
                    game = gameService.Create(
                        Enums.GameType.Tournament,
                        systemUser,
                        gameName + Guid.NewGuid().ToString(),
                        null,
                        pairing.Tournament.GetMapTemplateForGame(random),
                        pairing.Tournament.Options);
                }

                game.TournamentPairingId = pairing.Id;
                pairing.Games.Add(game);

                var teamA = game.AddTeam();
                foreach (var participant in pairing.TeamA.Participants)
                {
                    teamA.AddPlayer(participant.User);
                }

                var teamB = game.AddTeam();
                foreach (var participant in pairing.TeamB.Participants)
                {
                    teamB.AddPlayer(participant.User);
                }

                game.Start(this.mapTemplateProvider.GetTemplate(game.MapTemplateName), random);

                log.Log(LogLevel.Info, "Done.");
            }

            pairing.State = PairingState.Active;
        }
        public override bool ApplyPairing(TournamentPairing pairing)
        {
            if (pairing == null)
            {
                throw new ArgumentNullException("pairing");
            }

            if (this.Locked)
            {
                // If we (and, all of our decentants) are decided, return false, indicating that no node below us is in a state that needs a score.
                return(false);
            }

            if (!this.nodeA.IsDecided || !this.nodeA.Locked || !this.nodeB.IsDecided || !this.nodeB.Locked)
            {
                return((!this.nodeA.IsDecided && this.nodeA.ApplyPairing(pairing)) || (!this.nodeB.IsDecided && this.nodeB.ApplyPairing(pairing)));
            }
            else
            {
                var teamA  = pairing.TeamScores[0].Team;
                var scoreA = pairing.TeamScores[0].Score;
                var teamB  = pairing.TeamScores[1].Team;
                var scoreB = pairing.TeamScores[1].Score;

                if (teamA == null)
                {
                    teamA  = teamB;
                    scoreA = scoreB;
                    teamB  = null;
                    scoreB = null;
                }

                if (!TeamsMatch(teamA, this.nodeA.Team) || !TeamsMatch(teamB, this.nodeB.Team))
                {
                    var teamSwap  = teamA;
                    var scoreSwap = scoreA;
                    teamA  = teamB;
                    scoreA = scoreB;
                    teamB  = teamSwap;
                    scoreB = scoreSwap;
                }

                if (TeamsMatch(teamA, this.nodeA.Team) && TeamsMatch(teamB, this.nodeB.Team))
                {
                    this.nodeA.Score = scoreA;
                    this.nodeB.Score = scoreB;
                    this.Lock();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        private bool SetWins(TournamentPairing pairing, TournamentTeam teamA, TournamentTeam teamB, int winsA, int winsB)
        {
            if (pairing.TeamA == teamA && pairing.TeamB == teamB)
            {
                pairing.TeamAWon = winsA;
                pairing.TeamBWon = winsB;

                return(true);
            }

            return(false);
        }
 public bool GetPairing(long?bot, ref TournamentPairing team)
 {
     team = null;
     foreach (TournamentPairing pair in CurrentRound.Pairings)
     {
         foreach (TournamentTeamScore tts in pair.TeamScores)
         {
             if (tts.Team.TeamId == bot)
             {
                 team = pair;
                 return(true);
             }
         }
     }
     return(false);
 }
        public override bool ApplyPairing(TournamentPairing pairing)
        {
            if (pairing == null)
            {
                throw new ArgumentNullException("pairing");
            }

            if (this.IsDecided)
            {
                return(false);
            }
            else
            {
                return(this.decider.ApplyPairing(pairing));
            }
        }
Exemple #12
0
        public override bool ApplyPairing(TournamentPairing pairing)
        {
            if (pairing == null)
            {
                throw new ArgumentNullException("pairing");
            }

            if (this.IsDecided)
            {
                return false;
            }
            else
            {
                return this.decider.ApplyPairing(pairing);
            }
        }
Exemple #13
0
        private IEnumerable <BORank> GetRoundRankings(TournamentRound round, int roundNumber)
        {
            if (round.Pairings.Count != 1)
            {
                throw new InvalidTournamentStateException("At least one round has more than one pairing set.");
            }

            TournamentPairing pairing = round.Pairings[0];

            foreach (var teamScore in pairing.TeamScores)
            {
                if (teamScore.Score == null)
                {
                    yield break;
                }
            }

            int   r = 1, lastRank = 1;
            Score lastScore = null;

            var ranks = from team in pairing.TeamScores
                        orderby team.Score descending
                        select new BORank()
            {
                Team        = team.Team,
                Rank        = r++,
                Score       = team.Score,
                RoundNumber = roundNumber
            };

            foreach (var rank in ranks)
            {
                if (lastScore == rank.Score)
                {
                    rank.Rank = lastRank;
                }

                lastScore = rank.Score;
                lastRank  = rank.Rank;

                yield return(rank);
            }
        }
        /// <summary>
        /// Start all games
        /// </summary>
        public void Start()
        {
            Platform.Synchronize();
            Platform.LogEvent("Round " + Round.Id.ToString() + " Started ", ConsoleColor.DarkCyan);

            TournamentPairing pair = null;

            // start all games
            foreach (Game game in Round.Games)
            {
                if (GetPairing(game.Players[0].BotId, ref pair))
                {
                    GameInstance gi = new GameInstance(game, Competition, CurrentRound, pair);
                    GameInstances.Add(gi);
                    TInstance.ExecutedGames.Add(gi);
                    gi.StartGame();
                    Thread.Sleep(PlatformSettings.GameSpacing);
                }
            }
        }
Exemple #15
0
 public abstract bool ApplyPairing(TournamentPairing pairing);
        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(TestUtils.MockUserProvider(), unitOfWork, gameServiceMock.Object, TestUtils.MockMapTemplateProvider());

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

            tournament.State = TournamentState.Knockout;

            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(game, user1, team1)
            {
                Outcome = Enums.PlayerOutcome.Won
            });
            game.Teams.Add(team1);

            var team2 = new Team(game);

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

            pairing.Games = new[] { game };

            // Act
            service.SynchronizeGamesToPairings(new TestLogger(), 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 override bool ApplyPairing(TournamentPairing pairing)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <param name="isPerformanceRound">if the round is for seeding, otherwise its for the actual tournament.</param>
        /// <returns></returns>
        public static bool StartNextRound(Guid tournamentId, bool isPerformanceRound)
        {
            try
            {
                var dc = new ManagementContext();
                var tourny = GetTournament(tournamentId);
                TournamentApi.IPairingsGenerator pg = null;
                TournamentTypeEnum tempType = tourny.TournamentType;
                if (isPerformanceRound)
                    tempType = tourny.TouramentTypeForSeedingEnum;
                switch (tempType)
                {
                    case TournamentTypeEnum.Boil_Off:
                        pg = new BoilOffPairingsGenerator();
                        break;
                    case TournamentTypeEnum.Round_Robin:
                        pg = new RoundRobinPairingsGenerator();
                        break;
                    case TournamentTypeEnum.Round_Robin_Pool_Play:
                        pg = new RoundRobinPairingsGenerator(true);
                        break;
                    case TournamentTypeEnum.Double_Elimination:
                        pg = new EliminationTournament(2);
                        break;
                    case TournamentTypeEnum.Single_Elimination:
                        pg = new EliminationTournament(1);
                        break;
                }
                if (isPerformanceRound)
                    pg.LoadState(tourny.TeamsForTournamentApi, tourny.TournamentRoundsApiForSeeding);
                else
                    pg.LoadState(tourny.TeamsForTournamentApi, tourny.TournamentRoundsApi);

                var tempRound = pg.CreateNextRound(null);

                bool tournamentFinished = (tempRound == null) && (tourny.TournamentRoundsApi.Count > 1);
                if (tournamentFinished)
                    return true;
                var tournament = dc.GameTournaments.Where(x => x.TournamentId == tourny.Id).FirstOrDefault();
                TournamentRound newRound = new TournamentRound();
                if (isPerformanceRound)
                    newRound.RoundNumber = tourny.TournamentRoundsApiForSeeding.Count + 1;
                else
                    newRound.RoundNumber = tourny.TournamentRoundsApi.Count + 1;
                newRound.Tournament = tournament;
                var teams = tournament.Teams.ToList();
                foreach (var pairing in tempRound.Pairings)
                {
                    var newPairing = new TournamentPairing
                    {
                        Round = newRound,
                        GroupId = pairing.GroupId
                    };

                    for (int i = 0; i < pairing.TeamScores.Count; i++)
                    {
                        var newTeamPairing = new TournamentPairingTeam
                        {
                            Team = teams.Where(t => t.TeamId == pairing.TeamScores[i].Team.TeamId).FirstOrDefault(),
                            Pairing = newPairing,
                            Score = 0
                        };
                        newPairing.Teams.Add(newTeamPairing);
                        //dc.TournamentTeams.Add(newTeamPairing);
                    }
                    newRound.Pairings.Add(newPairing);
                }
                //}

                if (isPerformanceRound)
                    tournament.SeedingRounds.Add(newRound);
                else
                    tournament.Rounds.Add(newRound);

                int c = dc.SaveChanges();
                return c > 0;


            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return false;
        }
 public abstract bool ApplyPairing(TournamentPairing pairing);
Exemple #20
0
 public override bool ApplyPairing(TournamentPairing pairing)
 {
     throw new NotImplementedException();
 }
        public override bool ApplyPairing(TournamentPairing pairing)
        {
            if (pairing == null)
            {
                throw new ArgumentNullException("pairing");
            }

            if (this.Locked)
            {
                // If we (and, all of our decentants) are decided, return false, indicating that no node below us is in a state that needs a score.
                return false;
            }

            if (!this.nodeA.IsDecided || !this.nodeA.Locked || !this.nodeB.IsDecided || !this.nodeB.Locked)
            {
                return (!this.nodeA.IsDecided && this.nodeA.ApplyPairing(pairing)) || (!this.nodeB.IsDecided && this.nodeB.ApplyPairing(pairing));
            }
            else
            {
                var teamA = pairing.TeamScores[0].Team;
                var scoreA = pairing.TeamScores[0].Score;
                var teamB = pairing.TeamScores[1].Team;
                var scoreB = pairing.TeamScores[1].Score;

                if (teamA == null)
                {
                    teamA = teamB;
                    scoreA = scoreB;
                    teamB = null;
                    scoreB = null;
                }

                if(!TeamsMatch(teamA, this.nodeA.Team) || !TeamsMatch(teamB, this.nodeB.Team))
                {
                    var teamSwap = teamA;
                    var scoreSwap = scoreA;
                    teamA = teamB;
                    scoreA = scoreB;
                    teamB = teamSwap;
                    scoreB = scoreSwap;
                }

                if (TeamsMatch(teamA, this.nodeA.Team) && TeamsMatch(teamB, this.nodeB.Team))
                {
                    this.nodeA.Score = scoreA;
                    this.nodeB.Score = scoreB;
                    this.Lock();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }