aboutsummaryrefslogtreecommitdiff
path: root/server/src/scoreboard.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/scoreboard.rs')
-rw-r--r--server/src/scoreboard.rs36
1 files changed, 12 insertions, 24 deletions
diff --git a/server/src/scoreboard.rs b/server/src/scoreboard.rs
index 60b9356c..e97e22b2 100644
--- a/server/src/scoreboard.rs
+++ b/server/src/scoreboard.rs
@@ -17,36 +17,27 @@
*/
use anyhow::Result;
use directories::ProjectDirs;
-use hurrycurry_protocol::Score;
+use hurrycurry_protocol::{Score, Scoreboard, ScoreboardEntry};
use log::warn;
use serde::{Deserialize, Serialize};
-use std::{cmp::Reverse, collections::HashMap};
-use tokio::{
+use std::{
+ cmp::Reverse,
+ collections::HashMap,
fs::{create_dir_all, read_to_string, rename, File},
- io::AsyncWriteExt,
+ io::Write,
};
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct ScoreboardStore {
maps: HashMap<String, Scoreboard>,
}
-#[derive(Debug, Serialize, Deserialize, Clone, Default)]
-pub struct Scoreboard {
- pub plays: usize,
- pub best: Vec<ScoreboardEntry>,
-}
-#[derive(Debug, Serialize, Deserialize, Clone)]
-pub struct ScoreboardEntry {
- pub players: Vec<String>,
- pub score: Score,
-}
fn project_dirs() -> Option<ProjectDirs> {
ProjectDirs::from("org", "hurrycurry", "hurrycurry")
}
impl ScoreboardStore {
- pub async fn load() -> Result<Self> {
+ pub fn load() -> Result<Self> {
let Some(dir) = project_dirs() else {
warn!("scoreboard load skipped; no data dir for this platform");
return Ok(Self::default());
@@ -54,24 +45,21 @@ impl ScoreboardStore {
// TOCTOU because its easier that way
let path = dir.data_dir().join("scoreboards.json");
if !path.exists() {
- create_dir_all(dir.data_dir()).await?;
- ScoreboardStore::default().save().await?;
+ create_dir_all(dir.data_dir())?;
+ ScoreboardStore::default().save()?;
}
- let s = read_to_string(path).await?;
+ let s = read_to_string(path)?;
Ok(serde_json::from_str(&s)?)
}
- pub async fn save(&self) -> Result<()> {
+ pub fn save(&self) -> Result<()> {
let Some(dir) = project_dirs() else {
warn!("scoreboard save skipped; no data dir for this platform");
return Ok(());
};
let path = dir.data_dir().join("scoreboards.json");
let buffer_path = dir.data_dir().join("scoreboards.json~");
- File::create(&buffer_path)
- .await?
- .write_all(serde_json::to_string(self)?.as_bytes())
- .await?;
- rename(buffer_path, path).await?;
+ File::create(&buffer_path)?.write_all(serde_json::to_string(self)?.as_bytes())?;
+ rename(buffer_path, path)?;
Ok(())
}
pub fn get<'a>(&'a self, map: &str) -> Option<&'a Scoreboard> {