/**
  * Helper to get new participant instance
  *
  * @param  array        $part_data
  * @return Participant
  */
 protected function getParticipant($part_data)
 {
     // Create driver
     $driver = new Driver();
     $driver->setName($part_data['name'])->setHuman(false);
     // Has steam id
     if (isset($part_data['steamid'])) {
         $driver->setDriverId($part_data['steamid']);
         $driver->setHuman(true);
     }
     // Create participant and add driver
     $participant = Participant::createInstance();
     $participant->setDrivers(array($driver))->setFinishStatus(Participant::FINISH_NORMAL);
     // Create vehicle and add to participant
     $vehicle = new Vehicle();
     // TODO: Parse livery too?
     // $vehicle->setType( (string) $part_data['setup']['LiveryId']);
     // Has vehicle in root
     $vehicle_id = null;
     if (isset($part_data['VehicleId'])) {
         $vehicle_id = $part_data['VehicleId'];
     } elseif (isset($part_data['setup']) and isset($part_data['setup']['VehicleId'])) {
         $vehicle_id = $part_data['setup']['VehicleId'];
     }
     // Have friendly vehicle name
     if (isset($this->attribute_names['vehicles'][$vehicle_id])) {
         $vehicle->setName($this->attribute_names['vehicles'][$part_data['setup']['VehicleId']]['name']);
         $vehicle->setClass($this->attribute_names['vehicles'][$part_data['setup']['VehicleId']]['class']);
     } else {
         $vehicle->setName((string) $vehicle_id);
     }
     $participant->setVehicle($vehicle);
     return $participant;
 }
 /**
  * @see \Simresults\Data_Reader::readSessions()
  */
 protected function readSessions()
 {
     // Get array data
     $data = $this->array_data;
     // Init sessions array
     $sessions = array();
     // Loop each session from data
     foreach ($data as $session_data) {
         // Remember which vehicles are parsed
         $vehicle_names = array();
         // Init session
         $session = Session::createInstance();
         // Set session type
         $type = null;
         switch ($session_data['type']) {
             case 'qualify':
                 $type = Session::TYPE_QUALIFY;
                 break;
             case 'practice':
                 $type = Session::TYPE_PRACTICE;
                 break;
             case 'warmup':
                 $type = Session::TYPE_PRACTICE;
                 break;
             case 'race':
                 $type = Session::TYPE_RACE;
                 break;
         }
         $session->setType($type);
         // Set session name
         if (isset($session_data['name'])) {
             $session->setName($session_data['name']);
         }
         // Set max time
         if (isset($session_data['time'])) {
             $session->setMaxMinutes($session_data['time']);
         }
         // Set max laps
         if (isset($session_data['laps'])) {
             $session->setMaxLaps($session_data['laps']);
         }
         // Set game
         $game = new Game();
         $game->setName('Assetto Corsa');
         $session->setGame($game);
         // Has track
         if (isset($session_data['track'])) {
             $track = new Track();
             $track->setVenue($session_data['track']);
             $session->setTrack($track);
         }
         // Has date
         if (isset($session_data['date'])) {
             // Set it
             $session->setDateString($session_data['date']);
         }
         // Set server
         $server = new Server();
         $server->setDedicated(true);
         if (isset($session_data['server'])) {
             $server->setName($session_data['server']);
         } else {
             $server->setName('Unknown');
         }
         $session->setServer($server);
         // Add allowed vehicles
         foreach ($session_data['car_list'] as $vehicle_name) {
             $vehicle = new Vehicle();
             $vehicle->setName($vehicle_name);
             $session->addAllowedVehicle($vehicle);
         }
         // Set chats
         foreach ($session_data['chats'] as $chat_message) {
             $chat = new Chat();
             $chat->setMessage($chat_message);
             $session->addChat($chat);
         }
         // Set participants
         $participants = array();
         foreach ($session_data['participants'] as $part_data) {
             // No name
             if (!Helper::arrayGet($part_data, 'name')) {
                 continue;
             }
             // Create driver
             $driver = new Driver();
             $driver->setName($part_data['name']);
             // Total time not greater than 0
             if (0 >= ($total_time = Helper::arrayGet($part_data, 'total_time'))) {
                 // Total time is null
                 $total_time = null;
             }
             // Create participant and add driver
             $participant = Participant::createInstance();
             $participant->setDrivers(array($driver))->setTotalTime($total_time);
             // Has total time parsed data and should not be a forced DNF
             if ($total_time and !Helper::arrayGet($part_data, 'force_dnf')) {
                 $participant->setFinishStatus(Participant::FINISH_NORMAL);
             } else {
                 $participant->setFinishStatus(Participant::FINISH_DNF);
             }
             // Remember vehicle instances by vehicle name
             $vehicles = array();
             // Create vehicle and add to participant
             $vehicle = null;
             if (isset($part_data['vehicle'])) {
                 // Init vehicle
                 $vehicle = new Vehicle();
                 $vehicle->setName($part_data['vehicle']);
                 $participant->setVehicle($vehicle);
                 // Remember vehicle instance
                 $vehicles[$part_data['vehicle']] = $vehicle;
                 // Remember vehicle names for this entire session
                 $vehicle_names[$part_data['vehicle']] = 1;
             }
             // Has team
             if (isset($part_data['team'])) {
                 $participant->setTeam($part_data['team']);
             }
             // Has guid
             if (isset($part_data['guid'])) {
                 $driver->setDriverId($part_data['guid']);
             }
             // Collect laps
             foreach (Helper::arrayGet($part_data, 'laps', array()) as $lap_i => $lap_data) {
                 // Init new lap
                 $lap = new Lap();
                 // Set participant
                 $lap->setParticipant($participant);
                 // Set first driver of participant as lap driver. AC does
                 // not support swapping
                 $lap->setDriver($participant->getDriver());
                 // Set lap number
                 $lap->setNumber($lap_i + 1);
                 // Set lap times
                 $lap->setTime($lap_data['time']);
                 // No lap vehicle
                 if (!$lap_data['vehicle']) {
                     // Just use participant vehicle if it is available
                     if ($vehicle) {
                         $lap->setVehicle($vehicle);
                     }
                 } elseif (isset($vehicles[$v = $lap_data['vehicle']])) {
                     // Set vehicle instance
                     $lap->setVehicle($vehicles[$v]);
                 } else {
                     // Init vehicle
                     $vehicle = new Vehicle();
                     $vehicle->setName($lap_data['vehicle']);
                     $lap->setVehicle($vehicle);
                     // Remember vehicle
                     $vehicles[$lap_data['vehicle']] = $vehicle;
                 }
                 // Add lap to participant
                 $participant->addLap($lap);
             }
             // No laps and race result
             if (!$participant->getLaps() and $session->getType() === Session::TYPE_RACE) {
                 // Did not finish
                 $participant->setFinishStatus(Participant::FINISH_DNF);
             }
             // Add participant to collection
             $participants[] = $participant;
         }
         // Sort participants
         $this->sortParticipantsAndFixPositions($participants, $session);
         // Set participants to session
         $session->setParticipants($participants);
         // Only one vehicle type in this session
         if (count($vehicle_names) === 1) {
             // Find any participant without vehicle and fix missing.
             // This is an easy last resort fix when parsing was bugged
             // We assume everybody has this vehicle
             foreach ($session->getParticipants() as $participant) {
                 if (!$participant->getVehicle()) {
                     // Init vehicle
                     $vehicle = new Vehicle();
                     $vehicle->setName(key($vehicle_names));
                     $participant->setVehicle($vehicle);
                 }
             }
         }
         // Add session to collection
         $sessions[] = $session;
     }
     // Return all sessions
     return $sessions;
 }
