public static BestLapViewModel AsViewModel(this BestLap bestLap, bool isMetricUnits)
        {
            var lapViewModel = new BestLapViewModel
            {
                Id              = bestLap.id,
                IsPublic        = bestLap.IsPublic,
                TrackId         = bestLap.TrackId,
                UserName        = bestLap.UserName,
                UserDisplayName = bestLap.UserDisplayName,
                VehicleId       = bestLap.VehicleId,
                Vehicle         = bestLap.Vehicle != null
                            ? bestLap.Vehicle.AsViewModel()
                            : null,
                VerificationCode   = bestLap.VerificationCode,
                StartElapsedTime   = new TimeSpan(0),
                EndElapsedTime     = new TimeSpan(bestLap.LapTimeTicks),
                DataPoints         = new ObservableCollection <LapDataPointViewModel>(),
                Timestamp          = bestLap.Timestamp.ToLocalTime(),
                IsUnofficial       = bestLap.IsUnofficial,
                GpsDeviceName      = bestLap.GpsDeviceName,
                WeatherCondition   = bestLap.WeatherCondition,
                AmbientTemperature = bestLap.AmbientTemperature
            };

            double maximumSpeed = 0;

            foreach (var dataPoint in bestLap.DataPoints)
            {
                double?speedInUserUnits = isMetricUnits
                                            ? dataPoint.Speed * Constants.KMH_TO_METRES_PER_SECOND
                                            : dataPoint.Speed * Constants.KMH_TO_METRES_PER_SECOND / Constants.MILES_TO_KILOMETRES;

                lapViewModel.DataPoints.Add(new LapDataPointViewModel
                {
                    SectorNumber  = dataPoint.SectorNumber,
                    ElapsedTime   = new TimeSpan(dataPoint.ElapsedTimeTicks),
                    AccelerationX = dataPoint.AccelerationX,
                    AccelerationY = dataPoint.AccelerationY,
                    AccelerationZ = dataPoint.AccelerationZ,
                    Altitude      = dataPoint.Altitude,
                    Heading       = dataPoint.Heading,
                    Latitude      = dataPoint.Latitude,
                    Longitude     = dataPoint.Longitude,
                    Speed         = speedInUserUnits
                });
                if (speedInUserUnits.HasValue && speedInUserUnits.Value > maximumSpeed)
                {
                    maximumSpeed = speedInUserUnits.Value;
                }
            }
            lapViewModel.MaximumSpeed = maximumSpeed;

            return(lapViewModel);
        }
Example #2
0
        public static BestLap AsModel(this BestLapViewModel lapViewModel, VehicleViewModel vehicle, bool isMetricUnits, bool isPublic, string userName, long trackId, IEnumerable <TrackSectorViewModel> sectors)
        {
            DateTimeOffset utcLapTimestamp  = lapViewModel.Timestamp.ToUniversalTime();
            var            hashAlgorithm    = new HMACSHA256(Convert.FromBase64String(Constants.APP_LAPVERIFICATION_HASHKEY));
            string         verificationText = string.Format("{0}{1}{2}{3}{4}{5}", Constants.APP_LAPVERIFICATION_PREFIX, trackId, vehicle.Key.ToString().ToUpperInvariant(), lapViewModel.LapTime.Ticks, utcLapTimestamp.ToString("yyyy-MM-ddTHH:mm:ss.fffK"), userName);
            string         verificationCode = Convert.ToBase64String(hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(verificationText)));

            var model = new BestLap
            {
                LapTimeTicks       = lapViewModel.LapTime.Ticks,
                Timestamp          = utcLapTimestamp,
                VehicleId          = vehicle.Id,
                VehicleClass       = (int)vehicle.Class,
                Vehicle            = vehicle.AsModel(),
                VerificationCode   = verificationCode,
                TrackId            = trackId,
                IsPublic           = isPublic,
                UserName           = userName,
                IsUnofficial       = lapViewModel.IsUnofficial,
                GpsDeviceName      = lapViewModel.GpsDeviceName,
                WeatherCondition   = lapViewModel.WeatherCondition,
                AmbientTemperature = lapViewModel.AmbientTemperature
            };

            var dataPoints           = new List <LapDataPoint>();
            var geographicDataPoints = lapViewModel.DataPoints.Where(dp => dp != null && dp.Latitude.HasValue && dp.Longitude.HasValue);

            foreach (var sector in sectors)
            {
                LapDataPointViewModel lastDataPoint     = null;
                LapDataPointViewModel previousDataPoint = null;
                foreach (var dataPoint in geographicDataPoints.SkipWhile(dp => dp.SectorNumber != sector.SectorNumber))
                {
                    if (dataPoint.SectorNumber != sector.SectorNumber)
                    {
                        break;
                    }
                    lastDataPoint = dataPoint;
                    if (previousDataPoint != null && (dataPoint.ElapsedTime - previousDataPoint.ElapsedTime < TimeSpan.FromMilliseconds(900d)))
                    {
                        continue;
                    }
                    previousDataPoint = dataPoint;
                    dataPoints.Add(dataPoint.AsBestLapDataPoint(isMetricUnits));
                }
                // Last data point used for split times
                dataPoints.Add(lastDataPoint.AsBestLapDataPoint(isMetricUnits));
            }
            model.DataPoints = dataPoints;
            return(model);
        }
