ClientLeaderboard: top() method
Available since
@irtio/client 5.0BrowsersNodeThe top() method of the ClientLeaderboard interface resolves with a page of the best entries of a period, best first.
Syntax
top()
top(options)
Parameters
optionsOptional-
An object with any of these properties:
period- A string: the period, as the Description section of
Leaderboarddescribes. Defaults to"all". limit- A number: the most entries in the page, from 1 to 100. Defaults to 20.
after- A string or
null: thenextof the page before, for the page after it. signal- An
AbortSignalthat aborts this call.
Return value
A Promise that resolves with { entries, next }. next is null on the last page. Each entry is an object with these properties:
rank- Its rank, from 1.
player- The player’s ID.
score- The score the period keeps for the player.
meta- The
metathe last submit gave. updated- When the entry was last written, in wall-clock milliseconds.
Exceptions
DataError- Rejects with this if the read fails, such as with
E_FORBIDDENwhen the board doesn’t haveclientRead: true,E_INVALIDfor alimitout of range orE_UNKNOWNwhen the board isn’t declared. An aborted call rejects withE_UNAVAILABLE, namedAbortError, orTimeoutErrorfor anAbortSignal.timeout(), with the signal’s reason as itscause.
Description
top() goes without a token until the client has one. With the project’s players.readLeaderboardsWithoutToken on, it needs no guest, so a start screen can show a board without making one; otherwise the router’s 401 gets the client a guest first.
top() is never cached, so call it on an event or every few seconds rather than every frame.
Examples
Paging through the all-time board
A “more” button loads the next 20 entries of the all-time board, starting again from the top once it reaches the end.
// all-time.ts
import { dataClient } from "@irtio/client";
const board = dataClient().leaderboard("highscores");
let after: string | null = null;
export async function nextPage() {
const page = await board.top({ limit: 20, after });
after = page.next;
return page.entries.map((e) => `${e.rank}. ${e.player} ${e.score}`);
}
See also
ClientLeaderboard.around()Leaderboard.top(), the same from room code- Player data, collections and leaderboards
Runtime availability
| Browsers | Node | |
|---|---|---|
top() | 5.0 | 5.0 |