mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
feat: implement single player for cli client
This commit is contained in:
@@ -6,6 +6,7 @@ use std::{
|
|||||||
use checkers::{
|
use checkers::{
|
||||||
logic::board::pawn::{CapturePath, MovePath},
|
logic::board::pawn::{CapturePath, MovePath},
|
||||||
network::{client::Client, message::ServerMessage, network_identity::NetworkIdentity},
|
network::{client::Client, message::ServerMessage, network_identity::NetworkIdentity},
|
||||||
|
super_advanced_ai::{Bot, BotDificulty},
|
||||||
};
|
};
|
||||||
use log::{error, warn};
|
use log::{error, warn};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
@@ -14,6 +15,7 @@ const QUIT_COMMAND: &str = "/quit";
|
|||||||
const HELP_COMMAND: &str = "/help";
|
const HELP_COMMAND: &str = "/help";
|
||||||
const SEND_COMMAND: &str = "/send";
|
const SEND_COMMAND: &str = "/send";
|
||||||
const READY_COMMAND: &str = "/ready";
|
const READY_COMMAND: &str = "/ready";
|
||||||
|
const SINGLEPLAYER_COMMAND: &str = "/single";
|
||||||
|
|
||||||
const CAPTURE_COMMAND: &str = "/capture";
|
const CAPTURE_COMMAND: &str = "/capture";
|
||||||
const MOVE_COMMAND: &str = "/move";
|
const MOVE_COMMAND: &str = "/move";
|
||||||
@@ -22,7 +24,9 @@ enum CliCommands {
|
|||||||
Quit,
|
Quit,
|
||||||
Help,
|
Help,
|
||||||
Send,
|
Send,
|
||||||
|
|
||||||
Ready,
|
Ready,
|
||||||
|
Single,
|
||||||
|
|
||||||
Capture,
|
Capture,
|
||||||
Move,
|
Move,
|
||||||
@@ -46,6 +50,7 @@ impl TryFrom<&str> for CliCommands {
|
|||||||
HELP_COMMAND => Ok(CliCommands::Help),
|
HELP_COMMAND => Ok(CliCommands::Help),
|
||||||
SEND_COMMAND => Ok(CliCommands::Send),
|
SEND_COMMAND => Ok(CliCommands::Send),
|
||||||
READY_COMMAND => Ok(CliCommands::Ready),
|
READY_COMMAND => Ok(CliCommands::Ready),
|
||||||
|
SINGLEPLAYER_COMMAND => Ok(CliCommands::Single),
|
||||||
|
|
||||||
CAPTURE_COMMAND => Ok(CliCommands::Capture),
|
CAPTURE_COMMAND => Ok(CliCommands::Capture),
|
||||||
MOVE_COMMAND => Ok(CliCommands::Move),
|
MOVE_COMMAND => Ok(CliCommands::Move),
|
||||||
@@ -74,6 +79,11 @@ impl Default for ClientContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn run_bot() {
|
||||||
|
let bot = Bot::new(BotDificulty::Easy).await;
|
||||||
|
bot.game_loop().await;
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
@@ -112,12 +122,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
│\t {} - show this message
|
│\t {} - show this message
|
||||||
│\t {} - message the server you're ready
|
│\t {} - message the server you're ready
|
||||||
│\t {} - send text message to the server
|
│\t {} - send text message to the server
|
||||||
|
│\t {} - play alone
|
||||||
│
|
│
|
||||||
│\t {} <index>- choose a capture path from the list
|
│\t {} <index>- choose a capture path from the list
|
||||||
│\t {} <index> <index>- choose move from the list. First number is pawn you want to move and second is position it should take
|
│\t {} <index> <index>- choose move from the list. First number is pawn you want to move and second is position it should take
|
||||||
└───────────────────────────────────────
|
└───────────────────────────────────────
|
||||||
",
|
",
|
||||||
QUIT_COMMAND, HELP_COMMAND, READY_COMMAND, SEND_COMMAND, CAPTURE_COMMAND, MOVE_COMMAND
|
QUIT_COMMAND, HELP_COMMAND, READY_COMMAND, SEND_COMMAND, SINGLEPLAYER_COMMAND , CAPTURE_COMMAND, MOVE_COMMAND
|
||||||
);
|
);
|
||||||
|
|
||||||
println!("───────Type /help for help───────");
|
println!("───────Type /help for help───────");
|
||||||
@@ -199,6 +210,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
CliCommands::Ready => {
|
CliCommands::Ready => {
|
||||||
client.signal_readiness().await;
|
client.signal_readiness().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CliCommands::Single => {
|
||||||
|
println!("Tried connecting bot to the server! Make sure to be /ready");
|
||||||
|
tokio::spawn(run_bot());
|
||||||
|
}
|
||||||
|
|
||||||
CliCommands::Send => {
|
CliCommands::Send => {
|
||||||
let mut args: String = words[1..].join(" ");
|
let mut args: String = words[1..].join(" ");
|
||||||
args = args.trim().to_string();
|
args = args.trim().to_string();
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ impl Bot {
|
|||||||
.await
|
.await
|
||||||
.expect("Couldn't connect to the server. Is it online?");
|
.expect("Couldn't connect to the server. Is it online?");
|
||||||
|
|
||||||
|
client.update();
|
||||||
client.signal_readiness().await; // bot is always ready
|
client.signal_readiness().await; // bot is always ready
|
||||||
|
|
||||||
let mut receiver = client
|
let mut receiver = client
|
||||||
@@ -83,12 +84,8 @@ impl Bot {
|
|||||||
content,
|
content,
|
||||||
} => {
|
} => {
|
||||||
info!("Bot got message from [{}] - {}", sender, content);
|
info!("Bot got message from [{}] - {}", sender, content);
|
||||||
client
|
|
||||||
.send_text_message(
|
|
||||||
"Why are you messaging bot? Are you stupid?".to_string(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::network::message::ServerMessage::GameEnd { result } => {
|
crate::network::message::ServerMessage::GameEnd { result } => {
|
||||||
info!("Game has ended!");
|
info!("Game has ended!");
|
||||||
match result {
|
match result {
|
||||||
|
|||||||
Reference in New Issue
Block a user