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;
use clichess::RecvPositionError;
use clichess::UserRole;
use serde_json::json;
use shakmaty::fen::Fen;
use shakmaty::{Chess, Color, Outcome, Position, Setup};
@ -41,7 +42,7 @@ fn main() {
let mut client = Client {
player: clichess::Player {
role: clichess::UserRole::Spectator,
role: UserRole::Spectator,
username: username,
public_key: public_key,
},
@ -55,11 +56,18 @@ fn main() {
Some(role) => client.player.role = role.clone(),
None => return,
};
println!(
"Hello, {} !\n\r You're playing with the {} pieces",
client.player.username,
client.player.role.to_string()
);
if client.player.role == UserRole::Spectator {
println!(
"Hello, {} !\n\r You're spectating !",
client.player.username
)
} else {
println!(
"Hello, {} !\n\r You're playing with the {} pieces",
client.player.username,
client.player.role.to_string()
);
}
println!("Fetching initial chess position...");
let mut current_position = fetch_initial_chess_position(&client);
loop {
@ -215,20 +223,20 @@ fn parse_position(string: &str) -> Chess {
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;
loop {
println!("fetching roles from server...");
let available_roles = fetch_available_roles(client);
println!("available roles fetched.");
let mut prompt = String::new();
if !available_roles.contains(&clichess::UserRole::White)
&& !available_roles.contains(&clichess::UserRole::Black)
if !available_roles.contains(&UserRole::White)
&& !available_roles.contains(&UserRole::Black)
{
prompt.push_str("You can only spectate this game.");
} else if available_roles.contains(&clichess::UserRole::White) {
prompt.push_str("You can only spectate this game. Press enter to start spectating.");
} else if available_roles.contains(&UserRole::White) {
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(" or Spectate (S)?");
@ -237,11 +245,18 @@ fn prompt_user_for_role(client: &Client, stream: &mut UnixStream) -> Option<clic
}
println!("{}", prompt);
//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" {
clichess::write_to_stream(stream, String::from("exit")).unwrap();
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) {
Ok(r) => role = Some(r),
Err(e) => {
@ -264,6 +279,7 @@ fn prompt_user_for_role(client: &Client, stream: &mut UnixStream) -> Option<clic
println!(
"There was an issue with the server. Maybe your choice is not available anymore?"
);
clichess::write_to_stream(stream, String::from("ACK")).unwrap();
continue;
}
println!("Ok!");
@ -277,10 +293,10 @@ fn fetch_initial_chess_position(client: &Client) -> Chess {
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()
}
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()
}

View File

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