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);
        }
Exemple #2
0
        private static IEnumerable <PlayDay> GroupMatchesInDays(IEnumerable <PlayDay> daysAndMatches)
        {
            // Group matches of the same day in the same record
            // This is to handle the case where a team plays more than one match in the same day (yes, apparently it happens).

            var     result  = new List <PlayDay>();
            PlayDay lastDay = null;

            foreach (var day in daysAndMatches)
            {
                if (lastDay == null)
                {
                    lastDay = day;
                    result.Add(day);
                    continue;
                }

                if (day.Id == lastDay.Id)
                {
                    foreach (var m in day.Matches)
                    {
                        lastDay.Matches.Add(m);                             // Probably an array expansion on each, but it should be very few (how many matches does a team play on the same day?)
                    }
                }
                else
                {
                    result.Add(day);
                }
            }

            return(result);
        }
Exemple #3
0
        private static void InsertDay(IDbConnection c, IDbTransaction t, PlayDay day)
        {
            var dbDay = GetOrInsertDay(c, t, day);

            foreach (var match in day.Matches)
            {
                match.IdDay = dbDay.Id;
                match.Id    = c.Insert(match, t);
            }
        }
Exemple #4
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);
        }
Exemple #5
0
 public override int GetHashCode()
 {
     unchecked
     {
         int hash = 23;
         hash = hash * 37 + PlayDay.GetHashCode();
         hash = hash * 37 + GameId.GetHashCode();
         hash = hash * 37 + SeasonId.GetHashCode();
         hash = hash * 37 + Date.GetHashCode();
         hash = hash * 37 + (HomeTeam != null ? HomeTeam.GetHashCode() : 0);
         hash = hash * 37 + (AwayTeam != null ? AwayTeam.GetHashCode() : 0);
         hash = hash * 37 + (Arena != null ? Arena.GetHashCode() : 0);
         return(hash);
     }
 }
Exemple #6
0
        public static PlayDay GetOrInsertDay(IDbConnection c, IDbTransaction t, PlayDay day)
        {
            var dbDays = c.Query <PlayDay>("SELECT * FROM playdays WHERE idTournament = @IdTournament AND idStage = @IdStage AND name = @Name", day, t);

            long dayId = -1;
            var  count = dbDays.Count();

            if (count == 1)
            {
                // Exists, reuse it
                return(dbDays.First());
            }
            else if (count == 0)
            {
                // Doesn't exist, create
                dayId  = c.Insert(day, t);
                day.Id = dayId;
                return(day);
            }
            else
            {
                throw new PlannerException("Error.InternalInconsistency.MoreThanOneMatchDayForSame");
            }
        }