mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Merge branch 'shandley/disconnection-kick-warnings' into 'master'
Network timeout updates and singleplayer UI improvements Closes #25 See merge request veloren/veloren!599
This commit is contained in:
commit
06c41f1c31
@ -77,8 +77,17 @@ fn main() {
|
||||
match event {
|
||||
Event::Chat { message, .. } => println!("{}", message),
|
||||
Event::Disconnect => {} // TODO
|
||||
Event::DisconnectionNotification(time) => {
|
||||
let message = match time {
|
||||
0 => String::from("Goodbye!"),
|
||||
_ => format!("Connection lost. Kicking in {} seconds", time),
|
||||
};
|
||||
|
||||
println!("{}", message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up the server after a tick.
|
||||
client.cleanup();
|
||||
|
||||
|
@ -30,14 +30,20 @@ use std::{
|
||||
use uvth::{ThreadPool, ThreadPoolBuilder};
|
||||
use vek::*;
|
||||
|
||||
// The duration of network inactivity until the player is kicked to the main menu
|
||||
// @TODO in the future, this should be configurable on the server, and be provided to the client
|
||||
const SERVER_TIMEOUT: Duration = Duration::from_secs(20);
|
||||
|
||||
// After this duration has elapsed, the user will begin getting kick warnings in their chat window
|
||||
const SERVER_TIMEOUT_GRACE_PERIOD: Duration = Duration::from_secs(14);
|
||||
|
||||
pub enum Event {
|
||||
Chat {
|
||||
chat_type: ChatType,
|
||||
message: String,
|
||||
},
|
||||
Disconnect,
|
||||
DisconnectionNotification(u64),
|
||||
}
|
||||
|
||||
pub struct Client {
|
||||
@ -49,6 +55,7 @@ pub struct Client {
|
||||
postbox: PostBox<ClientMsg, ServerMsg>,
|
||||
|
||||
last_server_ping: Instant,
|
||||
last_server_pong: Instant,
|
||||
last_ping_delta: f64,
|
||||
|
||||
tick: u64,
|
||||
@ -134,6 +141,7 @@ impl Client {
|
||||
postbox,
|
||||
|
||||
last_server_ping: Instant::now(),
|
||||
last_server_pong: Instant::now(),
|
||||
last_ping_delta: 0.0,
|
||||
|
||||
tick: 0,
|
||||
@ -494,6 +502,23 @@ impl Client {
|
||||
fn handle_new_messages(&mut self) -> Result<Vec<Event>, Error> {
|
||||
let mut frontend_events = Vec::new();
|
||||
|
||||
// Check that we have an valid connection.
|
||||
// Use the last ping time as a 1s rate limiter, we only notify the user once per second
|
||||
if Instant::now().duration_since(self.last_server_ping) > Duration::from_secs(1) {
|
||||
let duration_since_last_pong = Instant::now().duration_since(self.last_server_pong);
|
||||
|
||||
// Dispatch a notification to the HUD warning they will be kicked in {n} seconds
|
||||
if duration_since_last_pong.as_secs() >= SERVER_TIMEOUT_GRACE_PERIOD.as_secs() {
|
||||
if let Some(seconds_until_kick) =
|
||||
SERVER_TIMEOUT.checked_sub(duration_since_last_pong)
|
||||
{
|
||||
frontend_events.push(Event::DisconnectionNotification(
|
||||
seconds_until_kick.as_secs(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let new_msgs = self.postbox.new_messages();
|
||||
|
||||
if new_msgs.len() > 0 {
|
||||
@ -508,6 +533,8 @@ impl Client {
|
||||
ServerMsg::InitialSync { .. } => return Err(Error::ServerWentMad),
|
||||
ServerMsg::Ping => self.postbox.send_message(ClientMsg::Pong),
|
||||
ServerMsg::Pong => {
|
||||
self.last_server_pong = Instant::now();
|
||||
|
||||
self.last_ping_delta = Instant::now()
|
||||
.duration_since(self.last_server_ping)
|
||||
.as_secs_f64()
|
||||
@ -591,7 +618,7 @@ impl Client {
|
||||
} else if let Some(err) = self.postbox.error() {
|
||||
return Err(err.into());
|
||||
// We regularily ping in the tick method
|
||||
} else if Instant::now().duration_since(self.last_server_ping) > SERVER_TIMEOUT {
|
||||
} else if Instant::now().duration_since(self.last_server_pong) > SERVER_TIMEOUT {
|
||||
return Err(Error::ServerTimeout);
|
||||
}
|
||||
Ok(frontend_events)
|
||||
|
@ -18,10 +18,14 @@ fn main() {
|
||||
|
||||
// Load settings
|
||||
let settings = ServerSettings::load();
|
||||
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: {}", metrics_port);
|
||||
|
||||
loop {
|
||||
let events = server
|
||||
.tick(Input::default(), clock.get_last_delta())
|
||||
|
@ -43,6 +43,7 @@ pub struct GlobalState {
|
||||
settings: Settings,
|
||||
window: Window,
|
||||
audio: AudioFrontend,
|
||||
info_message: Option<String>,
|
||||
}
|
||||
|
||||
impl GlobalState {
|
||||
@ -116,6 +117,7 @@ fn main() {
|
||||
audio,
|
||||
window: Window::new(&settings).expect("Failed to create window!"),
|
||||
settings,
|
||||
info_message: None,
|
||||
};
|
||||
let settings = &global_state.settings;
|
||||
|
||||
|
@ -113,9 +113,15 @@ impl PlayState for CharSelectionState {
|
||||
.borrow_mut()
|
||||
.tick(comp::ControllerInputs::default(), clock.get_last_delta())
|
||||
{
|
||||
error!("Failed to tick the scene: {:?}", err);
|
||||
global_state.info_message = Some(
|
||||
"Connection lost!\nDid the server restart?\nIs the client up to date?"
|
||||
.to_owned(),
|
||||
);
|
||||
error!("[session] Failed to tick the scene: {:?}", err);
|
||||
|
||||
return PlayStateResult::Pop;
|
||||
}
|
||||
|
||||
self.client.borrow_mut().cleanup();
|
||||
|
||||
// Finish the frame.
|
||||
|
@ -1,7 +1,8 @@
|
||||
use client::{error::Error as ClientError, Client};
|
||||
use common::comp;
|
||||
use crossbeam::channel::{unbounded, Receiver, TryRecvError};
|
||||
use log::info;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::{net::ToSocketAddrs, thread, time::Duration};
|
||||
|
||||
#[cfg(feature = "discord")]
|
||||
@ -11,10 +12,11 @@ use crate::{discord, discord::DiscordUpdate};
|
||||
pub enum Error {
|
||||
// Error parsing input string or error resolving host name.
|
||||
BadAddress(std::io::Error),
|
||||
// Parsing/host name resolution successful but could not connect.
|
||||
#[allow(dead_code)]
|
||||
ConnectionFailed(ClientError),
|
||||
// Parsing yielded an empty iterator (specifically to_socket_addrs()).
|
||||
NoAddress,
|
||||
// Parsing/host name resolution successful but could not connect.
|
||||
ConnectionFailed(ClientError),
|
||||
InvalidAuth,
|
||||
ClientCrashed,
|
||||
ServerIsFull,
|
||||
@ -24,24 +26,21 @@ pub enum Error {
|
||||
// and create the client (which involves establishing a connection to the server).
|
||||
pub struct ClientInit {
|
||||
rx: Receiver<Result<Client, Error>>,
|
||||
cancel: Arc<AtomicBool>,
|
||||
}
|
||||
impl ClientInit {
|
||||
pub fn new(
|
||||
connection_args: (String, u16, bool),
|
||||
player: comp::Player,
|
||||
password: String,
|
||||
wait: bool,
|
||||
) -> Self {
|
||||
let (server_address, default_port, prefer_ipv6) = connection_args;
|
||||
|
||||
let (tx, rx) = unbounded();
|
||||
let cancel = Arc::new(AtomicBool::new(false));
|
||||
let cancel2 = Arc::clone(&cancel);
|
||||
|
||||
thread::spawn(move || {
|
||||
// Sleep the thread to wait for the single-player server to start up.
|
||||
if wait {
|
||||
info!("Waiting for server to come up...");
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
}
|
||||
// Parse ip address or resolves hostname.
|
||||
// Note: if you use an ipv6 address, the number after the last colon will be used
|
||||
// as the port unless you use [] around the address.
|
||||
@ -55,51 +54,59 @@ impl ClientInit {
|
||||
|
||||
let mut last_err = None;
|
||||
|
||||
for socket_addr in first_addrs.into_iter().chain(second_addrs) {
|
||||
match Client::new(socket_addr, player.view_distance) {
|
||||
Ok(mut client) => {
|
||||
if let Err(ClientError::InvalidAuth) =
|
||||
client.register(player, password)
|
||||
{
|
||||
last_err = Some(Error::InvalidAuth);
|
||||
break;
|
||||
}
|
||||
//client.register(player, password);
|
||||
let _ = tx.send(Ok(client));
|
||||
|
||||
#[cfg(feature = "discord")]
|
||||
{
|
||||
if !server_address.eq("127.0.0.1") {
|
||||
discord::send_all(vec![
|
||||
DiscordUpdate::Details(server_address),
|
||||
DiscordUpdate::State("Playing...".into()),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
Err(err) => {
|
||||
match err {
|
||||
// Assume the connection failed and try next address.
|
||||
ClientError::Network(_) => {
|
||||
last_err = Some(Error::ConnectionFailed(err))
|
||||
}
|
||||
ClientError::TooManyPlayers => {
|
||||
last_err = Some(Error::ServerIsFull);
|
||||
'tries: for _ in 0..=60 {
|
||||
// 300 Seconds
|
||||
if cancel2.load(Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
for socket_addr in
|
||||
first_addrs.clone().into_iter().chain(second_addrs.clone())
|
||||
{
|
||||
match Client::new(socket_addr, player.view_distance) {
|
||||
Ok(mut client) => {
|
||||
if let Err(ClientError::InvalidAuth) =
|
||||
client.register(player.clone(), password.clone())
|
||||
{
|
||||
last_err = Some(Error::InvalidAuth);
|
||||
break;
|
||||
}
|
||||
ClientError::InvalidAuth => {
|
||||
last_err = Some(Error::InvalidAuth);
|
||||
//client.register(player, password);
|
||||
let _ = tx.send(Ok(client));
|
||||
|
||||
#[cfg(feature = "discord")]
|
||||
{
|
||||
if !server_address.eq("127.0.0.1") {
|
||||
discord::send_all(vec![
|
||||
DiscordUpdate::Details(server_address),
|
||||
DiscordUpdate::State("Playing...".into()),
|
||||
]);
|
||||
}
|
||||
}
|
||||
// TODO: Handle errors?
|
||||
_ => panic!(
|
||||
|
||||
return;
|
||||
}
|
||||
Err(err) => {
|
||||
match err {
|
||||
// Assume the connection failed and try again soon
|
||||
ClientError::Network(_) => {}
|
||||
ClientError::TooManyPlayers => {
|
||||
last_err = Some(Error::ServerIsFull);
|
||||
break 'tries;
|
||||
}
|
||||
ClientError::InvalidAuth => {
|
||||
last_err = Some(Error::InvalidAuth);
|
||||
break 'tries;
|
||||
}
|
||||
// TODO: Handle errors?
|
||||
_ => panic!(
|
||||
"Unexpected non-network error when creating client: {:?}",
|
||||
err
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
thread::sleep(Duration::from_secs(5));
|
||||
}
|
||||
// Parsing/host name resolution successful but no connection succeeded.
|
||||
let _ = tx.send(Err(last_err.unwrap_or(Error::NoAddress)));
|
||||
@ -111,7 +118,7 @@ impl ClientInit {
|
||||
}
|
||||
});
|
||||
|
||||
ClientInit { rx }
|
||||
ClientInit { rx, cancel }
|
||||
}
|
||||
/// Poll if the thread is complete.
|
||||
/// Returns None if the thread is still running, otherwise returns the Result of client creation.
|
||||
@ -122,4 +129,13 @@ impl ClientInit {
|
||||
Err(TryRecvError::Disconnected) => Some(Err(Error::ClientCrashed)),
|
||||
}
|
||||
}
|
||||
pub fn cancel(&mut self) {
|
||||
self.cancel.store(true, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ClientInit {
|
||||
fn drop(&mut self) {
|
||||
self.cancel();
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,23 @@
|
||||
mod client_init;
|
||||
#[cfg(feature = "singleplayer")]
|
||||
mod start_singleplayer;
|
||||
mod ui;
|
||||
|
||||
use super::char_selection::CharSelectionState;
|
||||
use crate::{window::Event, Direction, GlobalState, PlayState, PlayStateResult};
|
||||
use crate::{
|
||||
singleplayer::Singleplayer, window::Event, Direction, GlobalState, PlayState, PlayStateResult,
|
||||
};
|
||||
use argon2::{self, Config};
|
||||
use client_init::{ClientInit, Error as InitError};
|
||||
use common::{clock::Clock, comp};
|
||||
use log::warn;
|
||||
#[cfg(feature = "singleplayer")]
|
||||
use start_singleplayer::StartSingleplayerState;
|
||||
use std::time::Duration;
|
||||
use ui::{Event as MainMenuEvent, MainMenuUi};
|
||||
|
||||
pub struct MainMenuState {
|
||||
main_menu_ui: MainMenuUi,
|
||||
title_music_channel: Option<usize>,
|
||||
singleplayer: Option<Singleplayer>,
|
||||
}
|
||||
|
||||
impl MainMenuState {
|
||||
@ -25,6 +26,7 @@ impl MainMenuState {
|
||||
Self {
|
||||
main_menu_ui: MainMenuUi::new(global_state),
|
||||
title_music_channel: None,
|
||||
singleplayer: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -48,6 +50,9 @@ impl PlayState for MainMenuState {
|
||||
)
|
||||
}
|
||||
|
||||
// Reset singleplayer server if it was running already
|
||||
self.singleplayer = None;
|
||||
|
||||
loop {
|
||||
// Handle window events.
|
||||
for event in global_state.window.fetch_events() {
|
||||
@ -75,11 +80,11 @@ impl PlayState for MainMenuState {
|
||||
}
|
||||
Some(Err(err)) => {
|
||||
client_init = None;
|
||||
self.main_menu_ui.login_error(
|
||||
global_state.info_message = Some(
|
||||
match err {
|
||||
InitError::BadAddress(_) | InitError::NoAddress => "Server not found",
|
||||
InitError::InvalidAuth => "Invalid credentials",
|
||||
InitError::ServerIsFull => "Server is Full!",
|
||||
InitError::ServerIsFull => "Server is full",
|
||||
InitError::ConnectionFailed(_) => "Connection failed",
|
||||
InitError::ClientCrashed => "Client crashed",
|
||||
}
|
||||
@ -100,49 +105,37 @@ impl PlayState for MainMenuState {
|
||||
password,
|
||||
server_address,
|
||||
} => {
|
||||
let mut net_settings = &mut global_state.settings.networking;
|
||||
net_settings.username = username.clone();
|
||||
net_settings.password = password.clone();
|
||||
if !net_settings.servers.contains(&server_address) {
|
||||
net_settings.servers.push(server_address.clone());
|
||||
}
|
||||
if let Err(err) = global_state.settings.save_to_file() {
|
||||
warn!("Failed to save settings: {:?}", err);
|
||||
}
|
||||
|
||||
let player = comp::Player::new(
|
||||
username.clone(),
|
||||
Some(global_state.settings.graphics.view_distance),
|
||||
attempt_login(
|
||||
global_state,
|
||||
username,
|
||||
password,
|
||||
server_address,
|
||||
DEFAULT_PORT,
|
||||
&mut client_init,
|
||||
);
|
||||
|
||||
if player.is_valid() {
|
||||
// Don't try to connect if there is already a connection in progress.
|
||||
client_init = client_init.or(Some(ClientInit::new(
|
||||
(server_address, DEFAULT_PORT, false),
|
||||
player,
|
||||
{
|
||||
let salt = b"staticsalt_zTuGkGvybZIjZbNUDtw15";
|
||||
let config = Config::default();
|
||||
argon2::hash_encoded(password.as_bytes(), salt, &config)
|
||||
.unwrap()
|
||||
},
|
||||
false,
|
||||
)));
|
||||
} else {
|
||||
self.main_menu_ui
|
||||
.login_error("Invalid username or password".to_string());
|
||||
}
|
||||
}
|
||||
MainMenuEvent::CancelLoginAttempt => {
|
||||
// client_init contains Some(ClientInit), which spawns a thread which contains a TcpStream::connect() call
|
||||
// This call is blocking
|
||||
// TODO fix when the network rework happens
|
||||
self.singleplayer = None;
|
||||
client_init = None;
|
||||
self.main_menu_ui.cancel_connection();
|
||||
}
|
||||
#[cfg(feature = "singleplayer")]
|
||||
MainMenuEvent::StartSingleplayer => {
|
||||
return PlayStateResult::Push(Box::new(StartSingleplayerState::new()));
|
||||
let (singleplayer, server_settings) = Singleplayer::new(None); // TODO: Make client and server use the same thread pool
|
||||
|
||||
self.singleplayer = Some(singleplayer);
|
||||
|
||||
attempt_login(
|
||||
global_state,
|
||||
"singleplayer".to_owned(),
|
||||
"".to_owned(),
|
||||
server_settings.gameserver_address.ip().to_string(),
|
||||
server_settings.gameserver_address.port(),
|
||||
&mut client_init,
|
||||
);
|
||||
}
|
||||
MainMenuEvent::Settings => {} // TODO
|
||||
MainMenuEvent::Quit => return PlayStateResult::Shutdown,
|
||||
@ -152,6 +145,10 @@ impl PlayState for MainMenuState {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(info) = global_state.info_message.take() {
|
||||
self.main_menu_ui.show_info(info);
|
||||
}
|
||||
|
||||
// Draw the UI to the screen.
|
||||
self.main_menu_ui.render(global_state.window.renderer_mut());
|
||||
|
||||
@ -173,3 +170,44 @@ impl PlayState for MainMenuState {
|
||||
"Title"
|
||||
}
|
||||
}
|
||||
|
||||
fn attempt_login(
|
||||
global_state: &mut GlobalState,
|
||||
username: String,
|
||||
password: String,
|
||||
server_address: String,
|
||||
server_port: u16,
|
||||
client_init: &mut Option<ClientInit>,
|
||||
) {
|
||||
let mut net_settings = &mut global_state.settings.networking;
|
||||
net_settings.username = username.clone();
|
||||
net_settings.password = password.clone();
|
||||
if !net_settings.servers.contains(&server_address) {
|
||||
net_settings.servers.push(server_address.clone());
|
||||
}
|
||||
if let Err(err) = global_state.settings.save_to_file() {
|
||||
warn!("Failed to save settings: {:?}", err);
|
||||
}
|
||||
|
||||
let player = comp::Player::new(
|
||||
username.clone(),
|
||||
Some(global_state.settings.graphics.view_distance),
|
||||
);
|
||||
|
||||
if player.is_valid() {
|
||||
// Don't try to connect if there is already a connection in progress.
|
||||
if client_init.is_none() {
|
||||
*client_init = Some(ClientInit::new(
|
||||
(server_address, server_port, false),
|
||||
player,
|
||||
{
|
||||
let salt = b"staticsalt_zTuGkGvybZIjZbNUDtw15";
|
||||
let config = Config::default();
|
||||
argon2::hash_encoded(password.as_bytes(), salt, &config).unwrap()
|
||||
},
|
||||
));
|
||||
}
|
||||
} else {
|
||||
global_state.info_message = Some("Invalid username or password".to_string());
|
||||
}
|
||||
}
|
||||
|
@ -1,79 +0,0 @@
|
||||
use super::client_init::ClientInit;
|
||||
use crate::{
|
||||
menu::char_selection::CharSelectionState, singleplayer::Singleplayer, Direction, GlobalState,
|
||||
PlayState, PlayStateResult,
|
||||
};
|
||||
use common::comp;
|
||||
use log::{info, warn};
|
||||
use server::settings::ServerSettings;
|
||||
|
||||
pub struct StartSingleplayerState {
|
||||
// Necessary to keep singleplayer working
|
||||
_singleplayer: Singleplayer,
|
||||
server_settings: ServerSettings,
|
||||
}
|
||||
|
||||
impl StartSingleplayerState {
|
||||
/// Create a new `MainMenuState`.
|
||||
pub fn new() -> Self {
|
||||
let (_singleplayer, server_settings) = Singleplayer::new(None); // TODO: Make client and server use the same thread pool
|
||||
|
||||
Self {
|
||||
_singleplayer,
|
||||
server_settings,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PlayState for StartSingleplayerState {
|
||||
fn play(&mut self, direction: Direction, global_state: &mut GlobalState) -> PlayStateResult {
|
||||
match direction {
|
||||
Direction::Forwards => {
|
||||
let username = "singleplayer".to_owned();
|
||||
|
||||
let client_init = ClientInit::new(
|
||||
//TODO: check why we are converting out IP:Port to String instead of parsing it directly as SockAddr
|
||||
(
|
||||
self.server_settings.gameserver_address.ip().to_string(),
|
||||
self.server_settings.gameserver_address.port(),
|
||||
true,
|
||||
),
|
||||
comp::Player::new(
|
||||
username.clone(),
|
||||
Some(global_state.settings.graphics.view_distance),
|
||||
),
|
||||
String::default(),
|
||||
true,
|
||||
);
|
||||
|
||||
// Create the client.
|
||||
let client = loop {
|
||||
match client_init.poll() {
|
||||
Some(Ok(client)) => break client,
|
||||
Some(Err(err)) => {
|
||||
warn!("Failed to start single-player server: {:?}", err);
|
||||
return PlayStateResult::Pop;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
};
|
||||
|
||||
// Print the metrics port
|
||||
info!(
|
||||
"Metrics port: {}",
|
||||
self.server_settings.metrics_address.port()
|
||||
);
|
||||
|
||||
PlayStateResult::Push(Box::new(CharSelectionState::new(
|
||||
global_state,
|
||||
std::rc::Rc::new(std::cell::RefCell::new(client)),
|
||||
)))
|
||||
}
|
||||
Direction::Backwards => PlayStateResult::Pop,
|
||||
}
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Starting Single-Player"
|
||||
}
|
||||
}
|
@ -353,10 +353,11 @@ impl MainMenuUi {
|
||||
macro_rules! singleplayer {
|
||||
() => {
|
||||
events.push(Event::StartSingleplayer);
|
||||
events.push(Event::LoginAttempt {
|
||||
username: "singleplayer".to_string(),
|
||||
password: String::default(),
|
||||
server_address: "localhost".to_string(),
|
||||
self.connecting = Some(std::time::Instant::now());
|
||||
self.popup = Some(PopupData {
|
||||
msg: "Connecting...".to_string(),
|
||||
button_text: "Cancel".to_string(),
|
||||
popup_type: PopupType::ConnectionInfo,
|
||||
});
|
||||
};
|
||||
}
|
||||
@ -425,20 +426,20 @@ impl MainMenuUi {
|
||||
.rgba(1.0, 1.0, 1.0, 1.0)
|
||||
.font_size(25)
|
||||
.font_id(self.fonts.cyri);
|
||||
Rectangle::fill_with([65.0 * 6.0, 100.0], color::TRANSPARENT)
|
||||
Rectangle::fill_with([65.0 * 6.0, 140.0], color::TRANSPARENT)
|
||||
.rgba(0.1, 0.1, 0.1, 1.0)
|
||||
.parent(ui_widgets.window)
|
||||
.up_from(self.ids.banner_top, 20.0)
|
||||
.up_from(self.ids.banner_top, 15.0)
|
||||
.set(self.ids.login_error_bg, ui_widgets);
|
||||
Image::new(self.imgs.info_frame)
|
||||
.w_h(65.0 * 6.0, 100.0)
|
||||
.w_h(65.0 * 6.0, 140.0)
|
||||
.middle_of(self.ids.login_error_bg)
|
||||
.set(self.ids.error_frame, ui_widgets);
|
||||
text.mid_top_with_margin_on(self.ids.error_frame, 10.0)
|
||||
.set(self.ids.login_error, ui_widgets);
|
||||
if Button::image(self.imgs.button)
|
||||
.w_h(100.0, 30.0)
|
||||
.mid_bottom_with_margin_on(self.ids.login_error_bg, 5.0)
|
||||
.mid_bottom_with_margin_on(self.ids.login_error_bg, 10.0)
|
||||
.hover_image(self.imgs.button_hover)
|
||||
.press_image(self.imgs.button_press)
|
||||
.label_y(Relative::Scalar(2.0))
|
||||
@ -650,7 +651,7 @@ impl MainMenuUi {
|
||||
events
|
||||
}
|
||||
|
||||
pub fn login_error(&mut self, msg: String) {
|
||||
pub fn show_info(&mut self, msg: String) {
|
||||
self.popup = Some(PopupData {
|
||||
msg,
|
||||
button_text: "Okay".to_string(),
|
||||
|
@ -6,7 +6,7 @@ use crate::{
|
||||
window::{Event, GameInput},
|
||||
Direction, Error, GlobalState, PlayState, PlayStateResult,
|
||||
};
|
||||
use client::{self, Client};
|
||||
use client::{self, Client, Event::Chat};
|
||||
use common::{
|
||||
clock::Clock,
|
||||
comp,
|
||||
@ -14,6 +14,7 @@ use common::{
|
||||
msg::ClientState,
|
||||
terrain::{Block, BlockKind},
|
||||
vol::ReadVol,
|
||||
ChatType,
|
||||
};
|
||||
use log::error;
|
||||
use specs::Join;
|
||||
@ -55,13 +56,24 @@ impl SessionState {
|
||||
fn tick(&mut self, dt: Duration) -> Result<(), Error> {
|
||||
for event in self.client.borrow_mut().tick(self.inputs.clone(), dt)? {
|
||||
match event {
|
||||
client::Event::Chat {
|
||||
Chat {
|
||||
chat_type: _,
|
||||
message: _,
|
||||
} => {
|
||||
self.hud.new_message(event);
|
||||
}
|
||||
client::Event::Disconnect => {} // TODO
|
||||
client::Event::DisconnectionNotification(time) => {
|
||||
let message = match time {
|
||||
0 => String::from("Goodbye!"),
|
||||
_ => format!("Connection lost. Kicking in {} seconds", time),
|
||||
};
|
||||
|
||||
self.hud.new_message(Chat {
|
||||
chat_type: ChatType::Meta,
|
||||
message,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -325,7 +337,12 @@ impl PlayState for SessionState {
|
||||
|
||||
// Perform an in-game tick.
|
||||
if let Err(err) = self.tick(clock.get_avg_delta()) {
|
||||
error!("Failed to tick the scene: {:?}", err);
|
||||
global_state.info_message = Some(
|
||||
"Connection lost!\nDid the server restart?\nIs the client up to date?"
|
||||
.to_owned(),
|
||||
);
|
||||
error!("[session] Failed to tick the scene: {:?}", err);
|
||||
|
||||
return PlayStateResult::Pop;
|
||||
}
|
||||
|
||||
|
@ -30,14 +30,18 @@ impl Singleplayer {
|
||||
|
||||
// Create server
|
||||
let settings = ServerSettings::singleplayer();
|
||||
let server = Server::new(settings.clone()).expect("Failed to create server instance!");
|
||||
|
||||
let server = match client {
|
||||
Some(client) => server.with_thread_pool(client.thread_pool().clone()),
|
||||
None => server,
|
||||
};
|
||||
let thread_pool = client.map(|c| c.thread_pool().clone());
|
||||
let settings2 = settings.clone();
|
||||
|
||||
let thread = thread::spawn(move || {
|
||||
let server = Server::new(settings2).expect("Failed to create server instance!");
|
||||
|
||||
let server = match thread_pool {
|
||||
Some(pool) => server.with_thread_pool(pool),
|
||||
None => server,
|
||||
};
|
||||
|
||||
run_server(server, receiver);
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user