Example #3
0
        public static bool RecreateVerificationCode(this BestLap bestLap, string userName, long newTrackId)
        {
            DateTimeOffset utcLapTimestamp  = bestLap.Timestamp.ToUniversalTime();
            var            hashAlgorithm    = new HMACSHA256(Convert.FromBase64String(Constants.APP_LAPVERIFICATION_HASHKEY));
            string         verificationText = string.Format("{0}{1}{2}{3}{4}{5}", Constants.APP_LAPVERIFICATION_PREFIX, bestLap.TrackId, bestLap.Vehicle.Key.ToString().ToUpperInvariant(), bestLap.LapTimeTicks, utcLapTimestamp.ToString("yyyy-MM-ddTHH:mm:ss.fffK"), userName);
            string         verificationCode = Convert.ToBase64String(hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(verificationText)));

            if (verificationCode != bestLap.VerificationCode)
            {
                return(false);
            }
            bestLap.TrackId          = newTrackId;
            verificationText         = string.Format("{0}{1}{2}{3}{4}{5}", Constants.APP_LAPVERIFICATION_PREFIX, bestLap.TrackId, bestLap.Vehicle.Key.ToString().ToUpperInvariant(), bestLap.LapTimeTicks, utcLapTimestamp.ToString("yyyy-MM-ddTHH:mm:ss.fffK"), userName);
            verificationCode         = Convert.ToBase64String(hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(verificationText)));
            bestLap.VerificationCode = verificationCode;
            return(true);
        }
        private static TrackViewModel CreateTrackViewModelFromTrack(Track track, bool isMetricUnits, bool isLocal, BestLap bestLap, string fileNameWhenLoaded)
        {
            var bestLapViewModel = bestLap != null?bestLap.AsViewModel(isMetricUnits) : null;

            return(new TrackViewModel
            {
                Id = track.id,
                Name = track.Name,
                Latitude = track.Latitude,
                Longitude = track.Longitude,
                IsLocal = isLocal,
                Description = track.Description,
                Length = isMetricUnits
                                ? track.Length
                                : track.Length / Constants.MILES_TO_KILOMETRES,
                IsMetricUnits = isMetricUnits,
                BackgroundImagePath = track.BackgroundImagePath,
                Sectors = track.Sectors != null
                            ? new ObservableCollection <TrackSectorViewModel>(track.Sectors.Select(AsViewModel))
                            : null,
                BestLap = bestLapViewModel,
                MaximumSpeed = bestLapViewModel != null ? bestLapViewModel.MaximumSpeed : 0,
                FileNameWhenLoaded = fileNameWhenLoaded,
                TotalLaps = track.TotalLaps,
                Country = track.Country,
                Timestamp = track.Timestamp
            });
        }
        public BestLap UpdateFastestLap(Laptime lap, Driver driver)
        {
            var classId = driver.Car.CarClassId;
            if (!this.ClassBestLaps.ContainsKey(classId))
            {
                this.ClassBestLaps.Add(classId, BestLap.Default);
            }

            if (lap.Value > 0 && this.ClassBestLaps[classId].Laptime.Value > lap.Value)
            {
                var bestlap = new BestLap(lap, driver);
                this.ClassBestLaps[classId] = bestlap;

                this.OverallBestLap =
                    this.ClassBestLaps.Values.Where(l => l.Laptime.Value > 0)
                        .OrderBy(l => l.Laptime.Value)
                        .FirstOrDefault();

                return bestlap;
            }
            return null;
        }
Example #6
0
 public FastLapEventArgs(Driver driver, BestLap lap, double time)
     : base(time)
 {
     this.Driver = driver;
     this.Lap = lap;
 }