Class: SteamCondenser::Community::GameLeaderboard

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

Overview

The GameLeaderboard class represents a single leaderboard for a specific game

Author:

  • Sebastian Staudt

Constant Summary collapse

DISPLAY_TYPE_NONE =
0
DISPLAY_TYPE_NUMERIC =
1
DISPLAY_TYPE_SECONDS =
2
DISPLAY_TYPE_MILLISECONDS =
3
SORT_METHOD_NONE =
0
SORT_METHOD_ASC =
1
SORT_METHOD_DESC =
2
@@leaderboards =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XMLData

#parse

Constructor Details

#initialize(board_data) ⇒ GameLeaderboard (private)

Creates a new leaderboard instance with the given XML data

Parameters:

  • board_data (Hash<String, Object>)

    The XML data of the leaderboard


170
171
172
173
174
175
176
177
# File 'lib/steam-condenser/community/game_leaderboard.rb', line 170

def initialize(board_data)
  @url          = board_data['url']
  @id           = board_data['lbid'].to_i
  @name         = board_data['name']
  @entry_count  = board_data['entries'].to_i
  @sort_method  = board_data['sortmethod'].to_i
  @display_type = board_data['displaytype'].to_i
end

Instance Attribute Details

#display_typeFixnum (readonly)

Returns the display type of the scores on this leaderboard

Returns:

  • (Fixnum)

    The display type of the scores


36
37
38
# File 'lib/steam-condenser/community/game_leaderboard.rb', line 36

def display_type
  @display_type
end

#entry_countFixnum (readonly)

Returns the number of entries on this leaderboard

Returns:

  • (Fixnum)

    The number of entries on this leaderboard


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

def entry_count
  @entry_count
end

#idFixnum (readonly)

Returns the ID of the leaderboard

Returns:

  • (Fixnum)

    The ID of the leaderboard


46
47
48
# File 'lib/steam-condenser/community/game_leaderboard.rb', line 46

def id
  @id
end

#nameString (readonly)

Returns the name of the leaderboard

Returns:

  • (String)

    The name of the leaderboard


51
52
53
# File 'lib/steam-condenser/community/game_leaderboard.rb', line 51

def name
  @name
end

#sort_methodFixnum (readonly)

Returns the method that is used to sort the entries on the leaderboard

Returns:

  • (Fixnum)

    The sort method


56
57
58
# File 'lib/steam-condenser/community/game_leaderboard.rb', line 56

def sort_method
  @sort_method
end

Class Method Details

.leaderboard(app_id, id) ⇒ GameLeaderboard

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

Parameters:

  • app_id (Fixnum)

    The application ID of the game

  • id (Fixnum, String)

    The ID or name of the leaderboard to return

Returns:


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/steam-condenser/community/game_leaderboard.rb', line 63

def self.leaderboard(app_id, id)
  leaderboards = self.leaderboards app_id

  if id.is_a? Fixnum
    leaderboards[id]
  else
    leaderboards.each_value do |board|
      return board if board.name == id
    end
  end
end

.leaderboards(app_id) ⇒ Array<GameLeaderboard>

Returns an array containing all of a game's leaderboards

Parameters:

  • app_id (Fixnum)

    The application ID of the game

Returns:


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

def self.leaderboards(app_id)
  self.load_leaderboards app_id unless @@leaderboards.key? app_id

  @@leaderboards[app_id]
end

.load_leaderboards(app_id) ⇒ Object (private)

Loads the leaderboards of the specified games into the cache

Parameters:

  • app_id (Fixnum)

    The application ID of the game

Raises:

  • (SteamCondenserException)

    if an error occurs while fetching the leaderboards


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/steam-condenser/community/game_leaderboard.rb', line 197

def self.load_leaderboards(app_id)
  begin
    url = "http://steamcommunity.com/stats/#{app_id}/leaderboards/?xml=1"
    boards_data = MultiXml.parse(open(url, {:proxy => true})).values.first
  rescue
    raise SteamCondenser::Error.new 'XML data could not be parsed.', $!
  end

  error = boards_data['error']
  raise SteamCondenser::Error, error unless error.nil?

  @@leaderboards[app_id] = {}
  boards_data['leaderboard'].each do |board_data|
    leaderboard = GameLeaderboard.new board_data
    @@leaderboards[app_id][leaderboard.id] = leaderboard
  end
end

Instance Method Details

#entry_for_steam_id(steam_id) ⇒ GameLeaderboardEntry

Returns the entry on this leaderboard for the user with the given SteamID

raise [SteamCondenserException] if an error occurs while fetching the leaderboard

Parameters:

  • steam_id (Fixnum, SteamId)

    The 64bit SteamID or the SteamId object of the user

Returns:

Raises:


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/steam-condenser/community/game_leaderboard.rb', line 92

def entry_for_steam_id(steam_id)
  steam_id = steam_id.steam_id64 if steam_id.is_a? SteamId

  parse "#@url&steamid=#{steam_id}"

  error = @xml_data['error']
  raise SteamCondenser::Error, error unless error.nil?

  @xml_data['entries']['entry'].each do |entry_data|
    if entry_data['steamid'].to_i == steam_id
      return GameLeaderboardEntry.new entry_data, self
    end
  end

  nil
end

#entry_for_steam_id_friends(steam_id) ⇒ Array<GameLeaderboardEntry>

Returns an array of entries on this leaderboard for the user with the given SteamID and his/her friends

raise [SteamCondenserException] if an error occurs while fetching the leaderboard

Parameters:

  • steam_id (Fixnum, SteamId)

    The 64bit SteamID or the SteamId object of the user

Returns:

Raises:


118
119
120
121
122
123
124
125
126
127
# File 'lib/steam-condenser/community/game_leaderboard.rb', line 118

def entry_for_steam_id_friends(steam_id)
  steam_id = steam_id.steam_id64 if steam_id.is_a? SteamId

  parse "#@url&steamid=#{steam_id}"

  error = @xml_data['error']
  raise SteamCondenser::Error, error unless error.nil?

  parse_entries
end

#entry_range(first, last) ⇒ Array<GameLeaderboardEntry>

Returns the entries on this leaderboard for a given rank range

The range is inclusive and a maximum of 5001 entries can be returned in a single request.

raise [SteamCondenserException] if an error occurs while fetching the leaderboard

Parameters:

  • first (Fixnum)

    The first entry to return from the leaderboard

  • last (Fixnum)

    The last entry to return from the leaderboard

Returns:

Raises:


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/steam-condenser/community/game_leaderboard.rb', line 140

def entry_range(first, last)
  if last < first
    raise SteamCondenser::Error,
      'First entry must be prior to last entry for leaderboard entry lookup.'
  end

  if (last - first) > 5000
    raise SteamCondenser::Error,
      'Leaderboard entry lookup is currently limited to a maximum of 5001 entries per request.'
  end

  parse "#@url&start=#{first}&end=#{last}"

  error = @xml_data['error']
  raise SteamCondenser::Error, error unless error.nil?

  entries = []
  @xml_data['entries']['entry'].each do |entry_data|
    rank = entry_data['rank'].to_i
    entries[rank] = GameLeaderboardEntry.new entry_data, self
  end

  parse_entries
end

#parse_entriesArray<GameLeaderboardEntry> (private)

Parses all entries available in the XML data

Returns:


182
183
184
185
186
187
188
189
190
# File 'lib/steam-condenser/community/game_leaderboard.rb', line 182

def parse_entries
  entries = []
  @xml_data['entries']['entry'].each do |entry_data|
    entries[entry_data['rank'].to_i] =
      GameLeaderboardEntry.new entry_data, self
  end

  entries
end