コード例 #1
0
        /// <summary>
        /// Set or update a score on match
        /// </summary>
        /// <param name="matchId"></param>
        /// <param name="scoreOne"></param>
        /// <param name="scoreTwo"></param>
        /// <returns></returns>
        public IActionResult SetScore(int matchId, int scoreOne, int scoreTwo)
        {
            DbModels.Match match = this._repoWrapper.Match.GetById(matchId);
            if (match == null)
            {
                return(new NotFoundResult());
            }
            if (match.State == DbModels.MatchState.Finished)
            {
                return(new BadRequestResult());
            }
            if (match.TeamOne == null || match.TeamOne.Team == null || match.TeamTwo == null || match.TeamTwo.Team == null)
            {
                return(new BadRequestResult());
            }
            if (match.State == DbModels.MatchState.Planned)
            {
                match.State = DbModels.MatchState.Started;
            }

            match.TeamOne.Score = scoreOne;
            match.TeamTwo.Score = scoreTwo;

            this._repoWrapper.Match.SaveChanges();

            return(new OkResult());
        }
コード例 #2
0
        /// <summary>
        /// Convert the tree of matches to a directory, where the key is the current play round and the value is the list of all matches within the round
        /// </summary>
        /// <param name="finalMatch">root element to start navigation from</param>
        /// <returns>dictionary</returns>
        private Dictionary <int, List <DbModels.Match> > GenerateRoundLists(DbModels.Match finalMatch)
        {
            Dictionary <int, List <DbModels.Match> > result = new Dictionary <int, List <DbModels.Match> >();

            this.GenerateRoundListRecursion(result, finalMatch, 0);
            return(result);
        }
コード例 #3
0
        /// <summary>
        /// Generates the match tree.
        /// </summary>
        /// <param name="match">The match.</param>
        /// <param name="depth">The depth.</param>
        private void GenerateMatchTree(DbModels.Match match, int depth)
        {
            depth--;

            // Generate predecessors
            if (match.Predecessors == null)
            {
                match.Predecessors = new List <DbModels.Match>();
            }

            DbModels.Match preMatchOne = new DbModels.Match()
            {
                Successor = match, CreatedAt = DateTime.Now
            };
            match.Predecessors.Add(preMatchOne);
            DbModels.Match preMatchTwo = new DbModels.Match()
            {
                Successor = match, CreatedAt = DateTime.Now
            };
            match.Predecessors.Add(preMatchTwo);

            // Generate next level
            if (depth > 0)
            {
                this.GenerateMatchTree(preMatchOne, depth);
                this.GenerateMatchTree(preMatchTwo, depth);
            }
        }
コード例 #4
0
        /// <summary>
        /// End a match
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <param name="matchId"></param>
        /// <returns></returns>
        public ActionResult <Match> EndMatch(int tournamentId, int matchId)
        {
            DbModels.Match      match      = this._repoWrapper.Match.GetById(matchId);
            DbModels.Tournament tournament = this._repoWrapper.Tournament.GetById(tournamentId);

            if (match == null)
            {
                return(new ActionResult <Match>(new NotFoundResult()));
            }
            //Can´t end matches, which are allready ended
            if (match.State == DbModels.MatchState.Finished)
            {
                return(new ActionResult <Match>(new BadRequestResult()));
            }
            // If teams are not set or they don´t have scores => error
            if (match.TeamOne.Team == null || match.TeamTwo.Team == null || !match.TeamOne.Score.HasValue || !match.TeamTwo.Score.HasValue)
            {
                return(new ActionResult <Match>(new BadRequestResult()));
            }
            // can´t determinate winner if the score is even...
            if (match.TeamOne.Score == match.TeamTwo.Score)
            {
                return(new ActionResult <Match>(new BadRequestResult()));
            }
            DB.Models.Team winner = (match.TeamOne.Score > match.TeamTwo.Score) ? match.TeamOne.Team : match.TeamTwo.Team;

            // Winner: TeamOne

            if (match.Successor != null)
            {
                DbModels.MatchResult nextMatchForWinner = new DbModels.MatchResult()
                {
                    CreatedAt = DateTime.Now,
                    Match     = match.Successor,
                    Team      = winner
                };

                if (match.Successor.TeamOne == null)
                {
                    match.Successor.TeamOne = nextMatchForWinner;
                }
                else
                {
                    match.Successor.TeamTwo = nextMatchForWinner;
                }
            }
            //Final match
            else
            {
                tournament.State = DbModels.TournamentState.Finished;
            }

            match.State = DbModels.MatchState.Finished;
            this._repoWrapper.Match.SaveChanges();
            this._repoWrapper.Tournament.SaveChanges();

            return(new ActionResult <Match>(new Models.Match(new Models.Tournament(tournament), match)));
        }
