still compiles

This commit is contained in:
telastrus 2019-08-07 23:56:02 -04:00
parent 157c4816b3
commit 6d94d43021
8 changed files with 41 additions and 8 deletions

View File

@ -30,6 +30,9 @@ fn main() {
println!("Enter the server address"); println!("Enter the server address");
let server_addr = read_input(); let server_addr = read_input();
println!("Enter your password");
let password = read_input();
// Create a client. // Create a client.
let mut client = Client::new( let mut client = Client::new(
server_addr server_addr
@ -45,7 +48,7 @@ fn main() {
println!("Players online: {:?}", client.get_players()); println!("Players online: {:?}", client.get_players());
client.register(comp::Player::new(username, None)); client.register(comp::Player::new(username, None), password);
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
thread::spawn(move || loop { thread::spawn(move || loop {

View File

@ -132,9 +132,13 @@ impl Client {
} }
/// Request a state transition to `ClientState::Registered`. /// Request a state transition to `ClientState::Registered`.
pub fn register(&mut self, player: comp::Player) { pub fn register(&mut self, player: comp::Player, password: String) -> Result<(), Error> {
self.postbox.send_message(ClientMsg::Register { player }); self.postbox.send_message(ClientMsg::Register { player, password });
self.client_state = ClientState::Pending; /*match self.postbox.next_message() {
}
self.client_state = ClientState::Pending;*/
Err(Error::InvalidAuth)
} }
/// Request a state transition to `ClientState::Character`. /// Request a state transition to `ClientState::Character`.
@ -397,6 +401,7 @@ impl Client {
match msg { match msg {
ServerMsg::Error(e) => match e { ServerMsg::Error(e) => match e {
ServerError::TooManyPlayers => return Err(Error::ServerWentMad), ServerError::TooManyPlayers => return Err(Error::ServerWentMad),
ServerError::InvalidAuth => return Err(Error::InvalidAuth),
//TODO: ServerError::InvalidAlias => return Err(Error::InvalidAlias), //TODO: ServerError::InvalidAlias => return Err(Error::InvalidAlias),
}, },
ServerMsg::Shutdown => return Err(Error::ServerShutdown), ServerMsg::Shutdown => return Err(Error::ServerShutdown),

View File

@ -7,6 +7,7 @@ use vek::*;
pub enum ClientMsg { pub enum ClientMsg {
Register { Register {
player: comp::Player, player: comp::Player,
password: String,
}, },
Character { Character {
name: String, name: String,

View File

@ -69,6 +69,7 @@ pub enum ServerMsg {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ServerError { pub enum ServerError {
TooManyPlayers, TooManyPlayers,
InvalidAuth,
//TODO: InvalidAlias, //TODO: InvalidAlias,
} }

View File

@ -26,7 +26,7 @@ use log::debug;
use rand::Rng; use rand::Rng;
use specs::{join::Join, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity}; use specs::{join::Join, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity};
use std::{ use std::{
collections::HashSet, collections::{HashSet, HashMap},
i32, i32,
net::SocketAddr, net::SocketAddr,
sync::{mpsc, Arc}, sync::{mpsc, Arc},
@ -68,6 +68,9 @@ pub struct Server {
server_settings: ServerSettings, server_settings: ServerSettings,
server_info: ServerInfo, server_info: ServerInfo,
// TODO: anything but this
accounts: HashMap<String, String>,
} }
impl Server { impl Server {
@ -107,6 +110,7 @@ impl Server {
description: settings.server_description.clone(), description: settings.server_description.clone(),
git_hash: common::util::GIT_HASH.to_string(), git_hash: common::util::GIT_HASH.to_string(),
}, },
accounts: HashMap::new(),
server_settings: settings, server_settings: settings,
}; };
@ -468,6 +472,8 @@ impl Server {
fn handle_new_messages(&mut self) -> Result<Vec<Event>, Error> { fn handle_new_messages(&mut self) -> Result<Vec<Event>, Error> {
let mut frontend_events = Vec::new(); let mut frontend_events = Vec::new();
let accounts = &mut self.accounts;
let state = &mut self.state; let state = &mut self.state;
let mut new_chat_msgs = Vec::new(); let mut new_chat_msgs = Vec::new();
let mut disconnected_clients = Vec::new(); let mut disconnected_clients = Vec::new();
@ -522,7 +528,16 @@ impl Server {
ClientState::Pending => {} ClientState::Pending => {}
}, },
// Valid player // Valid player
ClientMsg::Register { player } if player.is_valid() => { ClientMsg::Register { player, password } if player.is_valid() => {
if let Some(pass) = accounts.get(&player.alias) {
if pass != &password {
client.error_state(RequestStateError::Denied);
break;
}
} else {
accounts.insert(player.alias.clone(), password);
}
println!("{:?}", accounts);
match client.client_state { match client.client_state {
ClientState::Connected => { ClientState::Connected => {
Self::initialize_player(state, entity, client, player); Self::initialize_player(state, entity, client, player);

View File

@ -30,7 +30,7 @@ pub struct ClientInit {
rx: Receiver<Result<Client, Error>>, rx: Receiver<Result<Client, Error>>,
} }
impl ClientInit { impl ClientInit {
pub fn new(connection_args: (String, u16, bool), player: comp::Player, wait: bool) -> Self { 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 (server_address, default_port, prefer_ipv6) = connection_args;
let (tx, rx) = channel(); let (tx, rx) = channel();
@ -57,7 +57,13 @@ impl ClientInit {
for socket_addr in first_addrs.into_iter().chain(second_addrs) { for socket_addr in first_addrs.into_iter().chain(second_addrs) {
match Client::new(socket_addr, player.view_distance) { match Client::new(socket_addr, player.view_distance) {
Ok(mut client) => { Ok(mut client) => {
client.register(player); match client.register(player, password) {
Err(ClientError::InvalidAuth) => {
last_err = Some(Error::InvalidAuth);
break;
}
_ => {}
}
let _ = tx.send(Ok(client)); let _ = tx.send(Ok(client));
#[cfg(feature = "discord")] #[cfg(feature = "discord")]

View File

@ -106,6 +106,7 @@ impl PlayState for MainMenuState {
client_init = client_init.or(Some(ClientInit::new( client_init = client_init.or(Some(ClientInit::new(
(server_address, DEFAULT_PORT, false), (server_address, DEFAULT_PORT, false),
player, player,
password,
false, false,
))); )));
} else { } else {

View File

@ -38,6 +38,7 @@ impl PlayState for StartSingleplayerState {
username.clone(), username.clone(),
Some(global_state.settings.graphics.view_distance), Some(global_state.settings.graphics.view_distance),
), ),
String::default(),
true, true,
); );