feat(web): use the shared high-score service
Keep browser settings and the existing local save fallback, but route the browser high-score table through the same-origin service when it is available. The WASM storage bridge now accepts fetched score JSON and queues one validated submission at a time. The JavaScript plugin fetches the canonical table, submits accepted name-entry scores, ignores stale responses, retries failures, and feeds successful responses back into the running game. Update the web and project documentation to describe the optional shared leaderboard and regenerate the tracked browser artifact. A missing service continues to leave the local table usable; the service documentation records that anonymous client scores are intentionally not tamper-resistant. Test Plan: - `just test` -- passed (3 service tests and 137 game tests) - `just clippy` -- passed - `just web-build` -- passed - `cargo +nightly fmt -- --check` -- passed - `node --check web/storage.js` and `prettier --check web/storage.js` -- passed - `rumdl check --flavor commonmark CHANGELOG.md README.md web/README.md highscore-server/README.md` -- passed - Browser WASM load through same-origin API proxy -- passed - `git diff --cached --check` -- passed
This commit is contained in:
@@ -7,8 +7,11 @@ use std::{
|
||||
|
||||
thread_local! {
|
||||
static LOADED_BYTES: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
|
||||
static LOADED_HIGH_SCORES: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
|
||||
static PENDING_SAVE: RefCell<Option<Vec<u8>>> = const { RefCell::new(None) };
|
||||
static PENDING_HIGH_SCORE: RefCell<Option<Vec<u8>>> = const { RefCell::new(None) };
|
||||
static SAVE_REVISION: Cell<u32> = const { Cell::new(0) };
|
||||
static HIGH_SCORE_REVISION: Cell<u32> = const { Cell::new(0) };
|
||||
}
|
||||
|
||||
pub fn take_loaded() -> Option<Vec<u8>> {
|
||||
@@ -21,6 +24,16 @@ pub fn queue_save(bytes: Vec<u8>) {
|
||||
SAVE_REVISION.with(|revision| revision.set(revision.get().wrapping_add(1)));
|
||||
}
|
||||
|
||||
pub fn take_high_scores() -> Option<Vec<u8>> {
|
||||
let bytes = LOADED_HIGH_SCORES.with(|loaded| mem::take(&mut *loaded.borrow_mut()));
|
||||
(!bytes.is_empty()).then_some(bytes)
|
||||
}
|
||||
|
||||
pub fn queue_high_score(bytes: Vec<u8>) {
|
||||
PENDING_HIGH_SCORE.with(|pending| *pending.borrow_mut() = Some(bytes));
|
||||
HIGH_SCORE_REVISION.with(|revision| revision.set(revision.get().wrapping_add(1)));
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn tdkpin_storage_crate_version() -> u32 {
|
||||
1
|
||||
@@ -41,6 +54,21 @@ pub extern "C" fn tdkpin_browser_storage_push(byte: u32) {
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn tdkpin_browser_storage_finish() {}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn tdkpin_browser_high_scores_clear() {
|
||||
LOADED_HIGH_SCORES.with(|loaded| loaded.borrow_mut().clear());
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn tdkpin_browser_high_scores_push(byte: u32) {
|
||||
if let Ok(byte) = u8::try_from(byte) {
|
||||
LOADED_HIGH_SCORES.with(|loaded| loaded.borrow_mut().push(byte));
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn tdkpin_browser_high_scores_finish() {}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn tdkpin_browser_storage_save_revision() -> u32 {
|
||||
SAVE_REVISION.with(Cell::get)
|
||||
@@ -71,3 +99,36 @@ pub extern "C" fn tdkpin_browser_storage_save_byte(index: u32) -> u32 {
|
||||
pub extern "C" fn tdkpin_browser_storage_save_ack() {
|
||||
PENDING_SAVE.with(|pending| *pending.borrow_mut() = None);
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn tdkpin_browser_high_score_revision() -> u32 {
|
||||
HIGH_SCORE_REVISION.with(Cell::get)
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn tdkpin_browser_high_score_length() -> u32 {
|
||||
PENDING_HIGH_SCORE.with(|pending| {
|
||||
pending
|
||||
.borrow()
|
||||
.as_ref()
|
||||
.map_or(0, |bytes| u32::try_from(bytes.len()).unwrap_or(u32::MAX))
|
||||
})
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn tdkpin_browser_high_score_byte(index: u32) -> u32 {
|
||||
PENDING_HIGH_SCORE.with(|pending| {
|
||||
pending
|
||||
.borrow()
|
||||
.as_ref()
|
||||
.and_then(|bytes| bytes.get(usize::try_from(index).ok()?))
|
||||
.map_or(0, |byte| u32::from(*byte))
|
||||
})
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn tdkpin_browser_high_score_ack(revision: u32) {
|
||||
if HIGH_SCORE_REVISION.with(Cell::get) == revision {
|
||||
PENDING_HIGH_SCORE.with(|pending| *pending.borrow_mut() = None);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user