private void FormPrelimRaceCommand_Load(object sender, EventArgs e)
        {
            //*******************************************************************
            // NOTE: The first heat was already loaded by the Prelim Race Form
            //*******************************************************************

            // Get the next race
            _activeRace = RaceDataStore.RaceProc.GetNextRace();

            this.LoadRacerData();

            // Hook into the RaceComplete event
        }
        /// <summary>
        /// Load the next Race
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonNextRace_Click(object sender, EventArgs e)
        {
            //TODO: Close race

            // Is the current race completed?
            if (this.IsRaceRecorded().Equals(false))
            {
                // Tell the user we can't continue
                MessageBox.Show(this, "Active race has not been completed.", "Are you sure", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }

            try
            {
                _activeRace = RaceDataStore.RaceProc.GetNextRace();

                this.LoadRacerData();
            }
            catch (NoMoreRacersException)
            {
                // Mark our heat as complete and increment the
                // heat number
                RaceDataStore.PrelimHeatCompleted = false;
                RaceDataStore.PrelimHeatNumber++;

                // Are we done with the prelims?
                if (RaceDataStore.PrelimHeatNumber > 4)
                {
                    if (OnPrelimCompleteEvent != null)
                    {
                        OnPrelimCompleteEvent();
                    }

                    MessageBox.Show(this,"The prelims have completed. Stay tuned for the posting of the Sweet 16 Racers.","Prelims Complete",MessageBoxButtons.OK, MessageBoxIcon.Information);

                    this.Close();
                }
                else
                {
                    this.toolStripLabelHeat.Text = String.Concat("Heat ", RaceDataStore.PrelimHeatNumber);

                    this.ClearAllTextBoxes(this);

                    RaceDataStore.RaceProc.LoadNextHeat();

                    // Get the next race
                    _activeRace = RaceDataStore.RaceProc.GetNextRace();

                    this.LoadRacerData();
                }
            }
        }
        private void RunEmulatedRace(Race activeRace)
        {
            int activeHeat = RaceDataStore.PrelimHeatNumber - 1;

            Random randomTimeGenerator = new Random();

            double time = randomTimeGenerator.NextDouble();
            activeRace.Lane1Racer.Heats[activeHeat].Time = randomTimeGenerator.Next(3, 6) + time;

            time = randomTimeGenerator.NextDouble();
            activeRace.Lane2Racer.Heats[activeHeat].Time = randomTimeGenerator.Next(3, 6) + time;

            time = randomTimeGenerator.NextDouble();
            activeRace.Lane3Racer.Heats[activeHeat].Time = randomTimeGenerator.Next(3, 6) + time;

            time = randomTimeGenerator.NextDouble();
            activeRace.Lane4Racer.Heats[activeHeat].Time = randomTimeGenerator.Next(3, 6) + time;

            // Raise an event so the Scoreboard form knows we are done
            this.OnRaceComplete();
        }
        private void LoadHeat4()
        {
            // Mark the Heat as active
            RaceDataStore.PrelimHeatCompleted = false;

            // Clear out our active race
            _activeRace = null;

            // Sort the racers by last name
            List<Racer> racerList = RaceDataStore.RacerList;
            racerList.Sort(delegate(Racer racer1, Racer racer2)
            {
                return Comparer<string>.Default.Compare(racer1.Heats[0].Time.ToString(), racer2.Heats[0].Time.ToString());
            });

            // Load our Lane stacks
            this.LoadLaneStacks(racerList);
        }
        public void RunTournamentRace(TournamentRace activeRace)
        {
            // Store a reference to the tournament race
            _activeTournamentRace = activeRace;

            // Make sure the active race object is null
            _activeRace = null;

            // Play our Rev Start wave
            _wavePlayer.SoundLocation = "StartRev.wav";
            _wavePlayer.PlaySync();

            if (RaceDataStore.EmulateRace)
            {
                Random randomTimeGenerator = new Random();

                double time1 = randomTimeGenerator.NextDouble();
                double time2 = randomTimeGenerator.NextDouble();

                time1 += randomTimeGenerator.Next(3, 6);
                time2 += randomTimeGenerator.Next(3, 6);

                activeRace.Racer1.RaceTime = time1;
                activeRace.Racer2.RaceTime = time2;

                if (time1 < time2)
                {
                    activeRace.Racer1.RecordRaceResult(true);
                    activeRace.Racer2.RecordRaceResult(false);
                }
                else
                {
                    activeRace.Racer1.RecordRaceResult(false);
                    activeRace.Racer2.RecordRaceResult(true);
                }

                this.OnRaceComplete();

                //// Advance the winner to the next round
                //this.AdvanceWinner(activeRace);
            }
            else
            {
                _trackCommunication.StartRace();
            }

            // Play the tire wave
            _wavePlayer.SoundLocation = "EndRev.wav";
            _wavePlayer.PlaySync();
        }
        public void RunRace(Race activeRace)
        {
            // Play our Rev Start wave
            _wavePlayer.SoundLocation = "StartRev.wav";
            _wavePlayer.PlaySync();

            // Are we in emulate mode?
            if (RaceDataStore.EmulateRace)
            {
                RunEmulatedRace(activeRace);
            }
            else
            {
                // Store a reference to the active Race
                _activeRace = activeRace;

                // Make sure the tournament race object is null
                _activeTournamentRace = null;

                _trackCommunication.StartRace();
            }

            _wavePlayer.SoundLocation = "EndRev.wav";
            _wavePlayer.PlaySync();
        }
        public Race GetNextRace()
        {
            // Is the Heat completed?
            if (RaceDataStore.PrelimHeatCompleted)
            {
                throw new NoMoreRacersException();
            }

            // Create a new Race object
            Race nextRace = new Race();

            // Do we have an active Race?
            if (_activeRace == null)
            {
                // Assign a new active race object
                _activeRace = new Race();

                // Load the on deck racers
                _activeRace.OnDeckLane1 = RaceDataStore.LanePool.Lane1Stack.Dequeue();
                _activeRace.OnDeckLane2 = RaceDataStore.LanePool.Lane2Stack.Dequeue();
                _activeRace.OnDeckLane3 = RaceDataStore.LanePool.Lane3Stack.Dequeue();
                _activeRace.OnDeckLane4 = RaceDataStore.LanePool.Lane4Stack.Dequeue();
            }

            // Copy the current on deck racers to the lanes
            nextRace.Lane1Racer = _activeRace.OnDeckLane1;
            nextRace.Lane2Racer = _activeRace.OnDeckLane2;
            nextRace.Lane3Racer = _activeRace.OnDeckLane3;
            nextRace.Lane4Racer = _activeRace.OnDeckLane4;

            // Do we have any more racers?
            if (RaceDataStore.LanePool.Lane1Stack.Count > 0)
            {
                // Get the next racers from our lane pool queues
                nextRace.OnDeckLane1 = RaceDataStore.LanePool.Lane1Stack.Dequeue();
                nextRace.OnDeckLane2 = RaceDataStore.LanePool.Lane2Stack.Dequeue();
                nextRace.OnDeckLane3 = RaceDataStore.LanePool.Lane3Stack.Dequeue();
                nextRace.OnDeckLane4 = RaceDataStore.LanePool.Lane4Stack.Dequeue();
            }
            else
            {
                // Build a default racer object
                Racer tempRacer = new Racer(string.Empty, string.Empty);
                tempRacer.PinewoodCar.Number = "last race of heat";

                nextRace.OnDeckLane1 = nextRace.OnDeckLane2 = nextRace.OnDeckLane3 = nextRace.OnDeckLane4 = null;

                // There are no more racers so mark the Heat as complete
                RaceDataStore.PrelimHeatCompleted = true;
            }

            // Assign this to our Active Race member variable
            _activeRace = nextRace;

            return nextRace;
        }