Class: GameStats
- Inherits:
-
Object
- Object
- GameStats
- Defined in:
- lib/steam/community/game_stats.rb
Overview
This class represents the game statistics for a single user and a specific game
It is subclassed for individual games if the games provide special statistics that are unique to this game.
Direct Known Subclasses
AlienSwarmStats, CSSStats, DefenseGridStats, DoDSStats, L4D2Stats, L4DStats, Portal2Stats, TF2Stats
Instance Attribute Summary collapse
-
#app_id ⇒ Fixnum
readonly
Returns the Steam application ID of the game these stats belong to.
-
#custom_url ⇒ String
readonly
Returns the custom URL of the player these stats belong to.
-
#game_friendly_name ⇒ String
readonly
Returns the friendly name of the game these stats belong to.
-
#game_name ⇒ String
readonly
Returns the full name of the game these stats belong to.
-
#hours_played ⇒ String
readonly
Returns the number of hours this game has been played by the player.
-
#privacy_state ⇒ String
readonly
Returns the privacy setting of the Steam ID profile.
-
#steam_id64 ⇒ Fixnum
readonly
Returns the 64bit numeric SteamID of the player these stats belong to.
Class Method Summary collapse
-
.create_game_stats(steam_id, game_name) ⇒ GameStats
Creates a ‘GameStats` (or one of its subclasses) instance for the given user and game.
Instance Method Summary collapse
-
#achievements ⇒ Array<GameAchievement>
Returns the achievements for this stats’ user and game.
-
#achievements_done ⇒ Fixnum
Returns the number of achievements done by this player.
-
#achievements_percentage ⇒ Float
Returns the percentage of achievements done by this player.
-
#base_url ⇒ String
Returns the base Steam Communtiy URL for the stats contained in this object.
-
#initialize(id, game_name) ⇒ GameStats
constructor
Creates a ‘GameStats` object and fetches data from the Steam Community for the given user and game.
-
#leaderboard(id) ⇒ GameLeaderboard
Returns the leaderboard for this game and the given leaderboard ID or name.
-
#leaderboards ⇒ Array<GameLeaderboard>
Returns an array containing all of this game’s leaderboards.
-
#public? ⇒ Boolean
Returns whether this Steam ID is publicly accessible.
Constructor Details
#initialize(id, game_name) ⇒ GameStats
Creates a ‘GameStats` object and fetches data from the Steam Community for the given user and game
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/steam/community/game_stats.rb', line 101 def initialize(id, game_name) if id.is_a? Numeric @steam_id64 = id else @custom_url = id.downcase end @game_friendly_name = game_name url = base_url + '?xml=all' @xml_data = REXML::Document.new(open(url, {:proxy => true}).read).root error = @xml_data.elements['error'] raise SteamCondenserError, error.text unless error.nil? @privacy_state = @xml_data.elements['privacyState'].text if public? @app_id = @xml_data.elements['game/gameLink'].text.match(/http:\/\/store.steampowered.com\/app\/([1-9][0-9]+)/)[1].to_i @custom_url = @xml_data.elements['player/customURL'].text if @custom_url.nil? @game_name = @xml_data.elements['game/gameName'].text @hours_played = @xml_data.elements['stats/hoursPlayed'].text unless @xml_data.elements['stats/hoursPlayed'].nil? @steam_id64 = @xml_data.elements['player/steamID64'].text.to_i if @steam_id64.nil? end end |
Instance Attribute Details
#app_id ⇒ Fixnum (readonly)
Returns the Steam application ID of the game these stats belong to
24 25 26 |
# File 'lib/steam/community/game_stats.rb', line 24 def app_id @app_id end |
#custom_url ⇒ String (readonly)
Returns the custom URL of the player these stats belong to
29 30 31 |
# File 'lib/steam/community/game_stats.rb', line 29 def custom_url @custom_url end |
#game_friendly_name ⇒ String (readonly)
Returns the friendly name of the game these stats belong to
34 35 36 |
# File 'lib/steam/community/game_stats.rb', line 34 def game_friendly_name @game_friendly_name end |
#game_name ⇒ String (readonly)
Returns the full name of the game these stats belong to
39 40 41 |
# File 'lib/steam/community/game_stats.rb', line 39 def game_name @game_name end |
#hours_played ⇒ String (readonly)
Returns the number of hours this game has been played by the player
44 45 46 |
# File 'lib/steam/community/game_stats.rb', line 44 def hours_played @hours_played end |
#privacy_state ⇒ String (readonly)
Returns the privacy setting of the Steam ID profile
49 50 51 |
# File 'lib/steam/community/game_stats.rb', line 49 def privacy_state @privacy_state end |
#steam_id64 ⇒ Fixnum (readonly)
Returns the 64bit numeric SteamID of the player these stats belong to
54 55 56 |
# File 'lib/steam/community/game_stats.rb', line 54 def steam_id64 @steam_id64 end |
Class Method Details
.create_game_stats(steam_id, game_name) ⇒ GameStats
Creates a ‘GameStats` (or one of its subclasses) instance for the given user and game
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/steam/community/game_stats.rb', line 63 def self.create_game_stats(steam_id, game_name) case game_name when 'alienswarm' require 'steam/community/alien_swarm/alien_swarm_stats' AlienSwarmStats.new(steam_id) when 'cs:s' require 'steam/community/css/css_stats' CSSStats.new(steam_id) when 'defensegrid:awakening' require 'steam/community/defense_grid/defense_grid_stats' DefenseGridStats.new(steam_id) when 'dod:s' require 'steam/community/dods/dods_stats' DoDSStats.new(steam_id) when 'l4d' require 'steam/community/l4d/l4d_stats' L4DStats.new(steam_id) when 'l4d2' require 'steam/community/l4d/l4d2_stats' L4D2Stats.new(steam_id) when 'portal2' require 'steam/community/portal2/portal2_stats' Portal2Stats.new steam_id when 'tf2' require 'steam/community/tf2/tf2_stats' TF2Stats.new(steam_id) else new(steam_id, game_name) end end |
Instance Method Details
#achievements ⇒ Array<GameAchievement>
Returns the achievements for this stats’ user and game
If the achievements’ data hasn’t been parsed yet, parsing is done now.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/steam/community/game_stats.rb', line 130 def achievements return unless public? if @achievements.nil? @achievements = Array.new @xml_data.elements.each('achievements/achievement') do |achievement_data| @achievements << GameAchievement.new(@steam_id64, @app_id, achievement_data) end @achievements_done = @achievements.reject{ |a| !a.unlocked? }.size end @achievements end |
#achievements_done ⇒ Fixnum
Returns the number of achievements done by this player
If achievements haven’t been parsed yet for this player and this game, parsing is done now.
152 153 154 155 |
# File 'lib/steam/community/game_stats.rb', line 152 def achievements_done achievements if @achievements_done.nil? @achievements_done end |
#achievements_percentage ⇒ Float
Returns the percentage of achievements done by this player
If achievements haven’t been parsed yet for this player and this game, parsing is done now.
164 165 166 |
# File 'lib/steam/community/game_stats.rb', line 164 def achievements_percentage achievements_done.to_f / @achievements.size end |
#base_url ⇒ String
Returns the base Steam Communtiy URL for the stats contained in this object
171 172 173 174 175 176 177 |
# File 'lib/steam/community/game_stats.rb', line 171 def base_url if @custom_url.nil? "http://steamcommunity.com/profiles/#{@steam_id64}/stats/#{@game_friendly_name}" else "http://steamcommunity.com/id/#{@custom_url}/stats/#{@game_friendly_name}" end end |
#leaderboard(id) ⇒ GameLeaderboard
Returns the leaderboard for this game and the given leaderboard ID or name
183 184 185 |
# File 'lib/steam/community/game_stats.rb', line 183 def leaderboard(id) GameLeaderboard.leaderboard @game_friendly_name, id end |
#leaderboards ⇒ Array<GameLeaderboard>
Returns an array containing all of this game’s leaderboards
190 191 192 |
# File 'lib/steam/community/game_stats.rb', line 190 def leaderboards GameLeaderboard.leaderboards @game_friendly_name end |
#public? ⇒ Boolean
Returns whether this Steam ID is publicly accessible
197 198 199 |
# File 'lib/steam/community/game_stats.rb', line 197 def public? @privacy_state == 'public' end |