ClientLeaderboard: around() method

Available since @irtio/client 5.0BrowsersNode

The around() method of the ClientLeaderboard interface resolves with a player’s rank in a period and the entries either side of theirs, or undefined when the player has no entry there. The player is this client’s unless you name another.

Syntax

around()
around(options)

Parameters

options Optional

An object with any of these properties:

player
A string: the player’s ID. Defaults to the player the client is.
period
A string: the period, as the Description section of Leaderboard describes. Defaults to "all".
count
A number: how many entries to give on each side of the player’s, from 0 to 10. Defaults to 2.
signal
An AbortSignal that aborts this call.

Return value

A Promise that resolves with { rank, entries }, or undefined. rank is the player’s rank, and entries holds the player’s entry with up to count on each side, each as top() gives them.

Exceptions

DataError
Rejects with this if the read fails, such as with E_FORBIDDEN when the board doesn’t have clientRead: true, E_INVALID for a count out of range or E_UNKNOWN when the board isn’t declared. An aborted call rejects with E_UNAVAILABLE, named AbortError, or TimeoutError for an AbortSignal.timeout(), with the signal’s reason as its cause.

Description

Without a player, around() needs the client’s player ID, so a client without a token gets a guest first. With a player, it goes without a token until the client has one, as top() does, and with the project’s players.readLeaderboardsWithoutToken on it needs no guest.

around() is never cached.

Examples

Showing a friend’s rank

A profile screen shows where a friend stands this month, and says so when they haven’t played.

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

const board = dataClient().leaderboard("points");

export async function friendRank(friend: string) {
  const near = await board.around({ player: friend, period: "month", count: 1 });
  return near ? `#${near.rank} this month` : "no games this month";
}

See also

Runtime availability

BrowsersNode
around()5.05.0