Class: SteamCondenser::Community::SteamId

Inherits:
Object
  • Object
show all
Includes:
Cacheable, XMLData
Defined in:
lib/steam-condenser/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, #cache_id_value, #cache_ids, #cached_instance, #fetched?, included

Constructor Details

#initialize(id) ⇒ SteamId

Creates a new SteamId instance for the given Steam ID


219
220
221
222
223
224
225
226
227
228
229
# File 'lib/steam-condenser/community/steam_id.rb', line 219

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.


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

def custom_url
  @custom_url
end

#groupsArray<SteamGroup> (readonly)

Returns the groups this user is a member of


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

def groups
  @groups
end

#head_lineString (readonly)

Returns the headline specified by the user


47
48
49
# File 'lib/steam-condenser/community/steam_id.rb', line 47

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


53
54
55
# File 'lib/steam-condenser/community/steam_id.rb', line 53

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.


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

def links
  @links
end

#locationString (readonly)

Returns the location of the user


66
67
68
# File 'lib/steam-condenser/community/steam_id.rb', line 66

def location
  @location
end

#member_sinceTime (readonly)

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


72
73
74
# File 'lib/steam-condenser/community/steam_id.rb', line 72

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.


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

def most_played_games
  @most_played_games
end

#nicknameString (readonly)

Returns the Steam nickname of the user


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

def nickname
  @nickname
end

#privacy_stateString (readonly)

Returns the privacy state of this Steam ID


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

def privacy_state
  @privacy_state
end

#real_nameString (readonly)

Returns the real name of this user


97
98
99
# File 'lib/steam-condenser/community/steam_id.rb', line 97

def real_name
  @real_name
end

#state_messageString (readonly)

Returns the message corresponding to this user's online state

See Also:


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

def state_message
  @state_message
end

#steam_id64Fixnum (readonly)

Returns this user's 64bit SteamID


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

def steam_id64
  @steam_id64
end

#steam_ratingFloat (readonly)

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


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

def steam_rating
  @steam_rating
end

#summaryString (readonly)

Returns the summary this user has provided


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

def summary
  @summary
end

#trade_ban_stateString (readonly)

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


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

def trade_ban_state
  @trade_ban_state
end

#visibility_stateFixnum (readonly)

Returns the visibility state of this Steam ID


129
130
131
# File 'lib/steam-condenser/community/steam_id.rb', line 129

def visibility_state
  @visibility_state
end

Class Method Details

.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

Raises:

  • (Error)

    if the community ID is to small


138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/steam-condenser/community/steam_id.rb', line 138

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

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

  steam_id2 = (steam_id2 - steam_id1) / 2

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

.community_id_to_steam_id3(community_id) ⇒ Object


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

def self.community_id_to_steam_id3(community_id)
  #First part of Steam ID 3 is always 1
  steam_id1 = 1
  steam_id2 = community_id - 76561197960265728

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

  "[U:#{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.

See Also:


210
211
212
# File 'lib/steam-condenser/community/steam_id.rb', line 210

