Esempio n. 1
0
 public function listTopGames($args = array())
 {
     $response = $this->call('listTopGames', $args);
     $games = [];
     if (is_object($response) === true && isset($response->top) === true) {
         foreach ($response->top as $topgame) {
             $game = new Game();
             $game->setName($topgame->game->name);
             $game->setViewers($topgame->viewers);
             $game->setChannels($topgame->channels);
             $game->setBoxLarge($topgame->game->box->large);
             $game->setBoxMedium($topgame->game->box->medium);
             $game->setBoxSmall($topgame->game->box->small);
             $game->setBoxTemplate($topgame->game->box->template);
             $game->setLogoLarge($topgame->game->logo->large);
             $game->setLogoMedium($topgame->game->logo->medium);
             $game->setLogoSmall($topgame->game->logo->small);
             $game->setLogoTemplate($topgame->game->logo->template);
             $games[] = $game;
         }
     }
     return $games;
 }
Esempio n. 2
0
 /**
  * @see \Simresults\Data_Reader::readSessions()
  */
 protected function readSessions()
 {
     // Get data
     $data = json_decode($this->data, TRUE);
     // Get date
     preg_match('/\\d{10}/i', $data['Time'], $time_matches);
     $date = new \DateTime();
     $date->setTimestamp($time_matches[0]);
     $date->setTimezone(new \DateTimeZone(self::$default_timezone));
     // Get other settings
     $other_settings = array();
     $known_setting_keys = array('Experience', 'Difficulty', 'FuelUsage', 'MechanicalDamage', 'FlagRules', 'CutRules', 'RaceSeriesFormat', 'WreckerPrevention', 'MandatoryPitstop', 'MandatoryPitstop');
     foreach ($known_setting_keys as $setting) {
         if ($setting_value = Helper::arrayGet($data, $setting)) {
             $other_settings[$setting] = $setting_value;
         }
     }
     // Init sessions array
     $sessions = array();
     // Gather all sessions
     foreach ($data['Sessions'] as $session_data) {
         // Init session
         $session = Session::createInstance();
         // Practice session by default
         $type = Session::TYPE_PRACTICE;
         // Check session type
         switch (strtolower($name = $session_data['Type'])) {
             case 'qualify':
                 $type = Session::TYPE_QUALIFY;
                 break;
             case 'warmup':
                 $type = Session::TYPE_WARMUP;
                 break;
             case 'race':
                 $type = Session::TYPE_RACE;
                 break;
         }
         // Set session values
         $session->setType($type)->setDate($date)->setOtherSettings($other_settings);
         // Set game
         $game = new Game();
         $game->setName('RaceRoom Racing Experience');
         $session->setGame($game);
         // Set server
         $server = new Server();
         $server->setName(Helper::arrayGet($data, 'Server'));
         $session->setServer($server);
         // Set track
         $track = new Track();
         $track->setVenue(Helper::arrayGet($data, 'Track'));
         $session->setTrack($track);
         // Get participants and their best lap (only lap)
         $participants = array();
         $players_data = Helper::arrayGet($session_data, 'Players', array());
         foreach ($players_data as $player_index => $player_data) {
             // Create driver
             $driver = new Driver();
             // Has name
             if ($name = Helper::arrayGet($player_data, 'Username') or $name = Helper::arrayGet($player_data, 'FullName')) {
                 $driver->setName($name);
             } else {
                 $driver->setName('unknown');
             }
             // Create participant and add driver
             $participant = Participant::createInstance();
             $participant->setDrivers(array($driver))->setPosition(Helper::arrayGet($player_data, 'Position', null));
             // Has finish status
             if ($status = Helper::arrayGet($player_data, 'FinishStatus')) {
                 // Figure out status
                 switch (strtolower($status)) {
                     case 'finished':
                         $participant->setFinishStatus(Participant::FINISH_NORMAL);
                         break;
                     case 'disqualified':
                         $participant->setFinishStatus(Participant::FINISH_DQ);
                         break;
                     default:
                         $participant->setFinishStatus(Participant::FINISH_DNF);
                         break;
                 }
             } else {
                 $participant->setFinishStatus(Participant::FINISH_NORMAL);
             }
             // Has total time
             if ($total_time = Helper::arrayGet($player_data, 'TotalTime')) {
                 $participant->setTotalTime(round($player_data['TotalTime'] / 1000, 4));
             }
             // Create vehicle and add to participant
             $vehicle = new Vehicle();
             $vehicle->setName(Helper::arrayGet($player_data, 'Car'));
             $participant->setVehicle($vehicle);
             // Has laps
             if ($laps = Helper::arrayGet($player_data, 'RaceSessionLaps')) {
                 foreach ($laps as $lap_key => $lap_data) {
                     // Negative lap time, skip
                     if ($lap_data['Time'] < 0) {
                         continue;
                     }
                     // Init new lap
                     $lap = new Lap();
                     // Set participant
                     $lap->setParticipant($participant);
                     // Set first driver of participant as lap driver. RR does
                     // not support swapping
                     $lap->setDriver($participant->getDriver());
                     // Set lap data
                     $lap->setNumber($lap_key + 1);
                     $lap->setPosition($lap_data['Position']);
                     $lap->setPitLap($lap_data['PitStopOccured']);
                     $lap->setTime(round($lap_data['Time'] / 1000, 4));
                     // Add lap to participant
                     $participant->addLap($lap);
                 }
             } elseif (0 < ($best_lap = Helper::arrayGet($player_data, 'BestLapTime'))) {
                 // Init new lap
                 $lap = new Lap();
                 // Set participant
                 $lap->setParticipant($participant);
                 // Set first driver of participant as lap driver. RR does
                 // not support swapping
                 $lap->setDriver($participant->getDriver());
                 // Set lap number
                 $lap->setNumber(1);
                 // Set lap time in seconds
                 $lap->setTime(round($best_lap / 1000, 4));
                 // Add lap to participant
                 $participant->addLap($lap);
             }
             // Add participant to collection
             $participants[] = $participant;
         }
         // Add participants to session
         $session->setParticipants($participants);
         // Add session to collection
         $sessions[] = $session;
     }
     // Return all sessions
     return $sessions;
 }
 /**
  * @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;
 }
Esempio n. 4
0
 /**
  * @see \Simresults\Data_Reader::readSessions()
  */
 protected function readSessions()
 {
     // 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 = Session::createInstance();
         // 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 = Participant::createInstance();
             $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;
         } else {
             // Sort participants
             $this->sortParticipantsAndFixPositions($participants, $session);
         }
         // Add extras to session
         $session->setOtherSettings($extras);
         // Set participants (sorted)
         $session->setParticipants($participants);
         // Add session to collection
         $sessions[] = $session;
     }
     // Return all sessions
     return $sessions;
 }
