Class: SteamId

Inherits:
Object
  • Object
show all
Includes:
Cacheable, XMLData
Defined in:
lib/steam/community/steam_id.rb

Overview

The SteamId class represents a Steam Community profile (also called Steam ID)

Author:

  • Sebastian Staudt

Instance Attribute Summary collapse

Attributes included from Cacheable

#fetch_time

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XMLData

#parse

Methods included from Cacheable

#cache, #fetched?, included

Constructor Details

#initialize(id) ⇒ SteamId

Creates a new ‘SteamId` instance for the given Steam ID

Parameters:

  • id (String, Fixnum)

    The custom URL of the Steam ID specified by the user or the 64bit SteamID



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/steam/community/steam_id.rb', line 207

def initialize(id)
  if id.is_a? Numeric
    @steam_id64 = id
  else
    if id =~ /^STEAM_[0-1]:[0-1]:[0-9]+$/ || id =~ /\[U:[0-1]:[0-9]+\]/
      @steam_id64 = self.steam_id_to_community_id id
    else
      @custom_url = id.downcase
    end
  end
end

Instance Attribute Details

#custom_urlString (readonly)

Note:

The custom URL is not necessarily the same as the user’s nickname.

Returns the custom URL of this Steam ID

The custom URL is a user specified unique string that can be used instead of the 64bit SteamID as an identifier for a Steam ID.

Returns:

  • (String)

    The custom URL of this Steam ID



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

def custom_url
  @custom_url
end

#groupsArray<SteamGroup> (readonly)

Returns the groups this user is a member of

Returns:

  • (Array<SteamGroup>)

    The groups this user is a member of



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

def groups
  @groups
end

#head_lineString (readonly)

Returns the headline specified by the user

Returns:

  • (String)

    The headline specified by the user



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

def head_line
  @head_line
end

#hours_playedFloat (readonly)

Returns the number of hours that this user played a game in the last two weeks

Returns:

  • (Float)

    The number of hours the user has played recently



48
49
50
# File 'lib/steam/community/steam_id.rb', line 48

def hours_played
  @hours_played
end

Returns the links that this user has added to his/her Steam ID

The keys of the hash contain the titles of the links while the values contain the corresponding URLs.

Returns:

  • (Hash<String, String>)

    The links of this user



56
57
58
# File 'lib/steam/community/steam_id.rb', line 56

def links
  @links
end

#locationString (readonly)

Returns the location of the user

Returns:

  • (String)

    The location of the user



61
62
63
# File 'lib/steam/community/steam_id.rb', line 61

def location
  @location
end

#member_sinceTime (readonly)

Returns the date of registration for the Steam account belonging to this SteamID

Returns:

  • (Time)

    The date of the Steam account registration



67
68
69
# File 'lib/steam/community/steam_id.rb', line 67

def member_since
  @member_since
end

#most_played_gamesHash<String, Float> (readonly)

Returns the games this user has played the most in the last two weeks

The keys of the hash contain the names of the games while the values contain the number of hours the corresponding game has been played by the user in the last two weeks.

Returns:

  • (Hash<String, Float>)

    The games this user has played the most recently



77
78
79
# File 'lib/steam/community/steam_id.rb', line 77

def most_played_games
  @most_played_games
end

#nicknameString (readonly)

Returns the Steam nickname of the user

Returns:

  • (String)

    The Steam nickname of the user



82
83
84
# File 'lib/steam/community/steam_id.rb', line 82

def nickname
  @nickname
end

#privacy_stateString (readonly)

Returns the privacy state of this Steam ID

Returns:

  • (String)

    The privacy state of this Steam ID



87
88
89
# File 'lib/steam/community/steam_id.rb', line 87

def privacy_state
  @privacy_state
end

#real_nameString (readonly)

Returns the real name of this user

Returns:

  • (String)

    The real name of this user



92
93
94
# File 'lib/steam/community/steam_id.rb', line 92

def real_name
  @real_name
end

#state_messageString (readonly)

Returns the message corresponding to this user’s online state

Returns:

  • (String)

    The message corresponding to this user’s online state

See Also:



99
100
101
# File 'lib/steam/community/steam_id.rb', line 99

def state_message
  @state_message
end

#steam_id64Fixnum (readonly)

Returns this user’s 64bit SteamID

Returns:

  • (Fixnum)

    This user’s 64bit SteamID



104
105
106
# File 'lib/steam/community/steam_id.rb', line 104

def steam_id64
  @steam_id64
end

#steam_ratingFloat (readonly)

Returns the Steam rating calculated over the last two weeks’ activity

Returns:

  • (Float)

    The Steam rating of this user



109
110
111
# File 'lib/steam/community/steam_id.rb', line 109

def steam_rating
  @steam_rating
end

#summaryString (readonly)

Returns the summary this user has provided

Returns:

  • (String)

    This user’s summary



114
115
116
# File 'lib/steam/community/steam_id.rb', line 114

def summary
  @summary
end

#trade_ban_stateString (readonly)

Returns this user’s ban state in Steam’s trading system

Returns:

  • (String)

    This user’s trading ban state



119
120
121
# File 'lib/steam/community/steam_id.rb', line 119

def trade_ban_state
  @trade_ban_state
end

#visibility_stateFixnum (readonly)

Returns the visibility state of this Steam ID

Returns:

  • (Fixnum)

    This Steam ID’s visibility State



124
125
126
# File 'lib/steam/community/steam_id.rb', line 124

def visibility_state
  @visibility_state
end

Class Method Details

.community_id_to_steam_id(community_id) ⇒ String Also known as: convert_community_id_to_steam_id

Converts a 64bit numeric SteamID as used by the Steam Community to a SteamID as reported by game servers

Parameters:

  • community_id (Fixnum)

    The SteamID string as used by the Steam Community

Returns:

  • (String)

    The converted SteamID, like ‘STEAM_0:0:12345`

