Leaderboards
Rank your players' best results: declare a board in leaderboards.json, then pg.storage in an instant Python game (PyLevate) or pycorner.js in a web app.
An app can have leaderboards: each signed-in player’s best result, ranked, with the name from their profile. A game might have score (the highest leads) and best-lap (the lowest leads).
Declare the boards
Add a file leaderboards.json to the project, with each board’s name and direction:
{"score": "high", "best-lap": "low"}
"high": the highest result leads. "low": the lowest leads, for times. That file is the only place a board’s direction is set, and only boards named in it exist. A name is lowercase letters, digits and dashes (at most 32 characters); an app has at most 5 boards.
A board takes whole numbers from 0 to 1 000 000 000. Give it a tighter range when you know one, so that impossible results are refused:
{"best-lap": {"order": "low", "min": 5000, "max": 3600000}}
Store times as whole milliseconds and money as cents.
In an instant Python game (PyLevate)
Nothing to import: pg.storage, which already keeps a game’s saves, does it through reserved keys.
import pylevate.game as pg
pg.storage.save('pycorner.score:score', score) # submit a result
best = pg.storage.load('pycorner.score:score', 0) # the player's own best
rank = pg.storage.load('pycorner.rank:score', 0) # the player's own rank, 0 when not on the board
board = pg.storage.load('pycorner.board:score', []) # the top 10
for entry in board:
line = str(entry['rank']) + '. ' + entry['name'] + ' ' + str(entry['score'])
if entry['me']:
line = line + ' (you)'
| Key | save | load |
|---|---|---|
pycorner.score:<board> | submits the result (a whole number) | the player’s own best |
pycorner.board:<board> | ignored | the top 10: a list of {'rank', 'name', 'score', 'me'} |
pycorner.rank:<board> | ignored | the player’s own rank |
pg.storage.forget('pycorner.score:<board>') takes the player’s own entry off the board.
Save the score whenever it changes, or when a round ends: only a result that beats the player’s best is sent, a moment later, and a board sends at most one every ten seconds. load answers at once from what the page holds: the boards are in the page when the game starts, are fetched again after a submit, and again when a list you read is older than a minute. Draw the board from load every time you show it and it stays current.
A visitor who is not signed in sees the boards but is not on them: their own best stays in their browser, under the same key, so the game code is the same for everybody. In the desktop build (pygame) the keys are ordinary saved values: the player’s best is kept in the local file and the board is empty.
In a web app
import pycorner from "/pycorner.js";
const board = pycorner.board("score");
const { top, me, players, signed_in } = await board.top(); // or board.top({ top: 25 })
// top: [{ rank, name, score, at, me }], me: { rank, score, at } or null
try {
const after = await board.submit(1234); // the same answer, plus `stored`
} catch (e) {
if (e.status === 401) { /* not signed in: keep their best in localStorage */ }
}
await board.forget(); // take the signed-in player's own entry off
From any other app
The same calls over HTTP, on the app’s own origin, with the app’s preview token in the Authorization: Bearer header (in the Python library: the token it already uses):
| Call | Answer |
|---|---|
GET /api/v1/data/x/boards/ | every declared board: {"signed_in", "boards": {name: board}} |
GET /api/v1/data/x/boards/<name>/?top=10 | one board (top up to 100) |
POST /api/v1/data/x/boards/<name>/ with {"score": 1234} | the board, with "stored": true or false |
DELETE /api/v1/data/x/boards/<name>/ | removes the caller’s own entry |
401: nobody is signed in. 400: not a whole number, or outside the board’s range. 404: no such board in leaderboards.json. 429: more than 6 submissions a minute by one person in one app.
What a board shows, and to whom
Each entry shows the player’s profile name (the name on their profile page, else the name they chose to show; never the email address or the username; without one, “Player” and four characters), the result and its date. Anybody who can open the app can see its boards, also where the app is embedded in another website. When a player closes their account, their entries disappear from every board at once and are erased with the account.
Scores can be faked
The result is sent by the player’s browser. Somebody who wants to can send a made-up one, and nothing on the server can tell a real 1234 from an invented one. PyCorner does the cheap things: it refuses results outside the board’s range, takes a few submissions a minute per person, keeps one best per person, and takes results only from signed-in accounts. So treat a board as fun, not as proof, and do not hang prizes on it.
You look after your boards under Share → Leaderboards: remove an entry that cannot be true, or reset a board. Resetting is also the way to start over after you changed what a board measures or its direction.