Esempio n. 5
0
 public static function searchForGames($query, $limit = 10)
 {
     self::validateApiKey();
     //Change these if we need to include more stuff.
     $fieldArr = ['id', 'name', 'original_release_date', 'image', 'api_detail_url', 'deck', 'image', 'platforms'];
     //sanitise query
     $query = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($query))))));
     //generate url
     $url = self::$gb . 'games?api_key=' . self::$apikey . '&format=json' . self::formatFieldList($fieldArr) . '&limit=' . $limit . '&filter=platforms:17|94|152,name:' . $query;
     //Limit to Mac, PC, Linux respectively.
     try {
         $json = self::makeApiCall($url);
     } catch (Exception $e) {
         throw new GBApiException('Error contacting Giantbomb. Please try again later.');
     }
     //QueryDb and try to find the object using the giantbomb unique id
     if ($json->results == null || count($json->results) < 1) {
         throw new GBApiException('No results found. Please try another search.');
     }
     $games = [];
     //initialise as empty array
     foreach ($json->results as $result) {
         if (!isset($result->original_release_date) || $result->original_release_date == null) {
             continue;
         }
         //skip if not out yet.
         $game = GameQuery::create()->findOneByGbId($result->id);
         if (!isset($game)) {
             $game = new Game();
             $game->setGbId($result->id);
             $game->setName(Game::generateUniqueName($result->name, $result->original_release_date));
         }
         $game->setGbUrl($result->api_detail_url);
         $game->setTitle($result->name);
         $game->setDescription($result->deck);
         //We don't care if they don't have thumbs. Catch the exceptions and move on
         try {
             $game->setGbThumb($result->image->screen_url);
         } catch (Exception $e) {
         }
         try {
             $game->setGbImage($result->image->medium_url);
         } catch (Exception $e) {
         }
         $game->save();
         //Fetch all of the title's platforms from the results we pulled from GB and push them into an array
         $gbplatforms = [];
         foreach ($result->platforms as $gbplatform) {
             array_push($gbplatforms, $gbplatform->id);
         }
         //Remove platforms no longer associated with the title
         $currentPlatforms = $game->getPlatforms();
         foreach ($currentPlatforms as $plat) {
             if (!in_array($plat->getGbId(), $gbplatforms)) {
                 $game->removePlatform($plat);
             }
         }
         //add new platforms associated with the title
         $allPlatforms = Platform::getAllPlatforms();
         foreach ($allPlatforms as $plat) {
             if (in_array($plat->getGbId(), $gbplatforms)) {
                 $found = false;
                 foreach ($currentPlatforms as $curPlat) {
                     if ($curPlat->getId() == $plat->getId()) {
                         $found = true;
                         break;
                     }
                 }
                 if ($found) {
                     continue;
                 }
                 $game->addPlatform($plat);
             }
         }
         //append result to list.
         $game->save();
         array_push($games, $game);
     }
     return $games;
 }
 /**
  * @see \Simresults\Data_Reader::readSessions()
  */
 protected function readSessions()
 {
     // Get data
     $data = json_decode($this->data, TRUE);
     // Init session
     $session = Session::createInstance();
     // Practice session by default
     $type = Session::TYPE_PRACTICE;
     // Check session name to get type
     // TODO: Could we prevent duplicate code for this with other readers?
     switch (strtolower($name = Helper::arrayGet($data, 'Type'))) {
         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($data, 'RaceLaps'));
     // Has Duration
     if ($seconds = (int) Helper::arrayGet($data, 'DurationSecs')) {
         $session->setMaxMinutes(round($seconds / 60));
     } else {
         $session->setMaxMinutes(0);
     }
     // Set game
     $game = new Game();
     $game->setName('Assetto Corsa');
     $session->setGame($game);
     // Set server (we do not know...)
     $server = new Server();
     $server->setName('Unknown');
     $session->setServer($server);
     // Set track
     $track = new Track();
     $track->setVenue(Helper::arrayGet($data, 'TrackName'));
     $session->setTrack($track);
     // Get participants from Cars data
     $participants_by_name = array();
     $players_data = Helper::arrayGet($data, 'Cars', array());
     foreach ($players_data as $player_index => $player_data) {
         // Build participant
         $participant = $this->getParticipant($name = $player_data['Driver']['Name'], $player_data['Driver']['Guid'], $player_data['Model'], $player_data['Driver']['Team'], $player_data['BallastKG'], $player_data['Skin']);
         // Add participant to collection
         $participants_by_name[$name] = $participant;
     }
     // Get participants from result data.
     // WARNING: This should be orded by position but these logs are BUGGED.
     //          DO NOT TRUST!
     $players_data = Helper::arrayGet($data, 'Result', array());
     foreach ($players_data as $player_index => $player_data) {
         // No participant found
         if (!isset($participants_by_name[$player_data['DriverName']])) {
             // Build participant
             $participant = $this->getParticipant($name = $player_data['DriverName'], $player_data['DriverGuid'], $player_data['CarModel'], null, $player_data['BallastKG']);
             // Add participant to collection
             $participants_by_name[$name] = $participant;
         }
         $participant = $participants_by_name[$player_data['DriverName']];
         // Total time available
         if ($total_time = $player_data['TotalTime']) {
             $participant->setTotalTime(round($total_time / 1000, 4));
         } else {
             // DNF
             $participant->setFinishStatus(Participant::FINISH_DNF);
         }
         // Set total time and position (but we can't trust, so we will
         // fix later again)
         $participant->setPosition($player_index + 1);
     }
     // Process laps
     foreach ($data['Laps'] as $lap_data) {
         // Init new lap
         $lap = new Lap();
         // No participant found
         if (!isset($participants_by_name[$lap_data['DriverName']])) {
             // Build participant
             $participant = $this->getParticipant($name = $lap_data['DriverName'], $lap_data['DriverGuid'], $lap_data['CarModel'], null, $lap_data['BallastKG']);
             // Add participant to collection
             $participants_by_name[$name] = $participant;
         }
         $lap_participant = $participants_by_name[$lap_data['DriverName']];
         // Set participant
         $lap->setParticipant($lap_participant);
         // Set first driver of participant as lap driver. AC does
         // not support swapping
         $lap->setDriver($lap_participant->getDriver());
         // Set lap time in seconds
         if ($lap_data['LapTime'] !== 99999) {
             $lap->setTime(round($lap_data['LapTime'] / 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);
     }
     // Get car incidents from events
     if ($data['Events']) {
         foreach ($data['Events'] as $event) {
             // Not car collision. continue to next
             if ($event['Type'] !== 'COLLISION_WITH_CAR') {
                 continue;
             }
             $incident = new Incident();
             $incident->setMessage(sprintf('%s reported contact with another vehicle ' . '%s. Impact speed: %s', $event['Driver']['Name'], $event['OtherDriver']['Name'], $event['ImpactSpeed']));
             $session->addIncident($incident);
         }
     }
     /**
      * Data fixing
      */
     // Get participant with normal array keys
     $participants = array_values($participants_by_name);
     // Sort participants
     $this->sortParticipantsAndFixPositions($participants, $session);
     // Set participants (sorted)
     $session->setParticipants($participants);
     // Return session
     return array($session);
 }
 /**
  * @see \Simresults\Data_Reader::readSessions()
  */
 protected function readSessions()
 {
     // Get data
     $data = json_decode(self::cleanJSON($this->data), TRUE);
     $data = $data['stats'];
     // No session data
     if (!($history_data = $data['history'])) {
         // Throw exception
         throw new Exception\Reader('Cannot read the session data');
     }
     // Remove sessions without stages
     $history_data = array_filter($history_data, function ($data) {
         return (bool) $data['stages'];
     });
     // Get attribute info of project cars to figure out vehicle names etc
     $this->attribute_names = $this->getAttributeNames();
     // Init sessions array
     $sessions = array();
     // Loop each history item
     foreach ($history_data as $history) {
         /**
          * Collect all participants
          */
         $initial_participants_by_ref = array();
         // Depricated! TODO: Remove
         $initial_participants_by_id = array();
         // Loop all member entries and create participants
         foreach ($history['members'] as $part_ref => $part_data) {
             // Get participant
             $participant = $this->getParticipant($part_data);
             // Add participant to collection
             // $initial_participants_by_ref[$part_ref] = $participant;
             $initial_participants_by_id[$part_data['participantid']] = $participant;
         }
         // Get additional info from participants entries
         // Disabled due to duplicate refids bugs 2015-12-14
         // foreach ($history['participants'] as $part_data)
         // {
         //     // Get previously parsed participant
         //     $participant = $initial_participants_by_ref[
         //         $part_data['RefId']];
         //     // Set whether participant is human
         //     $participant->getDriver()->setHuman(
         //         (bool) $part_data['IsPlayer']);
         // }
         // Get server configuration
         $read_settings = array('DamageType', 'FuelUsageType', 'PenaltiesType', 'ServerControlsSetup', 'ServerControlsTrack', 'ServerControlsVehicle', 'ServerControlsVehicleClass', 'TireWearType');
         $session_settings = array();
         foreach ($history['setup'] as $setup_key => $setup_value) {
             if (in_array($setup_key, $read_settings)) {
                 $session_settings[$setup_key] = $setup_value;
             }
         }
         // Loop all stages data
         foreach ($history['stages'] as $type_key => $session_data) {
             // Make new unique array of participants to prevent reference
             // issues across multiple sessions
             $participants_by_ref = array();
             $participants_by_id = array();
             foreach ($initial_participants_by_ref as $part_key => $part) {
                 $participants_by_ref[$part_key] = clone $part;
             }
             foreach ($initial_participants_by_id as $part_key => $part) {
                 $participants_by_id[$part_key] = clone $part;
             }
             // Init session
             $session = Session::createInstance();
             // Practice session by default
             $type = Session::TYPE_PRACTICE;
             // Setup name for session type
             $type_setup_name = ucfirst($type_key);
             // Check session name to get type
             // TODO: Could we prevent duplicate code for this with other readers?
             switch (strtolower(preg_replace('#\\d#', '', $type_key))) {
                 case 'qualifying':
                     $type = Session::TYPE_QUALIFY;
                     $type_setup_name = 'Qualify';
                     break;
                 case 'warmup':
                     $type = Session::TYPE_WARMUP;
                     break;
                 case 'race':
                     $type = Session::TYPE_RACE;
                     break;
             }
             // Date of this session
             $date = new \DateTime();
             $date->setTimestamp($session_data['start_time']);
             $date->setTimezone(new \DateTimeZone(self::$default_timezone));
             // Set session values
             $session->setType($type)->setName($type_key)->setMaxLaps($history['setup'][$type_setup_name . 'Length'])->setDate($date)->setOtherSettings($session_settings);
             // Set game
             $game = new Game();
             $game->setName('Project Cars');
             $session->setGame($game);
             // Set server
             // TODO: Set configurations
             $server = new Server();
             $server->setName($data['server']['name']);
             $session->setServer($server);
             // Set track
             $track = new Track();
             // Have friendly track name
             if (isset($this->attribute_names['tracks'][$history['setup']['TrackId']])) {
                 $track->setVenue($this->attribute_names['tracks'][$history['setup']['TrackId']]['name']);
             } else {
                 // TODO: We should test this works too? Same for vehicles
                 // when our json attribute config is missing items
                 $track->setVenue((string) $history['setup']['TrackId']);
             }
             $session->setTrack($track);
             // Remember participants with actual events
             $participants_with_events = array();
             // Parse events first only to collect missing participants
             foreach ($session_data['events'] as $event) {
                 // Participant unknown
                 if (!isset($participants_by_id[$event['participantid']])) {
                     // Build it and fallback to less info
                     $part = $this->getParticipant($event);
                     $participants_by_id[$event['participantid']] = $part;
                 }
             }
             // Parse event data such as laps
             $cut_data = array();
             foreach ($session_data['events'] as $event) {
                 // Get participant
                 $part = $participants_by_id[$event['participantid']];
                 // Remember this participant
                 $participants_with_events[] = $part;
                 // Is lap and the lap is valid (only checked for non-race)
                 if ($event['event_name'] === 'Lap' and ($session->getType() === Session::TYPE_RACE or $event['attributes']['CountThisLapTimes'])) {
                     // Init new lap
                     $lap = new Lap();
                     // Set participant
                     $lap->setParticipant($part);
                     // Set first driver of participant as lap driver. PJ
                     // does not support swapping
                     $lap->setDriver($part->getDriver());
                     // Set total time
                     $lap->setTime(round($event['attributes']['LapTime'] / 1000, 4));
                     // Add sectors
                     for ($sector_i = 1; $sector_i <= 3; $sector_i++) {
                         $lap->addSectorTime(round($event['attributes']['Sector' . $sector_i . 'Time'] / 1000, 4));
                     }
                     // Set lap position
                     $lap->setPosition($event['attributes']['RacePosition']);
                     // Set number
                     $lap->setNumber($event['attributes']['Lap'] + 1);
                     // Add lap to participant
                     $part->addLap($lap);
                 } elseif ($event['event_name'] === 'Impact') {
                     // Other participant is unknown by default
                     $other_participant_name = 'unknown';
                     // Other participant known
                     if (-1 != ($other_id = $event['attributes']['OtherParticipantId']) and isset($participants_by_id[$other_id])) {
                         // Set other name
                         $other_participant_name = $participants_by_id[$other_id]->getDriver()->getName();
                     } else {
                         // Skip for now until we know what -1 means
                         continue;
                     }
                     $incident = new Incident();
                     $incident->setMessage(sprintf('%s reported contact with another vehicle ' . '%s. CollisionMagnitude: %s', $part->getDriver()->getName(), $other_participant_name, $event['attributes']['CollisionMagnitude']));
                     // TODO: Add elapsed time
                     $date = new \DateTime();
                     $date->setTimestamp($event['time']);
                     $incident->setDate($date);
                     $incident->setElapsedSeconds($date->getTimestamp() - $session->getDate()->getTimestamp());
                     $session->addIncident($incident);
                 } elseif (in_array($event['event_name'], array('CutTrackStart', 'CutTrackEnd'))) {
                     $cut_data[] = $event;
                 } elseif ($event['event_name'] === 'State' and $event['attributes']['NewState'] === 'Retired') {
                     $part->setFinishStatus(Participant::FINISH_DNF);
                 }
             }
             /**
              * TODO: So many duplicate code below regarding results array
              *       reading! Fix this
              */
             // Has results array we can read finish statusses from
             if ($results = $session_data['results']) {
                 // Loop each result and process the lap
                 foreach ($results as $result) {
                     // Participant not found, continue to next
                     if (!isset($participants_by_id[$result['participantid']])) {
                         continue;
                     }
                     // Has DNF state
                     if (in_array(strtolower($result['attributes']['State']), array('dnf', 'retired'))) {
                         // Get participant
                         $part = $participants_by_id[$result['participantid']];
                         // Set DNF
                         $part->setFinishStatus(Participant::FINISH_DNF);
                     }
                 }
             }
             // We did not have any events data to process but we have
             // final results. Let's use this data to atleast get 1 best
             // lap of these participants
             if (!$session_data['events'] and $results = $session_data['results']) {
                 // Loop each result and process the lap
                 foreach ($results as $result) {
                     // Participant not found, continue to next
                     if (!isset($participants_by_id[$result['participantid']])) {
                         continue;
                     }
                     // Get participant
                     $part = $participants_by_id[$result['participantid']];
                     // Remember this participant (fake it had events)
                     $participants_with_events[] = $part;
                     // Has best lap
                     if ($result['attributes']['FastestLapTime']) {
                         // Init new lap
                         $lap = new Lap();
                         // Set participant
                         $lap->setParticipant($part);
                         // Set first driver of participant as lap driver. PJ
                         // does not support swapping
                         $lap->setDriver($part->getDriver());
                         // Set total time
                         $lap->setTime(round($result['attributes']['FastestLapTime'] / 1000, 4));
                         // Set number
                         $lap->setNumber(1);
                         // Add lap to participant
                         $part->addLap($lap);
                     }
                 }
             }
             /**
              * Process cut info
              */
             foreach ($cut_data as $key => $event) {
                 // Get participant
                 $part = $participants_by_id[$event['participantid']];
                 // Start of cut and lap actually exists
                 if ($event['event_name'] === 'CutTrackStart' and $lap = $part->getLap($event['attributes']['Lap'] + 1)) {
                     // Find the end of cutting by looping following events
                     for ($end_key = $key + 1; $end_key < count($cut_data); $end_key++) {
                         $next_event = $cut_data[$end_key];
                         // Next event is another cut start. Ignore current
                         // cut as theres no proper end data
                         if ($next_event['event_name'] === 'CutTrackStart' and $next_event['participantid'] == $event['participantid']) {
                             // Theres no end
                             break;
                         } elseif ($next_event['event_name'] === 'CutTrackEnd' and $next_event['participantid'] == $event['participantid']) {
                             $cut = new Cut();
                             $cut->setCutTime(round($next_event['attributes']['ElapsedTime'] / 1000, 4));
                             $cut->setTimeSkipped(round($next_event['attributes']['SkippedTime'] / 1000, 4));
                             $date = new \DateTime();
                             $date->setTimestamp($next_event['time']);
                             $date->setTimezone(new \DateTimeZone(self::$default_timezone));
                             $cut->setDate($date);
                             $cut->setLap($lap);
                             $cut->setElapsedSeconds($date->getTimestamp() - $session->getDate()->getTimestamp());
                             $cut->setElapsedSecondsInLap(round($event['attributes']['LapTime'] / 1000, 4));
                             $lap->addCut($cut);
                             // Stop searching
                             break;
                         }
                     }
                 }
             }
             /**
              * Cleanup
              */
             $participants = $participants_by_id;
             // Remove any participant who did not participate
             foreach ($participants as $part_id => $part) {
                 // No laps and not marked as participated
                 // TODO: Make test for strict comparison (true arg), log
                 // is on Project Cars forum for test
                 if (!in_array($part, $participants_with_events, true)) {
                     unset($participants[$part_id]);
                 }
             }
             // Get participant with normal array keys
             $participants = array_values($participants);
             // Session has predefined race result positions
             // WARNING: We only do this for race sessions because for
             // qualify and practice some drivers are missing from the
             // result
             if ($results = $session_data['results'] and $session->getType() === Session::TYPE_RACE) {
                 // Sort participants using our own sort
                 $tmp_sort = Helper::sortParticipantsByTotalTime($participants);
                 // Find whether our leading participant using our own sort
                 // is in the result
                 $leading_is_in_result = false;
                 foreach ($results as $result) {
                     // Participant not found, continue to next
                     if (!isset($participants_by_id[$result['participantid']])) {
                         continue;
                     }
                     // Get participant
                     $participant = $participants_by_id[$result['participantid']];
                     // Leading found
                     if ($participant === $tmp_sort[0]) {
                         $leading_is_in_result = true;
                     }
                 }
                 // Leading participant is in the results array
                 if ($leading_is_in_result) {
                     // Init sorted result array
                     $participants_resultsorted = array();
                     foreach ($results as $result) {
                         // Participant not found, continue to next
                         if (!isset($participants_by_id[$result['participantid']])) {
                             continue;
                         }
                         // Get participant
                         $participant = $participants_by_id[$result['participantid']];
                         // Set total time
                         $participant->setTotalTime(round($result['attributes']['TotalTime'] / 1000, 4));
                         // Add to sorted array and remove from normal array
                         $participants_resultsorted[] = $participant;
                         unset($participants[array_search($participant, $participants, true)]);
                     }
                     // Sort participants not sorted by result by total time
                     $participants = Helper::sortParticipantsByTotalTime($participants);
                     // Merge the sorted participants result with normal sort
                     // array. Merge them and remove any duplicates
                     // NOTE: We are not using array_unique as it's causing
                     // recursive depedency
                     $merged = array_merge($participants_resultsorted, $participants);
                     $final = array();
                     foreach ($merged as $current) {
                         if (!in_array($current, $final, true)) {
                             $final[] = $current;
                         }
                     }
                     $participants = $final;
                 } else {
                     // Sort participants
                     $this->sortParticipantsAndFixPositions($participants, $session);
                 }
             } else {
                 // Is race
                 if ($session->getType() === Session::TYPE_RACE) {
                     // Set all participants on unknown finish status
                     // We should of had a result for proper statusses
                     foreach ($participants as $part) {
                         $part->setFinishStatus(Participant::FINISH_NONE);
                     }
                 }
                 // Sort participants
                 $this->sortParticipantsAndFixPositions($participants, $session, TRUE);
             }
             // Set participants (sorted)
             $session->setParticipants($participants);
             $sessions[] = $session;
         }
         // End stages loop
     }
     // End history loop
     // Swap warmup and race positions if wrong
     $prevous_session = null;
     foreach ($sessions as $key => $session) {
         // Found warmup after race session
         if ($prevous_session and $prevous_session->getType() === Session::TYPE_RACE and $session->getType() === Session::TYPE_WARMUP) {
             // Swap them
             $sessions[$key] = $prevous_session;
             $sessions[$key - 1] = $session;
         }
         // Remember previous session
         $prevous_session = $session;
     }
     // Return sessions
     return $sessions;
 }
Esempio n. 8
0
 /**
  * Get the game
  *
  * @return  Game
  */
 protected function getGame()
 {
     // Create new game
     $game = new Game();
     // Get game version
     $game_version = $this->dom_value('GameVersion');
     // Default game name
     $game_name = 'rFactor 2';
     // Mod is from gamestockcar
     if (preg_match('/reiza[0-9]+\\.rfm/i', $this->dom_value('Mod'))) {
         $game_name = 'Game Stock Car Extreme';
     } elseif (preg_match('/reiza[0-9]+\\.srs/i', $this->dom_value('Mod'))) {
         $game_name = 'Automobilista';
     } elseif ($game_version === '1.255') {
         // It's rfactor 1
         $game_name = 'rFactor';
     }
     // Set game values
     $game->setName($game_name)->setVersion($game_version);
     // Return game
     return $game;
 }
Esempio n. 9
0
 /**
  * Get the game
  *
  * @return  Game
  */
 protected function getGame()
 {
     // Create new game
     $game = new Game();
     // Get game version
     $game_version = $this->dom_value('GameVersion');
     // Default game name
     $game_name = 'rFactor 2';
     // Game version matches rfactor 1 version. Let's hope rfactor 2 will
     // never use this version :)
     if ($game_version === '1.255') {
         // It's rfactor 1
         $game_name = 'rFactor';
     }
     // Set game values
     $game->setName($game_name)->setVersion($game_version);
     // Return game
     return $game;
 }
Esempio n. 10
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;
 }