add customizable port and worldseed

This commit is contained in:
Songtronix 2019-06-29 18:41:26 +02:00
parent c47bb5ed45
commit 1213d9844b
7 changed files with 79 additions and 13 deletions

5
Cargo.lock generated
View File

@ -2765,8 +2765,11 @@ name = "veloren-server"
version = "0.2.0"
dependencies = [
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)",
"specs 0.14.3 (registry+https://github.com/rust-lang/crates.io-index)",
"uvth 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"vek 0.9.8 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -9,4 +9,4 @@ server = { package = "veloren-server", path = "../server" }
common = { package = "veloren-common", path = "../common" }
log = "0.4"
pretty_env_logger = "0.3"
pretty_env_logger = "0.3"

View File

@ -1,6 +1,6 @@
use common::clock::Clock;
use log::info;
use server::{Event, Input, Server};
use server::{Event, Input, Server, ServerSettings};
use std::time::Duration;
const TPS: u64 = 30;
@ -13,9 +13,12 @@ fn main() {
// Set up an fps clock
let mut clock = Clock::start();
// Load settings
let settings = ServerSettings::load();
// Create server
let mut server = Server::new().expect("Failed to create server instance!");
let mut server = Server::new(settings).expect("Failed to create server instance!");
loop {
let events = server
@ -36,4 +39,6 @@ fn main() {
// Wait for the next tick.
clock.tick(Duration::from_millis(1000 / TPS));
}
//settings.save_to_file().expect("failed to save the settings!");
}

View File

@ -14,3 +14,7 @@ vek = "0.9"
uvth = "3.1.0"
lazy_static = "1.3.0"
scan_fmt = "0.1.3"
ron = "0.5.1"
serde = "1.0"
serde_derive = "1.0"

View File

@ -4,9 +4,10 @@ pub mod client;
pub mod cmd;
pub mod error;
pub mod input;
pub mod settings;
// Reexports
pub use crate::{error::Error, input::Input};
pub use crate::{error::Error, input::Input, settings::ServerSettings};
use crate::{
client::{Client, Clients},
@ -36,8 +37,6 @@ use world::World;
const CLIENT_TIMEOUT: f64 = 20.0; // Seconds
const DEFAULT_WORLD_SEED: u32 = 1337;
pub enum Event {
ClientConnected {
entity: EcsEntity,
@ -66,19 +65,20 @@ pub struct Server {
chunk_rx: mpsc::Receiver<(Vec2<i32>, TerrainChunk)>,
pending_chunks: HashSet<Vec2<i32>>,
server_settings: ServerSettings,
server_info: ServerInfo,
}
impl Server {
/// Create a new `Server` bound to the default socket.
#[allow(dead_code)]
pub fn new() -> Result<Self, Error> {
Self::bind(SocketAddr::from(([0; 4], 59003)))
pub fn new(settings: ServerSettings) -> Result<Self, Error> {
Self::bind(settings.address, settings)
}
/// Create a new server bound to the given socket.
#[allow(dead_code)]
pub fn bind<A: Into<SocketAddr>>(addrs: A) -> Result<Self, Error> {
pub fn bind<A: Into<SocketAddr>>(addrs: A, settings: ServerSettings) -> Result<Self, Error> {
let (chunk_tx, chunk_rx) = mpsc::channel();
let mut state = State::default();
@ -88,7 +88,7 @@ impl Server {
let this = Self {
state,
world: Arc::new(World::generate(DEFAULT_WORLD_SEED)),
world: Arc::new(World::generate(settings.world_seed)),
postoffice: PostOffice::bind(addrs.into())?,
clients: Clients::empty(),
@ -100,7 +100,9 @@ impl Server {
chunk_rx,
pending_chunks: HashSet::new(),
server_settings: settings,
server_info: ServerInfo {
// TODO: get from settings
name: "Server name".to_owned(),
description: "This is the best Veloren server.".to_owned(),
},

52
server/src/settings.rs Normal file
View File

@ -0,0 +1,52 @@
use serde_derive::{Deserialize, Serialize};
use std::{fs, io::prelude::*, path::PathBuf, net::SocketAddr};
/// `ControlSettings` contains keybindings.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct ServerSettings {
pub address: SocketAddr,
//pub max_players: u64,
pub world_seed: u32,
//pub pvp_enabled: bool,
//pub serverinfo: whatever
//pub login_server: whatever
}
impl Default for ServerSettings {
fn default() -> Self {
Self {
address: SocketAddr::from(([0; 4], 59003)),
world_seed: 1337,
}
}
}
impl ServerSettings {
pub fn load() -> Self {
let path = ServerSettings::get_settings_path();
// If file doesn't exist, use the default settings.
if let Ok(file) = fs::File::open(path) {
// TODO: Replace expect with returning default?
ron::de::from_reader(file).expect("Error parsing settings")
} else {
// TODO: temporary
Self::default().save_to_file().unwrap();
Self::default()
}
}
pub fn save_to_file(&self) -> std::io::Result<()> {
let path = ServerSettings::get_settings_path();
let mut config_file = fs::File::create(path)?;
let s: &str = &ron::ser::to_string_pretty(self, ron::ser::PrettyConfig::default()).unwrap();
config_file.write_all(s.as_bytes()).unwrap();
Ok(())
}
fn get_settings_path() -> PathBuf {
PathBuf::from(r"settings.ron")
}
}

View File

@ -2,7 +2,7 @@ use client::Client;
use common::clock::Clock;
use log::info;
use portpicker::pick_unused_port;
use server::{Event, Input, Server};
use server::{Event, Input, Server, ServerSettings};
use std::{
net::SocketAddr,
sync::mpsc::{channel, Receiver, Sender, TryRecvError},
@ -36,7 +36,7 @@ impl Singleplayer {
));
// Create server
let server = Server::bind(sock.clone()).expect("Failed to create server instance!");
let server = Server::bind(sock.clone(), ServerSettings::default()).expect("Failed to create server instance!");
let server = match client {
Some(client) => server.with_thread_pool(client.thread_pool().clone()),