Raises:



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/steam/community/steam_id.rb', line 133

def self.community_id_to_steam_id(community_id)
  steam_id1 = community_id % 2
  steam_id2 = community_id - 76561197960265728

  unless steam_id2 > 0
    raise SteamCondenserError, "SteamID #{community_id} is too small."
  end

  steam_id2 = (steam_id2 - steam_id1) / 2

  "STEAM_0:#{steam_id1}:#{steam_id2}"
end

.from_steam_id(steam_id) ⇒ SteamId

Creates a new ‘SteamId` instance using a SteamID as used on servers

The SteamID from the server is converted into a 64bit numeric SteamID first before this is used to retrieve the corresponding Steam Community profile.

Parameters:

  • steam_id (String)

    The SteamID string as used on servers, like ‘STEAM_0:0:12345`

Returns:

  • (SteamId)

    The ‘SteamId` belonging to the given SteamID

See Also:



198
199
200
# File 'lib/steam/community/steam_id.rb', line 198

def self.from_steam_id(steam_id)
  new(convert_steam_id_to_community_id(steam_id))
end

.resolve_vanity_url(vanity_url) ⇒ Fixnum

Resolves a vanity URL of a Steam Community profile to a 64bit numeric SteamID

Parameters:

  • vanity_url (String)

    The vanity URL of a Steam Community profile

Returns:

  • (Fixnum)

    The 64bit numeric SteamID



151
152
153
154
155
156
157
158
159
# File 'lib/steam/community/steam_id.rb', line 151

def self.resolve_vanity_url(vanity_url)
  params = { :vanityurl => vanity_url }
  json = WebApi.json 'ISteamUser', 'ResolveVanityURL', 1, params
  result = MultiJson.load(json, :symbolize_keys => true)[:response]

  return nil if result[:success] != 1

  result[:steamid].to_i
end

.steam_id_to_community_id(steam_id) ⇒ Fixnum Also known as: convert_steam_id_to_community_id

Converts a SteamID as reported by game servers to a 64bit numeric SteamID as used by the Steam Community

Parameters:

  • steam_id (String)

    The SteamID string as used on servers, like ‘STEAM_0:0:12345`

Returns:

  • (Fixnum)

    The converted 64bit numeric SteamID

Raises:



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/steam/community/steam_id.rb', line 169

def self.steam_id_to_community_id(steam_id)
  if steam_id == 'STEAM_ID_LAN' || steam_id == 'BOT'
    raise SteamCondenserError, "Cannot convert SteamID \"#{steam_id}\" to a community ID."
  elsif steam_id =~ /^STEAM_[0-1]:[0-1]:[0-9]+$/
    steam_id = steam_id[8..-1].split(':').map! { |s| s.to_i }
    steam_id[0] + steam_id[1] * 2 + 76561197960265728
  elsif steam_id =~ /\[U:[0-1]:[0-9]+\]/
    steam_id = steam_id[3..-2].split(':').map! { |s| s.to_i }
    steam_id[0] + steam_id[1] + 76561197960265727
  else
    raise SteamCondenserError, "SteamID \"#{steam_id}\" doesn't have the correct format."
  end
end

Instance Method Details

#banned?Boolean Also known as: is_banned?

