Example #1
0
 /**
  * @see \Simresults\Data_Reader::getSessions()
  */
 public function getSessions()
 {
     // Create new session instance
     $session = new Session();
     // Is race session
     if ($xml_session = $this->dom->getElementsByTagName('Race')->item(0)) {
         // Set type to race
         $session->setType(Session::TYPE_RACE);
     } elseif ($xml_session = $this->dom->getElementsByTagName('Qualify')->item(0)) {
         // Set type to qualify
         $session->setType(Session::TYPE_QUALIFY);
     } elseif ($xml_session = $this->dom->getElementsByTagName('Warmup')->item(0)) {
         // Set type to warmup
         $session->setType(Session::TYPE_WARMUP);
     } elseif ($xml_session = $this->dom->getElementsByTagName('Practice1')->item(0) or $xml_session = $this->dom->getElementsByTagName('Practice2')->item(0) or $xml_session = $this->dom->getElementsByTagName('Practice3')->item(0)) {
         // Set type to practice
         $session->setType(Session::TYPE_PRACTICE);
     } else {
         // Throw exception
         throw new Exception\Reader('Cannot read the session data');
     }
     // Using fixed setups?
     $session->setSetupFixed((bool) $this->dom_value('FixedSetups', $xml_session));
     // Get other settings
     $session->addOtherSetting('MechFailRate', (int) $this->dom_value('MechFailRate'))->addOtherSetting('DamageMult', (int) $this->dom_value('DamageMult'))->addOtherSetting('FuelMult', (int) $this->dom_value('FuelMult'))->addOtherSetting('TireMult', (int) $this->dom_value('TireMult'));
     // Create session date
     $date = new \DateTime(date('c', $this->dom_value('DateTime', $xml_session)));
     // Set UTC timezone by default
     $date->setTimezone(new \DateTimeZone(self::$default_timezone));
     // Set date to session
     $session->setDate($date);
     // Max laps is max value
     if (($max_laps = (int) $this->dom_value('Laps', $xml_session)) === 2147483647) {
         // Just set it to 0
         $max_laps = 0;
     }
     // Set max laps and minutes
     $session->setMaxLaps($max_laps)->setMaxMinutes((int) $this->dom_value('Minutes', $xml_session));
     // Set which mod was used
     $session->setMod($this->dom_value('Mod'));
     // Set server
     $session->setServer($this->getServer());
     // Set game
     $session->setGame($this->getGame());
     // Set track
     $session->setTrack($this->getTrack());
     // Get participants
     $participants = $this->getParticipants();
     // Find whether we are dealing with position corruption
     $position_corruption = false;
     // DNF statusses
     $dnf_statusses = array(Participant::FINISH_DNF, Participant::FINISH_DQ, Participant::FINISH_NONE);
     // This is a race session
     if ($session->getType() === Session::TYPE_RACE) {
         // Loop each participant to find position corruption
         foreach ($participants as $part_key => $part) {
             // There is a previous participant
             if (isset($participants[$part_key - 1]) and $prev_part = $participants[$part_key - 1]) {
                 // This participant did not finish
                 if (in_array($part->getFinishStatus(), $dnf_statusses)) {
                     // No need to compare to previous, continue to next
                     continue;
                 }
                 // Total time is lower than previous participant and participant
                 // is not lapped, that is not right...
                 if ($part->getTotalTime() < $prev_part->getTotalTime() and $part->getNumberOfLaps() === $prev_part->getNumberOfLaps()) {
                     // We are dealing with corrupted positions
                     $position_corruption = true;
                 }
             }
         }
         // We have position corruption
         if ($position_corruption) {
             // Sort participants by total time
             $participants = Helper::sortParticipantsByTotalTime($participants);
         }
     } else {
         // Loop each participant to find position corruption
         foreach ($participants as $part_key => $part) {
             // There is a previous participant
             if (isset($participants[$part_key - 1]) and $prev_part = $participants[$part_key - 1]) {
                 // This participant has no best lap
                 if (!$part->getBestLap()) {
                     // Just continue to next
                     continue;
                 }
                 // Previous participant has no best lap and this participant
                 // does OR this participant best lap is faster than previous
                 // best lap
                 if (!$prev_part->getBestLap() and $part->getBestLap() or $part->getBestLap()->getTime() < $prev_part->getBestLap()->getTime()) {
                     // We are dealing with corrupted positions
                     $position_corruption = true;
                 }
             }
         }
         // We have position corruption
         if ($position_corruption) {
             // Sort by best lap instead of position
             $participants = Helper::sortParticipantsByBestLap($participants);
         }
     }
     // There is position corruption
     if ($position_corruption) {
         // Fix the positions of the participants
         foreach ($participants as $key => $part) {
             // Set position
             $part->setPosition($key + 1);
         }
     }
     // Set participants
     $session->setParticipants($participants);
     // Set vehicles allowed
     $session->setAllowedVehicles($this->getAllowedVehicles());
     // Set chats
     $this->setChats($session);
     // Set incidents
     $this->setIncidents($session);
     // Set penalties
     $this->setPenalties($session);
     // Return the session
     return array($session);
 }