Exemplo n.º 1
0
 /**
  * Test the best lap
  */
 public function testBestLap()
 {
     // Get session
     $session = $this->getSessionWithData();
     // Get the best lap
     $lap = $session->getBestLap();
     // Get participants
     $participants = $session->getParticipants();
     // Validate best lap
     $this->assertSame($participants[2]->getBestLap(), $lap);
     //-- Create participant with 2 uncompleted laps and test NULL best lap
     //   from the session
     $participant = new Participant();
     $participant->setLaps(array(new Lap(), new Lap()));
     $session = new Session();
     $session->setParticipants(array($participant));
     // No best lap
     $this->assertNull($session->getBestLap());
 }
Exemplo n.º 2
0
 /**
  * Set participants and session type on a session instance
  *
  * @param  Session  $session
  * @param  array    $data
  */
 protected function setParticipantsAndSessionType(Session $session, array $data)
 {
     // Init participants array
     $participants = array();
     // Collect drivers in array
     $driver_data_array = array();
     // No grid position by default
     $set_grid_position = false;
     foreach ($data as $key => $driver_data) {
         // Not a driver driver_data
         if (!preg_match('/slot([0-9]+)/i', $key, $matches)) {
             continue;
         }
         // No qualtime
         if (!array_key_exists('qualtime', $driver_data)) {
             // Create qualtime, we need it for easier closure usage
             $driver_data['qualtime'] = null;
         } else {
             $set_grid_position = true;
         }
         $driver_data_array[] = $driver_data;
     }
     // Set grid positions
     if ($set_grid_position) {
         // Sort drivers by qualify time to figure out grid positions
         usort($driver_data_array, function ($a, $b) {
             // Same time
             if ($a['qualtime'] === $b['qualtime']) {
                 return 0;
             }
             // a has no time
             if (!$a['qualtime']) {
                 // $b is faster
                 return 1;
             }
             // b has no time
             if (!$b['qualtime']) {
                 // $a is faster
                 return -1;
             }
             return $a['qualtime'] < $b['qualtime'] ? -1 : 1;
         });
         // Set grid positions
         foreach ($driver_data_array as $driver_key => &$driver_data_array_item) {
             $driver_data_array_item['grid_position'] = $driver_key + 1;
         }
         unset($driver_data_array_item);
     }
     // All participants are dnf by default
     $all_dnf = true;
     // Loop each driver
     foreach ($driver_data_array as $driver_data) {
         // Create driver
         $driver = new Driver();
         $driver->setName($driver_data['driver']);
         // Create participant and add driver
         $participant = new Participant();
         $participant->setDrivers(array($driver))->setTeam(Helper::arrayGet($driver_data, 'team'));
         // Finish position will be set later using an special
         // sort
         // We have laps and must set grid positions
         if (Helper::arrayGet($driver_data, 'laps_collection') and $set_grid_position) {
             $participant->setGridPosition($driver_data['grid_position']);
         }
         // Create vehicle and add to participant
         $vehicle = new Vehicle();
         $vehicle->setName(Helper::arrayGet($driver_data, 'vehicle'));
         $participant->setVehicle($vehicle);
         // Has race time information
         if ($race_time = Helper::arrayGet($driver_data, 'racetime')) {
             // Not dnf by default if it's not 0:00:00.000
             $set_dnf = $race_time === '0:00:00.000';
             // Try setting seconds if not dnf
             if (!$set_dnf) {
                 try {
                     // Get seconds
                     $seconds = Helper::secondsFromFormattedTime($race_time);
                     // Set total time
                     $participant->setTotalTime($seconds);
                     // Is finished
                     $participant->setFinishStatus(Participant::FINISH_NORMAL);
                     $all_dnf = false;
                 } catch (\InvalidArgumentException $ex) {
                     $set_dnf = true;
                 }
             }
             // Should set this participant dnf
             if ($set_dnf) {
                 $participant->setFinishStatus(Participant::FINISH_DNF);
                 // Has reason
                 if (null !== ($reason = Helper::arrayGet($driver_data, 'reason'))) {
                     $participant->setFinishComment("DNF (reason {$reason})");
                 }
             }
         }
         // Laps count not found
         if (null === ($laps_count = Helper::arrayGet($driver_data, 'laps'))) {
             // Try racelaps key
             $laps_count = Helper::arrayGet($driver_data, 'racelaps');
         }
         // Has run laps
         if ($laps_count !== null and $laps_count > 0) {
             // Get laps collection
             $laps_collection = Helper::arrayGet($driver_data, 'laps_collection');
             // Loop laps by lap count due to missing laps in results
             // so we can fill up the gaps
             for ($lap_i = 1; $lap_i <= $laps_count; $lap_i++) {
                 // Init new lap
                 $lap = new Lap();
                 // Set participant
                 $lap->setParticipant($participant);
                 // Set first driver of participant as lap driver. Race07 does
                 // not support swapping
                 $lap->setDriver($participant->getDriver());
                 // Set lap number
                 $lap->setNumber($lap_i);
                 // Is first lap
                 if ($lap->getNumber() === 1) {
                     // Set grid position as lap position
                     $lap->setPosition(Helper::arrayGet($driver_data, 'grid_position'));
                 }
                 // Lap data exists
                 if (isset($laps_collection[$lap_i])) {
                     // Get lap data
                     $lap_data = $laps_collection[$lap_i];
                     // Set lap times
                     $lap->setTime($lap_data['time'])->setElapsedSeconds($lap_data['elapsed_time']);
                     $all_laps_missing = false;
                 }
                 // Add lap to participant
                 $participant->addLap($lap);
             }
             // All laps missing but has best lap
             if (sizeof($laps_collection) === 0 and ($racebestlap = Helper::arrayGet($driver_data, 'racebestlap') or $racebestlap = Helper::arrayGet($driver_data, 'bestlap'))) {
                 // Get first lap and change time
                 $participant->getLap(1)->setTime(Helper::secondsFromFormattedTime($racebestlap));
             }
         }
         // Add participant to collection
         $participants[] = $participant;
     }
     // All participants are dnf
     if ($all_dnf) {
         // Assume we're dealing with qualify session
         $session->setType(Session::TYPE_QUALIFY);
         $session->setName('Qualify or practice session');
     } else {
         // Race session
         $session->setType(Session::TYPE_RACE);
     }
     // Is race result
     if ($session->getType() === Session::TYPE_RACE) {
         // Sort participants by total time
         $participants = Helper::sortParticipantsByTotalTime($participants);
     } else {
         // Sort by best lap
         $participants = Helper::sortParticipantsByBestLap($participants);
     }
     // Fix participant positions
     foreach ($participants as $key => $part) {
         $part->setPosition($key + 1);
     }
     // Set participants on session
     $session->setParticipants($participants);
 }