Exemple #1
0
/*!
  \brief Fetches game achievement completion values.

  This method fetches the game completion values for the game app ID \p appID.

  \param appID The app ID we want the achievement completion values for.
  \return A \c QHash map with achievements and completion values.
*/
QHash< QString, float >
Steam::fetchAchievementCompletion( int appID )
{
  if( appID <= 0 )
    return( QHash< QString, float >() );

  QUrl url( QL( "http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v2/" ) );
  url.addQueryItem( QL( "gameid" ), QString::number( appID ) );
  QNetworkRequest request( url );

  QNetworkReply *reply = d->network->get( request );
  while( ! reply->isFinished() )
    qApp->processEvents();

  QByteArray data = reply->readAll();
  reply->deleteLater();
  reply = NULL;

  if( data.isEmpty() )
    return( QHash< QString, float >() );

  JSONStreamReader json;
  JSONObject *root = json.read( data );
  if( ! root || json.hasError() )
  {
    delete root;
    root = NULL;

    SteamPrivate::jsonError( json );
    return( QHash< QString, float >() );
  }

  JSONList *achievements = NULL;
  JSONObject *achievementpercentages = root->valueObject( QL( "achievementpercentages" ) );
  if( achievementpercentages )
    achievements = achievementpercentages->valueList( QL( "achievements" ) );

  QHash< QString, float > completion;
  for( int i = 0; achievements && i < achievements->count(); i++ )
  {
    JSONValue *val = achievements->at( i );
    JSONObject *achievement = NULL;
    if( val->type() == JSONValue::T_Object )
      achievement = dynamic_cast< JSONObject * >( val );

    if( achievement )
    {
      QString name  = achievement->valueScalar( QL( "name" ) )->sValue();
      float percent = achievement->valueScalar( QL( "percent" ) )->dValue();

      completion.insert( name, percent );
    }
  }

  delete root;
  root = NULL;

  return completion;
}
Exemple #2
0
/*!
  \brief Processes a app ID list reply.

  The data is rather big and in JSON format. We parse it, read it and return it
  to the main window as a signal.

  \param data The data to be parsed.
*/
void
Steam::processReplyAppIDs( const QByteArray &data )
{
  WAITCURSOR;
  QHash< QString, int > map;
  if( data.isEmpty() )
    return;

  JSONStreamReader jsonReader;
  JSONObject *json = jsonReader.read( data );
  if( ! json || jsonReader.hasError() )
  {
    SteamPrivate::jsonError( jsonReader );
    return;
  }

  JSONList *app = NULL;
  JSONObject *apps = NULL;
  JSONObject *applist = json->valueObject( QL( "applist" ) );

  if( applist )
    apps = applist->valueObject( QL( "apps" ) );

  if( apps )
    app = apps->valueList( QL( "app" ) );

  for( int i = 0; app && i < app->count(); i++ )
  {
    JSONValue *aApp = NULL;
    aApp = app->at( i );

    if( aApp->type() == JSONValue::T_Object )
    {
      JSONObject *obj = dynamic_cast< JSONObject * >( aApp );
      map.insert( obj->valueScalar( QL( "name" ) )->sValue(),
        obj->valueScalar( QL( "appid" ) )->iValue() );
    }
  }

  delete json;
  json = NULL;

  emit( appIDs( map ) );
}
Exemple #3
0
/*!
  \brief Processes the game's achievements list.

  It arrives as a JSON object. We parse it and send it to the mainwindow.

  \param data The data to be parsed.
*/
void
Steam::processReplyAchievements( const QByteArray &data )
{
  AchievementList achievementList;

  if( ! data.isEmpty() )
  {
    JSONStreamReader jsonReader;
    JSONObject *json = jsonReader.read( data );
    if( jsonReader.hasError() )
      SteamPrivate::jsonError( jsonReader );
    else
    {
      JSONObject *availableGameStats = NULL;
      JSONList *achievements = NULL;

      JSONObject *game = json->valueObject( QL( "game" ) );

      if( game )
        availableGameStats = game->valueObject( QL( "availableGameStats" ) );

      if( availableGameStats )
        achievements = availableGameStats->valueList( QL( "achievements" ) );
      
      for( int i = 0; achievements && i < achievements->count(); i++ )
      {
        JSONValue *val = achievements->at( i );
        if( ! val || ! val->type() == JSONValue::T_Object )
          continue;

        JSONObject *achievementJson = dynamic_cast< JSONObject * >( val );
        achievementList << Achievement( achievementJson );
      }
    }

    delete json;
    json = NULL;
  }

  emit( achievements( achievementList ) );
}