public virtual List <NascarRaceLap> UpdateRaceLaps(List <NascarRaceLap> lastLaps, RaceState state)
        {
            LapState lapState = LapState.OneToGreenFlag;

            switch (state)
            {
            case RaceState.PreRace:
            {
                lapState = LapState.OneToGreenFlag;
                break;
            }

            case RaceState.GreenFlag:
            case RaceState.WhiteFlag:
            case RaceState.Checkered:
            case RaceState.Overdrive:
            {
                lapState = LapState.GreenFlag;
                break;
            }

            case RaceState.Caution:
            case RaceState.OneToGo:
            case RaceState.EndOfStage:
            {
                lapState = LapState.CautionFlag;
                break;
            }
            }
            return(UpdateRaceLaps(lastLaps, lapState));
        }
 protected virtual void PrintStandings(List <NascarRaceLap> raceLaps, LapState state)
 {
     Console.WriteLine($"End of Lap {raceLaps[0].LapNumber} {state.ToString()}");
     Console.WriteLine();
     Console.WriteLine($"              Delta     Delta   Laps     Lap        Lap       Total        Delta    Actual              Lucky      Leader    Lap");
     Console.WriteLine($"     [CAR]    Next      Leader  Down     Time      Speed     Elapsed      Physical  Delta     Pit       Dog         Lap     Number");
     for (int x = 0; x < raceLaps.Count; x++)
     {
         var rl = raceLaps[x];
         Console.WriteLine($"{String.Format("{0,-2}", rl.Position)} - [{String.Format("{0,2}", rl.VehicleId)}]   {String.Format("{0,7}", rl.Delta < 0 ? rl.Delta.ToString("####") : rl.Delta.ToString("##.##0"))}   {String.Format("{0,7}", rl.DeltaLeader < 0 ? rl.DeltaLeader.ToString("####") : rl.DeltaLeader.ToString("##.##0"))}     {(rl.IsLeadLap ? "  " : String.Format("{0,-2}", rl.LeaderLap - rl.LapNumber))}    {String.Format("{0,8}", rl.LapTime.ToString("###.##0"))}  {String.Format("{0,8}", rl.LapSpeed.ToString("###.##0"))}   {String.Format("{0,8}", rl.TotalTime.ToString("######.##0"))}    {String.Format("{0, 8}", rl.DeltaPhysical.ToString("######.##0"))} {String.Format("{0, 8}", rl.DeltaTravelledLeader.ToString("######.##0"))}    {(rl.PitInLap && rl.PitOutLap ? "Pit In  " : rl.PitOutLap ? "Pit Out " : "-       ")}  {(rl.IsLuckyDog ? "Lucky Dog" : "-        ")}   {rl.LeaderLap}      {rl.LapNumber}");
     }
Exemple #3
0
 public EndLapCommandV1_0(LapState lapState, int gold, int score) : base(lapState, gold, score)
 {
 }
        public virtual List <NascarRaceLap> UpdateRaceLaps(List <NascarRaceLap> lastLaps, LapState state)
        {
            List <NascarRaceLap> newLaps = null;

            switch (state)
            {
            case LapState.OneToGreenFlag:
            {
                newLaps = OneToGoLaps(lastLaps);
                break;
            }

            case LapState.GreenFlag:
            {
                newLaps = GreenFlagLaps(lastLaps);
                break;
            }

            case LapState.CautionFlag:
            {
                newLaps = CautionLaps(lastLaps);
                break;
            }

            default:
            {
                throw new ArgumentException($"Invalid LapState: {state.ToString()}");
            }
            }

            PrintStandings(newLaps, state);

            return(newLaps);
        }
        protected virtual NascarRaceLap[] GetNewRaceLaps(List <NascarRaceLap> lastLaps, LapState state)
        {
            NascarRaceLap[] newLaps = lastLaps.Select(l => new NascarRaceLap()
            {
                Position     = l.Position,
                DriverId     = l.DriverId,
                VehicleId    = l.VehicleId,
                TotalTime    = l.TotalTime,
                LapNumber    = l.LapNumber,
                LapsSincePit = l.LapsSincePit,
                PitInLap     = l.PitInLap,
                PitOutLap    = l.PitOutLap,
                IsLuckyDog   = l.IsLuckyDog,
                LeaderLap    = l.LeaderLap
            }).ToArray();

            LapTimeResult lapTimeResult = null;

            for (int i = 0; i < newLaps.Length; i++)
            {
                if (newLaps[i].PitInLap && !newLaps[i].PitOutLap)
                {
                    newLaps[i].LapsSincePit = -1;
                    newLaps[i].PitOutLap    = true;
                }
                else
                {
                    if (!newLaps[i].PitInLap && newLaps[i].PitOutLap)
                    {
                        lapTimeResult        = LapTimeService.GetPitInLapTime();
                        newLaps[i].PitOutLap = false;
                    }

                    if (newLaps[i].PitInLap && newLaps[i].PitOutLap)
                    {
                        lapTimeResult = LapTimeService.GetPitOutLapTime();
                        LapTimeResult pitLapTimeResult = null;

                        if (state == LapState.CautionFlag || state == LapState.OneToGreenFlag)
                        {
                            pitLapTimeResult = LapTimeService.GetCautionLapTime();
                        }
                        else if (state == LapState.GreenFlag)
                        {
                            pitLapTimeResult = LapTimeService.GetLapTime(0);
                        }

                        lapTimeResult.LapTime += pitLapTimeResult.LapTime;
                        lapTimeResult.LapSpeed = LapTimeService.GetLapSpeed(lapTimeResult.LapTime);

                        newLaps[i].PitInLap = false;
                    }
                    else if (state == LapState.CautionFlag || state == LapState.OneToGreenFlag)
                    {
                        lapTimeResult = LapTimeService.GetCautionLapTime();
                    }
                    else if (state == LapState.GreenFlag)
                    {
                        lapTimeResult = LapTimeService.GetLapTime(newLaps[i].LapsSincePit);
                    }
                    newLaps[i].LapNumber    += 1;
                    newLaps[i].LapTime       = lapTimeResult.LapTime;
                    newLaps[i].LapSpeed      = lapTimeResult.LapSpeed;
                    newLaps[i].TotalTime    += lapTimeResult.LapTime;
                    newLaps[i].LapsSincePit += 1;
                }
            }

            Array.Sort(newLaps);

            newLaps[0].LeaderLap   = newLaps[0].LapNumber;
            newLaps[0].Delta       = 0;
            newLaps[0].DeltaLeader = 0;
            newLaps[0].Position    = 1;

            double leaderTotalTime = newLaps[0].TotalTime;
            int    leaderLap       = newLaps[0].LeaderLap;

            for (int i = 1; i < newLaps.Length; i++)
            {
                newLaps[i].Position  = i + 1;
                newLaps[i].LeaderLap = leaderLap;

                if (newLaps[i].LapsBehind > 0)
                {
                    // vehicle is laps down
                    newLaps[i].Delta                = newLaps[i].LapsBehind * -1;
                    newLaps[i].DeltaLeader          = newLaps[i].LapsBehind * -1;
                    newLaps[i].DeltaPhysical        = Math.Round((newLaps[i].TotalTime - (newLaps[0].AverageLapTime * newLaps[i].LapNumber)) % LapTimeService.BaseLapTime, 3);
                    newLaps[i].DeltaTravelledLeader = newLaps[i].TotalTime - (newLaps[0].AverageLapTime * newLaps[i].LapNumber) + (LapTimeService.BaseLapTime * newLaps[i].LapsBehind);
                }
                else
                {
                    // vehicle is on lead lap
                    newLaps[i].Delta                = newLaps[i].TotalTime - newLaps[i - 1].TotalTime;
                    newLaps[i].DeltaLeader          = newLaps[i].TotalTime - leaderTotalTime;
                    newLaps[i].DeltaPhysical        = Math.Round((newLaps[i].TotalTime - leaderTotalTime) % LapTimeService.BaseLapTime, 3);
                    newLaps[i].DeltaTravelledLeader = newLaps[i].DeltaLeader;
                }
            }

            return(newLaps);
        }