@openleaderboard/sdk 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +24 -1
- package/dist/index.js +15 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -30,6 +30,13 @@ export interface QueryOpts {
|
|
|
30
30
|
export interface SubmitOpts {
|
|
31
31
|
segments?: string[];
|
|
32
32
|
idem?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Event time of the score. Determines which time-window bucket it lands in
|
|
35
|
+
* (e.g. the daily board) — set it to the session start so a run that crosses
|
|
36
|
+
* midnight counts for the day it began, rather than when it was submitted.
|
|
37
|
+
* Accepts a Date or an ISO-8601 string; defaults to server receive time.
|
|
38
|
+
*/
|
|
39
|
+
time?: Date | string;
|
|
33
40
|
}
|
|
34
41
|
export interface WindowDef {
|
|
35
42
|
kind: string;
|
|
@@ -40,6 +47,15 @@ export interface BoardDef {
|
|
|
40
47
|
updatePolicy?: "best" | "last" | "increment";
|
|
41
48
|
tieBreak?: "lexical" | "firstToReach";
|
|
42
49
|
windows?: WindowDef[];
|
|
50
|
+
/**
|
|
51
|
+
* Enable the approximate-rank tier (a score histogram) so {@link
|
|
52
|
+
* LeaderboardClient.getApproxRank} can estimate rank in O(buckets) on very
|
|
53
|
+
* large boards. Requires `approxMax > approxMin`; `approxBuckets` defaults to 1024.
|
|
54
|
+
*/
|
|
55
|
+
approxRank?: boolean;
|
|
56
|
+
approxMin?: number;
|
|
57
|
+
approxMax?: number;
|
|
58
|
+
approxBuckets?: number;
|
|
43
59
|
}
|
|
44
60
|
type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
|
|
45
61
|
export interface ClientOptions {
|
|
@@ -72,8 +88,15 @@ export declare class LeaderboardClient {
|
|
|
72
88
|
createBoard(board: string, def?: BoardDef): Promise<void>;
|
|
73
89
|
/** Submit a score (write-behind: durably logged, ranked shortly after). */
|
|
74
90
|
submitScore(board: string, member: string, score: number, opts?: SubmitOpts): Promise<SubmitResult>;
|
|
75
|
-
/** A member's rank. Throws {@link NotFoundError} if absent. */
|
|
91
|
+
/** A member's exact rank (O(log N)). Throws {@link NotFoundError} if absent. */
|
|
76
92
|
getRank(board: string, member: string, q?: QueryOpts): Promise<RankEntry>;
|
|
93
|
+
/**
|
|
94
|
+
* A member's approximate rank from the board's score histogram (O(buckets),
|
|
95
|
+
* `exact: false`). The board must be created with `approxRank: true`; on very
|
|
96
|
+
* large boards this avoids the cost of an exact rank scan. Throws
|
|
97
|
+
* {@link NotFoundError} if the member is absent.
|
|
98
|
+
*/
|
|
99
|
+
getApproxRank(board: string, member: string, q?: QueryOpts): Promise<RankEntry>;
|
|
77
100
|
/** Top N entries (rank 1..N). */
|
|
78
101
|
getTop(board: string, n: number, q?: QueryOpts): Promise<RankEntry[]>;
|
|
79
102
|
/** A page of the ranking starting at offset (0-based). */
|
package/dist/index.js
CHANGED
|
@@ -45,6 +45,10 @@ export class LeaderboardClient {
|
|
|
45
45
|
update_policy: def.updatePolicy,
|
|
46
46
|
tie_break: def.tieBreak,
|
|
47
47
|
windows: def.windows?.map((w) => ({ kind: w.kind, custom_id: w.customId })),
|
|
48
|
+
approx_rank: def.approxRank,
|
|
49
|
+
approx_min: def.approxMin,
|
|
50
|
+
approx_max: def.approxMax,
|
|
51
|
+
approx_buckets: def.approxBuckets,
|
|
48
52
|
});
|
|
49
53
|
}
|
|
50
54
|
/** Submit a score (write-behind: durably logged, ranked shortly after). */
|
|
@@ -54,6 +58,7 @@ export class LeaderboardClient {
|
|
|
54
58
|
score,
|
|
55
59
|
segments: opts.segments,
|
|
56
60
|
idem: opts.idem,
|
|
61
|
+
time: opts.time instanceof Date ? opts.time.toISOString() : opts.time,
|
|
57
62
|
};
|
|
58
63
|
if (this.opts.signingSecret) {
|
|
59
64
|
const ts = Math.floor(Date.now() / 1000);
|
|
@@ -65,10 +70,19 @@ export class LeaderboardClient {
|
|
|
65
70
|
const r = await this.send("POST", `/v1/boards/${enc(board)}/scores`, body);
|
|
66
71
|
return { accepted: !!r.accepted, duplicate: !!r.duplicate };
|
|
67
72
|
}
|
|
68
|
-
/** A member's rank. Throws {@link NotFoundError} if absent. */
|
|
73
|
+
/** A member's exact rank (O(log N)). Throws {@link NotFoundError} if absent. */
|
|
69
74
|
async getRank(board, member, q = {}) {
|
|
70
75
|
return this.send("GET", `/v1/boards/${enc(board)}/rank${qs({ member, ...q })}`);
|
|
71
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* A member's approximate rank from the board's score histogram (O(buckets),
|
|
79
|
+
* `exact: false`). The board must be created with `approxRank: true`; on very
|
|
80
|
+
* large boards this avoids the cost of an exact rank scan. Throws
|
|
81
|
+
* {@link NotFoundError} if the member is absent.
|
|
82
|
+
*/
|
|
83
|
+
async getApproxRank(board, member, q = {}) {
|
|
84
|
+
return this.send("GET", `/v1/boards/${enc(board)}/rank${qs({ member, approx: "true", ...q })}`);
|
|
85
|
+
}
|
|
72
86
|
/** Top N entries (rank 1..N). */
|
|
73
87
|
async getTop(board, n, q = {}) {
|
|
74
88
|
const r = await this.send("GET", `/v1/boards/${enc(board)}/top${qs({ n: String(n), ...q })}`);
|