gameserv: ranked ladder rides StatServ's persisted counters, no separate store
All checks were successful
CI / check (push) Successful in 3m50s

This commit is contained in:
Jean Chevronnet 2026-07-17 12:07:35 +00:00
parent b763eab9ed
commit 108c15e95a
No known key found for this signature in database
3 changed files with 156 additions and 51 deletions

View file

@ -32,28 +32,40 @@ pub struct Game {
pub result: String, // "" | "<winner account>" | "draw"
}
// One (account, game type) ladder row. A player can be Gold at chess and Bronze
// at Connect Four, so each type keeps its own record.
// One (account, game type) ladder row, materialised from the shared stat counters
// (`game.<type>.<w|l|d>.<account>`) that StatServ already persists. A player can be
// Gold at chess and Bronze at Connect Four, so each type keeps its own record.
#[derive(Default, Clone)]
pub struct Stats {
pub wins: u32,
pub losses: u32,
pub draws: u32,
pub points: i32,
}
impl Stats {
// Points are DERIVED from the win/loss counts (draws don't score), floored at
// 0: win +10, loss -8. Deriving keeps the ladder to monotonic w/l/d counters,
// so it rides StatServ's existing counter persistence with nothing to sync.
pub fn points(&self) -> i32 {
(10 * self.wins as i32 - 8 * self.losses as i32).max(0)
}
pub fn rank(&self) -> &'static str {
if self.points >= 800 {
let p = self.points();
if p >= 800 {
"Master"
} else if self.points >= 400 {
} else if p >= 400 {
"Gold"
} else if self.points >= 150 {
} else if p >= 150 {
"Silver"
} else {
"Bronze"
}
}
pub fn any(&self) -> bool {
self.wins > 0 || self.losses > 0 || self.draws > 0
}
}
pub fn valid_type(t: &str) -> bool {