diff options
| author | metamuffin <metamuffin@disroot.org> | 2024-09-02 02:08:31 +0200 | 
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2024-09-02 14:18:02 +0200 | 
| commit | e769071244de0bdfe53c2b570b8cc6629cc5ec6c (patch) | |
| tree | 69698b79babf64778aadd0ece2918709aea2bb7e | |
| parent | 84e2b868e87043f4e3844738f4e1c2a7bcbb96c0 (diff) | |
| download | hurrycurry-e769071244de0bdfe53c2b570b8cc6629cc5ec6c.tar hurrycurry-e769071244de0bdfe53c2b570b8cc6629cc5ec6c.tar.bz2 hurrycurry-e769071244de0bdfe53c2b570b8cc6629cc5ec6c.tar.zst | |
scoreboard insert function
| -rw-r--r-- | server/src/scoreboard.rs | 13 | 
1 files changed, 11 insertions, 2 deletions
| diff --git a/server/src/scoreboard.rs b/server/src/scoreboard.rs index e7b97b8d..e546f973 100644 --- a/server/src/scoreboard.rs +++ b/server/src/scoreboard.rs @@ -18,7 +18,7 @@  use anyhow::Result;  use hurrycurry_protocol::Score;  use serde::{Deserialize, Serialize}; -use std::collections::HashMap; +use std::{cmp::Reverse, collections::HashMap};  use tokio::{      fs::{read_to_string, rename, File},      io::AsyncWriteExt, @@ -28,7 +28,7 @@ use tokio::{  pub struct ScoreboardStore {      maps: HashMap<String, Scoreboard>,  } -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, Default)]  pub struct Scoreboard {      plays: usize,      best: Vec<ScoreboardEntry>, @@ -62,4 +62,13 @@ impl ScoreboardStore {          rename(buffer_path, path).await?;          Ok(())      } +    pub fn insert(&mut self, map: &str, players: Vec<String>, score: Score) { +        let b = self.maps.entry(map.to_owned()).or_default(); +        b.plays += 1; +        b.best.push(ScoreboardEntry { players, score }); +        b.best.sort_by_key(|e| Reverse(e.score.points)); +        while b.best.len() > 16 { +            b.best.pop(); +        } +    }  } | 
