Skip to content

Commit a3e0f21

Browse files
authored
Merge pull request #11 from firefly-zero/stats
Boards and badges
2 parents 0eae0e2 + bbe102c commit a3e0f21

File tree

4 files changed

+92
-5
lines changed

4 files changed

+92
-5
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@ keywords = ["gamedev", "firefly-zero"]
1111
categories = ["game-development", "no-std", "rendering", "api-bindings", "wasm"]
1212

1313
[features]
14-
default = ["alloc", "sudo", "nalgebra_support"]
14+
default = ["alloc", "sudo", "nalgebra_support", "panic_info"]
15+
# If disabled, the crate uses no_std.
1516
std = []
17+
# Enable support for allocating functions, like load_file_buf.
18+
# Requires a global allocator to be configured.
1619
alloc = []
20+
# If app panics, show panic info (message, file name, line number).
21+
# Increases the binary size.
22+
panic_info = []
23+
# Enable support for functions requiring sudo.
1724
sudo = []
25+
# Enable support for casting graphic primitives to and from nalgebra crate primitives.
1826
nalgebra_support = ["nalgebra"]
1927

2028
[dependencies]
21-
nalgebra = { version = "0.33.0", optional = true, default-features = false }
29+
nalgebra = { version = "0.33.2", optional = true, default-features = false }

src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod menu;
2626
mod misc;
2727
mod net;
2828
pub mod shapes;
29+
mod stats;
2930
#[cfg(feature = "sudo")]
3031
pub mod sudo;
3132

@@ -35,12 +36,19 @@ pub use input::*;
3536
pub use menu::*;
3637
pub use misc::*;
3738
pub use net::*;
39+
pub use stats::*;
3840

3941
#[cfg(feature = "alloc")]
4042
extern crate alloc;
4143

4244
#[cfg(all(not(test), not(feature = "std"), target_family = "wasm"))]
4345
#[panic_handler]
44-
fn handle_panic(_: &core::panic::PanicInfo) -> ! {
46+
#[allow(unused_variables)]
47+
fn handle_panic(info: &core::panic::PanicInfo) -> ! {
48+
#[cfg(all(feature = "alloc", feature = "panic_info"))]
49+
if true {
50+
let msg = alloc::format!("{info}");
51+
log_error(&msg);
52+
}
4553
core::arch::wasm32::unreachable()
4654
}

src/stats.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use crate::Peer;
2+
3+
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
4+
pub struct Badge(pub u8);
5+
6+
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
7+
pub struct Board(pub u8);
8+
9+
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
10+
pub struct Progress {
11+
/// How many points the player already has.
12+
pub done: u16,
13+
/// How many points the player needs to earn the badge.
14+
pub goal: u16,
15+
}
16+
17+
impl Progress {
18+
#[must_use]
19+
pub fn earned(&self) -> bool {
20+
self.done >= self.goal
21+
}
22+
}
23+
24+
/// Get the progress of earning the badge.
25+
#[must_use]
26+
pub fn get_progress(p: Peer, b: Badge) -> Progress {
27+
add_progress(p, b, 0)
28+
}
29+
30+
/// Add the given value to the progress for the badge.
31+
///
32+
/// May be negative if you want to decrease the progress.
33+
/// If zero, does not change the progress.
34+
///
35+
/// If the Peer is [`Peer::COMBINED`], the progress is added to every peer
36+
/// and the returned value is the lowest progress.
37+
#[expect(clippy::must_use_candidate)]
38+
pub fn add_progress(p: Peer, b: Badge, v: i16) -> Progress {
39+
let r = unsafe { bindings::add_progress(u32::from(p.0), u32::from(b.0), i32::from(v)) };
40+
Progress {
41+
done: (r >> 16) as u16,
42+
goal: (r) as u16,
43+
}
44+
}
45+
46+
/// Get the personal best of the player.
47+
#[must_use]
48+
pub fn get_score(p: Peer, b: Badge) -> i16 {
49+
add_score(p, b, 0)
50+
}
51+
52+
/// Add the given score to the board.
53+
///
54+
/// May be negative if you want the lower scores
55+
/// to rank higher. Zero value is not added to the board.
56+
///
57+
/// If the Peer is [`Peer::COMBINED`], the score is added for every peer
58+
/// and the returned value is the lowest of their best scores.
59+
#[expect(clippy::must_use_candidate)]
60+
pub fn add_score(p: Peer, b: Badge, v: i16) -> i16 {
61+
let r = unsafe { bindings::add_score(u32::from(p.0), u32::from(b.0), i32::from(v)) };
62+
r as i16
63+
}
64+
65+
mod bindings {
66+
#[link(wasm_import_module = "stats")]
67+
extern {
68+
pub(crate) fn add_progress(peer_id: u32, badge_id: u32, val: i32) -> u32;
69+
pub(crate) fn add_score(peer_id: u32, board_id: u32, new_score: i32) -> i32;
70+
}
71+
}

0 commit comments

Comments
 (0)