public SessionBaseEntity MapToSessionBaseEntity(SessionDataDTO source, SessionBaseEntity target = null)
        {
            if (source == null)
            {
                return(null);
            }
            if (target == null)
            {
                target = GetSessionBaseEntity(source);
            }

            if (!MapToRevision(source, target))
            {
                return(target);
            }

            target.SessionType          = source.SessionType;
            target.CreatedByUserId      = source.CreatedByUserId;
            target.LastModifiedByUserId = source.LastModifiedByUserId;
            target.Date       = source.Date;
            target.Duration   = source.Duration;
            target.LocationId = source.LocationId;
            //target.Schedule = GetScheduleEntity(source.Schedule);
            target.SessionResult = GetResultEntity(source.SessionResult);
            //MapCollection(source.Reviews, target.Reviews, (src, trg) => GetReviewEntity(src), src => src.ReviewId);

            return(target);
        }
Ejemplo n.º 2
0
        public StandingsEntity GetSeasonStandings(SessionBaseEntity currentSession, LeagueDbContext dbContext, int maxRacesCount = -1)
        {
            StandingsEntity standings = new StandingsEntity()
            {
                ScoringTable = this,
                SessionId    = (currentSession?.SessionId).GetValueOrDefault()
            };

            if (currentSession == null)
            {
                return(standings);
            }

            if (maxRacesCount == -1)
            {
                maxRacesCount = Sessions.Count() - DropWeeks;
            }

            if (ScoringKind == ScoringKindEnum.Team)
            {
                return(GetSeasonTeamStandings(currentSession, dbContext, maxRacesCount));
            }

            var allScoredResults      = Scorings?.SelectMany(x => x.ScoredResults).ToList();
            var previousScoredResults = allScoredResults.Where(x => x.Result.Session.Date < currentSession.Date).ToList();

            var currentResult       = currentSession.SessionResult;
            var currentScoredResult = allScoredResults.SingleOrDefault(x => x.Result.Session == currentSession);

            var previousScoredRows    = previousScoredResults.SelectMany(x => x.FinalResults).ToList();
            var previousStandingsRows = previousScoredRows
                                        .AggregateByDriver(maxRacesCount, true)
                                        .OrderBy(x => - x.TotalPoints)
                                        .ThenBy(x => x.PenaltyPoints)
                                        .ThenBy(x => - x.Wins);

            previousStandingsRows.Select((value, index) => new { index, value }).ToList().ForEach(x => x.value.Position = x.index + 1);

            allScoredResults = previousScoredResults.ToList();
            allScoredResults.Add(currentScoredResult);
            var currentStandingsRows = allScoredResults
                                       .SelectMany(x => x.FinalResults)
                                       .AggregateByDriver(maxRacesCount, true)
                                       .OrderBy(x => - x.TotalPoints)
                                       .ThenBy(x => x.PenaltyPoints)
                                       .ThenBy(x => - x.Wins);

            currentStandingsRows.Select((value, index) => new { index, value }).ToList().ForEach(x => x.value.Position = x.index + 1);

            standings.StandingsRows = currentStandingsRows
                                      .Diff(previousStandingsRows)
                                      .OrderBy(x => - x.TotalPoints)
                                      .ThenBy(x => x.PenaltyPoints)
                                      .ThenBy(x => - x.Wins)
                                      .ToList();
            standings.StandingsRows.ForEach(x => x.ScoringTable = this);
            standings.Calculate();

            return(standings);
        }
        public SessionInfoDTO MapToSessionInfoDTO(SessionBaseEntity source, SessionInfoDTO target = null)
        {
            if (source == null)
            {
                return(null);
            }
            if (target == null)
            {
                target = new SessionInfoDTO();
            }

            MapToVersionInfoDTO(source, target);
            target.SessionId   = source.SessionId;
            target.SessionType = source.SessionType;

            return(target);
        }
        public SessionDataDTO MapToSessionDataDTO(SessionBaseEntity source, SessionDataDTO target = null)
        {
            if (source == null)
            {
                return(null);
            }
            if (target == null)
            {
                target = new SessionDataDTO();
            }

            MapToSessionInfoDTO(source, target);
            target.CreatedByUserId      = source.CreatedByUserId;
            target.LastModifiedByUserId = source.LastModifiedByUserId;
            target.Date          = source.Date.GetValueOrDefault();
            target.Duration      = source.Duration;
            target.LocationId    = source.LocationId;
            target.Schedule      = MapToScheduleInfoDTO(source.Schedule);
            target.SessionResult = MapToResultInfoDTO(source.SessionResult);
            target.Reviews       = source.Reviews.Select(x => MapToReviewInfoDTO(x)).ToArray();

            return(target);
        }
