예제 #1
0
        /// <summary>
        /// Add a new team
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult <Team> AddTeam(int tournamentId, string name)
        {
            DbModels.Tournament tournament = this._repoWrapper.Tournament.GetById(tournamentId);
            if (tournament == null)
            {
                return(new NotFoundResult());
            }

            // Only update while tournament not started
            if (tournament.State != DbModels.TournamentState.Created)
            {
                return(new BadRequestObjectResult("Tournament allready started of finished"));
            }

            DbModels.Team team = new DbModels.Team
            {
                Name = name
            };

            this._repoWrapper.Team.Create(team);
            this._repoWrapper.Team.SaveChanges();

            tournament.Teams.Add(team);
            this._repoWrapper.Tournament.SaveChanges();

            return(new ActionResult <Team>(new Team(team)));
        }
예제 #2
0
        /// <summary>
        /// Remove a team
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public ActionResult RemoveTeam(int tournamentId, int teamId)
        {
            DbModels.Tournament tournament = this._repoWrapper.Tournament.GetById(tournamentId);
            if (tournament == null)
            {
                return(new NotFoundResult());
            }

            // Only update while tournament not started
            if (tournament.State != DbModels.TournamentState.Created)
            {
                return(new BadRequestObjectResult("Tournament allready started of finished"));
            }

            DbModels.Team dbTeam = this._repoWrapper.Team.GetById(teamId);
            if (dbTeam == null)
            {
                return(new NotFoundResult());
            }

            this._repoWrapper.Team.Delete(dbTeam);
            this._repoWrapper.Team.SaveChanges();

            return(new OkResult());
        }
예제 #3
0
        /// <summary>
        /// update a play area
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <param name="playAreaId"></param>
        /// <param name="playArea"></param>
        /// <returns></returns>
        public IActionResult UpdatePlayArea(int tournamentId, int playAreaId, Models.PlayArea playArea)
        {
            DbModels.Tournament tournament = this._repoWrapper.Tournament.GetById(tournamentId);
            if (tournament == null)
            {
                return(new NotFoundResult());
            }

            // Only update while tournament not started
            if (tournament.State != DbModels.TournamentState.Created)
            {
                return(new BadRequestObjectResult("Tournament allready started of finished"));
            }

            DbModels.PlayArea dbPlayArea = this._repoWrapper.PlayArea.GetById(playAreaId);
            if (dbPlayArea == null)
            {
                return(new NotFoundResult());
            }

            dbPlayArea.Name        = playArea.Name;
            dbPlayArea.Description = playArea.Description;

            this._repoWrapper.PlayArea.SaveChanges();

            return(new OkResult());
        }
예제 #4
0
        /// <summary>
        /// Add a new play area
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public ActionResult <PlayArea> AddPlayArea(int tournamentId, string name, string description)
        {
            DbModels.Tournament tournament = this._repoWrapper.Tournament.GetById(tournamentId);
            if (tournament == null)
            {
                return(new NotFoundResult());
            }

            // Only update while tournament not started
            if (tournament.State != DbModels.TournamentState.Created)
            {
                return(new BadRequestObjectResult("Tournament allready started of finished"));
            }

            DbModels.PlayArea playarea = new DbModels.PlayArea
            {
                Tournament  = tournament,
                Description = description,
                Name        = name
            };

            this._repoWrapper.PlayArea.Create(playarea);
            this._repoWrapper.PlayArea.SaveChanges();

            return(new ActionResult <PlayArea>(new PlayArea(playarea)));
        }
예제 #5
0
        /// <summary>
        /// create bookings
        /// </summary>
        /// <param name="tournament"></param>
        /// <returns></returns>
        protected DbModels.PlayAreaBooking CreateBookingForPlayArea(DbModels.Tournament tournament)
        {
            // Get the earliest available time slot on any play area
            List <KeyValuePair <DbModels.PlayArea, DateTime> > currentAvailableTimeSlot = new List <KeyValuePair <DbModels.PlayArea, DateTime> >();

            tournament.PlayAreas.ToList().ForEach(pa =>
            {
                IEnumerable <DbModels.PlayAreaBooking> bookings = this._repoWrapper.PlayAreaBooking.Filter(b => b.PlayArea.Id == pa.Id);
                // In case there are no bookings for the current play area yet, the start time of the tournament is used
                if (bookings == null || (bookings != null && bookings.Count() == 0))
                {
                    currentAvailableTimeSlot.Add(new KeyValuePair <DbModels.PlayArea, DateTime>(pa, tournament.Start));
                }
                else
                {
                    // otherwise: take the end time of the max booking as start time for this one
                    currentAvailableTimeSlot.Add(new KeyValuePair <DbModels.PlayArea, DateTime>(pa, bookings.Max(b => b.End)));
                }
            });
            KeyValuePair <DbModels.PlayArea, DateTime> earliestAvailablePlayArea = currentAvailableTimeSlot.OrderBy(c => c.Value).First();

            DbModels.PlayAreaBooking booking = new DbModels.PlayAreaBooking()
            {
                CreatedAt = DateTime.Now,
                Start     = earliestAvailablePlayArea.Value,
                End       = earliestAvailablePlayArea.Value.AddMinutes(tournament.MatchDuration),
                PlayArea  = earliestAvailablePlayArea.Key
            };
            return(booking);
        }
