DataClient: player() method
Available since
@irtio/client 5.0BrowsersNodeThe player() method of the DataClient interface resolves with the player ID the client’s calls are made as. Without a token, it gets the client a guest first if it has none.
Syntax
player()
Parameters
None.
Return value
A Promise that resolves with the player ID, a string: the sub claim of the client’s token, or guest:<random> for a guest.
Exceptions
DataError- Rejects with this with code
E_FORBIDDENif the token isn’t a JSON Web Token (JWT) with asubclaim, or if the project doesn’t allow guests;E_LIMITif the router has minted 30 guests in the last minute for this address and project; andE_UNAVAILABLEif the router can’t be reached or doesn’t mint a guest.
Description
A guest’s token is the one connect() keeps, so player() gives the same ID as room.player of a room the guest joins, with the same guest option.
Examples
Highlighting the player’s own score
The board marks the player’s own entry, found by their player ID.
// highlight-me.ts
import { dataClient } from "@irtio/client";
const data = dataClient();
export async function drawBoard() {
const [me, { entries }] = await Promise.all([data.player(), data.leaderboard("points").top({ limit: 10 })]);
for (const e of entries) console.log(`${e.player === me ? ">" : " "} ${e.rank}. ${e.score}`);
}
See also
Runtime availability
| Browsers | Node | |
|---|---|---|
player() | 5.0 | 5.0 |