Class: GameAchievement

Inherits:
Object
  • Object
show all
Defined in:
lib/steam/community/game_achievement.rb

Overview

The GameAchievement class represents a specific achievement for a single game and for a single user

It also provides the ability to load the global unlock percentages of all achievements of a specific game.

Author:

  • Sebastian Staudt

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, game, achievement_data) ⇒ GameAchievement

Creates the achievement with the given name for the given user and game and achievement data

Parameters:

  • user (SteamId)

    The SteamID of the player this achievement belongs to

  • game (SteamGame)

    The game this achievement belongs to

  • achievement_data (Hash<String, Object>)

    The achievement data extracted from XML



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/steam/community/game_achievement.rb', line 87

def initialize(user, game, achievement_data)
  @api_name        = achievement_data['apiname']
  @description     = achievement_data['description']
  @game            = game
  @icon_closed_url = achievement_data['iconClosed']
  @icon_open_url   = achievement_data['iconOpen']
  @name            = achievement_data['name']
  @unlocked        = (achievement_data['closed'].to_i == 1)
  @user            = user

  if @unlocked && !achievement_data['unlockTimestamp'].nil?
    @timestamp  = Time.at(achievement_data['unlockTimestamp'].to_i)
  end
end

Instance Attribute Details

#api_nameString (readonly)

Returns the symbolic API name of this achievement

Returns:

  • (String)

     The API name of this achievement



22
23
24
# File 'lib/steam/community/game_achievement.rb', line 22

def api_name
  @api_name
end

#descriptionString (readonly)

Returns the description of this achievement

Returns:

  • (String)

     The description of this achievement



27
28
29
# File 'lib/steam/community/game_achievement.rb', line 27

def description
  @description
end

#gameSteam (readonly)

Return the game this achievement belongs to

Returns:

  • (Steam)

    The game this achievement belongs to



32
33
34
# File 'lib/steam/community/game_achievement.rb', line 32

def game
  @game
end

#icon_closed_urlString (readonly)

Returns the url for the closed icon of this achievement

Returns:

  • (String)

    The url of the closed achievement icon



37
38
39
# File 'lib/steam/community/game_achievement.rb', line 37

def icon_closed_url
  @icon_closed_url
end

#icon_open_urlString (readonly)

Returns the url for the open icon of this achievement

Returns:

  • (String)

    The url of the open achievement icon



42
43
44
# File 'lib/steam/community/game_achievement.rb', line 42

def icon_open_url
  @icon_open_url
end

#nameString (readonly)

Returns the name of this achievement

Returns:

  • (String)

     The name of this achievement



47
48
49
# File 'lib/steam/community/game_achievement.rb', line 47

def name
  @name
end

#timestampTime (readonly)

Returns the time this achievement has been unlocked by its owner

Returns:

  • (Time)

    The time this achievement has been unlocked



52
53
54
# File 'lib/steam/community/game_achievement.rb', line 52

def timestamp
  @timestamp
end

#userFixnum (readonly)

Returns the SteamID of the user who owns this achievement

Returns:

  • (Fixnum)

    The SteamID of this achievement’s owner



57
58
59
# File 'lib/steam/community/game_achievement.rb', line 57

def user
  @user
end

Class Method Details

.global_percentages(app_id) ⇒ Hash<Symbol, Float>

Loads the global unlock percentages of all achievements for the game with the given Steam Application ID

Parameters:

  • app_id (Fixnum)

    The unique Steam Application ID of the game (e.g. ‘440` for Team Fortress 2). See

    http://developer.valvesoftware.com/wiki/Steam_Application_IDs for
    all application IDs
    

Returns:

  • (Hash<Symbol, Float>)

    The symbolic achievement names with their corresponding unlock percentages

Raises:

  • (WebApiError)

    if the request to Steam’s Web API fails



69
70
71
72
73
74
75
76
77
78
# File 'lib/steam/community/game_achievement.rb', line 69

def self.global_percentages(app_id)
  percentages = {}

  data = WebApi.json('ISteamUserStats', 'GetGlobalAchievementPercentagesForApp', 2, { :gameid => app_id })
  MultiJson.load(data, { :symbolize_keys => true })[:achievementpercentages][:achievements].each do |percentage|
    percentages[percentage[:name].to_sym] = percentage[:percent]
  end

  percentages
end

Instance Method Details

#unlocked?Boolean

Returns whether this achievement has been unlocked by its owner

Returns:

  • (Boolean)

     ‘true` if the achievement has been unlocked by the user



105
106
107
# File 'lib/steam/community/game_achievement.rb', line 105

def unlocked?
  @unlocked
end