ClientLeaderboard: submit() method

Available since @irtio/client 5.0BrowsersNode

The submit() method of the ClientLeaderboard interface applies the board’s keep rule to this player’s entry in every period the board keeps, and resolves with what each period keeps.

Syntax

submit(score)
submit(score, options)

Parameters

score
A number: the score.
options Optional

An object with any of these properties:

meta
An object whose values are strings, numbers and booleans: data to keep with the entry, at most 1 KB.
signal
An AbortSignal that aborts this call.

Return value

A Promise that resolves with an array of { period, score }, one for each period the board keeps, with the score the entry holds there now.

Exceptions

DataError
Rejects with this if the submit fails, such as with E_FORBIDDEN when the board doesn’t have clientWrite: "own", E_UNKNOWN when the board isn’t declared or isn’t a leaderboard, or E_INVALID for meta over 1 KB or not flat. An aborted call rejects with E_UNAVAILABLE, named AbortError, or TimeoutError for an AbortSignal.timeout(), with the signal’s reason as its cause.

Description

The board’s keep setting decides what each period’s entry holds: "best" keeps the better of the old and new score by the board’s order, "latest" the new one, and "sum" adds them.

The score is always this player’s. Without a token, the client gets a guest first if it has none.

Warning: Any player can submit any number from a client. For a board that matters, submit from room code with Leaderboard.submit(), which decides the score.

Examples

Submitting a daily puzzle’s moves

A daily puzzle with a board kept as "best" submits the player’s move count with the puzzle’s number, and tells them when it’s their best today.

// puzzle-moves.ts
import { dataClient } from "@irtio/client";

const board = dataClient().leaderboard("puzzle-moves");

export async function solved(moves: number, puzzle: number) {
  const kept = await board.submit(moves, { meta: { puzzle } });
  const today = kept.find((p) => p.period.startsWith("day"));
  if (today?.score === moves) console.log(`a new best today: ${moves} moves`);
}

See also

Runtime availability

BrowsersNode
submit()5.05.0