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)

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 object for the given SteamID id, either numeric or the custom URL specified by the user. If fetch is true (default), fetch_data is used to load data into the object.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/steam/community/steam_id.rb', line 64

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 SteamCondenserException.new('SteamID could not be loaded.')
  end
end

Instance Attribute Details

#custom_urlObject (readonly)

Returns the value of attribute custom_url.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def custom_url
  @custom_url
end

#favorite_gameObject (readonly)

Returns the value of attribute favorite_game.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def favorite_game
  @favorite_game
end

#favorite_game_hours_playedObject (readonly)

Returns the value of attribute favorite_game_hours_played.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def favorite_game_hours_played
  @favorite_game_hours_played
end

#groupsObject (readonly)

Returns the value of attribute groups.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def groups
  @groups
end

#head_lineObject (readonly)

Returns the value of attribute head_line.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def head_line
  @head_line
end

#hours_playedObject (readonly)

Returns the value of attribute hours_played.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def hours_played
  @hours_played
end

#image_urlObject (readonly)

Returns the value of attribute image_url.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def image_url
  @image_url
end

Returns the value of attribute links.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def links
  @links
end

#locationObject (readonly)

Returns the value of attribute location.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def location
  @location
end

#member_sinceObject (readonly)

Returns the value of attribute member_since.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def member_since
  @member_since
end

#most_played_gamesObject (readonly)

Returns the value of attribute most_played_games.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def most_played_games
  @most_played_games
end

#nicknameObject (readonly)

Returns the value of attribute nickname.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def nickname
  @nickname
end

#privacy_stateObject (readonly)

Returns the value of attribute privacy_state.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def privacy_state
  @privacy_state
end

#real_nameObject (readonly)

Returns the value of attribute real_name.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def real_name
  @real_name
end

#state_messageObject (readonly)

Returns the value of attribute state_message.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def state_message
  @state_message
end

#steam_id64Object (readonly)

Returns the value of attribute steam_id64.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def steam_id64
  @steam_id64
end

#steam_ratingObject (readonly)

Returns the value of attribute steam_rating.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def steam_rating
  @steam_rating
end

#steam_rating_textObject (readonly)

Returns the value of attribute steam_rating_text.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def steam_rating_text
  @steam_rating_text
end

#summaryObject (readonly)

Returns the value of attribute summary.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def summary
  @summary
end

#vac_bannedObject (readonly)

Returns the value of attribute vac_banned.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def vac_banned
  @vac_banned
end

#visibility_stateObject (readonly)

Returns the value of attribute visibility_state.



20
21
22
# File 'lib/steam/community/steam_id.rb', line 20

def visibility_state
  @visibility_state
end

Class Method Details

.convert_community_id_to_steam_id(community_id) ⇒ Object

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



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/steam/community/steam_id.rb', line 28

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 SteamCondenserException.new("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) ⇒ Object

Converts the SteamID steam_id as reported by game servers to a 64bit SteamID



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/steam/community/steam_id.rb', line 43

def self.convert_steam_id_to_community_id(steam_id)
  if steam_id == 'STEAM_ID_LAN' or steam_id == 'BOT'
    raise SteamCondenserException.new("Cannot convert SteamID \"#{steam_id}\" to a community ID.")
  elsif steam_id.match(/^STEAM_[0-1]:[0-1]:[0-9]+$/).nil?
    raise SteamCondenserException.new("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) ⇒ Object

Creates a new SteamId object using the SteamID64 converted from a server SteamID given by steam_id



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

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

Instance Method Details

#base_urlObject

Returns the base URL for this SteamID



79
80
81
82
83
84
85
# File 'lib/steam/community/steam_id.rb', line 79

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 SteamID



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/steam/community/steam_id.rb', line 89

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 SteamCondenserException.new(profile.elements['error'].text)
  end

  @nickname         = profile.elements['steamID'].text
  @steam_id64       = profile.elements['steamID64'].text.to_i
  @vac_banned       = (profile.elements['vacBanned'].text == 1)

  unless REXML::XPath.first(profile, 'privacyMessage').nil?
    raise SteamCondenserException.new(profile.elements['privacyMessage'].text)
  end

  @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

  # Only public profiles can be scanned for further information
  if @privacy_state == 'public'
    @custom_url                       = profile.elements['customURL'].text.downcase
    @custom_url                       = nil if @custom_url.empty?

    # The favorite game cannot be set since 10/10/2008, but old profiles
    # still have this. May be removed in a future version.
    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                        = 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                        = profile.elements['realname'].text
    @steam_rating                     = profile.elements['steamRating'].text.to_f
    @summary                          = profile.elements['summary'].text

    # The most played games only exist if a user played at least one game in
    # the last two weeks
    @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, 'mostPlayedGames').nil?
      profile.elements.each('weblinks/weblink') do |link|
        @links[link.elements['title'].text] = link.elements['link'].text
      end
    end
  end

  super
end

#fetch_friendsObject

Fetches the friends of this user



159
160
161
162
163
164
165
166
167
# File 'lib/steam/community/steam_id.rb', line 159

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



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

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

  @games = {}
  games_data = REXML::Document.new(open(url, {:proxy => true}).read).root
  games_data.elements.each('games/game') do |game|
    game_name = game.elements['name'].text
    if game.elements['globalStatsLink'].nil?
      @games[game_name] = false
    else
      friendly_name = game.elements['globalStatsLink'].text.match(/http:\/\/steamcommunity.com\/stats\/([^?\/]+)\/achievements\//)[1]
      @games[game_name] = friendly_name.downcase
    end
  end

  true
end

#friendsObject

Returns an Array of SteamId representing all Steam Community friends of this user.



208
209
210
211
# File 'lib/steam/community/steam_id.rb', line 208

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

#full_avatar_urlObject

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



189
190
191
# File 'lib/steam/community/steam_id.rb', line 189

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

#game_stats(game_name) ⇒ Object

Returns a GameStats object for the given game for the owner of this SteamID



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

def game_stats(game_name)
  if games.has_value? game_name
    friendly_name = game_name
  elsif games.has_key? game_name.downcase
    friendly_name = games[game_name.downcase]
  else
    raise ArgumentError.new("Stats for game #{game_name} do not exist.")
  end

  GameStats.create_game_stats(@custom_url || @steam_id64, friendly_name)
end

#gamesObject

Returns a Hash with the games this user owns. The keys are the games’ names and the values are the “friendly names” used for stats or false if the games has no stats.



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

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

#icon_urlObject

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



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

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

#is_banned?Boolean

Returns whether the owner of this SteamID is VAC banned

Returns:

  • (Boolean)


227
228
229
# File 'lib/steam/community/steam_id.rb', line 227

def is_banned?
  @vac_banned
end

#is_in_game?Boolean

Returns whether the owner of this SteamId is playing a game

Returns:

  • (Boolean)


232
233
234
# File 'lib/steam/community/steam_id.rb', line 232

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)


237
238
239
# File 'lib/steam/community/steam_id.rb', line 237

def is_online?
  @online_state != 'offline'
end

#medium_avatar_urlObject

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



242
243
244
# File 'lib/steam/community/steam_id.rb', line 242

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