예제 #6
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)));
        }
예제 #7
0
 /// <summary>
 /// Gets the specified identifier.
 /// </summary>
 /// <param name="id">The identifier.</param>
 /// <returns></returns>
 Tournament ITournamentService.Get(int id)
 {
     DbModels.Tournament tournament = this._repoWrapper.Tournament.GetById(id);
     if (tournament == null)
     {
         return(null);
     }
     return(new Tournament(tournament));
 }
예제 #8
0
 internal Tournament(db.Tournament tournament)
 {
     _tournament   = tournament;
     Id            = tournament.Id;
     Description   = tournament.Description;
     Name          = tournament.Name;
     StartTime     = tournament.Start;
     MatchDuration = tournament.MatchDuration;
     TeamCount     = tournament.TeamCount;
 }
예제 #9
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]
                };
            }
        }
예제 #10
0
        /// <summary>
        /// Add a new tournament
        /// </summary>
        /// <param name="tournament"></param>
        /// <returns></returns>
        public ActionResult <Tournament> AddTournament(Tournament tournament)
        {
            DbModels.Tournament tmnt = new DbModels.Tournament
            {
                Name          = tournament.Name,
                Description   = tournament.Description,
                MatchDuration = tournament.MatchDuration,
                Start         = tournament.StartTime,
                State         = DbModels.TournamentState.Created,
                TeamCount     = tournament.TeamCount,
            };

            this._repoWrapper.Tournament.Create(tmnt);
            this._repoWrapper.Tournament.SaveChanges();
            return(new ActionResult <Tournament>(new Tournament(tmnt)));
        }
예제 #11
0
        /// <summary>
        /// Get a team
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public ActionResult <Team> GetTeam(int tournamentId, int teamId)
        {
            DbModels.Tournament tournament = this._repoWrapper.Tournament.GetById(tournamentId);
            if (tournament == null)
            {
                return(new NotFoundResult());
            }

            DbModels.Team dbTeam = this._repoWrapper.Team.GetById(teamId);
            if (dbTeam == null)
            {
                return(new NotFoundResult());
            }

            return(new ActionResult <Team>(new Team(dbTeam)));
        }
예제 #12
0
        /// <summary>
        /// Get an existing play area
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <param name="playAreaId"></param>
        /// <returns></returns>
        public ActionResult <PlayArea> GetPlayArea(int tournamentId, int playAreaId)
        {
            DbModels.Tournament tournament = this._repoWrapper.Tournament.GetById(tournamentId);
            if (tournament == null)
            {
                return(new NotFoundResult());
            }

            DbModels.PlayArea playarea = this._repoWrapper.PlayArea.GetById(playAreaId);
            if (playarea == null)
            {
                return(new NotFoundResult());
            }

            return(new ActionResult <PlayArea>(new PlayArea(playarea)));
        }
예제 #13
0
        /// <summary>
        /// Start tournament
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <returns></returns>
        public ActionResult <Tournament> Start(int tournamentId)
        {
            DbModels.Tournament tournament = this._repoWrapper.Tournament.GetById(tournamentId);
            if (tournament == null)
            {
                return(new NotFoundResult());
            }
            if (tournament.State != DbModels.TournamentState.Created)
            {
                return(new BadRequestObjectResult("Tournament allready started of finished"));
            }

            tournament.State = DbModels.TournamentState.Started;
            this.GenerateMatchPlan(tournament);
            this._repoWrapper.Tournament.SaveChanges();
            this._repoWrapper.PlayAreaBooking.SaveChanges();
            this._repoWrapper.Match.SaveChanges();

            return(this.Get(tournamentId));
        }
예제 #14
0
        /// <summary>
        /// Update Tournament
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <param name="tournament"></param>
        /// <returns></returns>
        public IActionResult UpdateTournament(int tournamentId, Tournament tournament)
        {
            DbModels.Tournament dbTournament = this._repoWrapper.Tournament.GetById(tournamentId);
            if (dbTournament == null)
            {
                return(new NotFoundResult());
            }

            // Only update while tournament not started
            if (dbTournament.State != DbModels.TournamentState.Created)
            {
                return(new BadRequestObjectResult("Tournament allready started of finished"));
            }

            dbTournament.Name          = tournament.Name;
            dbTournament.MatchDuration = tournament.MatchDuration;
            dbTournament.Description   = tournament.Description;
            dbTournament.Start         = tournament.StartTime;
            dbTournament.TeamCount     = tournament.TeamCount;

            this._repoWrapper.Team.SaveChanges();

            return(new OkResult());
        }