2019-06-05 13:13:24 +00:00
|
|
|
use client::Client;
|
2019-06-05 15:22:06 +00:00
|
|
|
use common::clock::Clock;
|
2019-08-15 22:07:09 +00:00
|
|
|
use crossbeam::channel::{unbounded, Receiver, Sender, TryRecvError};
|
2019-06-29 16:41:26 +00:00
|
|
|
use server::{Event, Input, Server, ServerSettings};
|
2019-05-06 14:26:10 +00:00
|
|
|
use std::{
|
2020-03-04 20:42:22 +00:00
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
2019-05-06 14:26:10 +00:00
|
|
|
thread::{self, JoinHandle},
|
2019-05-09 17:58:16 +00:00
|
|
|
time::Duration,
|
2019-05-06 14:26:10 +00:00
|
|
|
};
|
2020-06-21 10:22:26 +00:00
|
|
|
use tracing::info;
|
2019-04-17 13:41:34 +00:00
|
|
|
|
|
|
|
const TPS: u64 = 30;
|
|
|
|
|
2019-04-17 14:57:23 +00:00
|
|
|
enum Msg {
|
|
|
|
Stop,
|
|
|
|
}
|
|
|
|
|
2019-04-17 15:22:26 +00:00
|
|
|
/// Used to start and stop the background thread running the server
|
|
|
|
/// when in singleplayer mode.
|
2019-04-17 14:57:23 +00:00
|
|
|
pub struct Singleplayer {
|
2019-07-02 21:29:38 +00:00
|
|
|
_server_thread: JoinHandle<()>,
|
2019-04-17 14:57:23 +00:00
|
|
|
sender: Sender<Msg>,
|
2020-03-03 19:51:15 +00:00
|
|
|
// Wether the server is stopped or not
|
2020-03-04 20:42:22 +00:00
|
|
|
paused: Arc<AtomicBool>,
|
2019-04-17 14:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Singleplayer {
|
2019-10-11 12:19:55 +00:00
|
|
|
pub fn new(client: Option<&Client>) -> (Self, ServerSettings) {
|
2019-08-15 22:07:09 +00:00
|
|
|
let (sender, receiver) = unbounded();
|
2019-05-06 14:26:10 +00:00
|
|
|
|
2019-05-21 22:31:17 +00:00
|
|
|
// Create server
|
2020-05-13 23:37:53 +00:00
|
|
|
let settings = ServerSettings::singleplayer(
|
|
|
|
crate::settings::Settings::get_settings_path()
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join("saves")
|
|
|
|
.to_string_lossy()
|
|
|
|
.to_string(),
|
|
|
|
);
|
2019-06-05 13:13:24 +00:00
|
|
|
|
2019-10-18 20:05:37 +00:00
|
|
|
let thread_pool = client.map(|c| c.thread_pool().clone());
|
|
|
|
let settings2 = settings.clone();
|
2019-05-21 22:31:17 +00:00
|
|
|
|
2020-03-04 20:42:22 +00:00
|
|
|
let paused = Arc::new(AtomicBool::new(false));
|
|
|
|
let paused1 = paused.clone();
|
|
|
|
|
2020-07-01 07:30:38 +00:00
|
|
|
let server = Server::new(settings2).expect("Failed to create server instance!");
|
2019-10-18 20:05:37 +00:00
|
|
|
|
2020-07-01 07:30:38 +00:00
|
|
|
let server = match thread_pool {
|
|
|
|
Some(pool) => server.with_thread_pool(pool),
|
|
|
|
None => server,
|
|
|
|
};
|
2019-10-18 20:05:37 +00:00
|
|
|
|
2020-07-01 07:30:38 +00:00
|
|
|
let thread = thread::spawn(move || {
|
2020-03-04 20:42:22 +00:00
|
|
|
run_server(server, receiver, paused1);
|
2019-04-17 14:57:23 +00:00
|
|
|
});
|
2019-05-06 14:26:10 +00:00
|
|
|
|
2019-05-09 17:58:16 +00:00
|
|
|
(
|
|
|
|
Singleplayer {
|
2019-07-02 21:29:38 +00:00
|
|
|
_server_thread: thread,
|
2019-05-09 17:58:16 +00:00
|
|
|
sender,
|
2020-03-04 20:42:22 +00:00
|
|
|
paused,
|
2019-05-09 17:58:16 +00:00
|
|
|
},
|
2019-10-11 12:19:55 +00:00
|
|
|
settings,
|
2019-05-09 17:58:16 +00:00
|
|
|
)
|
2019-04-17 14:57:23 +00:00
|
|
|
}
|
2020-03-01 22:18:22 +00:00
|
|
|
|
2020-03-03 19:51:15 +00:00
|
|
|
/// Returns wether or not the server is paused
|
2020-03-03 21:26:59 +00:00
|
|
|
pub fn is_paused(&self) -> bool { self.paused.load(Ordering::SeqCst) }
|
2020-03-03 19:51:15 +00:00
|
|
|
|
|
|
|
/// Pauses if true is passed and unpauses if false (Does nothing if in that
|
|
|
|
/// state already)
|
2020-03-04 20:42:22 +00:00
|
|
|
pub fn pause(&self, state: bool) { self.paused.store(state, Ordering::SeqCst); }
|
2019-04-17 14:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Singleplayer {
|
|
|
|
fn drop(&mut self) {
|
2019-05-17 07:55:51 +00:00
|
|
|
// Ignore the result
|
|
|
|
let _ = self.sender.send(Msg::Stop);
|
2019-04-17 14:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 20:42:22 +00:00
|
|
|
fn run_server(mut server: Server, rec: Receiver<Msg>, paused: Arc<AtomicBool>) {
|
2019-04-17 13:41:34 +00:00
|
|
|
info!("Starting server-cli...");
|
|
|
|
|
|
|
|
// Set up an fps clock
|
2019-07-01 20:42:43 +00:00
|
|
|
let mut clock = Clock::start();
|
2019-07-05 11:58:52 +00:00
|
|
|
|
2019-04-17 13:41:34 +00:00
|
|
|
loop {
|
2020-03-03 19:51:15 +00:00
|
|
|
// Check any event such as stopping and pausing
|
2020-03-01 22:18:22 +00:00
|
|
|
match rec.try_recv() {
|
|
|
|
Ok(msg) => match msg {
|
|
|
|
Msg::Stop => break,
|
|
|
|
},
|
|
|
|
Err(err) => match err {
|
|
|
|
TryRecvError::Empty => (),
|
|
|
|
TryRecvError::Disconnected => break,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-03-03 19:51:15 +00:00
|
|
|
// Wait for the next tick.
|
|
|
|
clock.tick(Duration::from_millis(1000 / TPS));
|
|
|
|
|
|
|
|
// Skip updating the server if it's paused
|
2020-03-04 20:42:22 +00:00
|
|
|
if paused.load(Ordering::SeqCst) && server.number_of_players() < 2 {
|
2020-03-01 22:18:22 +00:00
|
|
|
continue;
|
2020-03-04 20:42:22 +00:00
|
|
|
} else if server.number_of_players() > 1 {
|
|
|
|
paused.store(false, Ordering::SeqCst);
|
2020-03-01 22:18:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 20:37:19 +00:00
|
|
|
let events = server
|
|
|
|
.tick(Input::default(), clock.get_last_delta())
|
2019-05-17 09:22:32 +00:00
|
|
|
.expect("Failed to tick server!");
|
2019-04-17 13:41:34 +00:00
|
|
|
|
|
|
|
for event in events {
|
|
|
|
match event {
|
2019-07-02 21:29:38 +00:00
|
|
|
Event::ClientConnected { .. } => info!("Client connected!"),
|
|
|
|
Event::ClientDisconnected { .. } => info!("Client disconnected!"),
|
|
|
|
Event::Chat { entity: _, msg } => info!("[Client] {}", msg),
|
2019-04-17 13:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Clean up the server after a tick.
|
2019-04-17 13:41:34 +00:00
|
|
|
server.cleanup();
|
|
|
|
}
|
|
|
|
}
|