private static async Task <PlayDay> GetDay(IDbConnection c, IDbTransaction t, GroupCoords coords, int dayIdx, DateTimeOffset?dayDate)
        {
            var days  = coords.Tournament.Days as PlayDay[];
            var dbDay = days.ElementAt(dayIdx - 1);

            if (dbDay == null)
            {
                dbDay = new PlayDay
                {
                    IdTournament  = coords.IdTournament,
                    IdStage       = coords.IdStage,
                    IdGroup       = coords.IdGroup,
                    Name          = Localization.Get("Jornada {0}", null, dayIdx),
                    SequenceOrder = dayIdx,
                    Matches       = new List <Match>()
                };

                if (dayDate != null)
                {
                    dbDay.DatesList.Add(dayDate.Value.DateTime);
                    dbDay.SetDatesFromDatesList();
                }

                days[dayIdx - 1] = dbDay;

                dbDay.Id = await c.InsertAsync(dbDay, t);
            }

            return(dbDay);
        }
Esempio n. 2
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);
        }