Fix concurrency issue when choosing role

This commit is contained in:
Artlef 2020-03-01 20:11:14 +01:00
parent bf32298884
commit e42c102135
2 changed files with 32 additions and 15 deletions

View File

@ -1,5 +1,6 @@
extern crate ctrlc; extern crate ctrlc;
use clichess::RecvPositionError; use clichess::RecvPositionError;
use clichess::UserRole;
use serde_json::json; use serde_json::json;
use shakmaty::fen::Fen; use shakmaty::fen::Fen;
use shakmaty::{Chess, Color, Outcome, Position, Setup}; use shakmaty::{Chess, Color, Outcome, Position, Setup};
@ -41,7 +42,7 @@ fn main() {
let mut client = Client { let mut client = Client {
player: clichess::Player { player: clichess::Player {
role: clichess::UserRole::Spectator, role: UserRole::Spectator,
username: username, username: username,
public_key: public_key, public_key: public_key,
}, },
@ -55,11 +56,18 @@ fn main() {
Some(role) => client.player.role = role.clone(), Some(role) => client.player.role = role.clone(),
None => return, None => return,
}; };
if client.player.role == UserRole::Spectator {
println!(
"Hello, {} !\n\r You're spectating !",
client.player.username
)
} else {
println!( println!(
"Hello, {} !\n\r You're playing with the {} pieces", "Hello, {} !\n\r You're playing with the {} pieces",
client.player.username, client.player.username,
client.player.role.to_string() client.player.role.to_string()
); );
}
println!("Fetching initial chess position..."); println!("Fetching initial chess position...");
let mut current_position = fetch_initial_chess_position(&client); let mut current_position = fetch_initial_chess_position(&client);
loop { loop {
@ -215,20 +223,20 @@ fn parse_position(string: &str) -> Chess {
position position
} }
fn prompt_user_for_role(client: &Client, stream: &mut UnixStream) -> Option<clichess::UserRole> { fn prompt_user_for_role(client: &Client, stream: &mut UnixStream) -> Option<UserRole> {
let mut role = None; let mut role = None;
loop { loop {
println!("fetching roles from server..."); println!("fetching roles from server...");
let available_roles = fetch_available_roles(client); let available_roles = fetch_available_roles(client);
println!("available roles fetched."); println!("available roles fetched.");
let mut prompt = String::new(); let mut prompt = String::new();
if !available_roles.contains(&clichess::UserRole::White) if !available_roles.contains(&UserRole::White)
&& !available_roles.contains(&clichess::UserRole::Black) && !available_roles.contains(&UserRole::Black)
{ {
prompt.push_str("You can only spectate this game."); prompt.push_str("You can only spectate this game. Press enter to start spectating.");
} else if available_roles.contains(&clichess::UserRole::White) { } else if available_roles.contains(&UserRole::White) {
prompt = String::from("Do you want to play as White (W)"); prompt = String::from("Do you want to play as White (W)");
if available_roles.contains(&clichess::UserRole::White) { if available_roles.contains(&UserRole::White) {
prompt.push_str(", Black (B)"); prompt.push_str(", Black (B)");
} }
prompt.push_str(" or Spectate (S)?"); prompt.push_str(" or Spectate (S)?");
@ -237,11 +245,18 @@ fn prompt_user_for_role(client: &Client, stream: &mut UnixStream) -> Option<clic
} }
println!("{}", prompt); println!("{}", prompt);
//wait for user to give a correct answer. //wait for user to give a correct answer.
let input = String::from(read_user_input(client).trim()); let mut input = String::from(read_user_input(client).trim());
if input.trim() == "exit" { if input.trim() == "exit" {
clichess::write_to_stream(stream, String::from("exit")).unwrap(); clichess::write_to_stream(stream, String::from("exit")).unwrap();
break; break;
} }
if !available_roles.contains(&UserRole::White)
&& !available_roles.contains(&UserRole::Black)
{
//we can only spectate
input = String::from("S");
}
match clichess::parse_to_role(&input) { match clichess::parse_to_role(&input) {
Ok(r) => role = Some(r), Ok(r) => role = Some(r),
Err(e) => { Err(e) => {
@ -264,6 +279,7 @@ fn prompt_user_for_role(client: &Client, stream: &mut UnixStream) -> Option<clic
println!( println!(
"There was an issue with the server. Maybe your choice is not available anymore?" "There was an issue with the server. Maybe your choice is not available anymore?"
); );
clichess::write_to_stream(stream, String::from("ACK")).unwrap();
continue; continue;
} }
println!("Ok!"); println!("Ok!");
@ -277,10 +293,10 @@ fn fetch_initial_chess_position(client: &Client) -> Chess {
parse_position(&fetch_message_from_server(client)) parse_position(&fetch_message_from_server(client))
} }
fn fetch_available_roles(client: &Client) -> Vec<clichess::UserRole> { fn fetch_available_roles(client: &Client) -> Vec<UserRole> {
roles_from_str(&fetch_message_from_server(client)).unwrap() roles_from_str(&fetch_message_from_server(client)).unwrap()
} }
fn roles_from_str(s: &str) -> Result<Vec<clichess::UserRole>, String> { fn roles_from_str(s: &str) -> Result<Vec<UserRole>, String> {
s.split(',').map(clichess::parse_to_role).collect() s.split(',').map(clichess::parse_to_role).collect()
} }

View File

@ -202,6 +202,7 @@ fn receive_user_role(
} else { } else {
println!("KO"); println!("KO");
clichess::write_to_stream(stream, String::from("KO")).unwrap(); clichess::write_to_stream(stream, String::from("KO")).unwrap();
clichess::read_line_from_stream(stream).expect("Player closed connection.");
continue; continue;
} }
} }