Class: SteamGame

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

Overview

This class represents a game available on Steam

Author:

  • Sebastian Staudt

Instance Attribute Summary collapse

Attributes included from Cacheable

#fetch_time

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cacheable

#cache, #fetch, #fetched?, included

Instance Attribute Details

#app_idFixnum (readonly)

Returns the Steam application ID of this game

Returns:

  • (Fixnum)

    The Steam application ID of this game



22
23
24
# File 'lib/steam/community/steam_game.rb', line 22

def app_id
  @app_id
end

#icon_urlString (readonly)

Returns the URL for the icon image of this game

Returns:

  • (String)

    The URL for the game icon



27
28
29
# File 'lib/steam/community/steam_game.rb', line 27

def icon_url
  @icon_url
end

#nameString (readonly)

Returns the full name of this game

Returns:

  • (String)

    The full name of this game



32
33
34
# File 'lib/steam/community/steam_game.rb', line 32

def name
  @name
end

#short_nameString (readonly)

Returns the short name of this game (also known as “friendly name”)

Returns:

  • (String)

    The short name of this game



37
38
39
# File 'lib/steam/community/steam_game.rb', line 37

def short_name
  @short_name
end

Class Method Details

.check_steam_inf(path) ⇒ Boolean

Checks if a game is up-to-date by reading information from a ‘steam.inf` file and comparing it using the Web API

Parameters:

  • path (String)

    The file system path of the ‘steam.inf` file

Returns:

  • (Boolean)

    ‘true` if the game is up-to-date



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

def self.check_steam_inf(path)
  steam_inf = File.read path
  begin
    app_id = steam_inf.match(/^\s*appID=(\d+)\s*$/im)[1].to_i
    version = steam_inf.match(/^\s*PatchVersion=([\d\.]+)\s*$/im)[1].gsub('.', '').to_i
  rescue
    raise SteamCondenserError, "The steam.inf file at \"#{path}\" is invalid."
  end
  uptodate? app_id, version
end

.new(app_id, game_data = nil) ⇒ Object

Creates a new instance of a game with the given data and caches it

Parameters:

  • app_id (Fixnum)

    The application ID of the game

  • game_data (Hash<String, Object>) (defaults to: nil)

    The XML data of the game



59
60
61
62
63
64
65
66
67
# File 'lib/steam/community/steam_game.rb', line 59

def self.new(app_id, game_data = nil)
  if cached? app_id
    class_variable_get(:@@cache)[app_id]
  else
    game = SteamGame.allocate
    game.send :initialize, app_id, game_data
    game
  end
end

.uptodate?(app_id, version) ⇒ Boolean

Returns whether the given version of the game with the given application ID is up-to-date

Parameters:

  • app_id (Fixnum)

    The application ID of the game to check

  • version (Fixnum)

    The version to check against the Web API

Returns:

  • (Boolean)

    ‘true` if the given version is up-to-date

Raises:



75
76
77
78
79
80
81
# File 'lib/steam/community/steam_game.rb', line 75

def self.uptodate?(app_id, version)
  params = { :appid => app_id, :version => version }
  result = WebApi.json 'ISteamApps', 'UpToDateCheck', 1, params
  result = MultiJson.load(result, { :symbolize_keys => true})[:response]
  raise SteamCondenserError, result[:error] unless result[:success]
  result[:up_to_date]
end

Instance Method Details

#has_stats?Boolean

Returns whether this game has statistics available

Returns:

  • (Boolean)

    ‘true` if this game has stats



96
97
98
# File 'lib/steam/community/steam_game.rb', line 96

def has_stats?
  !@short_name.nil?
end

#idFixnum, String

Returns a unique identifier for this game

This is either the numeric application ID or the unique short name

Returns:

  • (Fixnum, String)

    The application ID or short name of the game



105
106
107
# File 'lib/steam/community/steam_game.rb', line 105

def id
  @short_name == @app_id.to_s ? @app_id : @short_name
end

#leaderboard(id) ⇒ GameLeaderboard

Returns the leaderboard for this game and the given leaderboard ID or name

Parameters:

  • id (Fixnum, String)

    The ID or name of the leaderboard to return

Returns:



113
114
115
# File 'lib/steam/community/steam_game.rb', line 113

def leaderboard(id)
  GameLeaderboard.leaderboard @short_name, id
end

#leaderboardsArray<GameLeaderboard>

Returns an array containing all of this game’s leaderboards

Returns:



120
121
122
# File 'lib/steam/community/steam_game.rb', line 120

def leaderboards
  GameLeaderboard.leaderboards @short_name
end

#logo_thumbnail_urlString

Returns the URL for the logo thumbnail image of this game

Returns:

  • (String)

    The URL for the game logo thumbnail



135
136
137
138
# File 'lib/steam/community/steam_game.rb', line 135

def logo_thumbnail_url
  return nil if @logo_hash.nil?
  "http://media.steampowered.com/steamcommunity/public/images/apps/#@app_id/#{@logo_hash}_thumb.jpg"
end

#logo_urlString

Returns the URL for the logo image of this game

Returns:

  • (String)

    The URL for the game logo



127
128
129
130
# File 'lib/steam/community/steam_game.rb', line 127

def logo_url
  return nil if @logo_hash.nil?
  "http://media.steampowered.com/steamcommunity/public/images/apps/#@app_id/#@logo_hash.jpg"
end

#player_countFixnum

Returns the overall number of players currently playing this game

Returns:

  • (Fixnum)

    The number of players playing this game



86
87
88
89
90
91
# File 'lib/steam/community/steam_game.rb', line 86

def player_count
  params = { :appid => @app_id }
  result = WebApi.json 'ISteamUserStats', 'GetNumberOfCurrentPlayers', 1, params
  result = MultiJson.load(result, { :symbolize_keys => true})[:response]
  result[:player_count]
end

#store_urlString

Returns the URL of this game’s page in the Steam Store

Returns:

  • (String)

    This game’s store page



143
144
145
# File 'lib/steam/community/steam_game.rb', line 143

def store_url
  "http://store.steampowered.com/app/#@app_id"
end

#uptodate?(version) ⇒ Boolean

Returns whether the given version of this game is up-to-date

Parameters:

  • version (Fixnum)

    The version to check against the Web API

Returns:

  • (Boolean)

    ‘true` if the given version is up-to-date



151
152
153
# File 'lib/steam/community/steam_game.rb', line 151

def uptodate?(version)
  self.class.uptodate? @app_id, version
end

#user_stats(steam_id) ⇒ GameStats

Creates a stats object for the given user and this game

Parameters:

  • steam_id (String, Fixnum)

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

Returns:

  • (GameStats)

    The stats of this game for the given user



160
161
162
163
164
# File 'lib/steam/community/steam_game.rb', line 160

def user_stats(steam_id)
  return unless has_stats?

  GameStats.create_game_stats steam_id, @short_name
end