Class: GameLeaderboard

Inherits:
Object
  • Object
show all
Includes:
XMLData
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

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

Instance Attribute Details

#display_typeFixnum (readonly)

Returns the display type of the scores on this leaderboard

Returns:

  • (Fixnum)

    The display type of the scores



33
34
35
# File 'lib/steam/community/game_leaderboard.rb', line 33

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



38
39
40
# File 'lib/steam/community/game_leaderboard.rb', line 38

def entry_count
  @entry_count
end

#idFixnum (readonly)

Returns the ID of the leaderboard

Returns:

  • (Fixnum)

    The ID of the leaderboard



43
44
45
# File 'lib/steam/community/game_leaderboard.rb', line 43

def id
  @id
end

#nameString (readonly)

Returns the name of the leaderboard

Returns:

  • (String)

    The name of the leaderboard



48
49
50
# File 'lib/steam/community/game_leaderboard.rb', line 48

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



53
54
55
# File 'lib/steam/community/game_leaderboard.rb', line 53

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:



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/steam/community/game_leaderboard.rb', line 60

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:



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

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:



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

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 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:



115
116
117
118
119
120
121
122
123
124
# File 'lib/steam/community/game_leaderboard.rb', line 115

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 SteamCondenserError, 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:



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

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

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

  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

  parse_entries
end