Class: GameLeaderboard

Inherits:
Object
  • Object
show all
Defined in:
lib/steam/community/game_leaderboard.rb

Overview

The GameLeaderboard class represents a single leaderboard for a specific game

Author:

  • Sebastian Staudt

Constant Summary collapse

LEADERBOARD_DISPLAY_TYPE_NONE =
0
LEADERBOARD_DISPLAY_TYPE_NUMERIC =
1
LEADERBOARD_DISPLAY_TYPE_SECONDS =
2
LEADERBOARD_DISPLAY_TYPE_MILLISECONDS =
3
LEADERBOARD_SORT_METHOD_NONE =
0
LEADERBOARD_SORT_METHOD_ASC =
1
LEADERBOARD_SORT_METHOD_DESC =
2
@@leaderboards =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#display_typeFixnum (readonly)

Returns the display type of the scores on this leaderboard

Returns:

  • (Fixnum)

    The display type of the scores



30
31
32
# File 'lib/steam/community/game_leaderboard.rb', line 30

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



35
36
37
# File 'lib/steam/community/game_leaderboard.rb', line 35

def entry_count
  @entry_count
end

#idFixnum (readonly)

Returns the ID of the leaderboard

Returns:

  • (Fixnum)

    The ID of the leaderboard



40
41
42
# File 'lib/steam/community/game_leaderboard.rb', line 40

def id
  @id
end

#nameString (readonly)

Returns the name of the leaderboard

Returns:

  • (String)

    The name of the leaderboard



45
46
47
# File 'lib/steam/community/game_leaderboard.rb', line 45

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



50
51
52
# File 'lib/steam/community/game_leaderboard.rb', line 50

def sort_method
  @sort_method
end

Class Method Details

.leaderboard(game_name, id) ⇒ GameLeaderboard

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

Parameters:

  • game_name (String)

    The short name of the game

  • id (Fixnum, String)

    The ID or name of the leaderboard to return

Returns:



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

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

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

.leaderboards(game_name) ⇒ Array<GameLeaderboard>

Returns an array containing all of a game’s leaderboards

Parameters:

  • game_name (String)

    The name of the game

Returns:



73
74
75
76
77
# File 'lib/steam/community/game_leaderboard.rb', line 73

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

  @@leaderboards[game_name]
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:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/steam/community/game_leaderboard.rb', line 86

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

  url = "#@url&steamid=#{steam_id}"
  xml_data = MultiXml.parse(open(url, {:proxy => true})).values.first

  error = xml_data['error']
  raise SteamCondenserError, 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:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/steam/community/game_leaderboard.rb', line 113

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

  url = "#@url&steamid=#{steam_id}"
  xml_data = MultiXml.parse(open(url, {:proxy => true})).values.first

  error = xml_data['error']
  raise SteamCondenserError, 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

  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:



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

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

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

  url = "#@url&start=#{first}&end=#{last}"
  xml_data = MultiXml.parse(open(url, {:proxy => true})).values.first

  error = xml_data['error']
  raise SteamCondenserError, 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

  entries
end