Ejemplo n.º 5
0
        public TeamStandingsEntity GetSeasonTeamStandings(SessionBaseEntity currentSession, LeagueDbContext dbContext, int maxRacesCount = -1)
        {
            if (currentSession == null)
            {
                return(null);
            }

            if (maxRacesCount == -1)
            {
                maxRacesCount = Sessions.Count() - maxRacesCount;
            }

            var allScoredResults      = Scorings?.SelectMany(x => x.ScoredResults).OfType <ScoredTeamResultEntity>().ToList();
            var previousScoredResults = allScoredResults.Where(x => x.Result.Session.Date < currentSession.Date).ToList();

            foreach (var scoredTeamResult in previousScoredResults)
            {
                dbContext.Entry(scoredTeamResult).Collection(x => x.TeamResults).Query()
                .Include(x => x.Team)
                .Include(x => x.ScoredResultRows.Select(y => y.ScoredResult)).Load();
            }

            var currentResult       = currentSession.SessionResult;
            var currentScoredResult = allScoredResults.SingleOrDefault(x => x.Result.Session == currentSession);

            dbContext.Entry(currentScoredResult).Collection(x => x.TeamResults).Query()
            .Include(x => x.Team)
            .Include(x => x.ScoredResultRows.Select(y => y.ScoredResult)).Load();

            TeamStandingsEntity teamStandings = new TeamStandingsEntity()
            {
                ScoringTable = this,
                SessionId    = currentSession.SessionId
            };

            var previousScoredRows = previousScoredResults.SelectMany(x => x.TeamResults).ToList();
            IEnumerable <TeamStandingsRowEntity> previousStandingsRows;
            IEnumerable <TeamStandingsRowEntity> currentStandingsRows;

            if (DropRacesOption == DropRacesOption.PerDriverResults)
            {
                var allScoredDriverResults = Scorings?.SelectMany(x => x.GetResultsFromSource()).ToList();

                var previousScoredDriverResults = allScoredDriverResults.Where(x => x.Result.Session.Date < currentSession.Date).ToList();

                var currentDriverResult       = currentSession.SessionResult;
                var currentScoredDriverResult = allScoredDriverResults.SingleOrDefault(x => x.Result.Session == currentSession);

                var previousScoredDriverRows = previousScoredDriverResults.SelectMany(x => x.FinalResults);
                previousStandingsRows = previousScoredRows.AggregateByTeam(maxRacesCount, true, DropRacesOption, ResultsPerRaceCount, previousScoredDriverRows)
                                        .OrderBy(x => - x.TotalPoints);

                allScoredDriverResults = previousScoredDriverResults.ToList();
                allScoredDriverResults.Add(currentScoredDriverResult);
                allScoredResults = previousScoredResults.ToList();
                allScoredResults.Add(currentScoredResult);

                var allScoredDriverRows = allScoredDriverResults.SelectMany(x => x.FinalResults);
                currentStandingsRows = allScoredResults.SelectMany(x => x.TeamResults)
                                       .AggregateByTeam(maxRacesCount, true, DropRacesOption, ResultsPerRaceCount, allScoredDriverRows)
                                       .OrderBy(x => - x.TotalPoints)
                                       .ThenBy(x => x.PenaltyPoints)
                                       .ThenBy(x => x.Wins);
            }
            else
            {
                previousStandingsRows = previousScoredRows.AggregateByTeam(maxRacesCount, true, DropRacesOption, ResultsPerRaceCount).OrderBy(x => - x.TotalPoints);

                allScoredResults = previousScoredResults.ToList();
                allScoredResults.Add(currentScoredResult);

                currentStandingsRows = allScoredResults.SelectMany(x => x.TeamResults).AggregateByTeam(maxRacesCount, true, DropRacesOption, ResultsPerRaceCount).OrderBy(x => - x.TotalPoints);
            }

            previousStandingsRows.Select((value, index) => new { index, value }).ToList().ForEach(x => x.value.Position = x.index + 1);
            currentStandingsRows.Select((value, index) => new { index, value }).ToList().ForEach(x => x.value.Position  = x.index + 1);

            teamStandings.StandingsRows = currentStandingsRows
                                          .Diff(previousStandingsRows)
                                          .OrderBy(x => - x.TotalPoints)
                                          .ThenBy(x => x.PenaltyPoints)
                                          .ThenBy(x => - x.Wins)
                                          .ToList();
            //teamStandings.StandingsRows = currentStandingsRows.OrderBy(x => -x.TotalPoints).Cast<StandingsRowEntity>().ToList();
            teamStandings.StandingsRows.ForEach(x => x.ScoringTable = this);
            //teamStandings.Calculate();

            return(teamStandings);
        }
