Class: SteamCondenser::Community::GameStats

Inherits:
Object
  • Object
show all
Includes:
Cacheable
Defined in:
lib/steam-condenser/community/game_stats.rb

Overview

This class represents the game statistics for a single user and a specific game

Author:

  • Sebastian Staudt

Instance Attribute Summary collapse

Attributes included from Cacheable

#fetch_time

Instance Method Summary collapse

Methods included from Cacheable

#cache, #cache_id_value, #cache_ids, #cached_instance, #fetch, #fetched?, included

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

Parameters:

  • user_id (String, Fixnum)

    The custom URL or the 64bit Steam ID of the user

  • game_id (String)

    The application ID or friendly name of the game

Raises:

  • (Error)

    if the stats cannot be fetched


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/steam-condenser/community/game_stats.rb', line 50

def initialize(user_id, game_id)
  if user_id.is_a? String
    user_id = SteamId.resolve_vanity_url user_id
  end

  @schema = GameStatsSchema.new game_id
  @user   = SteamId.new user_id, false

  @app_id     = game_id
  @steam_id64 = @user.steam_id64

  params = { appid: @schema.app_id, steamid: @user.steam_id64 }
  data = WebApi.json 'ISteamUserStats', 'GetUserStatsForGame', 2, params

  @achievements = []
  data[:playerstats][:achievements].each do |achievement|
    api_name = achievement[:name]
    unlocked = achievement[:achieved] == 1
    @achievements << @schema.achievement(api_name).instance(@user, unlocked)
  end

  @values = []
  data[:playerstats][:stats].each do |datum|
    @values << @schema.datum(datum[:name]).instance(@user, datum[:value])
  end
end

Instance Attribute Details

#achievementsArray<GameAchievement::Instance> (readonly)

Returns the achievements for this stats' user and game

If the achievements' data hasn't been parsed yet, parsing is done now.

Returns:


28
29
30
# File 'lib/steam-condenser/community/game_stats.rb', line 28

def achievements
  @achievements
end

#schemaGameStatsSchema (readonly)

Returns the schema of the game these stats belong to

Returns:


38
39
40
# File 'lib/steam-condenser/community/game_stats.rb', line 38

def schema
  @schema
end

#userSteamId (readonly)

Returns the Steam ID of the player these stats belong to

Returns:

  • (SteamId)

    The Steam ID instance of the player


33
34
35
# File 'lib/steam-condenser/community/game_stats.rb', line 33

def user
  @user
end

#valuesArray<GameStatsValue::Instance> (readonly)

Returns:

  • (Array<GameStatsValue::Instance>)

41
42
43
# File 'lib/steam-condenser/community/game_stats.rb', line 41

def values
  @values
end

Instance Method Details

#achievements_doneFixnum

Returns the number of achievements done by this player

Returns:

  • (Fixnum)

    The number of achievements completed

See Also:


81
82
83
# File 'lib/steam-condenser/community/game_stats.rb', line 81

def achievements_done
  @achievements.size
end

#achievements_percentageFloat

Returns the percentage of achievements done by this player

Returns:

  • (Float)

    The percentage of achievements completed

See Also:


89
90
91
# File 'lib/steam-condenser/community/game_stats.rb', line 89

def achievements_percentage
  @achievements.size.to_f / @schema.achievements.size
end

#inspectObject


93
94
95
# File 'lib/steam-condenser/community/game_stats.rb', line 93

def inspect
  "#<#{self.class}:#{@user.id} \"#{@schema.app_name}\">"
end