Returns whether the owner of this SteamID is VAC banned

Returns:

  • (Boolean)

    ‘true` if the user has been banned by VAC



222
223
224
# File 'lib/steam/community/steam_id.rb', line 222

def banned?
  @vac_banned
end

#base_urlString

Returns the base URL for this Steam ID

This URL is different for Steam IDs having a custom URL.

Returns:

  • (String)

    The base URL for this SteamID



232
233
234
235
236
237
238
# File 'lib/steam/community/steam_id.rb', line 232

def base_url
  if @custom_url.nil?
    "http://steamcommunity.com/profiles/#@steam_id64"
  else
    "http://steamcommunity.com/id/#@custom_url"
  end
end

#fetchObject

Fetchs data from the Steam Community by querying the XML version of the profile specified by the ID of this Steam ID

Raises:

  • SteamCondenserError if the Steam ID data is not available, e.g. when it is private

See Also:



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/steam/community/steam_id.rb', line 246

def fetch
  begin
    profile = parse "#{base_url}?xml=1"

    raise SteamCondenserError, profile['error'] unless profile['error'].nil?

    unless profile['privacyMessage'].nil?
      raise SteamCondenserError, profile['privacyMessage']
    end

    @nickname         = CGI.unescapeHTML profile['steamID']
    @steam_id64       = profile['steamID64'].to_i
    @limited          = (profile['isLimitedAccount'].to_i == 1)
    @trade_ban_state  = profile['tradeBanState']
    @vac_banned       = (profile['vacBanned'].to_i == 1)

    @image_url        = profile['avatarIcon'][0..-5]
    @online_state     = profile['onlineState']
    @privacy_state    = profile['privacyState']
    @state_message    = profile['stateMessage']
    @visibility_state = profile['visibilityState'].to_i

    if public?
      @custom_url = profile['customURL'].downcase rescue ''
      @custom_url = nil if @custom_url.empty?

      @head_line    = CGI.unescapeHTML profile['headline'] || ''
      @hours_played = profile['hoursPlayed2Wk'].to_f
      @location     = profile['location']
      @member_since = Time.parse profile['memberSince']
      @real_name    = CGI.unescapeHTML profile['realname'] || ''
      @steam_rating = profile['steamRating'].to_f
      @summary      = CGI.unescapeHTML profile['summary'] || ''

      @most_played_games = {}
      unless profile['mostPlayedGames'].to_s.strip.empty?
        [profile['mostPlayedGames']['mostPlayedGame']].flatten.each do |most_played_game|
          @most_played_games[most_played_game['gameName']] = most_played_game['hoursPlayed'].to_f
        end
      end

      @groups = []
      unless profile['groups'].to_s.strip.empty?
        [profile['groups']['group']].flatten.each do |group|
          @groups << SteamGroup.new(group['groupID64'].to_i, false)
        end
      end

      @links = {}
      unless profile['weblinks'].to_s.strip.empty?
        [profile['weblinks']['weblink']].flatten.each do |link|
          @links[CGI.unescapeHTML link['title']] = link['link']
        end
      end
    end
  rescue
    raise $! if $!.is_a? SteamCondenserError
    raise SteamCondenserError, 'XML data could not be parsed.', $!.backtrace
  end
end

#fetch_friendsObject

Fetches the friends of this user

This creates a new ‘SteamId` instance for each of the friends without fetching their data.

See Also:



314
315
316
317
318
319
320
# File 'lib/steam/community/steam_id.rb', line 314

def fetch_friends
  friends_data = parse "#{base_url}/friends?xml=1"
  @friends = []
  friends_data['friends']['friend'].each do |friend|
    @friends << SteamId.new(friend.to_i, false)
  end
end

#fetch_gamesObject

Fetches the games this user owns

