Class: SteamId

Inherits:
Object
  • Object
show all
Includes:
Cacheable
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 Cacheable

#cache, #fetched?, included

Constructor Details

#initialize(id, fetch = true) ⇒ 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

  • fetch (Boolean) (defaults to: true)

    if ‘true` the Steam ID’s data is loaded into the object



194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/steam/community/steam_id.rb', line 194

def initialize(id, fetch = true)
  begin
    if id.is_a? Numeric
      @steam_id64 = id
    else
      @custom_url = id.downcase
    end

    super(fetch)
  rescue REXML::ParseException
    raise SteamCondenserError, 'SteamID could not be loaded.'
  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



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

def custom_url
  @custom_url
end

#favorite_gameString (readonly)

Deprecated.

The favorite game is no longer listed for new users

Returns the favorite game of this user

Returns:

  • (String)

    The favorite game of this user



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

def favorite_game
  @favorite_game
end

#favorite_game_hours_playedString (readonly)

Deprecated.

The favorite game is no longer listed for new users

Returns the number of hours that this user played his/her favorite game in the last two weeks

Returns:

  • (String)

    The number of hours the favorite game has been played recently



45
46
47
# File 'lib/steam/community/steam_id.rb', line 45

def favorite_game_hours_played
  @favorite_game_hours_played
end

#groupsArray<SteamGroup> (readonly)

Returns the groups this user is a member of

Returns:

  • (Array<SteamGroup>)

    The groups this user is a member of



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

def groups
  @groups
end

#head_lineString (readonly)

Returns the headline specified by the user

Returns:

  • (String)

    The headline specified by the user



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

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



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

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



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

def links
  @links
end

#locationString (readonly)

Returns the location of the user

Returns:

  • (String)

    The location of the user



74
75
76
# File 'lib/steam/community/steam_id.rb', line 74

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



80
81
82
# File 'lib/steam/community/steam_id.rb', line 80

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



90
91
92
# File 'lib/steam/community/steam_id.rb', line 90

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



95
96
97
# File 'lib/steam/community/steam_id.rb', line 95

def nickname
  @nickname
end

#privacy_stateString (readonly)

Returns the privacy state of this Steam ID

Returns:

  • (String)

    The privacy state of this Steam ID



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

def privacy_state
  @privacy_state
end

#real_nameString (readonly)

Returns the real name of this user

Returns:

  • (String)

    The real name of this user



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

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:

  • #ingame?
  • #online?


112
113
114
# File 'lib/steam/community/steam_id.rb', line 112

def state_message
  @state_message
end

#steam_id64Fixnum (readonly)

Returns this user’s 64bit SteamID

Returns:

  • (Fixnum)

    This user’s 64bit SteamID



117
118
119
# File 'lib/steam/community/steam_id.rb', line 117

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



122
123
124
# File 'lib/steam/community/steam_id.rb', line 122

def steam_rating
  @steam_rating
end

#summaryString (readonly)

Returns the summary this user has provided

Returns:

  • (String)

    This user’s summary



127
128
129
# File 'lib/steam/community/steam_id.rb', line 127

def summary
  @summary
end

#visibility_stateFixnum (readonly)

Returns the visibility state of this Steam ID

Returns:

  • (Fixnum)

    This Steam ID’s visibility State



132
133
134
# File 'lib/steam/community/steam_id.rb', line 132

def visibility_state
  @visibility_state
end

Class Method Details

.convert_community_id_to_steam_id(community_id) ⇒ String

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:



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/steam/community/steam_id.rb', line 141

def self.convert_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

.convert_steam_id_to_community_id(steam_id) ⇒ Fixnum

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:



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/steam/community/steam_id.rb', line 162

def self.convert_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.match(/^STEAM_[0-1]:[0-1]:[0-9]+$/).nil?
    raise SteamCondenserError, "SteamID \"#{steam_id}\" doesn't have the correct format."
  end

  steam_id = steam_id[6..-1].split(':').map!{|s| s.to_i}

  steam_id[1] + steam_id[2] * 2 + 76561197960265728
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:



184
185
186
# File 'lib/steam/community/steam_id.rb', line 184

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

Instance Method Details

#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



213
214
215
216
217
218
219
# File 'lib/steam/community/steam_id.rb', line 213

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:



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
# File 'lib/steam/community/steam_id.rb', line 227

def fetch
  profile_url = open(base_url + '?xml=1', {:proxy => true})
  profile = REXML::Document.new(profile_url.read).root

  unless REXML::XPath.first(profile, 'error').nil?
    raise SteamCondenserError, profile.elements['error'].text
  end

  unless REXML::XPath.first(profile, 'privacyMessage').nil?
    raise SteamCondenserError, profile.elements['privacyMessage'].text
  end
  
  begin
    @nickname         = CGI.unescapeHTML profile.elements['steamID'].text
    @steam_id64       = profile.elements['steamID64'].text.to_i
    @vac_banned       = (profile.elements['vacBanned'].text == 1)

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

    if @privacy_state == 'public'
      @custom_url                       = profile.elements['customURL'].text.downcase
      @custom_url                       = nil if @custom_url.empty?

      unless REXML::XPath.first(profile, 'favoriteGame').nil?
        @favorite_game                  = profile.elements['favoriteGame/name'].text
        @favorite_game_hours_played     = profile.elements['favoriteGame/hoursPlayed2wk'].text
      end

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

      @most_played_games = {}
      unless REXML::XPath.first(profile, 'mostPlayedGames').nil?
        profile.elements.each('mostPlayedGames/mostPlayedGame') do |most_played_game|
          @most_played_games[most_played_game.elements['gameName'].text] = most_played_game.elements['hoursPlayed'].text.to_f
        end
      end

      @groups = []
      unless REXML::XPath.first(profile, 'groups').nil?
        profile.elements.each('groups/group') do |group|
          @groups << SteamGroup.new(group.elements['groupID64'].text.to_i, false)
        end
      end

      @links = {}
      unless REXML::XPath.first(profile, 'weblinks').nil?
        profile.elements.each('weblinks/weblink') do |link|
          @links[CGI.unescapeHTML link.elements['title'].text] = link.elements['link'].text
        end
      end
    end
  rescue
    raise SteamCondenserError, 'XML data could not be parsed.'
  end

  super
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:



302
303
304
305
306
307
308
309
310
# File 'lib/steam/community/steam_id.rb', line 302

def fetch_friends
  url = "#{base_url}/friends?xml=1"

  @friends = []
  friends_data = REXML::Document.new(open(url, {:proxy => true}).read).root
  friends_data.elements.each('friends/friend') do |friend|
    @friends << SteamId.new(friend.text.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:



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

def fetch_games
  url = "#{base_url}/games?xml=1"

  @games     = {}
  @playtimes = {}
  games_data = REXML::Document.new(open(url, {:proxy => true}).read).root
  games_data.elements.each('games/game') do |game_data|
    game = SteamGame.new(game_data)
    @games[game.app_id] = game
    recent = total = 0
    unless game_data.elements['hoursLast2Weeks'].nil?
      recent = game_data.elements['hoursLast2Weeks'].text.to_f
    end
    unless game_data.elements['hoursOnRecord'].nil?
      total = game_data.elements['hoursOnRecord'].text.to_f
    end
    @playtimes[game.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:



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

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



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

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:



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

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:



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

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



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

def icon_url
  "#{@image_url}.jpg"
end

#is_banned?Boolean

Returns whether the owner of this SteamID is VAC banned

Returns:

  • (Boolean)

    ‘true` if the user has been banned by VAC



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

def is_banned?
  @vac_banned
end

#is_in_game?Boolean

Returns whether the owner of this SteamId is playing a game

Returns:

  • (Boolean)

    ‘true` if the user is in-game



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

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

#is_online?Boolean

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

Returns:

  • (Boolean)

    ‘true` if the user is online



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

def is_online?
  @online_state != 'offline'
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



423
424
425
# File 'lib/steam/community/steam_id.rb', line 423

def medium_avatar_url
  "#{@image_url}_medium.jpg"
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



434
435
436
437
# File 'lib/steam/community/steam_id.rb', line 434

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



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

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