End-of-game scores are POSTed to a configurable backend URL; the main menu
gains a "Global Leaderboard" entry that fetches and renders the top N for
the current board. Default `global_leaderboard=0` ships the code dormant
until the backend at leaderboard_url is live.
Architecture:
- httpclient.h: thin async interface. http_post_json / http_get fire and
forget; the user callback runs from http_pump() (native) or the
Emscripten runtime (web). No blocking calls.
- httpclient_native.cpp: libcurl-multi, polled per frame.
- httpclient_web.cpp: emscripten_fetch (-sFETCH=1 link option).
- leaderboard.{h,cpp}: portable game-side module. Hand-rolled JSON
build/parse for the tiny fixed-shape wire format; no JSON dep.
Identity: player name + a player-typed "board name" (shared room code).
A per-install RFC-4122-v4 UUID `client_id` is auto-generated on first run
via SDL_rand_bits and stored in the config — server uses it for rate
limiting / dedup, not as a public identity.
Wire format (informational, server is out-of-tree):
POST /api/scores body: {board, name, client_id, score, skill,
gametype, end_scene, end_lives, end_health,
playtime, start_date, end_date}
resp: {rank, total}
GET /api/scores?board=&limit=50
resp: {board, total, scores:[{name,score,end_scene}]}
Hook points:
- manage.cpp game_start: populate hi.start_date; reset_submission().
- manage.cpp game_stop: populate hi.end_date and the profile name on hi,
then leaderboard::submit_score after scorefile.record.
- kobo.cpp main: http_init/http_shutdown around the run; generate
client_id on first run when empty.
- kobo.cpp kobo_gfxengine_t::frame: http_pump() before gsm.frame().
- states.cpp st_game_over post_render: renders "Submitting…" / "Rank #N of M"
/ "(offline)" under "GAME OVER".
- states.cpp main_menu: "Global Leaderboard" button (only when enabled
+ board name set).
- states.cpp st_global_leaderboard_t: new browser state listing top 16.
- options.cpp game_options: onoff toggle.
Out of scope for this commit (left as follow-ups):
- In-game text editor for board name / URL (form toolkit has no free-text
widget; users edit via config file or CLI flags for now).
- Retry queue across sessions.
- Server implementation. tools/mock-leaderboard.py is a 100-line Python
scratch server for local testing only.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
70 lines
2.0 KiB
C++
70 lines
2.0 KiB
C++
/*
|
|
* leaderboard.h — global leaderboard client.
|
|
*
|
|
* Two flows:
|
|
*
|
|
* 1. submit_score() is called after each completed game (manage.cpp). It
|
|
* POSTs the run's s_hiscore_t to leaderboard_url + "/api/scores". The
|
|
* game-over screen polls submission() to render the in-flight UI.
|
|
*
|
|
* 2. fetch_board() is called when the in-game leaderboard browser opens.
|
|
* It GETs the top N for the current board. The browser state polls
|
|
* browse() to render the list.
|
|
*
|
|
* Both flows are no-ops when prefs->global_leaderboard is 0, or when the
|
|
* board name is empty, or when the URL is empty.
|
|
*/
|
|
#ifndef KOBO_LEADERBOARD_H
|
|
#define KOBO_LEADERBOARD_H
|
|
|
|
#include "score.h"
|
|
|
|
#define LEADERBOARD_BROWSE_MAX 50
|
|
#define LEADERBOARD_NAME_MAX 32
|
|
|
|
enum leaderboard_status_t {
|
|
LB_IDLE = 0,
|
|
LB_INFLIGHT,
|
|
LB_DONE,
|
|
LB_FAILED, // server error, network error, or feature disabled
|
|
LB_OFFLINE // transport failure (status 0)
|
|
};
|
|
|
|
struct leaderboard_submission_t {
|
|
leaderboard_status_t status;
|
|
int rank; // valid when status == LB_DONE
|
|
int total; // valid when status == LB_DONE
|
|
};
|
|
|
|
struct leaderboard_entry_t {
|
|
char name[LEADERBOARD_NAME_MAX];
|
|
unsigned int score;
|
|
int end_scene;
|
|
};
|
|
|
|
struct leaderboard_browse_t {
|
|
leaderboard_status_t status;
|
|
int count;
|
|
int total;
|
|
leaderboard_entry_t entries[LEADERBOARD_BROWSE_MAX];
|
|
char board[LEADERBOARD_NAME_MAX];
|
|
};
|
|
|
|
namespace leaderboard {
|
|
// Called from manage.cpp after scorefile.record(). Safe to call always;
|
|
// it self-checks prefs and silently no-ops if disabled.
|
|
void submit_score(const s_hiscore_t *hi);
|
|
|
|
// Called from the browser state on enter. Replaces previous results.
|
|
void fetch_board();
|
|
|
|
// State accessors — game UI reads these every frame.
|
|
const leaderboard_submission_t *submission();
|
|
const leaderboard_browse_t *browse();
|
|
|
|
// Reset submission state to LB_IDLE (e.g. when starting a new game).
|
|
void reset_submission();
|
|
}
|
|
|
|
#endif // KOBO_LEADERBOARD_H
|