mirror of https://github.com/zyllian/classics.git synced 2025-05-10 02:26:53 -07:00

add basic server configuration + password protection

This commit is contained in:
Zoey 2024-04-19 11:50:11 -07:00
parent ca94ec10f2
commit c78303cf44
No known key found for this signature in database
GPG key ID: 8611B896D1AAFAF2
7 changed files with 146 additions and 17 deletions

View file

@ -1,3 +1,4 @@
pub mod config;
mod network;
use std::sync::Arc;
@ -8,6 +9,8 @@ use tokio::{net::TcpListener, sync::RwLock};
use crate::{level::Level, player::Player};
use self::config::ServerConfig;
const DEFAULT_SERVER_SIZE: usize = 128;
/// the server
@ -28,18 +31,25 @@ pub struct ServerData {
pub players: Vec<Player>,
/// list of player ids which have been freed up
pub free_player_ids: Vec<i8>,
/// the server's config
pub config: ServerConfig,
}
impl Server {
/// creates a new server with a generated level
pub async fn new() -> std::io::Result<Self> {
pub async fn new(config: ServerConfig) -> std::io::Result<Self> {
println!("generating level");
let mut rng = rand::thread_rng();
let mut level = Level::new(
DEFAULT_SERVER_SIZE,
DEFAULT_SERVER_SIZE,
DEFAULT_SERVER_SIZE,
);
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);
for x in 0..level.x_size {
for y in 0..(level.y_size / 2) {
for z in 0..level.z_size {
@ -49,11 +59,11 @@ impl Server {
}
println!("done!");
Self::new_with_level(level).await
Self::new_with_level(config, level).await
}
/// creates a new server with the given level
pub async fn new_with_level(level: Level) -> std::io::Result<Self> {
pub async fn new_with_level(config: ServerConfig, level: Level) -> std::io::Result<Self> {
let listener = TcpListener::bind("127.0.0.1:25565").await?;
Ok(Self {
@ -61,6 +71,7 @@ impl Server {
level,
players: Default::default(),
free_player_ids: Vec::new(),
config,
})),
listener,
})