This fills the game hash with the names of the games as keys. The values will either be ‘false` if the game does not have stats or the game’s “friendly name”.

See Also:



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/steam/community/steam_id.rb', line 329

def fetch_games
  games_data = parse "#{base_url}/games?xml=1"
  @games     = {}
  @playtimes = {}
  games_data['games']['game'].each do |game_data|
    app_id = game_data['appID'].to_i
    game = SteamGame.new app_id, game_data
    @games[app_id] = game

    recent = game_data['hoursLast2Weeks'].to_f
    total = (game_data['hoursOnRecord'] || '').delete(',').to_f

    @playtimes[app_id] = [(recent * 60).to_i, (total * 60).to_i]
  end

  true
end

#friendsArray<SteamId>

Returns the Steam Community friends of this user

If the friends haven’t been fetched yet, this is done now.

Returns:

  • (Array<SteamId>)

     The friends of this user

See Also:



379
380
381
382
# File 'lib/steam/community/steam_id.rb', line 379

def friends
  fetch_friends if @friends.nil?
  @friends
end

#full_avatar_urlString

Returns the URL of the full-sized version of this user’s avatar

Returns:

  • (String)

    The URL of the full-sized avatar



350
351
352
# File 'lib/steam/community/steam_id.rb', line 350

def full_avatar_url
  "#{@image_url}_full.jpg"
end

#game_stats(id) ⇒ GameStats

Returns the stats for the given game for the owner of this SteamID

Parameters:

  • id (Fixnum, String)

    The full or short name or the application ID of the game stats should be fetched for

Returns:

  • (GameStats)

    The statistics for the game with the given name

Raises:

See Also:



363
364
365
366
367
368
369
370
371
# File 'lib/steam/community/steam_id.rb', line 363

def game_stats(id)
  game = find_game id

  unless game.has_stats?
    raise SteamCondenserError, "\"#{game.name}\" does not have stats."
  end

  GameStats.create_game_stats(@custom_url || @steam_id64, game.short_name)
end

#gamesHash<Fixnum, SteamGame>

Returns the games this user owns

The keys of the hash are the games’ application IDs and the values are the corresponding game instances.

If the friends haven’t been fetched yet, this is done now.

Returns:

  • (Hash<Fixnum, SteamGame>)

    The games this user owns

See Also:



393
394
395
396
# File 'lib/steam/community/steam_id.rb', line 393

def games
  fetch_games if @games.nil?
  @games
end

#icon_urlString

Returns the URL of the icon version of this user’s avatar

Returns:

  • (String)

    The URL of the icon-sized avatar



401
402
403
# File 'lib/steam/community/steam_id.rb', line 401

def icon_url
  "#@image_url.jpg"
end

#idFixnum, String

Returns a unique identifier for this Steam ID

This is either the 64bit numeric SteamID or custom URL

Returns:

  • (Fixnum, String)

    The 64bit numeric SteamID or the custom URL



410
411
412
# File 'lib/steam/community/steam_id.rb', line 410

def id
  @custom_url.nil? ? @steam_id64 : @custom_url
end

#in_game?Boolean Also known as: is_in_game?

Returns whether the owner of this SteamId is playing a game

Returns:

  • (Boolean)

    ‘true` if the user is in-game



417
418
419
# File 'lib/steam/community/steam_id.rb', line 417

def in_game?
  @online_state == 'in-game'
end

#limited?Boolean

Returns whether this Steam account is limited

Returns:

  • (Boolean)

    ‘true` if this account is limited



425
426
427
# File 'lib/steam/community/steam_id.rb', line 425

def limited?
  @limited
end

#medium_avatar_urlString

Returns the URL of the medium-sized version of this user’s avatar

Returns:

  • (String)

    The URL of the medium-sized avatar



432
433
434
# File 'lib/steam/community/steam_id.rb', line 432

def medium_avatar_url
  "#{@image_url}_medium.jpg"
end

#online?Boolean Also known as: is_online?

Returns whether the owner of this SteamID is currently logged into Steam

Returns:

  • (Boolean)

    ‘true` if the user is online



439
440
441
# File 'lib/steam/community/steam_id.rb', line 439

def online?
  @online_state != 'offline'
end

#public?Boolean

Returns whether this Steam ID is publicly accessible

Returns:

  • (Boolean)

    ‘true` if this Steam ID is public



447
448
449
# File 'lib/steam/community/steam_id.rb', line 447

def public?
  @privacy_state == 'public'
end

#recent_playtime(id) ⇒ Fixnum

Returns the time in minutes this user has played this game in the last two weeks

Parameters:

  • id (Fixnum, String)

    The full or short name or the application ID of the game

Returns:

  • (Fixnum)

    The number of minutes this user played the given game in the last two weeks



458
459
460
461
# File 'lib/steam/community/steam_id.rb', line 458

def recent_playtime(id)
  game = find_game id
  @playtimes[game.app_id][0]
end

#total_playtime(id) ⇒ Fixnum

Returns the total time in minutes this user has played this game

Parameters:

  • id (Fixnum, String)

    The full or short name or the application ID of the game

Returns:

  • (Fixnum)

    The total number of minutes this user played the given game



469
470
471
472
# File 'lib/steam/community/steam_id.rb', line 469

def total_playtime(id)
  game = find_game id
  @playtimes[game.app_id][1]
end