Skip to content

OrderedDataStore

A data store whose number values you can rank, for leaderboards.

An ordered data store has everything a DataStore has, plus getSorted. Only number values are ranked; other values are skipped. Keys are usually user ids, so keep players’ display names in a second store if you want to show them.

getSorted(options?: { ascending?: boolean; limit?: number }): Promise<{ key: string; value: number }[]>

Up to limit entries (1 to 100, default 10) with number values, highest first, or lowest first with ascending: true. Entries with equal values are ordered by key.

Inherited from DataStore: get, set, update, increment, remove

import { DataStoreService, Player, print } from '@rally/sdk';
const best = DataStoreService.getOrderedDataStore('BestTimes');
const names = DataStoreService.getDataStore('Names');
async function finish(player: Player, seconds: number) {
await names.set(player.userId, player.displayName);
await best.update<number>(player.userId, (old) =>
old === undefined || seconds < old ? seconds : undefined, // keep the fastest
);
const top = await best.getSorted({ ascending: true, limit: 10 });
for (const [i, row] of top.entries()) {
print(`${i + 1}. ${(await names.get<string>(row.key)) ?? '?'}: ${row.value}s`);
}
}
// Call finish(player, seconds) when a player reaches the goal.