feat: add saving client settings to file

This commit is contained in:
2026-08-18 22:08:15 +02:00
parent 894c4c1cfb
commit a6cc7109b3
5 changed files with 45 additions and 19 deletions

View File

@@ -21,14 +21,9 @@ use crate::{
}, },
network_identity::NetworkIdentity, network_identity::NetworkIdentity,
}, },
settings::{client_settings::ClientSettings, general_settings::SettingsLike},
}; };
#[derive(Clone)]
pub struct ClientSettings {
server_url: SocketAddr,
name: String,
}
pub enum ClientCommands { pub enum ClientCommands {
SendCapture(CapturePath), SendCapture(CapturePath),
SendMove { from: Position, to: Position }, SendMove { from: Position, to: Position },
@@ -64,10 +59,7 @@ pub struct Client {
impl Client { impl Client {
pub async fn new() -> Option<Client> { pub async fn new() -> Option<Client> {
let settings = ClientSettings { let settings = ClientSettings::new();
server_url: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 6767),
name: "Morbius".into(),
};
let (conn_sender, conn_reciever) = let (conn_sender, conn_reciever) =
mpsc::channel::<(SocketAddr, Message<ServerMessage>)>(1024); mpsc::channel::<(SocketAddr, Message<ServerMessage>)>(1024);

View File

@@ -0,0 +1,26 @@
use std::net::SocketAddr;
use serde::{Deserialize, Serialize};
use crate::settings::general_settings::{DEFAULT_URL, SettingsLike};
#[derive(Clone, Serialize, Deserialize)]
pub struct ClientSettings {
pub server_url: SocketAddr,
pub name: String,
}
impl Default for ClientSettings {
fn default() -> Self {
Self {
server_url: DEFAULT_URL,
name: "Player".to_string(),
}
}
}
impl SettingsLike for ClientSettings {
fn get_file_path() -> Result<std::path::PathBuf, Box<dyn core::error::Error>> {
Ok(Self::get_config_dir()?.join("client.toml"))
}
}

View File

@@ -1,9 +1,14 @@
use core::error; use core::error;
use std::path::PathBuf; use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
path::PathBuf,
};
use log::warn; use log::warn;
use serde::{Serialize, de::DeserializeOwned}; use serde::{Serialize, de::DeserializeOwned};
pub const DEFAULT_URL: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 6767);
// maybe consider some better name for this // maybe consider some better name for this
pub trait SettingsLike: Serialize + DeserializeOwned + Default { pub trait SettingsLike: Serialize + DeserializeOwned + Default {
fn get_config_dir() -> Result<PathBuf, Box<dyn error::Error>> { fn get_config_dir() -> Result<PathBuf, Box<dyn error::Error>> {
@@ -31,6 +36,8 @@ pub trait SettingsLike: Serialize + DeserializeOwned + Default {
Ok(toml::from_slice(&std::fs::read(Self::get_file_path()?)?)?) // i love rust Ok(toml::from_slice(&std::fs::read(Self::get_file_path()?)?)?) // i love rust
} }
/// Try to create new instance of `Self` with values from config file. If there is no content
/// defaults to Self::default. If config file is invalid overwrites it with default values
fn new() -> Self { fn new() -> Self {
match Self::read_from_file() { match Self::read_from_file() {
Ok(settings) => return settings, Ok(settings) => return settings,

View File

@@ -1,2 +1,3 @@
pub mod client_settings;
pub mod general_settings; pub mod general_settings;
pub mod server_settings; pub mod server_settings;

View File

@@ -5,7 +5,7 @@ use std::{
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::settings::general_settings::SettingsLike; use crate::settings::general_settings::{DEFAULT_URL, SettingsLike};
// TODO! More settings! // TODO! More settings!
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@@ -15,19 +15,19 @@ pub struct ServerSettings {
pub allow_spectators: bool, pub allow_spectators: bool,
} }
impl SettingsLike for ServerSettings {
fn get_file_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
Ok(Self::get_config_dir()?.join("server.toml"))
}
}
impl Default for ServerSettings { impl Default for ServerSettings {
fn default() -> Self { fn default() -> Self {
Self { Self {
addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 6767), addr: DEFAULT_URL,
max_connections: 2, // Maybe this should be a const somewhere in the server code? It max_connections: 2, // Maybe this should be a const somewhere in the server code? It
// makes no sense to have different value than this // makes no sense to have different value than this
allow_spectators: false, allow_spectators: false,
} }
} }
} }
impl SettingsLike for ServerSettings {
fn get_file_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
Ok(Self::get_config_dir()?.join("server.toml"))
}
}