/**
  * Execute the console command.
  *
  * @return void
  */
 public function fire()
 {
     $this->customInfo('Requesting all applications from Steam');
     $steamApps = $this->steamApps->getAppList();
     $this->customInfo('Importing ' . count($steamApps) . ' applications from Steam into database');
     $successCount = 0;
     $failureCount = 0;
     $applicationsCreated = 0;
     foreach ($steamApps as $steamApp) {
         $newApp = NULL;
         try {
             // Search for the application in the database to update it if it already exists
             $application = Application::where('steam_app_id', $steamApp->id)->first();
             if (!$application) {
                 $application = new Application();
                 $newApp = true;
             }
             // Assign fields
             $application->steam_app_id = $steamApp->id;
             $application->name = $steamApp->name;
             // Insert new OR update existing with fields above
             $application->save();
             $successCount++;
             if ($newApp) {
                 $applicationsCreated++;
             }
         } catch (\Exception $e) {
             $this->customError('Unable to import application ' . $steamApp->id . ' "' . $steamApp->name . '" : ' . $e->getMessage());
             $failureCount++;
         }
     }
     $this->customInfo($successCount . ' Steam applications successfully imported. New: ' . $applicationsCreated);
     if ($failureCount > 0) {
         $this->customError($failureCount . ' Steam applications were not imported due to errors');
     }
 }
 /**
  * Execute the console command.
  *
  * @return void
  */
 public function fire()
 {
     $users = User::all()->lists('steam_id_64');
     if (count($users) == 0) {
         $this->customError('No users in database');
         return;
     }
     $this->customInfo('Requesting current status of ' . count($users) . ' users from Steam');
     $steamUsers = $this->steamUsers->getUsers($users);
     if (count($steamUsers) < count($users)) {
         $this->customError('Steam responded with ' . (count($users) - count($steamUsers)) . ' fewer users than requested');
     }
     $this->customInfo('Importing ' . count($steamUsers) . ' user states from Steam into database');
     $successCount = 0;
     $failureCount = 0;
     $appFailureCount = 0;
     foreach ($steamUsers as $steamUser) {
         $currentApplication = NULL;
         $currentServer = NULL;
         try {
             // Find the user to which the state belongs to
             $user = User::where('steam_id_64', $steamUser->id)->first();
             // Update user details with steam details if they have changed
             if ($user->username != $steamUser->username) {
                 $user->username = $steamUser->username;
             }
             if ($user->avatar != $steamUser->avatar_url) {
                 $user->avatar = $steamUser->avatar_url;
             }
             $user->save();
             // If the user is currently running an app
             if (is_numeric($steamUser->current_app_id)) {
                 // Find database application ID
                 $currentApplication = Application::where('steam_app_id', $steamUser->current_app_id)->first();
                 if (!count($currentApplication)) {
                     $this->customError('Steam app not found in database. App: ' . $steamUser->current_app_id . ' User: '******' ' . $steamUser->username);
                     $appFailureCount++;
                     $currentApplication = NULL;
                 }
             }
             // If the user is currently running an app and connected to a server
             if ($currentApplication && isset($steamUser->current_server_ip)) {
                 // Find database server ID
                 $currentServer = Server::where('address', $steamUser->current_server_ip)->where('port', $steamUser->current_server_port)->first();
                 // Create server if it does not already exist
                 if (!count($currentServer)) {
                     $currentServer = new Server();
                     $currentServer->application_id = $currentApplication->id;
                     $currentServer->address = $steamUser->current_server_ip;
                     $currentServer->port = $steamUser->current_server_port;
                     $currentServer->save();
                 }
             }
             // Create a new state
             $state = new State();
             $state->user_id = $user->id;
             $state->status = $steamUser->status;
             if ($currentApplication) {
                 $state->application_id = $currentApplication->id;
             }
             if ($currentServer) {
                 $state->server_id = $currentServer->id;
             }
             $state->save();
             $successCount++;
             // Only incremented if no exceptions above
         } catch (\Exception $e) {
             $this->customError('Unable to insert user state for ' . $steamUser->id . ' "' . $steamUser->username . '" : ' . $e->getMessage());
             $failureCount++;
         }
     }
     // Provide info on results
     if ($successCount > 0) {
         $this->customInfo($successCount . ' Steam user states successfully added');
     }
     if ($failureCount > 0) {
         $this->customError($failureCount . ' Steam user states were not added due to errors');
     }
     if ($appFailureCount > 0) {
         $this->customError($appFailureCount . ' applications were not found in the database - Consider running steam:import-apps');
     }
 }