Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
11d3b0755d | |||
ae3791a24b | |||
8377717cc3 | |||
4748d61bca | |||
d167e1c84f | |||
30aee61cad | |||
55fe2fb538 | |||
00ea1759d4 | |||
9c17bcfeb8 | |||
27be72f3be | |||
cb6ad49e85 | |||
69adc99e19 | |||
4c54d9b5e5 | |||
8d098c1ea4 | |||
fdb701dbf1 | |||
11e3c7d48e |
158
Cargo.lock
generated
158
Cargo.lock
generated
@ -4,190 +4,172 @@
|
||||
name = "arrayvec"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||
|
||||
[[package]]
|
||||
name = "btoi"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e4ced8205e70d9e553d008d53ded735808fa6133597318d48f74fc2bf9861471"
|
||||
dependencies = [
|
||||
"num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clichess"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"colored 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"shakmaty 0.16.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "colored"
|
||||
version = "1.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"shakmaty",
|
||||
"termion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4"
|
||||
dependencies = [
|
||||
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "numtoa"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27"
|
||||
dependencies = [
|
||||
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.1.57"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
|
||||
|
||||
[[package]]
|
||||
name = "redox_termios"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
|
||||
dependencies = [
|
||||
"redox_syscall",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449"
|
||||
dependencies = [
|
||||
"serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48c575e0cc52bdd09b47f330f646cf59afc586e9c4e3ccd6fc1f625b8ea1dad7"
|
||||
dependencies = [
|
||||
"itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shakmaty"
|
||||
version = "0.16.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a8f8f7fd48325e0b530cd257df0aa735efeefc923c70133d412f05d69de69108"
|
||||
dependencies = [
|
||||
"arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"btoi 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"arrayvec",
|
||||
"bitflags",
|
||||
"btoi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termion"
|
||||
version = "1.5.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c22cec9d8978d906be5ac94bceb5a010d885c626c4c8855721a4dbd20e3ac905"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"numtoa",
|
||||
"redox_syscall",
|
||||
"redox_termios",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8"
|
||||
"checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90"
|
||||
"checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2"
|
||||
"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||
"checksum btoi 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e4ced8205e70d9e553d008d53ded735808fa6133597318d48f74fc2bf9861471"
|
||||
"checksum colored 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "433e7ac7d511768127ed85b0c4947f47a254131e37864b2dc13f52aa32cd37e5"
|
||||
"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
|
||||
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
|
||||
"checksum num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4"
|
||||
"checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27"
|
||||
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
|
||||
"checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8"
|
||||
"checksum serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449"
|
||||
"checksum serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64"
|
||||
"checksum serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)" = "48c575e0cc52bdd09b47f330f646cf59afc586e9c4e3ccd6fc1f625b8ea1dad7"
|
||||
"checksum shakmaty 0.16.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a8f8f7fd48325e0b530cd257df0aa735efeefc923c70133d412f05d69de69108"
|
||||
"checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238"
|
||||
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
|
||||
"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
|
||||
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "clichess"
|
||||
version = "0.3.0"
|
||||
version = "0.4.0"
|
||||
authors = ["Artlef <artlef@localhost>"]
|
||||
edition = "2018"
|
||||
|
||||
@ -8,6 +8,6 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
shakmaty = "0.16.2"
|
||||
colored = "1.9"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
termion = "1.5.5"
|
||||
|
@ -1,18 +1,28 @@
|
||||
extern crate termion;
|
||||
|
||||
use clichess::{ClientRequest, GameInfo, RecvPositionError, UserRole, EXIT_MSG};
|
||||
use shakmaty::{Chess, Color, Outcome, Position, Setup};
|
||||
use shakmaty::{fen, Chess, Color, Outcome, Position, Setup};
|
||||
use std::io;
|
||||
use std::os::unix::net::UnixStream;
|
||||
use std::sync::mpsc::{channel, Receiver, TryRecvError};
|
||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time;
|
||||
use termion::event::{parse_event, Event, Key};
|
||||
|
||||
use std::io::{stdin, stdout, Read, Write};
|
||||
use termion::raw::{IntoRawMode, RawTerminal};
|
||||
|
||||
struct Client {
|
||||
player: clichess::Player,
|
||||
side: Color,
|
||||
display_sender: Sender<DisplayMessage>,
|
||||
waiting_server_msg_receiver: Receiver<WaitingServerMsg>,
|
||||
keyboard_input_recv: Receiver<String>,
|
||||
server_message_recv: Receiver<String>,
|
||||
opponent_name: Option<String>,
|
||||
last_move: Option<String>,
|
||||
is_player_turn: Arc<Mutex<bool>>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@ -28,9 +38,19 @@ fn main() {
|
||||
return;
|
||||
}
|
||||
};
|
||||
let keyboard_input_recv = setup_keyboard_input_recv();
|
||||
|
||||
let is_player_turn = Arc::new(Mutex::new(true));
|
||||
let (display_sender, display_receiver) = channel();
|
||||
let (waiting_server_msg_sender, waiting_server_msg_receiver) = channel();
|
||||
let keyboard_input_recv = start_keyboard_input_thread(
|
||||
display_sender.clone(),
|
||||
waiting_server_msg_sender.clone(),
|
||||
is_player_turn.clone(),
|
||||
);
|
||||
start_display_thread(display_receiver);
|
||||
//start a thread to listen to server messages
|
||||
let server_message_recv = setup_server_message_recv(&stream).unwrap();
|
||||
let server_message_recv =
|
||||
setup_server_message_recv(&stream, waiting_server_msg_sender.clone()).unwrap();
|
||||
|
||||
let mut client = Client {
|
||||
player: clichess::Player {
|
||||
@ -39,90 +59,54 @@ fn main() {
|
||||
public_key,
|
||||
},
|
||||
side: Color::White,
|
||||
display_sender: display_sender.clone(),
|
||||
waiting_server_msg_receiver,
|
||||
keyboard_input_recv,
|
||||
server_message_recv,
|
||||
opponent_name: Option::None,
|
||||
last_move: Option::None,
|
||||
is_player_turn,
|
||||
};
|
||||
|
||||
let login_result = login(&mut client, &mut stream);
|
||||
if login_result == LoginResult::UserExited {
|
||||
send_request(&client, &mut stream, ClientRequest::Exit);
|
||||
client.display_sender.send(DisplayMessage::Exit).unwrap();
|
||||
return;
|
||||
}
|
||||
client.display_sender.send(DisplayMessage::Clear).unwrap();
|
||||
if client.player.role == UserRole::Black {
|
||||
client.side = Color::Black;
|
||||
}
|
||||
if client.player.role == UserRole::Spectator {
|
||||
println!(
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Information(format!(
|
||||
"Hello, {} !\n\r You're spectating !",
|
||||
client.player.username
|
||||
)
|
||||
)))
|
||||
.unwrap();
|
||||
} else {
|
||||
println!(
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Information(format!(
|
||||
"Hello, {} !\n\r You're playing with the {} pieces",
|
||||
client.player.username,
|
||||
client.player.role.to_string()
|
||||
);
|
||||
)))
|
||||
.unwrap();
|
||||
}
|
||||
let mut current_position = fetch_initial_chess_position(&client, &mut stream);
|
||||
|
||||
loop {
|
||||
let is_player_turn = clichess::is_player_turn(&client.player, current_position.turn());
|
||||
if is_player_turn || client.player.role == UserRole::Spectator {
|
||||
client
|
||||
.opponent_name
|
||||
.clone()
|
||||
.map(|name| print!("{} played", name));
|
||||
client
|
||||
.last_move
|
||||
.clone()
|
||||
.map(|chessmove| println!(" {}", chessmove));
|
||||
}
|
||||
println!(
|
||||
"{}",
|
||||
clichess::board_representation(¤t_position, client.side)
|
||||
);
|
||||
//check if game is over.
|
||||
if current_position.is_game_over() {
|
||||
client.display_sender.send(DisplayMessage::Help).unwrap();
|
||||
if !game_loop(&mut client, &mut stream) {
|
||||
break;
|
||||
}
|
||||
if is_player_turn {
|
||||
//it's the user turn, taking user input
|
||||
let mut input = client.keyboard_input_recv.recv().unwrap();
|
||||
input = String::from(input.trim());
|
||||
if input == EXIT_MSG {
|
||||
break;
|
||||
}
|
||||
let response = send_request(&client, &mut stream, ClientRequest::Play(input.clone()));
|
||||
if response == String::from("KO") {
|
||||
println!("Invalid move: {}", input);
|
||||
//go back to taking user input
|
||||
continue;
|
||||
}
|
||||
client.last_move = Some(input);
|
||||
match get_current_position(&mut client, &mut stream) {
|
||||
Ok(position) => current_position = position,
|
||||
Err(_) => break,
|
||||
};
|
||||
} else {
|
||||
match wait_for_next_move(&mut client, &mut stream) {
|
||||
Ok(position) => current_position = position,
|
||||
Err(_) => break,
|
||||
};
|
||||
}
|
||||
}
|
||||
match current_position.outcome() {
|
||||
None => println!("Bye"),
|
||||
Some(Outcome::Draw) => println!("Draw game."),
|
||||
Some(Outcome::Decisive { winner }) => {
|
||||
if winner == Color::White {
|
||||
println!("White has won the game.")
|
||||
} else {
|
||||
println!("Black has won the game.")
|
||||
}
|
||||
}
|
||||
client.display_sender.send(DisplayMessage::Clear).unwrap();
|
||||
}
|
||||
client.display_sender.send(DisplayMessage::Clear).unwrap();
|
||||
send_request(&client, &mut stream, ClientRequest::Exit);
|
||||
client.display_sender.send(DisplayMessage::Exit).unwrap();
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
@ -154,6 +138,129 @@ fn login(client: &mut Client, stream: &mut UnixStream) -> LoginResult {
|
||||
LoginResult::Success
|
||||
}
|
||||
|
||||
fn game_loop(client: &mut Client, stream: &mut UnixStream) -> bool {
|
||||
let mut replay = false;
|
||||
let mut current_position = fetch_initial_chess_position(client, stream);
|
||||
|
||||
loop {
|
||||
let is_player_turn_loop = clichess::is_player_turn(&client.player, current_position.turn());
|
||||
{
|
||||
let mut is_player_turn = client.is_player_turn.lock().unwrap();
|
||||
*is_player_turn = is_player_turn_loop;
|
||||
}
|
||||
if is_player_turn_loop || client.player.role == UserRole::Spectator {
|
||||
let mut message = String::from("");
|
||||
let opponent_name = client.opponent_name.clone();
|
||||
if opponent_name.is_some() {
|
||||
message.push_str(&format!("{} played", opponent_name.expect("is some")));
|
||||
client
|
||||
.last_move
|
||||
.clone()
|
||||
.map(|chessmove| message.push_str(&format!(" {}", chessmove)));
|
||||
}
|
||||
if message.len() > 0 {
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Information(message))
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Chessboard {
|
||||
fen: fen::fen(¤t_position),
|
||||
side: client.side.clone(),
|
||||
})
|
||||
.unwrap();
|
||||
//check if game is over.
|
||||
if current_position.is_game_over() {
|
||||
break;
|
||||
}
|
||||
if is_player_turn_loop {
|
||||
//it's the user turn, taking user input
|
||||
let mut input = client.keyboard_input_recv.recv().unwrap();
|
||||
input = String::from(input.trim());
|
||||
if input == EXIT_MSG {
|
||||
client.waiting_server_msg_receiver.recv().unwrap();
|
||||
break;
|
||||
}
|
||||
let response = send_request(client, stream, ClientRequest::Play(input.clone()));
|
||||
if response == String::from("KO") {
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Message(format!("Invalid move: {}", input)))
|
||||
.unwrap();
|
||||
//go back to taking user input
|
||||
continue;
|
||||
}
|
||||
client.last_move = Some(input);
|
||||
match get_current_position(client, stream) {
|
||||
Ok(position) => current_position = position,
|
||||
Err(_) => break,
|
||||
};
|
||||
//clear message
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Message(String::default()))
|
||||
.unwrap();
|
||||
} else {
|
||||
match wait_for_next_move(client, stream) {
|
||||
Ok(position) => current_position = position,
|
||||
Err(_) => break,
|
||||
};
|
||||
}
|
||||
}
|
||||
let end_message = match current_position.outcome() {
|
||||
None => "",
|
||||
Some(Outcome::Draw) => "Draw game.",
|
||||
Some(Outcome::Decisive { winner }) => {
|
||||
if winner == Color::White {
|
||||
"White has won the game."
|
||||
} else {
|
||||
"Black has won the game."
|
||||
}
|
||||
}
|
||||
}
|
||||
.to_string();
|
||||
if !end_message.is_empty() {
|
||||
//accept input
|
||||
if client.player.role != UserRole::Spectator {
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Message(format!(
|
||||
"{}\n\rPress enter to rematch.",
|
||||
end_message
|
||||
)))
|
||||
.unwrap();
|
||||
let mut is_player_turn = client.is_player_turn.lock().unwrap();
|
||||
*is_player_turn = true;
|
||||
} else {
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Message(format!("{}", end_message)))
|
||||
.unwrap();
|
||||
thread::sleep(time::Duration::from_secs(5));
|
||||
return false;
|
||||
}
|
||||
let buffer = client.keyboard_input_recv.recv().unwrap();
|
||||
|
||||
if buffer == EXIT_MSG {
|
||||
client.waiting_server_msg_receiver.recv().unwrap();
|
||||
} else {
|
||||
replay = true;
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Message(format!(
|
||||
"{}\n\rWaiting for your opponent to restart...",
|
||||
end_message
|
||||
)))
|
||||
.unwrap();
|
||||
send_request(client, stream, ClientRequest::Reset);
|
||||
}
|
||||
}
|
||||
replay
|
||||
}
|
||||
|
||||
fn send_request(client: &Client, stream: &mut UnixStream, request: ClientRequest) -> String {
|
||||
let response: String;
|
||||
match serde_json::to_string(&request) {
|
||||
@ -163,27 +270,230 @@ fn send_request(client: &Client, stream: &mut UnixStream, request: ClientRequest
|
||||
}
|
||||
|
||||
Err(e) => {
|
||||
println!("Error when parsing client request: {}", e);
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Message(format!(
|
||||
"Error when parsing client request: {}",
|
||||
e
|
||||
)))
|
||||
.unwrap();
|
||||
response = String::from("KO");
|
||||
}
|
||||
};
|
||||
response
|
||||
}
|
||||
|
||||
fn setup_keyboard_input_recv() -> Receiver<String> {
|
||||
#[derive(PartialEq, Clone)]
|
||||
pub enum DisplayMessage {
|
||||
Chessboard { fen: String, side: Color },
|
||||
Message(String),
|
||||
Information(String),
|
||||
Debug(String),
|
||||
Help,
|
||||
Input(Key),
|
||||
RemoveLastInput,
|
||||
SwitchAsciiMode,
|
||||
Enter,
|
||||
Clear,
|
||||
Exit,
|
||||
}
|
||||
|
||||
fn start_display_thread(request_recv: Receiver<DisplayMessage>) {
|
||||
thread::spawn(move || {
|
||||
let stdout = stdout();
|
||||
let mut stdout = stdout.lock().into_raw_mode().unwrap();
|
||||
write!(
|
||||
stdout,
|
||||
"{}{}",
|
||||
termion::clear::All,
|
||||
termion::cursor::Goto(3, 17)
|
||||
)
|
||||
.unwrap();
|
||||
stdout.flush().unwrap();
|
||||
let mut ascii_mode = false;
|
||||
let mut last_fen_position = String::default();
|
||||
let mut last_side = Color::White;
|
||||
loop {
|
||||
let msg = request_recv.recv().unwrap();
|
||||
match msg {
|
||||
DisplayMessage::Chessboard { fen, side } => {
|
||||
let current_position = clichess::parse_position(&fen);
|
||||
last_fen_position = fen.clone();
|
||||
last_side = side.clone();
|
||||
clichess::print_board_representation(
|
||||
¤t_position,
|
||||
side,
|
||||
&mut stdout,
|
||||
ascii_mode,
|
||||
);
|
||||
}
|
||||
DisplayMessage::SwitchAsciiMode => {
|
||||
ascii_mode = !ascii_mode;
|
||||
write!(stdout, "{}", termion::cursor::Save,).unwrap();
|
||||
|
||||
clichess::print_board_representation(
|
||||
&clichess::parse_position(&last_fen_position),
|
||||
last_side,
|
||||
&mut stdout,
|
||||
ascii_mode,
|
||||
);
|
||||
write!(stdout, "{}", termion::cursor::Restore).unwrap();
|
||||
}
|
||||
DisplayMessage::Message(s) => write!(
|
||||
stdout,
|
||||
"{}{}{}{}{}",
|
||||
termion::cursor::Save,
|
||||
termion::cursor::Goto(3, 18),
|
||||
termion::clear::CurrentLine,
|
||||
s,
|
||||
termion::cursor::Restore
|
||||
)
|
||||
.unwrap(),
|
||||
DisplayMessage::Information(s) => write!(
|
||||
stdout,
|
||||
"{}{}{}{}{}",
|
||||
termion::cursor::Save,
|
||||
termion::cursor::Goto(3, 3),
|
||||
termion::clear::CurrentLine,
|
||||
s,
|
||||
termion::cursor::Restore
|
||||
)
|
||||
.unwrap(),
|
||||
DisplayMessage::Clear => write!(stdout, "{}", termion::clear::All).unwrap(),
|
||||
DisplayMessage::Enter => write!(
|
||||
stdout,
|
||||
"{}{}",
|
||||
termion::clear::CurrentLine,
|
||||
termion::cursor::Goto(3, 17)
|
||||
)
|
||||
.unwrap(),
|
||||
DisplayMessage::Input(k) => display_key(&mut stdout, k),
|
||||
DisplayMessage::RemoveLastInput => write!(
|
||||
stdout,
|
||||
"{}{}",
|
||||
termion::cursor::Left(1),
|
||||
termion::clear::UntilNewline
|
||||
)
|
||||
.unwrap(),
|
||||
DisplayMessage::Help => print_help(&mut stdout),
|
||||
DisplayMessage::Debug(s) => write!(
|
||||
stdout,
|
||||
"{}{}{}{}{}",
|
||||
termion::cursor::Save,
|
||||
termion::cursor::Goto(3, 30),
|
||||
termion::clear::CurrentLine,
|
||||
s,
|
||||
termion::cursor::Restore
|
||||
)
|
||||
.unwrap(),
|
||||
DisplayMessage::Exit => break,
|
||||
}
|
||||
stdout.flush().unwrap();
|
||||
}
|
||||
write!(stdout, "{}", termion::clear::All,).unwrap();
|
||||
stdout.flush().unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
fn print_help(stdout: &mut RawTerminal<io::StdoutLock>) {
|
||||
write!(stdout, "{}", termion::cursor::Save).unwrap();
|
||||
let help = [
|
||||
"move are parsed using SAN (Nc3) or UCI (b1c3)",
|
||||
"@ to toggle ascii mode",
|
||||
"Ctrl-C to quit",
|
||||
];
|
||||
for (i, h) in help.iter().enumerate() {
|
||||
write!(stdout, "{}{}", termion::cursor::Goto(3, 22 + (i as u16)), h).unwrap();
|
||||
}
|
||||
write!(stdout, "{}", termion::cursor::Restore).unwrap();
|
||||
}
|
||||
|
||||
fn start_keyboard_input_thread(
|
||||
display_sender: Sender<DisplayMessage>,
|
||||
waiting_server_msg_sender: Sender<WaitingServerMsg>,
|
||||
is_player_turn: Arc<Mutex<bool>>,
|
||||
) -> Receiver<String> {
|
||||
let (sender, receiver) = channel();
|
||||
thread::spawn(move || {
|
||||
loop {
|
||||
let mut buffer = String::new();
|
||||
//wait for user input
|
||||
io::stdin().read_line(&mut buffer).unwrap();
|
||||
sender.send(buffer).unwrap();
|
||||
let stdin = stdin();
|
||||
let stdin = stdin.lock();
|
||||
let mut bytes = stdin.bytes();
|
||||
loop {
|
||||
let b = bytes.next().unwrap().unwrap();
|
||||
let e = parse_event(b, &mut bytes).unwrap();
|
||||
let is_player_turn = is_player_turn.lock().unwrap();
|
||||
if !*is_player_turn
|
||||
&& e != Event::Key(Key::Ctrl('c'))
|
||||
&& e != Event::Key(Key::Char('@'))
|
||||
{
|
||||
//ignore input when it's not the client turn
|
||||
continue;
|
||||
}
|
||||
match e {
|
||||
Event::Key(Key::Ctrl('c')) => {
|
||||
sender.send(String::from(EXIT_MSG)).unwrap();
|
||||
waiting_server_msg_sender
|
||||
.send(WaitingServerMsg::UserCanceled)
|
||||
.unwrap();
|
||||
break;
|
||||
}
|
||||
Event::Key(Key::Char('\n')) => {
|
||||
display_sender.send(DisplayMessage::Enter).unwrap();
|
||||
sender.send(buffer.clone()).unwrap();
|
||||
if buffer == EXIT_MSG {
|
||||
waiting_server_msg_sender
|
||||
.send(WaitingServerMsg::UserCanceled)
|
||||
.unwrap();
|
||||
}
|
||||
buffer.clear();
|
||||
}
|
||||
Event::Key(Key::Char('@')) => {
|
||||
display_sender
|
||||
.send(DisplayMessage::SwitchAsciiMode)
|
||||
.unwrap();
|
||||
}
|
||||
Event::Key(Key::Char(c)) => {
|
||||
if buffer.len() < 10 {
|
||||
buffer.push_str(&c.to_string());
|
||||
display_sender
|
||||
.send(DisplayMessage::Input(Key::Char(c)))
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
Event::Key(Key::Backspace) => {
|
||||
if buffer.len() > 0 {
|
||||
buffer.pop();
|
||||
display_sender
|
||||
.send(DisplayMessage::RemoveLastInput)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
Event::Key(_) => continue,
|
||||
Event::Mouse(_) => continue,
|
||||
Event::Unsupported(_) => continue,
|
||||
};
|
||||
}
|
||||
});
|
||||
receiver
|
||||
}
|
||||
|
||||
fn setup_server_message_recv(stream: &UnixStream) -> io::Result<Receiver<String>> {
|
||||
fn display_key(stdout: &mut RawTerminal<io::StdoutLock>, key: Key) {
|
||||
match key {
|
||||
Key::Char(c) => write!(stdout, "{}", c).unwrap(),
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
|
||||
enum WaitingServerMsg {
|
||||
MsgReceived,
|
||||
UserCanceled,
|
||||
}
|
||||
|
||||
fn setup_server_message_recv(
|
||||
stream: &UnixStream,
|
||||
waiting_server_msg_sender: Sender<WaitingServerMsg>,
|
||||
) -> io::Result<Receiver<String>> {
|
||||
let (sender, receiver) = channel();
|
||||
let thread_stream = stream.try_clone()?;
|
||||
|
||||
@ -193,6 +503,9 @@ fn setup_server_message_recv(stream: &UnixStream) -> io::Result<Receiver<String>
|
||||
let buffer =
|
||||
clichess::read_line_from_stream(&thread_stream).expect("Error message from server");
|
||||
sender.send(buffer).unwrap();
|
||||
waiting_server_msg_sender
|
||||
.send(WaitingServerMsg::MsgReceived)
|
||||
.unwrap();
|
||||
}
|
||||
});
|
||||
Ok(receiver)
|
||||
@ -217,24 +530,15 @@ fn wait_for_next_move(
|
||||
client: &mut Client,
|
||||
stream: &mut UnixStream,
|
||||
) -> Result<Chess, RecvPositionError> {
|
||||
println!("Waiting for opponent move...");
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Information(String::from(
|
||||
"Waiting for opponent move...",
|
||||
)))
|
||||
.unwrap();
|
||||
send_request(client, stream, ClientRequest::WaitForNextMove);
|
||||
let response;
|
||||
loop {
|
||||
thread::sleep(time::Duration::from_millis(10));
|
||||
{
|
||||
let server_response_recv = &client.server_message_recv;
|
||||
match server_response_recv.try_recv() {
|
||||
Ok(msg) => {
|
||||
response = msg;
|
||||
break;
|
||||
}
|
||||
Err(TryRecvError::Disconnected) => println!("Error: server disconnected."),
|
||||
Err(TryRecvError::Empty) => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
if response.is_empty() {
|
||||
let response = fetch_message_from_server(client);
|
||||
if response == EXIT_MSG {
|
||||
Err(RecvPositionError::UserCanceledError)
|
||||
} else {
|
||||
let game_info: GameInfo = serde_json::from_str(&response).unwrap();
|
||||
@ -253,22 +557,10 @@ fn update_client(game_info: &GameInfo, client: &mut Client) {
|
||||
}
|
||||
|
||||
fn fetch_message_from_server(client: &Client) -> String {
|
||||
let response;
|
||||
loop {
|
||||
thread::sleep(time::Duration::from_millis(10));
|
||||
{
|
||||
let server_response_recv = &client.server_message_recv;
|
||||
match server_response_recv.try_recv() {
|
||||
Ok(msg) => {
|
||||
response = msg;
|
||||
break;
|
||||
match client.waiting_server_msg_receiver.recv().unwrap() {
|
||||
WaitingServerMsg::MsgReceived => client.server_message_recv.recv().unwrap(),
|
||||
WaitingServerMsg::UserCanceled => client.keyboard_input_recv.recv().unwrap(),
|
||||
}
|
||||
Err(TryRecvError::Disconnected) => println!("Error: server disconnected."),
|
||||
Err(TryRecvError::Empty) => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
enum PromptedRoleResponse {
|
||||
@ -292,9 +584,14 @@ fn prompt_user_for_role(client: &Client, stream: &mut UnixStream) -> PromptedRol
|
||||
} else {
|
||||
prompt = String::from("Do you want to play as Black (B) or Spectate (S)?");
|
||||
}
|
||||
println!("{}", prompt);
|
||||
let mut input = String::from(client.keyboard_input_recv.recv().unwrap().trim());
|
||||
if input.trim() == EXIT_MSG {
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Information(prompt))
|
||||
.unwrap();
|
||||
let mut input = client.keyboard_input_recv.recv().unwrap();
|
||||
input = String::from(input.trim());
|
||||
if input == EXIT_MSG {
|
||||
client.waiting_server_msg_receiver.recv().unwrap();
|
||||
return PromptedRoleResponse::Exit;
|
||||
}
|
||||
|
||||
@ -305,13 +602,21 @@ fn prompt_user_for_role(client: &Client, stream: &mut UnixStream) -> PromptedRol
|
||||
match clichess::parse_to_role(&input) {
|
||||
Ok(r) => {
|
||||
if !available_roles.contains(&r) {
|
||||
println!("Sorry, this side is not available.");
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Message(String::from(
|
||||
"Sorry, this side is not available.",
|
||||
)))
|
||||
.unwrap();
|
||||
return PromptedRoleResponse::Retry;
|
||||
}
|
||||
return PromptedRoleResponse::Role(r);
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{}", e);
|
||||
client
|
||||
.display_sender
|
||||
.send(DisplayMessage::Message(format!("{}", e)))
|
||||
.unwrap();
|
||||
return PromptedRoleResponse::Retry;
|
||||
}
|
||||
};
|
||||
|
20
src/bin/fetch_game_info.rs
Normal file
20
src/bin/fetch_game_info.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use clichess::ClientRequest;
|
||||
use std::os::unix::net::UnixStream;
|
||||
|
||||
|
||||
fn main() {
|
||||
let version = env!("CARGO_PKG_VERSION");
|
||||
eprintln!("Running clichess fetch_game_info version {}", version);
|
||||
let mut stream = match UnixStream::connect("/tmp/clichess.socket") {
|
||||
Ok(sock) => sock,
|
||||
Err(_) => {
|
||||
eprintln!("clichess daemon is not running.");
|
||||
return;
|
||||
}
|
||||
};
|
||||
clichess::write_to_stream(&mut stream, serde_json::to_string(&ClientRequest::GetGameInfo).unwrap()).unwrap();
|
||||
let response = clichess::read_line_from_stream(&stream).expect("Error message from server");
|
||||
clichess::write_to_stream(&mut stream, serde_json::to_string(&ClientRequest::Exit).unwrap()).unwrap();
|
||||
clichess::read_line_from_stream(&stream).expect("Error message from server");
|
||||
println!("{}", response);
|
||||
}
|
@ -19,6 +19,7 @@ struct Server {
|
||||
cvar: Arc<(Mutex<bool>, Condvar)>,
|
||||
opponent_name: Arc<Mutex<Option<String>>>,
|
||||
last_move: Arc<Mutex<Option<String>>>,
|
||||
confirm_reset: Arc<(Mutex<bool>, Condvar)>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@ -31,6 +32,9 @@ fn main() {
|
||||
|
||||
let listener = UnixListener::bind("/tmp/clichess.socket").unwrap();
|
||||
|
||||
//boolean shared amongst all server instances
|
||||
let confirm_reset = Arc::new((Mutex::new(false), Condvar::new()));
|
||||
|
||||
// accept connections and process them, spawning a new thread for each one
|
||||
for stream in listener.incoming() {
|
||||
match stream {
|
||||
@ -48,6 +52,7 @@ fn main() {
|
||||
cvar: condvar_pair,
|
||||
opponent_name: Arc::new(Mutex::new(Option::None)),
|
||||
last_move: Arc::new(Mutex::new(Option::None)),
|
||||
confirm_reset: confirm_reset.clone(),
|
||||
};
|
||||
/* connection succeeded */
|
||||
thread::spawn(move || handle_player(stream, &mut server, others_serv_msg_sender));
|
||||
@ -89,10 +94,12 @@ fn handle_player(
|
||||
Ok(ClientRequest::WaitForNextMove) => {
|
||||
//return true if user exited.
|
||||
if wait_for_next_move(&mut stream, server) {
|
||||
send_ok(&mut stream);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(ClientRequest::Play(movestr)) => play(&mut stream, server, movestr),
|
||||
Ok(ClientRequest::Reset) => reset(&mut stream, server),
|
||||
Ok(ClientRequest::Exit) => {
|
||||
send_ok(&mut stream);
|
||||
break;
|
||||
@ -207,7 +214,10 @@ fn wait_for_opponent_move(server: &Server) -> Result<GameInfo, RecvPositionError
|
||||
Err(TryRecvError::Empty) => { /*nothing to do*/ }
|
||||
}
|
||||
match server.client_message_recv.try_recv() {
|
||||
Ok(_) => returned_result = Err(RecvPositionError::UserCanceledError),
|
||||
Ok(_) => {
|
||||
println!("User exited.");
|
||||
returned_result = Err(RecvPositionError::UserCanceledError);
|
||||
}
|
||||
Err(TryRecvError::Disconnected) => println!("Error: client disconnected."),
|
||||
Err(TryRecvError::Empty) => { /*nothing to do*/ }
|
||||
}
|
||||
@ -243,7 +253,11 @@ fn play(stream: &mut UnixStream, server: &Server, movestr: String) {
|
||||
println!("User tried to play {}", movestr.clone());
|
||||
match clichess::try_to_play_move(&chess, movestr.clone()) {
|
||||
Ok(played_chess) => *chess = played_chess,
|
||||
Err(e) => println!("Error: {}", e),
|
||||
Err(e) => {
|
||||
println!("Error: {}", e);
|
||||
send_ko(stream);
|
||||
return;
|
||||
}
|
||||
};
|
||||
let player_name = player
|
||||
.expect("current server is in the server list")
|
||||
@ -283,6 +297,30 @@ fn play(stream: &mut UnixStream, server: &Server, movestr: String) {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn reset(stream: &mut UnixStream, server: &Server) {
|
||||
//wait for the other server.
|
||||
println!("client {} wants to restart game.", server.id);
|
||||
let (lock, cvar) = &*server.confirm_reset;
|
||||
let mut reset_confirmed = lock.lock().unwrap();
|
||||
if *reset_confirmed {
|
||||
println!("restarting game.");
|
||||
{
|
||||
let mut chess = server.chess_position.lock().unwrap();
|
||||
*chess = Chess::default();
|
||||
}
|
||||
*reset_confirmed = false;
|
||||
cvar.notify_one();
|
||||
} else {
|
||||
//wait for other server
|
||||
println!("client {} is waiting.", server.id);
|
||||
*reset_confirmed = true;
|
||||
while *reset_confirmed {
|
||||
reset_confirmed = cvar.wait(reset_confirmed).unwrap();
|
||||
}
|
||||
}
|
||||
send_ok(stream);
|
||||
}
|
||||
|
||||
fn send_ok(stream: &mut UnixStream) {
|
||||
clichess::write_to_stream(stream, String::from("OK")).unwrap();
|
||||
}
|
||||
|
86
src/lib.rs
86
src/lib.rs
@ -1,4 +1,3 @@
|
||||
use colored::Colorize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use shakmaty::fen::Fen;
|
||||
use shakmaty::san::ParseSanError;
|
||||
@ -11,6 +10,7 @@ use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::io::{BufReader, Write};
|
||||
use std::os::unix::net::UnixStream;
|
||||
use termion::raw::RawTerminal;
|
||||
|
||||
//message to send to the server to signal we disconnected.
|
||||
pub const EXIT_MSG: &str = "exit";
|
||||
@ -47,6 +47,7 @@ pub enum ClientRequest {
|
||||
GetGameInfo,
|
||||
WaitForNextMove,
|
||||
Play(String),
|
||||
Reset,
|
||||
Exit,
|
||||
}
|
||||
|
||||
@ -129,25 +130,27 @@ impl fmt::Display for MoveInputError {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn board_representation(chess: &Chess, side: Color) -> String {
|
||||
let mut s = String::from("");
|
||||
pub fn print_board_representation(
|
||||
chess: &Chess,
|
||||
side: Color,
|
||||
stdout: &mut RawTerminal<io::StdoutLock>,
|
||||
ascii_mode: bool,
|
||||
) {
|
||||
let mut rank_numbers;
|
||||
if side == Color::White {
|
||||
rank_numbers = 8;
|
||||
} else {
|
||||
rank_numbers = 1;
|
||||
}
|
||||
for v in board_string_representation(chess, side) {
|
||||
let mut linenb = 6;
|
||||
for v in board_string_representation(chess, side, ascii_mode) {
|
||||
write!(stdout, "{}", termion::cursor::Goto(10, linenb)).unwrap();
|
||||
for square_to_print in v {
|
||||
s.push_str(&format!(
|
||||
"{}",
|
||||
square_to_print
|
||||
.square_representation
|
||||
.color(square_to_print.color)
|
||||
.on_color(square_to_print.background_color)
|
||||
));
|
||||
print_square(&square_to_print, stdout, ascii_mode);
|
||||
}
|
||||
s.push_str(&format!("{}\n\r", rank_numbers));
|
||||
linenb += 1;
|
||||
resetcolors(stdout);
|
||||
write!(stdout, "{}", rank_numbers).unwrap();
|
||||
if side == Color::White {
|
||||
rank_numbers -= 1;
|
||||
} else {
|
||||
@ -158,14 +161,50 @@ pub fn board_representation(chess: &Chess, side: Color) -> String {
|
||||
if side == Color::Black {
|
||||
files.reverse();
|
||||
}
|
||||
resetcolors(stdout);
|
||||
|
||||
write!(stdout, "{}", termion::cursor::Goto(10, linenb)).unwrap();
|
||||
for file in files {
|
||||
s.push_str(&format!("{} ", file));
|
||||
write!(stdout, "{} ", file).unwrap();
|
||||
}
|
||||
s.push_str("\n\r");
|
||||
s
|
||||
write!(stdout, "{}", termion::cursor::Goto(5, 15)).unwrap();
|
||||
stdout.flush().unwrap();
|
||||
}
|
||||
|
||||
fn board_string_representation(chess: &Chess, side: Color) -> Vec<Vec<SquareToPrint>> {
|
||||
fn print_square(
|
||||
square_to_print: &SquareToPrint,
|
||||
stdout: &mut RawTerminal<io::StdoutLock>,
|
||||
ascii_mode: bool,
|
||||
) {
|
||||
match square_to_print.color.as_ref() {
|
||||
"black" => write!(stdout, "{}", termion::color::Fg(termion::color::Black)),
|
||||
_ => write!(stdout, "{}", termion::color::Fg(termion::color::Reset)),
|
||||
}
|
||||
.unwrap();
|
||||
|
||||
match square_to_print.background_color.as_ref() {
|
||||
"white" => write!(stdout, "{}", termion::color::Bg(termion::color::White)),
|
||||
"green" => write!(stdout, "{}", termion::color::Bg(termion::color::Green)),
|
||||
_ => write!(stdout, "{}", termion::color::Bg(termion::color::Reset)),
|
||||
}
|
||||
.unwrap();
|
||||
if ascii_mode {
|
||||
write!(stdout, "{}", termion::color::Fg(termion::color::Reset)).unwrap();
|
||||
write!(stdout, "{}", termion::color::Bg(termion::color::Reset)).unwrap();
|
||||
}
|
||||
write!(stdout, "{}", square_to_print.square_representation).unwrap();
|
||||
}
|
||||
|
||||
fn resetcolors(stdout: &mut RawTerminal<io::StdoutLock>) {
|
||||
write!(stdout, "{}", termion::color::Fg(termion::color::Reset)).unwrap();
|
||||
write!(stdout, "{}", termion::color::Bg(termion::color::Reset)).unwrap();
|
||||
}
|
||||
|
||||
fn board_string_representation(
|
||||
chess: &Chess,
|
||||
side: Color,
|
||||
ascii_mode: bool,
|
||||
) -> Vec<Vec<SquareToPrint>> {
|
||||
let mut full_board_to_print = Vec::new();
|
||||
for _ in 0..8 {
|
||||
full_board_to_print.push(Vec::new())
|
||||
@ -180,7 +219,7 @@ fn board_string_representation(chess: &Chess, side: Color) -> Vec<Vec<SquareToPr
|
||||
} else {
|
||||
"green".to_string()
|
||||
},
|
||||
square_representation: get_square_representation(&square, chess),
|
||||
square_representation: get_square_representation(&square, chess, ascii_mode),
|
||||
};
|
||||
full_board_to_print[j as usize].push(square_to_print);
|
||||
}
|
||||
@ -204,11 +243,18 @@ pub fn try_to_play_move(chess: &Chess, movestr: String) -> Result<Chess, MoveInp
|
||||
Ok((*chess).clone().play(&mv)?)
|
||||
}
|
||||
|
||||
fn get_square_representation(square: &Square, chess: &Chess) -> String {
|
||||
fn get_square_representation(square: &Square, chess: &Chess, ascii_mode: bool) -> String {
|
||||
let board = (*chess).board();
|
||||
match board.piece_at(*square) {
|
||||
Some(piece) => format!("{} ", piece_char_to_utf8(piece.char())),
|
||||
None => " ".to_string(),
|
||||
Some(piece) => format!(
|
||||
"{} ",
|
||||
if ascii_mode {
|
||||
piece.char()
|
||||
} else {
|
||||
piece_char_to_utf8(piece.char())
|
||||
}
|
||||
),
|
||||
None => if ascii_mode { ". " } else { " " }.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user