Class: SteamCondenser::Community::SteamGame

Inherits:
Object
  • Object
show all
Includes:
Cacheable
Defined in:
lib/steam-condenser/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, #cache_id_value, #cache_ids, #cached_instance, #fetch, #fetched?, included

Constructor Details

#initialize(app_id, game_data) ⇒ SteamGame (private)

Note:

The real constructor of SteamGame is new

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<Symbol, Object>, Hash<String, Object>)

    The JSON data of the game


154
155
156
157
158
159
# File 'lib/steam-condenser/community/steam_game.rb', line 154

def initialize(app_id, game_data)
  @app_id    = app_id
  @icon_hash = game_data[:img_icon_url]
  @logo_hash = game_data[:img_logo_url]
  @name      = game_data[:name]
end

Instance Attribute Details

#app_idFixnum (readonly) Also known as: id

Returns the Steam application ID of this game

Returns:

  • (Fixnum)

    The Steam application ID of this game


24
25
26
# File 'lib/steam-condenser/community/steam_game.rb', line 24

def app_id
  @app_id
end

#nameString (readonly)

Returns the full name of this game

Returns:

  • (String)

    The full name of this game


30
31
32
# File 'lib/steam-condenser/community/steam_game.rb', line 30

def name
  @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


37
38
39
40
41
42
43
44
45
46
# File 'lib/steam-condenser/community/steam_game.rb', line 37

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 SteamCondenser::Error, "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


52
53
54
55
56
57
58
59
60
# File 'lib/steam-condenser/community/steam_game.rb', line 52

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:


68
69
70
71
72
73
74
# File 'lib/steam-condenser/community/steam_game.rb', line 68

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

Instance Method Details

#icon_urlString

Returns the URL for the icon image of this game

Returns:

  • (String)

    The URL for the game icon


79
80
81
# File 'lib/steam-condenser/community/steam_game.rb', line 79

def icon_url
  "http://media.steampowered.com/steamcommunity/public/images/apps/#@app_id/#@icon_hash.jpg"
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:


88
89
90
# File 'lib/steam-condenser/community/steam_game.rb', line 88

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

#leaderboardsArray<GameLeaderboard>

Returns an array containing all of this game's leaderboards

Returns:


95
96
97
# File 'lib/steam-condenser/community/steam_game.rb', line 95

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


109
110
111
# File 'lib/steam-condenser/community/steam_game.rb', line 109

def logo_thumbnail_url
  "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


102
103
104
# File 'lib/steam-condenser/community/steam_game.rb', line 102

def logo_url
  "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


116
117
118
119
120
# File 'lib/steam-condenser/community/steam_game.rb', line 116

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

#store_urlString

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

Returns:

  • (String)

    This game's store page


125
126
127
# File 'lib/steam-condenser/community/steam_game.rb', line 125

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


133
134
135
# File 'lib/steam-condenser/community/steam_game.rb', line 133

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


142
143
144
# File 'lib/steam-condenser/community/steam_game.rb', line 142

def user_stats(steam_id)
  GameStats.new steam_id, @app_id
end