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



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

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 = SteamId.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



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

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



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

def groups
  @groups
end

#head_lineString (readonly)

Returns the headline specified by the user

Returns:

  • (String)

    The headline specified by the user



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

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



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

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



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

def links
  @links
end

#locationString (readonly)

Returns the location of the user

Returns:

  • (String)

    The location of the user



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

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



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

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



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

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



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

def nickname
  @nickname
end

#privacy_stateString (readonly)

Returns the privacy state of this Steam ID

Returns:

  • (String)

    The privacy state of this Steam ID



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

def privacy_state
  @privacy_state
end

#real_nameString (readonly)

Returns the real name of this user

Returns:

  • (String)

    The real name of this user



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

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:



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

def state_message
  @state_message
end

#steam_id64Fixnum (readonly)

Returns this user’s 64bit SteamID

Returns:

  • (Fixnum)

    This user’s 64bit SteamID



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

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



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

def steam_rating
  @steam_rating
end

#summaryString (readonly)

Returns the summary this user has provided

Returns:

  • (String)

    This user’s summary



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

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



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

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



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

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:



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

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:



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

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



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

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:



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

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 = $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 = $1.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



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

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



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

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:



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
# File 'lib/steam/community/steam_id.rb', line 247

def fetch
  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
    @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 = {}
    [(profile['mostPlayedGames'] || {})['mostPlayedGame']].compact.flatten.each do |most_played_game|
      @most_played_games[most_played_game['gameName']] = most_played_game['hoursPlayed'].to_f
    end

    @groups = []
    [(profile['groups'] || {})['group']].compact.flatten.each do |group|
      @groups << SteamGroup.new(group['groupID64'].to_i, false)
    end

    @links = {}
    [(profile['weblinks'] || {})['weblink']].compact.flatten.each do |link|
      @links[CGI.unescapeHTML link['title']] = link['link']
    end
  end
rescue
  raise $! if $!.is_a? SteamCondenserError
  raise SteamCondenserError, 'XML data could not be parsed.'
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:



307
308
309
310
311
312
# File 'lib/steam/community/steam_id.rb', line 307

def fetch_friends
  friends_data = parse "#{base_url}/friends?xml=1"
  @friends = friends_data['friends']['friend'].map do |friend|
    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:



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/steam/community/steam_id.rb', line 321

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

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

    @recent_playtimes[app_id] = (recent * 60).to_i
    @total_playtimes[app_id]  = (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:



372
373
374
375
# File 'lib/steam/community/steam_id.rb', line 372

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



343
344
345
# File 'lib/steam/community/steam_id.rb', line 343

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:



356
357
358
359
360
361
362
363
364
# File 'lib/steam/community/steam_id.rb', line 356

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:



386
387
388
389
# File 'lib/steam/community/steam_id.rb', line 386

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



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

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



403
404
405
# File 'lib/steam/community/steam_id.rb', line 403

def id
  @custom_url || @steam_id64
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



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

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



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

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



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

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



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

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



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

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



451
452
453
# File 'lib/steam/community/steam_id.rb', line 451

def recent_playtime(id)
  @recent_playtimes[find_game(id).app_id]
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



461
462
463
# File 'lib/steam/community/steam_id.rb', line 461

def total_playtime(id)
  @total_playtimes[find_game(id).app_id]
end