veloren/server-cli/src/main.rs

139 lines
4.0 KiB
Rust
Raw Normal View History

2019-08-19 12:39:23 +00:00
#![deny(unsafe_code)]
2020-08-31 08:41:11 +00:00
mod tui_runner;
mod tuilog;
2020-07-27 16:26:34 +00:00
#[macro_use] extern crate lazy_static;
2020-08-31 08:41:11 +00:00
use crate::{
tui_runner::{Message, Tui},
tuilog::TuiLog,
};
use common::clock::Clock;
2019-06-29 16:41:26 +00:00
use server::{Event, Input, Server, ServerSettings};
use tracing::{error, info, warn, Level};
use tracing_subscriber::{filter::LevelFilter, EnvFilter, FmtSubscriber};
2020-07-28 13:47:01 +00:00
use clap::{App, Arg};
2020-08-31 08:41:11 +00:00
use std::{io, sync::mpsc, time::Duration};
2020-07-27 16:26:34 +00:00
const TPS: u64 = 30;
const RUST_LOG_ENV: &str = "RUST_LOG";
2020-07-27 16:26:34 +00:00
lazy_static! {
static ref LOG: TuiLog<'static> = TuiLog::default();
}
fn main() -> io::Result<()> {
2020-07-28 13:47:01 +00:00
let matches = App::new("Veloren server cli")
.version(
format!(
"{}-{}",
env!("CARGO_PKG_VERSION"),
common::util::GIT_HASH.to_string()
)
.as_str(),
)
.author("The veloren devs <https://gitlab.com/veloren/veloren>")
2020-07-28 15:02:36 +00:00
.about("The veloren server cli provides an easy to use interface to start a veloren server")
2020-07-28 13:47:01 +00:00
.arg(
Arg::with_name("basic")
.short("b")
.long("basic")
.help("Disables the tui")
.takes_value(false),
)
.get_matches();
let basic = matches.is_present("basic");
2020-08-31 08:41:11 +00:00
let (mut tui, msg_r) = Tui::new();
2020-07-28 13:47:01 +00:00
// Init logging
let filter = match std::env::var_os(RUST_LOG_ENV).map(|s| s.into_string()) {
Some(Ok(env)) => {
let mut filter = EnvFilter::new("veloren_world::sim=info")
.add_directive("veloren_world::civ=info".parse().unwrap())
.add_directive(LevelFilter::INFO.into());
for s in env.split(',').into_iter() {
match s.parse() {
Ok(d) => filter = filter.add_directive(d),
Err(err) => println!("WARN ignoring log directive: `{}`: {}", s, err),
};
}
filter
},
_ => EnvFilter::from_env(RUST_LOG_ENV)
.add_directive("veloren_world::sim=info".parse().unwrap())
.add_directive("veloren_world::civ=info".parse().unwrap())
.add_directive(LevelFilter::INFO.into()),
};
2020-07-28 13:47:01 +00:00
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::ERROR)
2020-07-28 13:47:01 +00:00
.with_env_filter(filter);
if basic {
subscriber.init();
} else {
subscriber.with_writer(|| LOG.clone()).init();
}
2020-07-28 13:47:01 +00:00
if !basic {
2020-08-31 08:41:11 +00:00
tui.run();
2020-07-28 13:47:01 +00:00
}
info!("Starting server...");
// Set up an fps clock
let mut clock = Clock::start();
// Load settings
let settings = ServerSettings::load();
let server_port = &settings.gameserver_address.port();
let metrics_port = &settings.metrics_address.port();
// Create server
let mut server = Server::new(settings).expect("Failed to create server instance!");
info!("Server is ready to accept connections.");
info!(?metrics_port, "starting metrics at port");
info!(?server_port, "starting server at port");
loop {
let events = server
.tick(Input::default(), clock.get_last_delta())
.expect("Failed to tick server");
for event in events {
match event {
Event::ClientConnected { entity: _ } => info!("Client connected!"),
Event::ClientDisconnected { entity: _ } => info!("Client disconnected!"),
Event::Chat { entity: _, msg } => info!("[Client] {}", msg),
}
}
// Clean up the server after a tick.
server.cleanup();
2020-08-31 08:41:11 +00:00
match msg_r.try_recv() {
Ok(msg) => match msg {
Message::Quit => {
info!("Closing the server");
break;
},
},
Err(e) => match e {
mpsc::TryRecvError::Empty => {},
mpsc::TryRecvError::Disconnected => panic!(),
},
};
// Wait for the next tick.
clock.tick(Duration::from_millis(1000 / TPS));
}
2020-08-31 08:41:11 +00:00
drop(tui);
std::thread::sleep(Duration::from_millis(10));
Ok(())
}