DataClient: leaderboard() method

Available since @irtio/client 5.0BrowsersNode

The leaderboard() method of the DataClient interface returns one of the project’s leaderboards: a collection declared with leaderboard settings in irt.config.ts.

Syntax

leaderboard(name)

Parameters

name
A string: the leaderboard’s name.

Return value

A ClientLeaderboard.

Exceptions

None. Its calls reject with a DataError; E_UNKNOWN means the board isn’t declared in the live build or isn’t a leaderboard.

Description

Reading a board needs its clientRead: true, and submitting a score its clientWrite: "own". The board’s periods, ranks and keep rule are as the Room API’s Leaderboard describes.

Examples

Showing the player’s rank

After a run, the client shows the player’s rank this week and the two players either side.

// my-rank.ts
import { dataClient } from "@irtio/client";

const laps = dataClient().leaderboard("laps");

export async function showRank() {
  const near = await laps.around({ period: "week" });
  if (!near) console.log("no lap this week yet");
  else for (const e of near.entries) console.log(`${e.rank}. ${e.player} ${e.score} ms`);
}

See also

Runtime availability

BrowsersNode
leaderboard()5.05.0