/** * {@inheritdoc} */ public function getLastedLaps() { // There is cache if ($this->cache_lasted_laps !== null) { return $this->cache_lasted_laps; } // Return number of laps lasted and cache it return $this->cache_lasted_laps = parent::getLastedLaps(); }
/** * @see \Simresults\Data_Reader::getSessions() */ public function getSessions() { // Get data $data = json_decode($this->data, TRUE); // No session data if (!($sessions_data = Helper::arrayGet($data, 'sessions'))) { // Throw exception throw new Exception\Reader('Cannot read the session data'); } // Init sessions array $sessions = array(); // Get extra data for all sessions $extras = array(); foreach (Helper::arrayGet($data, 'extras', array()) as $extras_data) { // Get name $name = Helper::arrayGet($extras_data, 'name'); // Loop all values and add as extra settings foreach ($extras_data as $extra_data_key => $extra_data_value) { // Is name if ($extra_data_key === 'name') { // Skip this continue; } // Add to extras collection $extras[ucfirst($name) . ' ' . $extra_data_key] = $extra_data_value; } } // Gather all sessions foreach ($sessions_data as $session_data) { // Init session $session = new Session(); // Get participants (do for each session to prevent re-used objects // between sessions) $participants = array(); $players_data = Helper::arrayGet($data, 'players', array()); foreach ($players_data as $player_index => $player_data) { // Create driver $driver = new Driver(); $driver->setName(Helper::arrayGet($player_data, 'name')); // Create participant and add driver $participant = new Participant(); $participant->setDrivers(array($driver))->setFinishStatus(Participant::FINISH_NORMAL); // Create vehicle and add to participant $vehicle = new Vehicle(); $vehicle->setName(Helper::arrayGet($player_data, 'car')); $participant->setVehicle($vehicle); // Add participant to collection $participants[] = $participant; } // Practice session by default $type = Session::TYPE_PRACTICE; // Check session name to get type // TODO: Should be checked when full game is released. Also create // tests for it! switch (strtolower($name = Helper::arrayGet($session_data, 'name'))) { case 'qualify session': case 'qualify': $type = Session::TYPE_QUALIFY; break; case 'warmup session': $type = Session::TYPE_WARMUP; break; case 'race session': case 'quick race': case 'race': $type = Session::TYPE_RACE; break; } // Set session values $session->setType($type)->setName($name)->setMaxLaps((int) Helper::arrayGet($session_data, 'lapsCount'))->setMaxMinutes((int) Helper::arrayGet($session_data, 'duration')); // Set game $game = new Game(); $game->setName('Assetto Corsa'); $session->setGame($game); // Set server (we do not know...) $server = new Server(); $server->setName('Unknown or offline'); $session->setServer($server); // Set track $track = new Track(); $track->setVenue(Helper::arrayGet($data, 'track')); $session->setTrack($track); // Get the laps $laps_data = Helper::arrayGet($session_data, 'laps', array()); // No laps data if (!$laps_data) { // Use best laps if possible $laps_data = Helper::arrayGet($session_data, 'bestLaps', array()); } // Process laps foreach ($laps_data as $lap_data) { // Init new lap $lap = new Lap(); // Set participant $lap->setParticipant($lap_participant = $participants[$lap_data['car']]); // Set first driver of participant as lap driver. AC does // not support swapping $lap->setDriver($lap_participant->getDriver()); // Set lap number (+1 because AC is zero based) $lap->setNumber($lap_data['lap'] + 1); // Set lap time in seconds $lap->setTime(round($lap_data['time'] / 1000, 4)); // Set sector times in seconds foreach (Helper::arrayGet($lap_data, 'sectors', array()) as $sector_time) { $lap->addSectorTime(round($sector_time / 1000, 4)); } // Add lap to participant $lap_participant->addLap($lap); } // Session has predefined race result positions if ($race_result = Helper::arrayGet($session_data, 'raceResult')) { // Create new participants order $participants_sorted = array(); foreach ($race_result as $race_position => $race_position_driver) { $participants_sorted[] = $participants[$race_position_driver]; } $participants = $participants_sorted; } elseif ($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 (sorted) $session->setParticipants($participants); // Fix elapsed seconds for all participant laps foreach ($participants as $participant) { $elapsed_time = 0; foreach ($participant->getLaps() as $lap) { // Set elapsed seconds and increment it $lap->setElapsedSeconds($elapsed_time); $elapsed_time += $lap->getTime(); } } // 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 for ($i = 2; $i <= $session_lasted_laps; $i++) { // Get laps by lap number from session $laps_sorted = $session->getLapsByLapNumberSortedByTime($i); // Sort the laps by elapsed time $laps_sorted = Helper::sortLapsByElapsedTime($laps_sorted); // Loop each lap and fix position data foreach ($laps_sorted as $lap_key => $lap) { // Fix lap position $lap->setPosition($lap_key + 1); } } // Add extras to session $session->setOtherSettings($extras); // Add session to collection $sessions[] = $session; } // Return all sessions return $sessions; }
/** * @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; }