示例#3
0
 /**
  * @see \Simresults\Data_Reader::getSessions()
  */
 public function getSessions()
 {
     // Get array data
     $data = $this->array_data;
     // Init sessions array
     $sessions = array();
     // Remember last qualify session to make up grid positions
     $last_qualify_session = null;
     // Loop each session from data
     foreach ($data as $session_data) {
         // Remember which vehicles are parsed
         $vehicle_names = array();
         // Init session
         $session = new Session();
         // Set session type
         $type = null;
         switch ($session_data['type']) {
             case 'qualify':
                 $type = Session::TYPE_QUALIFY;
                 break;
             case 'practice':
                 $type = Session::TYPE_PRACTICE;
                 break;
             case 'warmup':
                 $type = Session::TYPE_PRACTICE;
                 break;
             case 'race':
                 $type = Session::TYPE_RACE;
                 break;
         }
         $session->setType($type);
         // Set session name
         if (isset($session_data['name'])) {
             $session->setName($session_data['name']);
         }
         // Set max time
         if (isset($session_data['time'])) {
             $session->setMaxMinutes($session_data['time']);
         }
         // Set max laps
         if (isset($session_data['laps'])) {
             $session->setMaxLaps($session_data['laps']);
         }
         // Set game
         $game = new Game();
         $game->setName('Assetto Corsa');
         $session->setGame($game);
         // Has track
         if (isset($session_data['track'])) {
             $track = new Track();
             $track->setVenue($session_data['track']);
             $session->setTrack($track);
         }
         // Has date
         if (isset($session_data['date'])) {
             // Set it
             $session->setDateString($session_data['date']);
         }
         // Set server
         $server = new Server();
         $server->setDedicated(true);
         if (isset($session_data['server'])) {
             $server->setName($session_data['server']);
         } else {
             $server->setName('Unknown');
         }
         $session->setServer($server);
         // Add allowed vehicles
         foreach ($session_data['car_list'] as $vehicle_name) {
             $vehicle = new Vehicle();
             $vehicle->setName($vehicle_name);
             $session->addAllowedVehicle($vehicle);
         }
         // Set chats
         foreach ($session_data['chats'] as $chat_message) {
             $chat = new Chat();
             $chat->setMessage($chat_message);
             $session->addChat($chat);
         }
         // Set participants
         $participants = array();
         foreach ($session_data['participants'] as $part_data) {
             // No name
             if (!Helper::arrayGet($part_data, 'name')) {
                 continue;
             }
             // Create driver
             $driver = new Driver();
             $driver->setName($part_data['name']);
             // Total time not greater than 0
             if (0 >= ($total_time = Helper::arrayGet($part_data, 'total_time'))) {
                 // Total time is null
                 $total_time = null;
             }
             // Create participant and add driver
             $participant = new Participant();
             $participant->setDrivers(array($driver))->setTotalTime($total_time);
             // Has total time parsed data and should not be a forced DNF
             if ($total_time and !Helper::arrayGet($part_data, 'force_dnf')) {
                 $participant->setFinishStatus(Participant::FINISH_NORMAL);
             } else {
                 $participant->setFinishStatus(Participant::FINISH_DNF);
             }
             // Remember vehicle instances by vehicle name
             $vehicles = array();
             // Create vehicle and add to participant
             $vehicle = null;
             if (isset($part_data['vehicle'])) {
                 // Init vehicle
                 $vehicle = new Vehicle();
                 $vehicle->setName($part_data['vehicle']);
                 $participant->setVehicle($vehicle);
                 // Remember vehicle instance
                 $vehicles[$part_data['vehicle']] = $vehicle;
                 // Remember vehicle names for this entire session
                 $vehicle_names[$part_data['vehicle']] = 1;
             }
             // Has team
             if (isset($part_data['team'])) {
                 $participant->setTeam($part_data['team']);
             }
             // Has guid
             if (isset($part_data['guid'])) {
                 $driver->setDriverId($part_data['guid']);
             }
             // Collect laps
             foreach (Helper::arrayGet($part_data, 'laps', array()) as $lap_i => $lap_data) {
                 // Init new lap
                 $lap = new Lap();
                 // Set participant
                 $lap->setParticipant($participant);
                 // Set first driver of participant as lap driver. AC does
                 // not support swapping
                 $lap->setDriver($participant->getDriver());
                 // Set lap number
                 $lap->setNumber($lap_i + 1);
                 // Set lap times
                 $lap->setTime($lap_data['time']);
                 // No lap vehicle
                 if (!$lap_data['vehicle']) {
                     // Just use participant vehicle if it is available
                     if ($vehicle) {
                         $lap->setVehicle($vehicle);
                     }
                 } elseif (isset($vehicles[$v = $lap_data['vehicle']])) {
                     // Set vehicle instance
                     $lap->setVehicle($vehicles[$v]);
                 } else {
                     // Init vehicle
                     $vehicle = new Vehicle();
                     $vehicle->setName($lap_data['vehicle']);
                     $lap->setVehicle($vehicle);
                     // Remember vehicle
                     $vehicles[$lap_data['vehicle']] = $vehicle;
                 }
                 // Add lap to participant
                 $participant->addLap($lap);
             }
             // No laps and race result
             if (!$participant->getLaps() and $session->getType() === Session::TYPE_RACE) {
                 // Did not finish
                 $participant->setFinishStatus(Participant::FINISH_DNF);
             }
             // Add participant to collection
             $participants[] = $participant;
         }
         // 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 to session
         $session->setParticipants($participants);
         // Fix elapsed seconds for all participant laps
         foreach ($session->getParticipants() as $participant) {
             $elapsed_time = 0;
             foreach ($participant->getLaps() as $lap) {
                 // Set elapsed seconds and increment it
                 $lap->setElapsedSeconds($elapsed_time);
                 $elapsed_time += $lap->getTime();
             }
         }
         // Is qualify
         if ($session->getType() === Session::TYPE_QUALIFY) {
             // Remember last qualify session
             $last_qualify_session = $session;
         } else {
             if ($session->getType() === Session::TYPE_RACE and $last_qualify_session) {
                 // Get pairticpants of last qualify session and store names
                 $last_qualify_session_participants = array();
                 foreach ($last_qualify_session->getParticipants() as $part) {
                     $last_qualify_session_participants[] = $part->getDriver()->getName();
                 }
                 // Loop this session participants
                 foreach ($participants as $part) {
                     // Found participant in qualify array
                     if (false !== ($key = array_search($part->getDriver()->getName(), $last_qualify_session_participants))) {
                         $part->setGridPosition($key + 1);
                     }
                 }
             }
         }
         // Fix driver positions for laps
         $session_lasted_laps = $session->getLastedLaps();
         // Loop each lap number, beginning from 2, because we can't
         // figure out positions for lap 1 in AC
         // TODO: Duplicate code with RACE07 and AC normal reader
         for ($i = 2; $i <= $session_lasted_laps; $i++) {
             // Get laps sorted by elapsed time
             $laps_sorted = $session->getLapsByLapNumberSortedByTime($i);
             // Sort laps by elapsed time
             $laps_sorted = Helper::sortLapsByElapsedTime($laps_sorted);
             // Loop each lap and fix position data
             foreach ($laps_sorted as $lap_key => $lap) {
                 // Only fix position if lap has a time, this way users of this
                 // library can easier detect whether it's a dummy lap and
                 // decide how to show them
                 if ($lap->getTime() or $lap->getElapsedSeconds()) {
                     $lap->setPosition($lap_key + 1);
                 }
             }
         }
         // Only one vehicle type in this session
         if (count($vehicle_names) === 1) {
             // Find any participant without vehicle and fix missing.
             // This is an easy last resort fix when parsing was bugged
             // We assume everybody has this vehicle
             foreach ($session->getParticipants() as $participant) {
                 if (!$participant->getVehicle()) {
                     // Init vehicle
                     $vehicle = new Vehicle();
                     $vehicle->setName(key($vehicle_names));
                     $participant->setVehicle($vehicle);
                 }
             }
         }
         // Add session to collection
         $sessions[] = $session;
     }
     // Return all sessions
     return $sessions;
 }