internal void FillTimingInfo(DriverInfo driverInfo, DriverData r3EDriverData, R3ESharedData r3RData)
        {
            driverInfo.Timing.GapAhead  = -TimeSpan.FromSeconds(r3EDriverData.TimeDeltaFront);
            driverInfo.Timing.GapBehind = -TimeSpan.FromSeconds(r3EDriverData.TimeDeltaBehind);

            if (driverInfo.IsPlayer)
            {
                driverInfo.Timing.LastLapTime = r3RData.LapTimePreviousSelf != -1 ? TimeSpan.FromSeconds(r3RData.LapTimePreviousSelf) : TimeSpan.Zero;
                if (r3RData.SectorTimesCurrentSelf.Sector1 != -1 || r3RData.SectorTimesCurrentSelf.Sector2 != -1 ||
                    r3RData.SectorTimesPreviousSelf.Sector3 != -1)
                {
                    driverInfo.Timing.LastSector1Time = TimeSpan.FromSeconds(r3RData.SectorTimesCurrentSelf.Sector1);
                    driverInfo.Timing.LastSector2Time = TimeSpan.FromSeconds(r3RData.SectorTimesCurrentSelf.Sector2 - r3RData.SectorTimesCurrentSelf.Sector1);
                    driverInfo.Timing.LastSector3Time = TimeSpan.FromSeconds(r3RData.SectorTimesPreviousSelf.Sector3 - r3RData.SectorTimesPreviousSelf.Sector2);
                }
                driverInfo.Timing.CurrentSector  = r3EDriverData.TrackSector;
                driverInfo.Timing.CurrentLapTime = TimeSpan.FromSeconds(r3RData.LapTimeCurrentSelf);
                return;
            }

            if (r3EDriverData.SectorTimeCurrentSelf.Sector1 != -1 || r3EDriverData.SectorTimeCurrentSelf.Sector2 != -1 ||
                r3EDriverData.SectorTimePreviousSelf.Sector3 != -1)
            {
                driverInfo.Timing.LastSector1Time = TimeSpan.FromSeconds(r3EDriverData.SectorTimeCurrentSelf.Sector1);
                driverInfo.Timing.LastSector2Time = TimeSpan.FromSeconds(r3EDriverData.SectorTimeCurrentSelf.Sector2 - r3EDriverData.SectorTimeCurrentSelf.Sector1);
                driverInfo.Timing.LastSector3Time = TimeSpan.FromSeconds(r3EDriverData.SectorTimePreviousSelf.Sector3 - r3EDriverData.SectorTimePreviousSelf.Sector2);
            }

            driverInfo.Timing.CurrentLapTime = TimeSpan.FromSeconds(r3EDriverData.LapTimeCurrentSelf);
            driverInfo.Timing.LastLapTime    = r3EDriverData.SectorTimePreviousSelf.Sector3 != -1 ? TimeSpan.FromSeconds(r3EDriverData.SectorTimePreviousSelf.Sector3) : TimeSpan.Zero;
            driverInfo.Timing.CurrentSector  = r3EDriverData.TrackSector;
        }
        internal static void ComputeDistanceToPlayer(DriverInfo player, DriverInfo driverInfo, R3ESharedData r3RData)
        {
            if (player == null)
            {
                return;
            }

            if (driverInfo.FinishStatus == DriverFinishStatus.Dq || driverInfo.FinishStatus == DriverFinishStatus.Dnf ||
                driverInfo.FinishStatus == DriverFinishStatus.Dnq || driverInfo.FinishStatus == DriverFinishStatus.Dns)
            {
                driverInfo.DistanceToPlayer = double.MaxValue;
                return;
            }

            double trackLength       = r3RData.LayoutLength;
            double playerLapDistance = player.LapDistance;

            double distanceToPlayer = playerLapDistance - driverInfo.LapDistance;

            if (distanceToPlayer < -(trackLength / 2))
            {
                distanceToPlayer = distanceToPlayer + trackLength;
            }

            if (distanceToPlayer > (trackLength / 2))
            {
                distanceToPlayer = distanceToPlayer - trackLength;
            }

            driverInfo.DistanceToPlayer = distanceToPlayer;
        }
        internal void AddDriversData(SimulatorDataSet data, R3ESharedData r3RData)
        {
            if (r3RData.NumCars == -1)
            {
                return;
            }

            data.DriversInfo = new DriverInfo[r3RData.NumCars];
            string     playerName  = FromByteArray(r3RData.PlayerName);
            DriverInfo playersInfo = null;

            for (int i = 0; i < r3RData.NumCars; i++)
            {
                DriverData r3RDriverData = r3RData.DriverData[i];
                DriverInfo driverInfo    = CreateDriverInfo(r3RData, r3RDriverData, playerName);

                /*if (string.IsNullOrWhiteSpace(driverInfo.DriverName))
                 * {
                 *  continue;
                 * }*/

                if (driverInfo.IsPlayer)
                {
                    playersInfo = driverInfo;
                    driverInfo.CurrentLapValid = r3RData.CurrentLapValid == 1;
                    _lastPlayer = driverInfo;
                }
                else
                {
                    AddWheelInfo(driverInfo, r3RDriverData);
                    driverInfo.CurrentLapValid = r3RDriverData.CurrentLapValid == 1;
                }

                AddWheelInfo(driverInfo, r3RDriverData);
                data.DriversInfo[i] = driverInfo;
                if (driverInfo.Position == 1)
                {
                    data.SessionInfo.LeaderCurrentLap = driverInfo.CompletedLaps + 1;
                    data.LeaderInfo = driverInfo;
                }

                AddLappingInformation(data, r3RData, driverInfo);
                FillTimingInfo(driverInfo, r3RDriverData, r3RData);

                if (driverInfo.FinishStatus == DriverFinishStatus.Finished && !driverInfo.IsPlayer && driverInfo.Position > _lastPlayer?.Position)
                {
                    driverInfo.CompletedLaps--;
                    driverInfo.FinishStatus = DriverFinishStatus.None;
                }

                _connector.StoreLastTickInfo(driverInfo);
            }

            if (playersInfo != null)
            {
                data.PlayerInfo = playersInfo;
            }
        }
        private static void AddWheelInfo(DriverInfo driverInfo, DriverData r3RDriverData)
        {
            driverInfo.CarInfo.WheelsInfo.FrontLeft.TyreType  = ((Constant.TireSubtype)r3RDriverData.TireSubtypeFront).ToString();
            driverInfo.CarInfo.WheelsInfo.FrontRight.TyreType = driverInfo.CarInfo.WheelsInfo.FrontLeft.TyreType;

            driverInfo.CarInfo.WheelsInfo.RearLeft.TyreType  = ((Constant.TireSubtype)r3RDriverData.TireSubtypeRear).ToString();
            driverInfo.CarInfo.WheelsInfo.RearRight.TyreType = driverInfo.CarInfo.WheelsInfo.RearLeft.TyreType;

            if (driverInfo.CarInfo.WheelsInfo.FrontLeft.TyreType == "Unavailable")
            {
                driverInfo.CarInfo.WheelsInfo.FrontLeft.TyreType  = "Prime";
                driverInfo.CarInfo.WheelsInfo.FrontRight.TyreType = "Prime";
            }

            if (driverInfo.CarInfo.WheelsInfo.RearLeft.TyreType == "Unavailable")
            {
                driverInfo.CarInfo.WheelsInfo.RearLeft.TyreType  = "Prime";
                driverInfo.CarInfo.WheelsInfo.RearRight.TyreType = "Prime";
            }
        }
        private DriverInfo CreateDriverInfo(R3ESharedData r3RData, DriverData r3RDriverData, string playerName)
        {
            DriverInfo driverInfo = new DriverInfo
            {
                DriverName    = FromByteArray(r3RDriverData.R3EDriverInfo.Name),
                CompletedLaps = r3RDriverData.CompletedLaps,
                CarName       = string.Empty,
                InPits        = r3RDriverData.InPitlane == 1
            };

            driverInfo.IsPlayer        = driverInfo.DriverName == playerName;
            driverInfo.Position        = r3RDriverData.Place;
            driverInfo.PositionInClass = r3RDriverData.PlaceClass;
            driverInfo.Speed           = Velocity.FromMs(r3RDriverData.CarSpeed);
            driverInfo.LapDistance     = r3RDriverData.LapDistance;
            driverInfo.TotalDistance   = r3RDriverData.CompletedLaps * r3RData.LayoutLength + r3RDriverData.LapDistance;
            driverInfo.CarName         = _database.GetCarName(r3RDriverData.R3EDriverInfo.ModelId);
            driverInfo.CarClassName    = _database.GetClassName(r3RDriverData.R3EDriverInfo.ClassId);
            driverInfo.CarClassId      = r3RDriverData.R3EDriverInfo.ClassPerformanceIndex.ToString();
            driverInfo.FinishStatus    = FromR3RStatus(r3RDriverData.FinishStatus);
            driverInfo.WorldPosition   = new Point3D(Distance.FromMeters(r3RDriverData.Position.X * -1), Distance.FromMeters(r3RDriverData.Position.Y), Distance.FromMeters(r3RDriverData.Position.Z));
            ComputeDistanceToPlayer(_lastPlayer, driverInfo, r3RData);
            return(driverInfo);
        }
 private void AddLappingInformation(SimulatorDataSet data, R3ESharedData r3RData, DriverInfo driverInfo)
 {
     if (data.SessionInfo.SessionType == SessionType.Race && _lastPlayer != null &&
         _lastPlayer.CompletedLaps != 0)
     {
         driverInfo.IsBeingLappedByPlayer =
             driverInfo.TotalDistance < (_lastPlayer.TotalDistance - r3RData.LayoutLength * 0.5);
         driverInfo.IsLappingPlayer =
             _lastPlayer.TotalDistance < (driverInfo.TotalDistance - r3RData.LayoutLength * 0.5);
     }
 }