コード例 #5
0
        /// <summary>
        /// Rerurn a match for a tournament
        /// </summary>
        /// <param name="torunamentId"></param>
        /// <param name="matchId"></param>
        /// <returns></returns>
        public ActionResult <Match> GetMatch(int torunamentId, int matchId)
        {
            DbModels.Match match = this._repoWrapper.Match.GetById(matchId);
            if (match == null)
            {
                return(new ActionResult <Match>(new NotFoundResult()));
            }

            return(new ActionResult <Match>(new Match(new Tournament(this._repoWrapper.Tournament.GetById(torunamentId)), match)));
        }
コード例 #6
0
        /// <summary>
        /// Generates the match plan.
        /// </summary>
        /// <param name="tournament">The tournament.</param>
        private void GenerateMatchPlan(DbModels.Tournament tournament)
        {
            // Generate match tree
            int depth = (int)Math.Log(tournament.TeamCount, 2);

            DbModels.Match finalMatch = new DbModels.Match()
            {
                CreatedAt = DateTime.Now
            };
            this.GenerateMatchTree(finalMatch, depth - 1);

            // gather list of matches per round
            Dictionary <int, List <DbModels.Match> > matchesPerRound = this.GenerateRoundLists(finalMatch);

            // Add matches to tournament
            tournament.Matches = new List <DbModels.Match>();
            matchesPerRound.ToList().ForEach(kv =>
            {
                kv.Value.ForEach(m =>
                {
                    tournament.Matches.Add(m);
                });
            });
            this._repoWrapper.Tournament.SaveChanges();

            // Assign play area booking to each match
            matchesPerRound.OrderByDescending(l => l.Key).ToList().ForEach(l => l.Value.ForEach(m =>
            {
                m.PlayAreaBooking = this.CreateBookingForPlayArea(tournament);
                //little work-around: without this save, the next call of CreateBookingForPlayArea will not determinate the correct next slot
                this._repoWrapper.Tournament.SaveChanges();
            }));

            // randomize team list for fairness
            //TODO: Shuffle wieder einschalten
            List <DbModels.Team> teams = tournament.Teams.ToList().ShuffleToNewList();

            // assign teams to matches
            List <DbModels.Match> firstRoundMatches = matchesPerRound.GetValueOrDefault(matchesPerRound.Keys.Max());

            for (int i = 0; i < firstRoundMatches.Count; i++)
            {
                firstRoundMatches[i].TeamOne = new DbModels.MatchResult()
                {
                    Team = teams[i * 2], CreatedAt = DateTime.Now, Match = firstRoundMatches[i]
                };
                firstRoundMatches[i].TeamTwo = new DbModels.MatchResult()
                {
                    Team = teams[i * 2 + 1], CreatedAt = DateTime.Now, Match = firstRoundMatches[i]
                };
            }
        }
コード例 #7
0
        /// <summary>
        /// Generates the round list recursion.
        /// </summary>
        /// <param name="matchList">The match list.</param>
        /// <param name="parentMatch">The parent match.</param>
        /// <param name="round">The round.</param>
        private void GenerateRoundListRecursion(Dictionary <int, List <DbModels.Match> > matchList, DbModels.Match parentMatch, int round)
        {
            if (!matchList.TryGetValue(round, out List <DbModels.Match> matchesThisRound))
            {
                matchesThisRound = new List <DbModels.Match>();
                matchList.Add(round, matchesThisRound);
            }

            matchesThisRound.Add(parentMatch);
            if (parentMatch.Predecessors != null)
            {
                parentMatch.Predecessors.ToList().ForEach(c =>
                {
                    this.GenerateRoundListRecursion(matchList, c, round + 1);
                });
            }
        }