Beispiel #1
0
        public void SetToDefaults()
        {
            FormulaOptions defaults = (FormulaOptions)this.defaultOptions.Clone();

            this.UseNr2003TrackTypes = defaults.UseNr2003TrackTypes;
            this.MappingMethod       = defaults.MappingMethod;
        }
        private Dictionary <string, DriverStats> GetSingleDriverStatsByType(Dictionary <string, List <DriverStats> > driverStatsByType, CarMappingMethod mappingMethod, string carNumber, Driver driver)
        {
            Dictionary <string, DriverStats> statsByType = new Dictionary <string, DriverStats>();

            foreach (TrackType tt in trackTypes)
            {
                var q = from s in driverStatsByType[tt.Id]
                        select s;

                switch (mappingMethod)
                {
                case CarMappingMethod.NAME:
                    q = from s in q
                        where s.Driver == driver
                        select s;
                    break;

                case CarMappingMethod.NUMBER:
                    q = from s in q
                        where s.CarNumber == carNumber
                        select s;
                    break;

                case CarMappingMethod.NUMBER_AND_NAME:
                    q = from s in q
                        where s.Driver == driver &&
                        s.CarNumber == carNumber
                        select s;
                    break;
                }


                DriverStats stats = q.FirstOrDefault();

                //IF NO STATS WERE FOUND FOR THIS DRIVER FOR THIS TRACKTYPE, INITIALIZE AN EMPTY DRIVERSTATS
                if (stats == null)
                {
                    stats = new DriverStats();
                }

                statsByType.Add(tt.Id, stats);
            }

            return(statsByType);
        }
        private List <DriverStats> ComputeDriverStats(List <DriverRaceData> driverRaceDataList, List <RaceInfo> racesInfo, CarMappingMethod mappingMethod)
        {
            //CALCULATE PITSTOP RANK
            Dictionary <Race, decimal?> pitStopTimes = new Dictionary <Race, decimal?>();

            foreach (RaceInfo r in racesInfo)
            {
                int position = 1;

                List <DriverRaceData> drdList = (from drd in driverRaceDataList
                                                 where drd.Race.Id == r.Race.Id &&
                                                 drd.PitStopData != null &&
                                                 drd.PitStopData.AverageTime != null
                                                 orderby drd.PitStopData.AverageTime ascending
                                                 select drd).ToList();

                foreach (DriverRaceData drd in drdList)
                {
                    drd.PitStopRank = position;
                    position++;
                }
            }
            //END CALCULATE PITSTOP RANK


            List <DriverStats> stats = (from drd in driverRaceDataList
                                        group drd by new { drd.RaceResult.CarNumber, drd.RaceResult.Driver } into gr
                                        from drd in gr
                                        orderby gr.Average(drd2 => drd2.RaceResult.FinishPosition).Round(4) ascending
                                        select new DriverStats
            {
                CarNumber = drd.RaceResult.CarNumber,
                Driver = drd.Driver,
                AvgFinish = gr.Average(drd2 => (int?)drd2.RaceResult.FinishPosition).Round(4),
                AvgFinishExcludingDnf = gr.Where(drd2 => drd2.RaceResult.RaceState.IsDNF == false).Average(drd2 => (int?)drd2.RaceResult.FinishPosition).Round(4),
                LeadLapFinishes = (from drd2 in gr
                                   join rinf in racesInfo on drd2.Race.Id equals rinf.Race.Id
                                   where drd2.RaceResult.Laps == rinf.WinnerLaps
                                   select drd2).Count(),
                AvgStart = gr.Average(drd2 => drd2.RaceResult.StartPosition).Round(4),
                BestFinish = (from drd2 in gr
                              where drd2.RaceResult != null
                              select drd2.RaceResult.FinishPosition).Min(),
                BestStart = (from drd2 in gr
                             where drd2.RaceResult != null
                             select drd2.RaceResult.StartPosition).Min(),
                RaceStarts = gr.Count(),
                PercRaceStarts = ((gr.Count() * 1.0) / (racesInfo.Count() * 1.0)).Round(4),
                AvgNumberOfStarters = (from drd2 in gr
                                       join rinf in racesInfo on drd2.Race.Id equals rinf.Race.Id
                                       select rinf.NumberOfStarters).Average().Round(4),
                Wins = (from drd2 in gr where drd2.RaceResult.FinishPosition == 1 select drd2).Count(),
                Poles = (from drd2 in gr where drd2.RaceResult.StartPosition == 1 select drd2).Count(),
                Top5 = (from drd2 in gr where drd2.RaceResult.FinishPosition <= 5 select drd2).Count(),
                Top10 = (from drd2 in gr where drd2.RaceResult.FinishPosition <= 10 select drd2).Count(),
                DnfCrash = (from drd2 in gr where drd2.RaceResult.RaceState.IsDNFCrash select drd2).Count(),
                DnfMechanical = (from drd2 in gr where drd2.RaceResult.RaceState.IsDNFMechanical select drd2).Count(),
                PercLapsCompleted = (from drd2 in gr
                                     join rinf in racesInfo on drd2.Race.Id equals rinf.Race.Id
                                     select(drd2.RaceResult.Laps * 1.0) / (rinf.WinnerLaps * 1.0)).Average().Round(4),
                PercLapsLed = (from drd2 in gr
                               join rinf in racesInfo on drd2.Race.Id equals rinf.Race.Id
                               select(drd2.RaceResult.LapsLed * 1.0) / (rinf.WinnerLaps * 1.0)).Average().Round(4),
                AvgMidRacePosition = (from drd2 in gr
                                      where drd2.LoopData != null
                                      select drd2.LoopData.MidRacePosition).Average().Round(4),
                AvgHighestPosition = (from drd2 in gr
                                      where drd2.LoopData != null
                                      select drd2.LoopData.HighestPosition).Average().Round(4),
                AvgLowestPosition = (from drd2 in gr
                                     where drd2.LoopData != null
                                     select drd2.LoopData.LowestPosition).Average().Round(4),
                AvgPosition = (from drd2 in gr
                               where drd2.LoopData != null
                               select drd2.LoopData.AveragePosition).Average().Round(4),
                AvgGreenFlagPasses = (from drd2 in gr
                                      where drd2.LoopData != null
                                      select drd2.LoopData.GreenFlagPasses).Average().Round(4),
                AvgGreenFlagPassed = (from drd2 in gr
                                      where drd2.LoopData != null
                                      select drd2.LoopData.GreenFlagPassed).Average().Round(4),
                AvgQualityPasses = (from drd2 in gr
                                    where drd2.LoopData != null
                                    select drd2.LoopData.QualityPasses).Average().Round(4),
                AvgPercQualityPasses = (from drd2 in gr
                                        where drd2.LoopData != null
                                        select
                                        drd2.LoopData.GreenFlagPasses == 0 ? 0 : (drd2.LoopData.QualityPasses * 1.0) / (drd2.LoopData.GreenFlagPasses * 1.0)).Average().Round(4),
                AvgFastestLaps = (from drd2 in gr
                                  where drd2.LoopData != null
                                  select drd2.LoopData.FastestLaps).Average().Round(4),
                AvgPercFastestLaps = (from drd2 in gr
                                      where drd2.LoopData != null
                                      select(drd2.LoopData.FastestLaps * 1.0) / (drd2.RaceResult.Laps * 1.0)).Average().Round(4),
                AvgTop15Laps = (from drd2 in gr
                                where drd2.LoopData != null
                                select drd2.LoopData.Top15Laps).Average().Round(4),
                AvgPercTop15Laps = (from drd2 in gr
                                    where drd2.LoopData != null
                                    select(drd2.LoopData.Top15Laps * 1.0) / (drd2.RaceResult.Laps * 1.0)).Average().Round(4),
                AvgRating = (from drd2 in gr
                             where drd2.LoopData != null
                             select drd2.LoopData.Rating / 150.0).Average().Round(4),
                AvgPitStopTimeRank = gr.Average(drd2 => drd2.PitStopRank).Round(4),
                RacesInGarage = ((
                                     (from drd2 in gr
                                      where drd2.PitStopData != null
                                      select drd2.PitStopData.InGarage.ToInt() * new int?(1)).NullableSum()
                                     )),
                PercRacesInGarage = ((
                                         (from drd2 in gr
                                          where drd2.PitStopData != null
                                          select(drd2.PitStopData.InGarage.ToInt() * new double?(1.0)) / (gr.Count() * 1.0)).NullableSum().Round(4)
                                         ))
            })
                                       .Distinct(new DriverStatsComparerByNumberAndName())
                                       .ToList();

            if (mappingMethod == CarMappingMethod.NAME)
            {
                stats = (from drd in driverRaceDataList
                         group drd by new { drd.RaceResult.Driver } into gr
                         from drd in gr
                         orderby gr.Average(drd2 => drd2.RaceResult.FinishPosition).Round(4) ascending
                         select new DriverStats
                {
                    CarNumber = null,
                    Driver = drd.Driver,
                    AvgFinish = gr.Average(drd2 => (int?)drd2.RaceResult.FinishPosition).Round(4),
                    AvgFinishExcludingDnf = gr.Where(drd2 => drd2.RaceResult.RaceState.IsDNF == false).Average(drd2 => (int?)drd2.RaceResult.FinishPosition).Round(4),
                    LeadLapFinishes = (from drd2 in gr
                                       join rinf in racesInfo on drd2.Race.Id equals rinf.Race.Id
                                       where drd2.RaceResult.Laps == rinf.WinnerLaps
                                       select drd2).Count(),
                    AvgStart = gr.Average(drd2 => drd2.RaceResult.StartPosition).Round(4),
                    BestFinish = (from drd2 in gr
                                  where drd2.RaceResult != null
                                  select drd2.RaceResult.FinishPosition).Min(),
                    BestStart = (from drd2 in gr
                                 where drd2.RaceResult != null
                                 select drd2.RaceResult.StartPosition).Min(),
                    RaceStarts = gr.Count(),
                    PercRaceStarts = ((gr.Count() * 1.0) / (racesInfo.Count() * 1.0)).Round(4),
                    AvgNumberOfStarters = (from drd2 in gr
                                           join rinf in racesInfo on drd2.Race.Id equals rinf.Race.Id
                                           select rinf.NumberOfStarters).Average().Round(4),
                    Wins = (from drd2 in gr where drd2.RaceResult.FinishPosition == 1 select drd2).Count(),
                    Poles = (from drd2 in gr where drd2.RaceResult.StartPosition == 1 select drd2).Count(),
                    Top5 = (from drd2 in gr where drd2.RaceResult.FinishPosition <= 5 select drd2).Count(),
                    Top10 = (from drd2 in gr where drd2.RaceResult.FinishPosition <= 10 select drd2).Count(),
                    DnfCrash = (from drd2 in gr where drd2.RaceResult.RaceState.IsDNFCrash select drd2).Count(),
                    DnfMechanical = (from drd2 in gr where drd2.RaceResult.RaceState.IsDNFMechanical select drd2).Count(),
                    PercLapsCompleted = (from drd2 in gr
                                         join rinf in racesInfo on drd2.Race.Id equals rinf.Race.Id
                                         select(drd2.RaceResult.Laps * 1.0) / (rinf.WinnerLaps * 1.0)).Average().Round(4),
                    PercLapsLed = (from drd2 in gr
                                   join rinf in racesInfo on drd2.Race.Id equals rinf.Race.Id
                                   select(drd2.RaceResult.LapsLed * 1.0) / (rinf.WinnerLaps * 1.0)).Average().Round(4),
                    AvgMidRacePosition = (from drd2 in gr
                                          where drd2.LoopData != null
                                          select drd2.LoopData.MidRacePosition).Average().Round(4),
                    AvgHighestPosition = (from drd2 in gr
                                          where drd2.LoopData != null
                                          select drd2.LoopData.HighestPosition).Average().Round(4),
                    AvgLowestPosition = (from drd2 in gr
                                         where drd2.LoopData != null
                                         select drd2.LoopData.LowestPosition).Average().Round(4),
                    AvgPosition = (from drd2 in gr
                                   where drd2.LoopData != null
                                   select drd2.LoopData.AveragePosition).Average().Round(4),
                    AvgGreenFlagPasses = (from drd2 in gr
                                          where drd2.LoopData != null
                                          select drd2.LoopData.GreenFlagPasses).Average().Round(4),
                    AvgGreenFlagPassed = (from drd2 in gr
                                          where drd2.LoopData != null
                                          select drd2.LoopData.GreenFlagPassed).Average().Round(4),
                    AvgQualityPasses = (from drd2 in gr
                                        where drd2.LoopData != null
                                        select drd2.LoopData.QualityPasses).Average().Round(4),
                    AvgPercQualityPasses = (from drd2 in gr
                                            where drd2.LoopData != null
                                            select
                                            drd2.LoopData.GreenFlagPasses == 0 ? 0 : (drd2.LoopData.QualityPasses * 1.0) / (drd2.LoopData.GreenFlagPasses * 1.0)).Average().Round(4),
                    AvgFastestLaps = (from drd2 in gr
                                      where drd2.LoopData != null
                                      select drd2.LoopData.FastestLaps).Average().Round(4),
                    AvgPercFastestLaps = (from drd2 in gr
                                          where drd2.LoopData != null
                                          select(drd2.LoopData.FastestLaps * 1.0) / (drd2.RaceResult.Laps * 1.0)).Average().Round(4),
                    AvgTop15Laps = (from drd2 in gr
                                    where drd2.LoopData != null
                                    select drd2.LoopData.Top15Laps).Average().Round(4),
                    AvgPercTop15Laps = (from drd2 in gr
                                        where drd2.LoopData != null
                                        select(drd2.LoopData.Top15Laps * 1.0) / (drd2.RaceResult.Laps * 1.0)).Average().Round(4),
                    AvgRating = (from drd2 in gr
                                 where drd2.LoopData != null
                                 select drd2.LoopData.Rating / 150.0).Average().Round(4),
                    AvgPitStopTimeRank = gr.Average(drd2 => drd2.PitStopRank).Round(4),
                    RacesInGarage = ((
                                         (from drd2 in gr
                                          where drd2.PitStopData != null
                                          select drd2.PitStopData.InGarage.ToInt() * new int?(1)).NullableSum()
                                         )),
                    PercRacesInGarage = ((
                                             (from drd2 in gr
                                              where drd2.PitStopData != null
                                              select(drd2.PitStopData.InGarage.ToInt() * new double?(1.0)) / (gr.Count() * 1.0)).NullableSum().Round(4)
                                             ))
                })
                        .Distinct(new DriverStatsComparerByNumberAndName())
                        .ToList();
            }
            else if (mappingMethod == CarMappingMethod.NUMBER)
            {
                stats = (from drd in driverRaceDataList
                         group drd by new { drd.RaceResult.CarNumber } into gr
                         from drd in gr
                         orderby gr.Average(drd2 => drd2.RaceResult.FinishPosition).Round(4) ascending
                         select new DriverStats
                {
                    CarNumber = drd.RaceResult.CarNumber,
                    Driver = null,
                    AvgFinish = gr.Average(drd2 => (int?)drd2.RaceResult.FinishPosition).Round(4),
                    AvgFinishExcludingDnf = gr.Where(drd2 => drd2.RaceResult.RaceState.IsDNF == false).Average(drd2 => (int?)drd2.RaceResult.FinishPosition).Round(4),
                    LeadLapFinishes = (from drd2 in gr
                                       join rinf in racesInfo on drd2.Race.Id equals rinf.Race.Id
                                       where drd2.RaceResult.Laps == rinf.WinnerLaps
                                       select drd2).Count(),
                    AvgStart = gr.Average(drd2 => drd2.RaceResult.StartPosition).Round(4),
                    BestFinish = (from drd2 in gr
                                  where drd2.RaceResult != null
                                  select drd2.RaceResult.FinishPosition).Min(),
                    BestStart = (from drd2 in gr
                                 where drd2.RaceResult != null
                                 select drd2.RaceResult.StartPosition).Min(),
                    RaceStarts = gr.Count(),
                    PercRaceStarts = ((gr.Count() * 1.0) / (racesInfo.Count() * 1.0)).Round(4),
                    AvgNumberOfStarters = (from drd2 in gr
                                           join rinf in racesInfo on drd2.Race.Id equals rinf.Race.Id
                                           select rinf.NumberOfStarters).Average().Round(4),
                    Wins = (from drd2 in gr where drd2.RaceResult.FinishPosition == 1 select drd2).Count(),
                    Poles = (from drd2 in gr where drd2.RaceResult.StartPosition == 1 select drd2).Count(),
                    Top5 = (from drd2 in gr where drd2.RaceResult.FinishPosition <= 5 select drd2).Count(),
                    Top10 = (from drd2 in gr where drd2.RaceResult.FinishPosition <= 10 select drd2).Count(),
                    DnfCrash = (from drd2 in gr where drd2.RaceResult.RaceState.IsDNFCrash select drd2).Count(),
                    DnfMechanical = (from drd2 in gr where drd2.RaceResult.RaceState.IsDNFMechanical select drd2).Count(),
                    PercLapsCompleted = (from drd2 in gr
                                         join rinf in racesInfo on drd2.Race.Id equals rinf.Race.Id
                                         select(drd2.RaceResult.Laps * 1.0) / (rinf.WinnerLaps * 1.0)).Average().Round(4),
                    PercLapsLed = (from drd2 in gr
                                   join rinf in racesInfo on drd2.Race.Id equals rinf.Race.Id
                                   select(drd2.RaceResult.LapsLed * 1.0) / (rinf.WinnerLaps * 1.0)).Average().Round(4),
                    AvgMidRacePosition = (from drd2 in gr
                                          where drd2.LoopData != null
                                          select drd2.LoopData.MidRacePosition).Average().Round(4),
                    AvgHighestPosition = (from drd2 in gr
                                          where drd2.LoopData != null
                                          select drd2.LoopData.HighestPosition).Average().Round(4),
                    AvgLowestPosition = (from drd2 in gr
                                         where drd2.LoopData != null
                                         select drd2.LoopData.LowestPosition).Average().Round(4),
                    AvgPosition = (from drd2 in gr
                                   where drd2.LoopData != null
                                   select drd2.LoopData.AveragePosition).Average().Round(4),
                    AvgGreenFlagPasses = (from drd2 in gr
                                          where drd2.LoopData != null
                                          select drd2.LoopData.GreenFlagPasses).Average().Round(4),
                    AvgGreenFlagPassed = (from drd2 in gr
                                          where drd2.LoopData != null
                                          select drd2.LoopData.GreenFlagPassed).Average().Round(4),
                    AvgQualityPasses = (from drd2 in gr
                                        where drd2.LoopData != null
                                        select drd2.LoopData.QualityPasses).Average().Round(4),
                    AvgPercQualityPasses = (from drd2 in gr
                                            where drd2.LoopData != null
                                            select
                                            drd2.LoopData.GreenFlagPasses == 0 ? 0 : (drd2.LoopData.QualityPasses * 1.0) / (drd2.LoopData.GreenFlagPasses * 1.0)).Average().Round(4),
                    AvgFastestLaps = (from drd2 in gr
                                      where drd2.LoopData != null
                                      select drd2.LoopData.FastestLaps).Average().Round(4),
                    AvgPercFastestLaps = (from drd2 in gr
                                          where drd2.LoopData != null
                                          select(drd2.LoopData.FastestLaps * 1.0) / (drd2.RaceResult.Laps * 1.0)).Average().Round(4),
                    AvgTop15Laps = (from drd2 in gr
                                    where drd2.LoopData != null
                                    select drd2.LoopData.Top15Laps).Average().Round(4),
                    AvgPercTop15Laps = (from drd2 in gr
                                        where drd2.LoopData != null
                                        select(drd2.LoopData.Top15Laps * 1.0) / (drd2.RaceResult.Laps * 1.0)).Average().Round(4),
                    AvgRating = (from drd2 in gr
                                 where drd2.LoopData != null
                                 select drd2.LoopData.Rating / 150.0).Average().Round(4),
                    AvgPitStopTimeRank = gr.Average(drd2 => drd2.PitStopRank).Round(4),
                    RacesInGarage = ((
                                         (from drd2 in gr
                                          where drd2.PitStopData != null
                                          select drd2.PitStopData.InGarage.ToInt() * new int?(1)).NullableSum()
                                         )),
                    PercRacesInGarage = ((
                                             (from drd2 in gr
                                              where drd2.PitStopData != null
                                              select(drd2.PitStopData.InGarage.ToInt() * new double?(1.0)) / (gr.Count() * 1.0)).NullableSum().Round(4)
                                             ))
                })
                        .Distinct(new DriverStatsComparerByNumberAndName())
                        .ToList();
            }

            return(stats);
        }
        private async void butImportRatings_Click(object sender, EventArgs e)
        {
            bool showImportDialog = true;

            if (this.colCars.IsDirty == true)
            {
                DialogResult sure = MessageBox.Show("You have unsaved changes in your carlist. If you import ratings from real life data, you may possibly overwrite these changes. Are you sure you want to continue ?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                if (sure == DialogResult.No)
                {
                    showImportDialog = false;
                }
            }


            if (showImportDialog)
            {
                var result = await UserManager.LoginAsync(this);

                if (result != null && !result.Succeeded)
                {
                    MessageBox.Show(result.Error, "Login unsuccessful", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                if (await UserManager.IsLoggedInAsync())
                {
                    if (!UserManager.IsEmailVerified() == true)
                    {
                        MessageBox.Show("You'll need to verify your email address before you can proceed. Please check your mails.", "Email Verification Required", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    else if (UserManager.IsEmailVerified() == true)
                    {
                        frmGetRealData fRealData = new frmGetRealData();
                        DialogResult   dres      = fRealData.ShowDialog();

                        if (dres == DialogResult.OK)
                        {
                            ////START FROM A BLANK CARLIST
                            //this.colCars.IsDirty = false; //set to false to avoid confirmation dialog
                            //this.LoadCarList();


                            RealDriverCollection RealDrivers = fRealData.RealDrivers;

                            CarMappingMethod mappingMethod = fRealData.MappingMethod;
                            bool             rosterOnly    = fRealData.CarListOnly;

                            //IF THE "ROSTER ONLY" CHECKBOX WAS CHECKED, DON'T ALLOW TO MANUALLY REMAP TO ANOTHER REAL LIFE DRIVER (THERE'S NO SENSE ANYWAY)
                            if (rosterOnly == true)
                            {
                                this.dgCars.Columns["mappedToRealDriverDataGridViewComboBoxColumn"].ReadOnly = true;
                            }
                            else
                            {
                                this.dgCars.Columns["mappedToRealDriverDataGridViewComboBoxColumn"].ReadOnly = false;
                            }


                            if (RealDrivers != null)
                            {
                                this.mainProc.RealDrivers = this.MapRealDataToCars(RealDrivers, mappingMethod);
                                this.LogUnmappedDrivers();
                            }

                            this.dgCars.Focus();
                            this.dgCars.Refresh();

                            if (this.dgCars.Rows.Count > 0)
                            {
                                this.dgCars.Rows[0].Selected = true;
                                this.dgCars_SelectionChanged(null, null);
                            }
                        }
                    }
                }
            }
        }
        private RealDriverCollection MapRealDataToCars(RealDriverCollection RealDrivers, CarMappingMethod mappingMethod)
        {
            if (this.bsCars.DataSource != null)
            {
                CarCollection colCars = (CarCollection)this.bsCars.DataSource;

                foreach (NR2003Car c in colCars)
                {
                    RealDriver foundDriver = null;

                    switch (mappingMethod)
                    {
                    case CarMappingMethod.NAME:
                        foundDriver = RealDrivers.FindByName(c.DriverFirstName, c.DriverLastName);
                        break;

                    case CarMappingMethod.NUMBER:
                        foundDriver = RealDrivers.FindByNumber(c.Number);
                        break;

                    case CarMappingMethod.NUMBER_AND_NAME:
                        foundDriver = RealDrivers.FindByNumberAndName(c.Number, c.DriverFirstName, c.DriverLastName);
                        break;
                    }


                    if (foundDriver != null)
                    {
                        c.MappedToRealDriver = foundDriver;

                        c.Selected = true;

                        foundDriver.MatchFound = true;
                    }
                }

                return(RealDrivers);
            }
            else
            {
                return(null);
            }
        }