def self.from_steam_id(steam_id)
  new(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


168
169
170
171
172
173
174
175
176
# File 'lib/steam-condenser/community/steam_id.rb', line 168

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

  return nil if result[:success] != 1

  result[:steamid].to_i
end

.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

Raises:

  • (Error)

    if the SteamID doesn't have the correct format


185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/steam-condenser/community/steam_id.rb', line 185

def self.steam_id_to_community_id(steam_id)
  if steam_id == 'STEAM_ID_LAN' || steam_id == 'BOT'
    raise SteamCondenser::Error, "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 SteamCondenser::Error, "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


234
235
236
# File 'lib/steam-condenser/community/steam_id.rb', line 234

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.


244
245
246
247
248
249
250
# File 'lib/steam-condenser/community/steam_id.rb', line 244

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

#fetchObject

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

Raises:

  • (Error)

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

See Also:


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
306
307
308
309
# File 'lib/steam-condenser/community/steam_id.rb', line 258

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

  raise SteamCondenser::Error, profile['error'] unless profile['error'].nil?

  unless profile['privacyMessage'].nil?
    raise SteamCondenser::Error, 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? SteamCondenser::Error
  raise SteamCondenser::Error, '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:


318
319
320
321
322
323
324
325
# File 'lib/steam-condenser/community/steam_id.rb', line 318

def fetch_friends
  params = { :relationship => 'friend', :steamid => @steam_id64 }

  friends_data = WebApi.json 'ISteamUser', 'GetFriendList', 1, params
  @friends = friends_data[:friendslist][:friends].map do |friend|
    SteamId.new(friend[:steamid].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:


334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/steam-condenser/community/steam_id.rb', line 334

def fetch_games
  params = {
    :include_appinfo           => 1,
    :include_played_free_games => 1,
    :steamId                   => @steam_id64
  }
  games_data = WebApi.json 'IPlayerService', 'GetOwnedGames', 1, params
  @games            = {}
  @recent_playtimes = {}
  @total_playtimes  = {}
  games_data[:response][:games].each do |game_data|
    app_id = game_data[:appid]
    @games[app_id] = SteamGame.new app_id, game_data

    @recent_playtimes[app_id] = game_data[:playtime_2weeks] || 0
    @total_playtimes[app_id]  = game_data[:playtime_forever] || 0
  end

  @games
end

#friendsArray<SteamId>

Returns the Steam Community friends of this user

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

See Also:


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

def friends
  @friends || fetch_friends
end

#full_avatar_urlString

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


358
359
360
# File 'lib/steam-condenser/community/steam_id.rb', line 358

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

#game_stats(app_id) ⇒ GameStats

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

Raises:

  • (Error)

    if the user does not own this game or it does not have any stats


369
370
371
# File 'lib/steam-condenser/community/steam_id.rb', line 369

def game_stats(app_id)
  GameStats.new @custom_url || @steam_id64, app_id
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.

See Also:


392
393
394
# File 'lib/steam-condenser/community/steam_id.rb', line 392

def games
  @games || fetch_games
end

#icon_urlString

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


399
400
401
# File 'lib/steam-condenser/community/steam_id.rb', line 399

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


408
409
410
# File 'lib/steam-condenser/community/steam_id.rb', line 408

def id
  @custom_url || @steam_id64
end

#in_game?Boolean

Returns whether the owner of this SteamId is playing a game


415
416
417
# File 'lib/steam-condenser/community/steam_id.rb', line 415

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

#limited?Boolean

Returns whether this Steam account is limited


422
423
424
# File 'lib/steam-condenser/community/steam_id.rb', line 422

def limited?
  @limited
end

#medium_avatar_urlString

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


429
430
431
# File 'lib/steam-condenser/community/steam_id.rb', line 429

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

#online?Boolean

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


436
437
438
# File 'lib/steam-condenser/community/steam_id.rb', line 436

def online?
  @online_state != 'offline'
end

#public?Boolean

Returns whether this Steam ID is publicly accessible


443
444
445
# File 'lib/steam-condenser/community/steam_id.rb', line 443

def public?
  @privacy_state == 'public'
end

#recent_playtime(app_id) ⇒ Fixnum

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


453
454
455
# File 'lib/steam-condenser/community/steam_id.rb', line 453

def recent_playtime(app_id)
  @recent_playtimes[app_id]
end

#steam_levelFixnum

Returns the current Steam level of this user

If the Steam level hasn't been updated yet, this is done now.


463
464
465
# File 'lib/steam-condenser/community/steam_id.rb', line 463

def steam_level
  @steam_level || update_steam_level
end

#total_playtime(app_id) ⇒ Fixnum

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


472
473
474
# File 'lib/steam-condenser/community/steam_id.rb', line 472

def total_playtime(app_id)
  @total_playtimes[app_id]
end

#update_steam_levelFixnum

Updates the Steam level of this user using the Web API

See Also:


480
481
482
483
# File 'lib/steam-condenser/community/steam_id.rb', line 480

def update_steam_level
  data = WebApi.json 'IPlayerService', 'GetSteamLevel', 1, { :steamid => @steam_id64 }
  @steam_level = data[:response][:player_level]
end