RoomClient: startCountdown() method

Available since @irtio/client 5.0BrowsersNode

The startCountdown() method of the RoomClient interface starts a lobby’s countdown, as the lobby’s leader. It’s the lobby’s call start.

Syntax

startCountdown()

Parameters

None.

Return value

A Promise that resolves with undefined once the countdown has started.

Exceptions

IrtError

Rejects with this if the call fails. Its name says why:

OperationError
The lobby refused, and its reason says why: this player isn’t the leader, the lobby has fewer people and bots than its minPlayers, or it’s already counting down or handing off. In a room that isn’t a lobby, the room has no start call to answer.
TimeoutError
The lobby didn’t answer within 10 seconds.
NetworkError
The connection dropped before the answer, or the client is reconnecting.
InvalidStateError
The client hasn’t been ready yet, or it’s closed.

Description

The leader can start the countdown under any of the lobby’s start rules, whoever is ready. Under "leader", it’s the only way the countdown starts. The leader is room.presence.leader, and the presence list’s leader event fires when it changes. When the countdown ends, the lobby hands its players off to the room it starts; see Lobbies and parties.

room.lobby changes to the "countdown" phase, with startsAt set, and the lobby event fires.

Examples

Showing a start button to the leader

The button shows only to the leader while the lobby waits. When the lobby refuses, the player sees its reason.

// start-button.ts
import { connect, IrtError } from "@irtio/client";

const room = await connect("lobby", { type: "lobby" });
const button = document.querySelector<HTMLButtonElement>("#start")!;

function show() {
  button.hidden = room.presence.leader !== room.player || room.lobby?.phase !== "waiting";
}
room.addEventListener("lobby", show);
room.presence.addEventListener("leader", show);

button.addEventListener("click", async () => {
  try {
    await room.startCountdown();
  } catch (e) {
    // Such as fewer players than the lobby's minPlayers
    if (e instanceof IrtError && e.name === "OperationError") console.warn(e.reason);
    else throw e;
  }
});

See also

Runtime availability

BrowsersNode
startCountdown()5.05.0