Class: GameStats
- Inherits:
-
Object
- Object
- GameStats
- Includes:
- XMLData
- 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
-
#game ⇒ SteamGame
readonly
Returns 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.
-
#user ⇒ SteamId
readonly
Returns the Steam ID of the player these stats belong to.
Class Method Summary collapse
-
.base_url(user_id, game_id) ⇒ Object
Returns the base Steam Communtiy URL for the given player and game IDs.
-
.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(user_id, game_id) ⇒ GameStats
constructor
Creates a ‘GameStats` object and fetches data from the Steam Community for the given user and game.
-
#public? ⇒ Boolean
Returns whether this Steam ID is publicly accessible.
Methods included from XMLData
Constructor Details
#initialize(user_id, game_id) ⇒ GameStats
Creates a ‘GameStats` object and fetches data from the Steam Community for the given user and game
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/steam/community/game_stats.rb', line 102 def initialize(user_id, game_id) parse "#{self.class.base_url(user_id, game_id)}?xml=all" @user = SteamId.new user_id, false error = @xml_data['error'] raise SteamCondenserError, error unless error.nil? @privacy_state = @xml_data['privacyState'] if public? app_id = @xml_data['game']['gameLink'].match(/http:\/\/steamcommunity\.com\/+app\/+([1-9][0-9]*)/)[1].to_i @game = SteamGame.new app_id, @xml_data['game'] @hours_played = @xml_data['stats']['hoursPlayed'] unless @xml_data['stats']['hoursPlayed'].nil? end end |
Instance Attribute Details
#game ⇒ SteamGame (readonly)
Returns the game these stats belong to
24 25 26 |
# File 'lib/steam/community/game_stats.rb', line 24 def game @game end |
#hours_played ⇒ String (readonly)
Returns the number of hours this game has been played by the player
29 30 31 |
# File 'lib/steam/community/game_stats.rb', line 29 def hours_played @hours_played end |
#privacy_state ⇒ String (readonly)
Returns the privacy setting of the Steam ID profile
34 35 36 |
# File 'lib/steam/community/game_stats.rb', line 34 def privacy_state @privacy_state end |
#user ⇒ SteamId (readonly)
Returns the Steam ID of the player these stats belong to
39 40 41 |
# File 'lib/steam/community/game_stats.rb', line 39 def user @user end |
Class Method Details
.base_url(user_id, game_id) ⇒ Object
Returns the base Steam Communtiy URL for the given player and game IDs
47 48 49 50 51 52 53 54 55 |
# File 'lib/steam/community/game_stats.rb', line 47 def self.base_url(user_id, game_id) game_url = game_id.is_a?(Fixnum) ? "appid/#{game_id}" : game_id if user_id.is_a? Fixnum "http://steamcommunity.com/profiles/#{user_id}/stats/#{game_url}" else "http://steamcommunity.com/id/#{user_id}/stats/#{game_url}" end end |
.create_game_stats(steam_id, game_name) ⇒ GameStats
Creates a ‘GameStats` (or one of its subclasses) instance for the given user and game
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 93 |
# File 'lib/steam/community/game_stats.rb', line 64 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.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/steam/community/game_stats.rb', line 123 def achievements return unless public? if @achievements.nil? @achievements = Array.new @xml_data['achievements']['achievement'].each do |achievement| @achievements << GameAchievement.new(@user, @game, achievement) 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.
145 146 147 148 |
# File 'lib/steam/community/game_stats.rb', line 145 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.
157 158 159 |
# File 'lib/steam/community/game_stats.rb', line 157 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
164 165 166 |
# File 'lib/steam/community/game_stats.rb', line 164 def base_url self.class.base_url @user.id, @game.id end |
#public? ⇒ Boolean
Returns whether this Steam ID is publicly accessible
171 172 173 |
# File 'lib/steam/community/game_stats.rb', line 171 def public? @privacy_state == 'public' end |