pub fn say_hello(mut name: String) -> Result<TcpStream, StreamError> {

    // host address
    let name_file_port_server = "Config/port_client.dat";
    let host = match std::fs::read_to_string(name_file_port_server) {
        Ok(s) => s.trim().to_string(),
        Err(_) => get_address()
    };

    match TcpStream::connect(&host) {
        Ok(mut stream) => {
            println!("Successfully connected to {}", &host);
            
            loop {
                
                if name.len() == 0 {
                    // get the player name
                    let mut cont = true;
                    println!("Player name:");
                    while cont {
                        match get_input() {
                            Ok(s) => {
                                name = s.trim().to_string();
                                cont = false
                            },
                            Err(_) => println!("Could not parse the input")
                        };
                    }
                }

                send_str_to_server(&mut stream, &name)?;
                println!("Sent the name to server; awaiting reply...");
    
                let mut buffer: [u8; 1] = [0];
                stream.read_exact(&mut buffer)?;
                match buffer[0] {
                    1 => {
                        match get_str_from_server(&mut stream) {
                            Ok(s) => {
                                
                                // set the terminal appearance
                                reset_style();

                                // clear the terminal
                                clear_terminal();

                                // print the message sent by the server
                                println!("{}", s);
                            }
                            Err(e) => {
                                println!("Failed to receive data: {}", e);
                            }
                        }
                        break;
                    },
                    2 => {
                        match get_str_from_server(&mut stream) {
                            Ok(s) => { 
                                // print the message sent by the server
                                println!("{}", s);
                            }
                            Err(e) => {
                                println!("Failed to receive data: {}", e);
                            }
                        }
                        break;
                    },
                    _ => {
                        name.clear();
                        println!("{}", get_str_from_server(&mut stream)?)
                    }
                };
            }
            Ok(stream)
        }
        Err(e) => { Err(StreamError::from(e)) }
    }
}
/// try to connect to the server and send the player name
///
/// If the connection is successful, clear the terminal, print the reply from the server, and
/// return a `TcpStream`. 
/// If not, return a `StreamError`.
try to connect to the server and send the player name
If the connection is successful, clear the terminal, print the reply from the server, and
return a `TcpStream`.
If not, return a `StreamError`.
try to connect to the server and send the player name If the connection is successful clear the terminal print the reply from the server and return a ` TcpStream ` . If not return a ` StreamError ` .
pub fn say_hello(mut name: String) -> Result<TcpStream, StreamError> {
    // host address
    let name_file_port_server = "Config/port_client.dat";
    let host = match std::fs::read_to_string(name_file_port_server) {
        Ok(s) => s.trim().to_string(),
        Err(_) => get_address()
    };
    match TcpStream::connect(&host) {
        Ok(mut stream) => {
            println!("Successfully connected to {}", &host);
            loop {
                if name.len() == 0 {
                    // get the player name
                    let mut cont = true;
                    println!("Player name:");
                    while cont {
                        match get_input() {
                            Ok(s) => {
                                name = s.trim().to_string();
                                cont = false
                            },
                            Err(_) => println!("Could not parse the input")
                        };
                    }
                }
                send_str_to_server(&mut stream, &name)?;
                println!("Sent the name to server; awaiting reply...");
                let mut buffer: [u8; 1] = [0];
                stream.read_exact(&mut buffer)?;
                match buffer[0] {
                    1 => {
                        match get_str_from_server(&mut stream) {
                            Ok(s) => {
                                // set the terminal appearance
                                reset_style();
                                // clear the terminal
                                clear_terminal();
                                // print the message sent by the server
                                println!("{}", s);
                            }
                            Err(e) => {
                                println!("Failed to receive data: {}", e);
                            }
                        }
                        break;
                    },
                    2 => {
                        match get_str_from_server(&mut stream) {
                            Ok(s) => { 
                                // print the message sent by the server
                                println!("{}", s);
                            }
                            Err(e) => {
                                println!("Failed to receive data: {}", e);
                            }
                        }
                        break;
                    },
                    _ => {
                        name.clear();
                        println!("{}", get_str_from_server(&mut stream)?)
                    }
                };
            }
            Ok(stream)
        }
        Err(e) => { Err(StreamError::from(e)) }
    }
}
pub fn say_hello ( mut name : String ) -> Result < TcpStream , StreamError > { // host address let name_file_port_server = " " ; let host = match std :: fs :: read_to_string ( name_file_port_server ) { Ok ( s ) => s . trim ( ) . to_string ( ) , Err ( _ ) => get_address ( ) } ; match TcpStream :: connect ( & host ) { Ok ( mut stream ) => { println ! ( " " host ) ; loop { if name . len ( ) == 0 { // get the player name let mut cont = true ; println ! ( " " ) ; while cont { match get_input ( ) { Ok ( s ) => { name = s . trim ( ) . to_string ( ) ; cont = false } , Err ( _ ) => println ! ( " " ) } ; } } send_str_to_server ( & mut stream , & name ) ? ; println ! ( " " ) ; let mut buffer : [ u8 ; 1 ] = [ 0 ] ; stream . read_exact ( & mut buffer ) ? ; match buffer [ 0 ] { 1 => { match get_str_from_server ( & mut stream ) { Ok ( s ) => { // set the terminal appearance reset_style ( ) ; // clear the terminal clear_terminal ( ) ; // print the message sent by the server println ! ( " " s ) ; } Err ( e ) => { println ! ( " " e ) ; } } break ; } , 2 => { match get_str_from_server ( & mut stream ) { Ok ( s ) => { // print the message sent by the server println ! ( " " s ) ; } Err ( e ) => { println ! ( " " e ) ; } } break ; } , _ => { name . clear ( ) ; println ! ( " " get_str_from_server ( mut stream ) ) } } ; } Ok ( stream ) } Err ( e ) => { Err ( StreamError :: from ( e ) ) } } }
try to connect to the server and send the player name
If the connection is successful, clear the terminal, print the reply from the server, and
return a `TcpStream`.
try to connect to the server and send the player name If the connection is successful clear the terminal print the reply from the server and return a ` TcpStream ` .
name
String
name
String