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>
96 lines
2.4 KiB
C++
96 lines
2.4 KiB
C++
/*
|
|
* httpclient_web.cpp — emscripten_fetch backend.
|
|
*
|
|
* emscripten_fetch is async and fires onsuccess/onerror on the main thread.
|
|
* We attach a small heap-owned context to userData; the trampolines invoke
|
|
* the user callback and free the context.
|
|
*
|
|
* http_pump() is a no-op because the runtime drives completion delivery.
|
|
*/
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
#include "httpclient.h"
|
|
|
|
#include <emscripten/fetch.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
namespace {
|
|
|
|
struct ctx_t {
|
|
http_callback_t cb;
|
|
void *user;
|
|
};
|
|
|
|
void deliver(emscripten_fetch_t *fetch, int status)
|
|
{
|
|
ctx_t *c = static_cast<ctx_t *>(fetch->userData);
|
|
http_response_t resp;
|
|
resp.status = status;
|
|
resp.body = fetch->data ? fetch->data : "";
|
|
resp.len = (size_t)fetch->numBytes;
|
|
c->cb(&resp, c->user);
|
|
emscripten_fetch_close(fetch);
|
|
delete c;
|
|
}
|
|
|
|
void on_success(emscripten_fetch_t *fetch)
|
|
{
|
|
deliver(fetch, (int)fetch->status);
|
|
}
|
|
|
|
void on_error(emscripten_fetch_t *fetch)
|
|
{
|
|
// Distinguish: a non-2xx with body (e.g. 500 + JSON) reports its status;
|
|
// a true transport failure (no connection, CORS reject) reports 0.
|
|
int status = (fetch->status >= 100 && fetch->status < 600)
|
|
? (int)fetch->status : 0;
|
|
deliver(fetch, status);
|
|
}
|
|
|
|
void issue(const char *method, const char *url, const char *body,
|
|
http_callback_t cb, void *user)
|
|
{
|
|
emscripten_fetch_attr_t attr;
|
|
emscripten_fetch_attr_init(&attr);
|
|
strncpy(attr.requestMethod, method, sizeof(attr.requestMethod) - 1);
|
|
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
|
|
attr.onsuccess = on_success;
|
|
attr.onerror = on_error;
|
|
|
|
static const char *headers[] = {
|
|
"Content-Type", "application/json", nullptr
|
|
};
|
|
if(body)
|
|
{
|
|
attr.requestHeaders = headers;
|
|
attr.requestData = body;
|
|
attr.requestDataSize = strlen(body);
|
|
}
|
|
|
|
auto *c = new ctx_t{cb, user};
|
|
attr.userData = c;
|
|
emscripten_fetch(&attr, url);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
extern "C" void http_init(void) {}
|
|
extern "C" void http_shutdown(void) {}
|
|
extern "C" void http_pump(void) {}
|
|
|
|
extern "C" void http_post_json(const char *url, const char *body,
|
|
http_callback_t cb, void *user)
|
|
{
|
|
if(!cb) return;
|
|
issue("POST", url, body ? body : "", cb, user);
|
|
}
|
|
|
|
extern "C" void http_get(const char *url, http_callback_t cb, void *user)
|
|
{
|
|
if(!cb) return;
|
|
issue("GET", url, nullptr, cb, user);
|
|
}
|
|
|
|
#endif /* __EMSCRIPTEN__ */
|