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");
let server_addr = read_input();
println!("Enter your password");
let password = read_input();
// Create a client.
let mut client = Client::new(
server_addr
@ -45,7 +48,7 @@ fn main() {
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();
thread::spawn(move || loop {

View File

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

View File

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

View File

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

View File

@ -26,7 +26,7 @@ use log::debug;
use rand::Rng;
use specs::{join::Join, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity};
use std::{
collections::HashSet,
collections::{HashSet, HashMap},
i32,
net::SocketAddr,
sync::{mpsc, Arc},
@ -68,6 +68,9 @@ pub struct Server {
server_settings: ServerSettings,
server_info: ServerInfo,
// TODO: anything but this
accounts: HashMap<String, String>,
}
impl Server {
@ -107,6 +110,7 @@ impl Server {
description: settings.server_description.clone(),
git_hash: common::util::GIT_HASH.to_string(),
},
accounts: HashMap::new(),
server_settings: settings,
};
@ -468,6 +472,8 @@ impl Server {
fn handle_new_messages(&mut self) -> Result<Vec<Event>, Error> {
let mut frontend_events = Vec::new();
let accounts = &mut self.accounts;
let state = &mut self.state;
let mut new_chat_msgs = Vec::new();
let mut disconnected_clients = Vec::new();
@ -522,7 +528,16 @@ impl Server {
ClientState::Pending => {}
},
// 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 {
ClientState::Connected => {
Self::initialize_player(state, entity, client, player);

View File

@ -30,7 +30,7 @@ pub struct ClientInit {
rx: Receiver<Result<Client, Error>>,
}
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 (tx, rx) = channel();
@ -57,7 +57,13 @@ impl ClientInit {
for socket_addr in first_addrs.into_iter().chain(second_addrs) {
match Client::new(socket_addr, player.view_distance) {
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));
#[cfg(feature = "discord")]

View File

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

View File

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