Ejemplo n.º 6
0
        public StandingsEntity GetSeasonStandings(SessionBaseEntity currentSession, LeagueDbContext dbContext)
        {
            var allSessions = GetAllSessions();

            return(GetSeasonStandings(currentSession, dbContext, allSessions.Count - DropWeeks));
        }
        /// <summary>
        /// Get all reviews belonging to a session, specified by its sessionId
        /// </summary>
        /// <param name="sessionId">Id of the session</param>
        /// <returns>DTO containing all reviews and summary data</returns>
        public SessionReviewsDTO GetReviewsFromSession(long sessionId, SessionBaseEntity preLoadedSession = null, IncidentReviewDataDTO[] preLoadedReviews = null)
        {
            // Load session from db
            SessionBaseEntity session;

            // if session id == 0 load latest session, else load specified session
            if (sessionId == 0 && preLoadedSession == null)
            {
                session = DbContext.Set <SessionBaseEntity>()
                          .Where(x => x.SessionResult != null)
                          .OrderByDescending(x => x.Date)
                          .FirstOrDefault();
            }
            else if (preLoadedSession == null)
            {
                session = DbContext.Set <SessionBaseEntity>().Find(sessionId);
            }
            else
            {
                session = preLoadedSession;
            }

            if (session == null)
            {
                return(new SessionReviewsDTO());
            }

            var mapper = new DTOMapper(DbContext);

            // get session race number
            int raceNr = 0;

            if (session.SessionType == iRLeagueManager.Enums.SessionType.Race)
            {
                var season         = session.Schedule.Season;
                var seasonSessions = season.Schedules.SelectMany(x => x.Sessions).Where(x => x.SessionType == iRLeagueManager.Enums.SessionType.Race).OrderBy(x => x.Date);
                raceNr = (seasonSessions.Select((x, i) => new { number = i + 1, item = x }).FirstOrDefault(x => x.item.SessionId == sessionId)?.number).GetValueOrDefault();
            }

            IncidentReviewDataDTO[] reviews = preLoadedReviews;
            if (preLoadedReviews == null)
            // get all reviews ids for this session and retrieve reviews data from ModelDataProvider
            {
                var reviewIds         = session.Reviews.Select(x => x.ReviewId);
                var modelDataProvider = new ModelDataProvider(DbContext);
                reviews = modelDataProvider.GetReviews(reviewIds.ToArray());
            }

            // get custom vote categories information from database
            var voteCatIds = reviews.Where(x => x.AcceptedReviewVotes?.Count() > 0).SelectMany(x => x.AcceptedReviewVotes.Select(y => y.VoteCategoryId)).Distinct();
            var voteCats   = DbContext.Set <VoteCategoryEntity>().Local.Where(x => voteCatIds.Contains(x.CatId)).ToList().Select(x => mapper.MapToVoteCategoryDTO(x));

            if (voteCats.Count() < voteCatIds.Count())
            {
                voteCats = DbContext.Set <VoteCategoryEntity>().Where(x => voteCatIds.Contains(x.CatId)).ToList().Select(x => mapper.MapToVoteCategoryDTO(x));
            }

            /* construct DTOs */
            // get all vote results that resulted in a penalty
            var penalties = reviews
                            .Where(x => x.AcceptedReviewVotes?.Count() > 0)
                            .SelectMany(x => x.AcceptedReviewVotes)
                            .Where(x => x.CatPenalty > 0 && x.MemberAtFaultId != null);
            // summarize penalites for each driver
            var driverPenalties = penalties
                                  .GroupBy(x => x.MemberAtFaultId)
                                  .Select(x => new MemberPenaltySummaryDTO()
            {
                MemberId  = x.Key.GetValueOrDefault(),
                Name      = DbContext.Set <LeagueMemberEntity>().Find(x.Key.GetValueOrDefault()).Fullname,
                Count     = x.Count(),
                Points    = x.Sum(y => y.CatPenalty),
                Penalties = x.ToArray()
            });
            // summarized penalties for all reviews
            var penaltySummary = new ReviewsPenaltySummaryDTO()
            {
                Count        = driverPenalties.Sum(x => x.Count),
                Points       = driverPenalties.Sum(x => x.Points),
                DrvPenalties = driverPenalties.ToArray()
            };
            // create review convencience DTO
            var reviewData = new SessionReviewsDTO()
            {
                Reviews   = reviews,
                Total     = reviews.Count(),
                Open      = reviews.Count(x => x.AcceptedReviewVotes == null || x.AcceptedReviewVotes.Count() == 0),
                Voted     = reviews.Count(x => x.Comments.Any(y => y.CommentReviewVotes.Count() > 0)),
                Closed    = reviews.Count(x => x.AcceptedReviewVotes?.Count() > 0),
                Penalties = penaltySummary,
                Results   = reviews
                            .Where(x => x.AcceptedReviewVotes?.Count() > 0)
                            .SelectMany(x => x.AcceptedReviewVotes)
                            .Where(x => x.VoteCategoryId != null)
                            .GroupBy(x => x.VoteCategoryId)
                            .Select(x => new CountValue <VoteCategoryDTO>()
                {
                    Count = x.Count(), Value = voteCats.SingleOrDefault(y => y.CatId == x.Key.Value)
                })
                            .ToArray(),
                SessionId = sessionId,
                RaceNr    = raceNr
            };

            /* END construct DTOs */

            return(reviewData);
        }