Leaderboard: submit() method

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

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

Syntax

submit(player, score)
submit(player, score, options)

Parameters

player
The player’s ID.
score
The score, a number.
options Optional

An object with any of these properties:

meta
Flat strings, numbers and booleans to keep with the entry, at most 1 KB.
once
A key that makes the submit safe to repeat, as for PlayerData.set().

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

The promise rejects with a DataError, such as E_UNKNOWN when the board isn’t declared or isn’t a leaderboard, or E_INVALID for a bot’s player ID.

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.

Submit scores from room code, which decides them, for any board that matters; a score a client submits can be faked.

Examples

Telling a player they set a weekly best

// best.ts
import { defineRoom } from "@irtio/room";

export default defineRoom({
  async onMessage(room, connection, message) {
    const m = message as { type?: string; points?: number };
    if (m.type !== "round-over" || typeof m.points !== "number") return;
    const kept = await room.leaderboard("points").submit(connection.player, m.points);
    const week = kept.find((p) => p.period.startsWith("week"));
    if (week?.score === m.points) room.send(connection, { type: "weekly-best", points: m.points });
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
submit()0.10.10.10.1