mirror of
https://github.com/zyllian/classics.git
synced 2025-05-10 05:06:41 -07:00
make all config options default
This commit is contained in:
parent
e309a46cd3
commit
8dc89d959e
6 changed files with 73 additions and 31 deletions
|
@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
|
|||
use super::{block::BLOCK_STRING_ID_MAP, Level};
|
||||
|
||||
/// enum for different kinds of level generation
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum LevelGeneration {
|
||||
/// an empty level
|
||||
|
@ -17,7 +17,7 @@ pub enum LevelGeneration {
|
|||
}
|
||||
|
||||
/// enum for level presents
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "flat_type")]
|
||||
pub enum FlatPreset {
|
||||
/// the level is mostly stone, then dirt, then a layer of grass on the top
|
||||
|
@ -27,7 +27,7 @@ pub enum FlatPreset {
|
|||
}
|
||||
|
||||
/// description of a flat world's layer
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct FlatLayer {
|
||||
/// the block for the layer
|
||||
pub block: String,
|
||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -2,7 +2,10 @@
|
|||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use server::{config::ServerConfig, Server};
|
||||
use server::{
|
||||
config::{OptionalServerConfig, ServerConfig},
|
||||
Server,
|
||||
};
|
||||
|
||||
mod level;
|
||||
mod packet;
|
||||
|
@ -15,16 +18,16 @@ const CONFIG_FILE: &str = "./server-config.json";
|
|||
async fn main() -> std::io::Result<()> {
|
||||
let config_path = PathBuf::from(CONFIG_FILE);
|
||||
let config = if config_path.exists() {
|
||||
serde_json::from_str(&std::fs::read_to_string(config_path)?)
|
||||
serde_json::from_str::<OptionalServerConfig>(&std::fs::read_to_string(&config_path)?)
|
||||
.expect("failed to deserialize config")
|
||||
.build_default()
|
||||
} else {
|
||||
let config = ServerConfig::default();
|
||||
std::fs::write(
|
||||
config_path,
|
||||
serde_json::to_string_pretty(&config).expect("failed to serialize default config"),
|
||||
)?;
|
||||
config
|
||||
ServerConfig::default()
|
||||
};
|
||||
std::fs::write(
|
||||
config_path,
|
||||
serde_json::to_string_pretty(&config).expect("failed to serialize default config"),
|
||||
)?;
|
||||
|
||||
println!("starting server with config: {config:#?}");
|
||||
|
||||
|
|
|
@ -9,8 +9,6 @@ use crate::{level::Level, player::Player};
|
|||
|
||||
use self::config::ServerConfig;
|
||||
|
||||
const DEFAULT_SERVER_SIZE: usize = 128;
|
||||
|
||||
/// the server
|
||||
#[derive(Debug)]
|
||||
pub struct Server {
|
||||
|
@ -38,16 +36,11 @@ impl Server {
|
|||
pub async fn new(config: ServerConfig) -> std::io::Result<Self> {
|
||||
println!("generating level");
|
||||
let mut rng = rand::thread_rng();
|
||||
let (level_x, level_y, level_z) = if let Some(size) = &config.level_size {
|
||||
(size.x, size.y, size.z)
|
||||
} else {
|
||||
(
|
||||
DEFAULT_SERVER_SIZE,
|
||||
DEFAULT_SERVER_SIZE,
|
||||
DEFAULT_SERVER_SIZE,
|
||||
)
|
||||
};
|
||||
let mut level = Level::new(level_x, level_y, level_z);
|
||||
let mut level = Level::new(
|
||||
config.level_size.x,
|
||||
config.level_size.y,
|
||||
config.level_size.z,
|
||||
);
|
||||
config.generation.generate(&mut level, &mut rng);
|
||||
println!("done!");
|
||||
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
use std::collections::BTreeMap;
|
||||
|
||||
use optional_struct::optional_struct;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::level::generation::LevelGeneration;
|
||||
|
||||
/// configuration for the server
|
||||
#[optional_struct]
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct ServerConfig {
|
||||
/// the server's name
|
||||
|
@ -15,28 +17,39 @@ pub struct ServerConfig {
|
|||
#[serde(rename = "password")]
|
||||
pub protection_mode: ServerProtectionMode,
|
||||
/// the level's size
|
||||
pub level_size: Option<ConfigCoordinates>,
|
||||
pub level_size: ConfigCoordinates,
|
||||
/// the level's spawn point
|
||||
pub spawn: Option<ConfigCoordinates>,
|
||||
/// the method to generate the server's level with
|
||||
pub generation: LevelGeneration,
|
||||
}
|
||||
|
||||
impl OptionalServerConfig {
|
||||
/// builds the server config filling with default options
|
||||
pub fn build_default(self) -> ServerConfig {
|
||||
self.build(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ServerConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: "classic server wowie".to_string(),
|
||||
motd: "here's the default server motd".to_string(),
|
||||
protection_mode: ServerProtectionMode::None,
|
||||
level_size: None,
|
||||
level_size: ConfigCoordinates {
|
||||
x: 256,
|
||||
y: 64,
|
||||
z: 256,
|
||||
},
|
||||
spawn: None,
|
||||
generation: LevelGeneration::Empty,
|
||||
generation: LevelGeneration::Flat(crate::level::generation::FlatPreset::StoneAndGrass),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// coordinates as stored in configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ConfigCoordinates {
|
||||
/// the X coordinate
|
||||
pub x: usize,
|
||||
|
@ -47,7 +60,7 @@ pub struct ConfigCoordinates {
|
|||
}
|
||||
|
||||
/// enum for the different kinds of server protection
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum ServerProtectionMode {
|
||||
/// the server is unprotected and anyone can join with any username
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue