コード例 #1
0
        public static void SaveRounds(IDbConnection conn, GroupCoords coords, CalendarResult result)
        {
            var transaction = conn.BeginTransaction();

            try
            {
                var dbGroup = conn.Get <StageGroup>(coords.IdGroup);
                if (dbGroup == null)
                {
                    throw new Exception("Error.NotFound");
                }
                if ((dbGroup.Flags & (int)StageGroupFlags.HasGeneratedCalendar) > 0)
                {
                    throw new Exception("Tournament.AlreadyHasCalendar");
                }

                foreach (var day in result.Days)
                {
                    InsertDay(conn, transaction, day);
                }

                dbGroup.Flags = dbGroup.Flags | (int)StageGroupFlags.HasGeneratedCalendar;
                conn.Update(dbGroup, transaction);

                transaction.Commit();
            }
            catch (System.Exception)
            {
                transaction.Rollback();
                throw;
            }
        }
コード例 #2
0
        public IActionResult Delete([FromBody] GroupCoords coords)
        {
            return(DbTransaction((c, t) => {
                if (!IsOrganizationAdmin())
                {
                    throw new UnauthorizedAccessException();
                }

                CalendarStorer.DeleteGroupRounds(c, t, coords);
                return true;
            }));
        }
コード例 #3
0
        public static List <List <Match> > CreateRounds(IList <long> teamIds, GroupCoords coords, int numTeams)
        {
            var num     = teamIds.Count == 0 ? numTeams : teamIds.Count;
            var numDays = (int)Math.Log(num, 2);
            var result  = new List <List <Match> >();
            var teams   = teamIds;

            for (var i = numDays; i > 0; --i)
            {
                var day = CreateRoundMatches((1 << i) / 2, coords, teams);
                teams = null;   // Only fill teamids in the first round

                result.Add(day);
            }

            return(result);
        }
コード例 #4
0
        public static void SpreadMatchesInCalendar(
            CalendarResult result,
            List <List <Match> > matchRounds,
            DailySlot[][] weekdaySlots,
            DateTime startDate,
            IList <long> fieldIds,
            int numSlots,
            int gameDuration,
            bool wantsRandom,
            GroupCoords coords,
            DateTime[] forbiddenDays,
            IEnumerable <TeamLocationPreference> fieldPreferences,
            Func <int, string> roundNameCallback)
        {
            var days = new List <PlayDay>();
            //var fieldIds = GetFieldIds(fields);

            var roundNumber = 1;

            // First, spread the rounds in days in the calendar
            foreach (var matchRound in matchRounds)
            {
                startDate = GetUnforbiddenStartDate(startDate, weekdaySlots, forbiddenDays);

                var slots     = GetRoundSlots(startDate, weekdaySlots, numSlots, gameDuration, fieldIds);
                var roundName = roundNameCallback(roundNumber);

                var round = CreateAndFillRound(result, matchRound, slots, coords, fieldPreferences, roundName);
                round.IdTournament  = coords.IdTournament;
                round.IdStage       = coords.IdStage;
                round.IdGroup       = coords.IdGroup;
                round.Name          = roundName;
                round.SequenceOrder = roundNumber;
                days.Add(round);

                startDate = startDate.AddDays(7);

                roundNumber++;
            }

            result.Days = days;
        }
コード例 #5
0
        public static int DeleteGroupRounds(IDbConnection c, IDbTransaction t, GroupCoords coords)
        {
            var tournament = c.Get <Tournament>(coords.IdTournament, t);

            if (tournament == null)
            {
                throw new Exception("Error.NotFound");
            }
            if (tournament.Status >= (int)TournamentStatus.Playing)
            {
                throw new Exception("Tournament.AlreadyStarted");
            }

            var sql = @"
                DELETE FROM matches m JOIN playdays p ON p.idMatch = m.id WHERE p.idGroup = @idGroup;
            ";

            var result = c.Execute(sql, new { idGroup = coords.IdGroup }, t);

            return(result);
        }
コード例 #6
0
        private static List <Match> CreateRoundMatches(int numMatches, GroupCoords coords, IList <long> teamIds)
        {
            var result = new List <Match>(numMatches);

            for (int i = 0; i < numMatches; ++i)
            {
                var match = new Match
                {
                    IdTournament = coords.IdTournament,
                    IdStage      = coords.IdStage,
                    IdGroup      = coords.IdGroup
                };

                if (teamIds != null && teamIds.FirstOrDefault() != 0)
                {
                    match.IdHomeTeam    = teamIds[i * 2];
                    match.IdVisitorTeam = teamIds[i * 2 + 1];
                }

                result.Add(match);
            }

            return(result);
        }
コード例 #7
0
        public static PlayDay CreateAndFillRound(CalendarResult result, List <Match> matchesInRound, IList <CalendarSlot> slots, GroupCoords coords, IEnumerable <TeamLocationPreference> fieldPreferences, string roundName)
        {
            // Round Id -1: when saving to the database, will have to set proper ID here and to the matches.
            var day = new PlayDay {
                Id = -1, Matches = new List <Match>()
            };

            // Spread fieldPreferences in the slots (consuming them). Then fill the rest of the matches.
            AssignPreferencesToSlots(result, matchesInRound, slots, fieldPreferences, roundName);

            foreach (var match in matchesInRound)
            {
                match.IdTournament = coords.IdTournament;
                match.IdStage      = coords.IdStage;
                match.IdGroup      = coords.IdGroup;
                match.Status       = (int)MatchStatus.Created;
                match.IdDay        = day.Id;

                match.Status = (int)MatchStatus.Created;

                if (IsFillerMatch(match))
                {
                    // Add match, but do not consume slot
                    match.Status = (int)MatchStatus.Skip;
                    day.Matches.Add(match);
                    continue;
                }

                // If the match doesn't already have time
                if (match.StartTime == default(DateTime))
                {
                    if (slots.Count == 0)
                    {
                        throw new Exception("Error.Calendar.NotEnoughHours");
                    }

                    // Assign first available slot and remove from the list.
                    var slot = slots[0];
                    slot.AssignToMatch(match);
                    slots.RemoveAt(0);
                }

                day.Matches.Add(match);
            }

            day.SetDatesFromDatesList();

            return(day);
        }