Compare commits
6 Commits
3c2b96f3a2
...
b1145cc362
Author | SHA1 | Date | |
---|---|---|---|
b1145cc362 | |||
e0132b68ed | |||
e97f4b593a | |||
1c08e2ec01 | |||
70d70c2851 | |||
04ee20dac2 |
@@ -90,12 +90,12 @@ impl Client {
|
||||
match Self::recv_next_msg(&mut stream).await {
|
||||
Ok(msg) => {
|
||||
if let Err(e) = sender.send(msg.clone()) {
|
||||
log::error!("Failed to forward ws message! {} (msg={:?})", e, msg);
|
||||
log::debug!("Failed to forward ws message! {} (msg={:?})", e, msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Failed receive next message from websocket! {}", e);
|
||||
log::debug!("Failed receive next message from websocket! {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -162,4 +162,11 @@ impl Client {
|
||||
pub async fn recv_next_message(&self) -> Res<ServerMessage> {
|
||||
Ok(self.receiver.recv()?)
|
||||
}
|
||||
|
||||
/// Close connection
|
||||
pub async fn close_connection(&mut self) {
|
||||
if let Err(e) = self.sink.send(Message::Close(None)).await {
|
||||
log::debug!("Failed to close WS connection! {:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -14,15 +14,14 @@ use tui::Terminal;
|
||||
|
||||
use cli_player::cli_args::{cli_args, TestDevScreen};
|
||||
use cli_player::client::Client;
|
||||
use cli_player::server::start_server_if_missing;
|
||||
use cli_player::ui_screens::configure_game_rules::GameRulesConfigurationScreen;
|
||||
use cli_player::ui_screens::game_screen::GameScreen;
|
||||
use cli_player::ui_screens::input_screen::InputScreen;
|
||||
use cli_player::ui_screens::popup_screen::PopupScreen;
|
||||
use cli_player::ui_screens::select_play_mode_screen::{SelectPlayModeResult, SelectPlayModeScreen};
|
||||
use cli_player::ui_screens::*;
|
||||
use sea_battle_backend::consts::{MAX_PLAYER_NAME_LENGTH, MIN_PLAYER_NAME_LENGTH};
|
||||
use sea_battle_backend::data::GameRules;
|
||||
use sea_battle_backend::human_player_ws::ServerMessage;
|
||||
use sea_battle_backend::utils::Res;
|
||||
|
||||
/// Test code screens
|
||||
@@ -74,12 +73,17 @@ async fn run_dev<B: Backend>(
|
||||
|
||||
/// Ask the user to specify its username
|
||||
fn query_username<B: Backend>(terminal: &mut Terminal<B>) -> Res<String> {
|
||||
let hostname = hostname::get()?.to_string_lossy().to_string();
|
||||
let mut hostname = hostname::get()?.to_string_lossy().to_string();
|
||||
if hostname.len() > MAX_PLAYER_NAME_LENGTH {
|
||||
hostname = hostname[0..MAX_PLAYER_NAME_LENGTH].to_string();
|
||||
}
|
||||
|
||||
let res =
|
||||
InputScreen::new("Please specify the name to which other players should identify you:")
|
||||
.set_title("Player name")
|
||||
.set_value(&hostname)
|
||||
.set_min_length(MIN_PLAYER_NAME_LENGTH)
|
||||
.set_max_length(MAX_PLAYER_NAME_LENGTH)
|
||||
.show(terminal)?;
|
||||
|
||||
Ok(res.value().unwrap_or(hostname))
|
||||
@@ -103,21 +107,11 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Res {
|
||||
}
|
||||
}
|
||||
|
||||
match choice {
|
||||
let client = match choice {
|
||||
ScreenResult::Ok(SelectPlayModeResult::PlayRandom) => {
|
||||
PopupScreen::new("Connecting...").show_once(terminal)?;
|
||||
|
||||
let client = Client::start_random_play(&username).await?;
|
||||
PopupScreen::new("Waiting for opponent...").show_once(terminal)?;
|
||||
|
||||
// Wait for the server to become ready
|
||||
while !matches!(
|
||||
client.recv_next_message().await?,
|
||||
ServerMessage::OpponentConnected
|
||||
) {}
|
||||
|
||||
// Display game screen
|
||||
GameScreen::new(client).show(terminal).await?;
|
||||
Client::start_random_play(&username).await?
|
||||
}
|
||||
|
||||
// Play against bot
|
||||
@@ -130,28 +124,21 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Res {
|
||||
|
||||
// Then connect to server
|
||||
PopupScreen::new("Connecting...").show_once(terminal)?;
|
||||
let client = Client::start_bot_play(&rules).await?;
|
||||
|
||||
// Wait for the server to become ready
|
||||
while !matches!(
|
||||
client.recv_next_message().await?,
|
||||
ServerMessage::OpponentConnected
|
||||
) {}
|
||||
|
||||
// Display game screen
|
||||
GameScreen::new(client).show(terminal).await?;
|
||||
Client::start_bot_play(&rules).await?
|
||||
}
|
||||
|
||||
ScreenResult::Canceled | ScreenResult::Ok(SelectPlayModeResult::Exit) => return Ok(()),
|
||||
}
|
||||
};
|
||||
|
||||
// Display game screen
|
||||
GameScreen::new(client).show(terminal).await?;
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::Builder::from_env(Env::default()).init();
|
||||
|
||||
start_server_if_missing().await;
|
||||
cli_args(); // Avoid a crash if help argument is triggered
|
||||
|
||||
// setup terminal
|
||||
enable_raw_mode()?;
|
||||
|
@@ -26,5 +26,7 @@ pub async fn start_server_if_missing() {
|
||||
.expect("Failed to run local server!")
|
||||
}));
|
||||
});
|
||||
|
||||
network_utils::wait_for_port(cli_args().listen_port()).await;
|
||||
}
|
||||
}
|
||||
|
@@ -26,9 +26,11 @@ use crate::ui_widgets::game_map_widget::{ColoredCells, GameMapWidget};
|
||||
|
||||
type CoordinatesMapper = HashMap<Coordinates, Coordinates>;
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
#[derive(Eq, PartialEq, Ord, PartialOrd)]
|
||||
enum GameStatus {
|
||||
Pending,
|
||||
Connecting,
|
||||
WaitingForAnotherPlayer,
|
||||
OpponentConnected,
|
||||
WaitingForOpponentBoatsConfig,
|
||||
OpponentReady,
|
||||
Starting,
|
||||
@@ -45,14 +47,14 @@ enum GameStatus {
|
||||
|
||||
impl GameStatus {
|
||||
pub fn can_show_game_maps(&self) -> bool {
|
||||
self != &GameStatus::Pending
|
||||
&& self != &GameStatus::WaitingForOpponentBoatsConfig
|
||||
&& self != &GameStatus::OpponentReady
|
||||
self > &GameStatus::Starting
|
||||
}
|
||||
|
||||
pub fn status_text(&self) -> &str {
|
||||
match self {
|
||||
GameStatus::Pending => "Game is pending...",
|
||||
GameStatus::Connecting => "Connecting...",
|
||||
GameStatus::WaitingForAnotherPlayer => "Waiting for another player...",
|
||||
GameStatus::OpponentConnected => "Opponent connected!",
|
||||
GameStatus::WaitingForOpponentBoatsConfig => "Waiting for ### boats configuration",
|
||||
GameStatus::OpponentReady => "### is ready!",
|
||||
GameStatus::Starting => "Game is starting...",
|
||||
@@ -90,6 +92,7 @@ impl Buttons {
|
||||
|
||||
pub struct GameScreen {
|
||||
client: Client,
|
||||
invite_code: Option<String>,
|
||||
status: GameStatus,
|
||||
opponent_name: Option<String>,
|
||||
game: CurrentGameStatus,
|
||||
@@ -102,7 +105,8 @@ impl GameScreen {
|
||||
pub fn new(client: Client) -> Self {
|
||||
Self {
|
||||
client,
|
||||
status: GameStatus::Pending,
|
||||
invite_code: None,
|
||||
status: GameStatus::Connecting,
|
||||
opponent_name: None,
|
||||
game: Default::default(),
|
||||
curr_shoot_position: Coordinates::new(0, 0),
|
||||
@@ -141,6 +145,7 @@ impl GameScreen {
|
||||
KeyCode::Char('q')
|
||||
if confirm(terminal, "Do you really want to leave game?") =>
|
||||
{
|
||||
self.client.close_connection().await;
|
||||
return Ok(ScreenResult::Canceled);
|
||||
}
|
||||
|
||||
@@ -188,7 +193,10 @@ impl GameScreen {
|
||||
.await?;
|
||||
self.status = GameStatus::RematchRejected;
|
||||
}
|
||||
Buttons::QuitGame => return Ok(ScreenResult::Ok(())),
|
||||
Buttons::QuitGame => {
|
||||
self.client.close_connection().await;
|
||||
return Ok(ScreenResult::Ok(()));
|
||||
}
|
||||
},
|
||||
|
||||
_ => {}
|
||||
@@ -206,6 +214,16 @@ impl GameScreen {
|
||||
coordinates_mapper.get(&Coordinates::new(mouse.column, mouse.row))
|
||||
{
|
||||
self.curr_shoot_position = *c;
|
||||
|
||||
if self.can_fire()
|
||||
&& self.game.can_fire_at_location(self.curr_shoot_position)
|
||||
{
|
||||
self.client
|
||||
.send_message(&ClientMessage::Fire {
|
||||
location: self.curr_shoot_position,
|
||||
})
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -214,10 +232,23 @@ impl GameScreen {
|
||||
// Handle incoming messages
|
||||
while let Some(msg) = self.client.try_recv_next_message().await? {
|
||||
match msg {
|
||||
ServerMessage::SetInviteCode { .. } => unimplemented!(),
|
||||
ServerMessage::InvalidInviteCode => unimplemented!(),
|
||||
ServerMessage::WaitingForAnotherPlayer => unimplemented!(),
|
||||
ServerMessage::OpponentConnected => unimplemented!(),
|
||||
ServerMessage::SetInviteCode { code } => {
|
||||
self.status = GameStatus::WaitingForAnotherPlayer;
|
||||
self.invite_code = Some(code);
|
||||
}
|
||||
|
||||
ServerMessage::InvalidInviteCode => {
|
||||
PopupScreen::new("Invalid invite code!").show(terminal)?;
|
||||
return Ok(ScreenResult::Ok(()));
|
||||
}
|
||||
|
||||
ServerMessage::WaitingForAnotherPlayer => {
|
||||
self.status = GameStatus::WaitingForAnotherPlayer;
|
||||
}
|
||||
|
||||
ServerMessage::OpponentConnected => {
|
||||
self.status = GameStatus::OpponentConnected;
|
||||
}
|
||||
|
||||
ServerMessage::SetOpponentName { name } => self.opponent_name = Some(name),
|
||||
|
||||
@@ -232,6 +263,7 @@ impl GameScreen {
|
||||
.await?
|
||||
}
|
||||
ScreenResult::Canceled => {
|
||||
self.client.close_connection().await;
|
||||
return Ok(ScreenResult::Canceled);
|
||||
}
|
||||
};
|
||||
@@ -427,13 +459,19 @@ impl GameScreen {
|
||||
}
|
||||
|
||||
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) -> CoordinatesMapper {
|
||||
let status_text = self
|
||||
let mut status_text = self
|
||||
.status
|
||||
.status_text()
|
||||
.replace("###", self.opponent_name());
|
||||
|
||||
// If the game is in a state where game maps can not be shown
|
||||
if !self.status.can_show_game_maps() {
|
||||
if self.status == GameStatus::WaitingForAnotherPlayer {
|
||||
if let Some(code) = &self.invite_code {
|
||||
status_text.push_str(&format!("\n Invite code: {}", code));
|
||||
}
|
||||
}
|
||||
|
||||
PopupScreen::new(&status_text).show_in_frame(f);
|
||||
return HashMap::default();
|
||||
}
|
||||
|
@@ -55,6 +55,16 @@ impl<'a> InputScreen<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_min_length(mut self, v: usize) -> Self {
|
||||
self.min_len = v;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_max_length(mut self, v: usize) -> Self {
|
||||
self.max_len = v;
|
||||
self
|
||||
}
|
||||
|
||||
/// Get error contained in input
|
||||
fn error(&self) -> Option<&'static str> {
|
||||
if self.value.len() > self.max_len {
|
||||
|
@@ -58,6 +58,8 @@ impl Player for BotPlayer {
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
fn waiting_for_opponent_boats_layout(&self) {}
|
||||
|
||||
fn notify_other_player_ready(&self) {}
|
||||
|
||||
fn notify_game_starting(&self) {}
|
||||
|
@@ -21,3 +21,6 @@ pub const MULTI_PLAYER_PLAYER_BOATS: [usize; 5] = [2, 3, 3, 4, 5];
|
||||
pub const ALPHABET: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
pub const INVITE_CODE_LENGTH: usize = 5;
|
||||
|
||||
pub const MIN_PLAYER_NAME_LENGTH: usize = 1;
|
||||
pub const MAX_PLAYER_NAME_LENGTH: usize = 10;
|
||||
|
@@ -57,6 +57,8 @@ pub struct PlayConfiguration {
|
||||
pub max_boats_number: usize,
|
||||
pub bot_types: &'static [BotDescription],
|
||||
pub ordinate_alphabet: &'static str,
|
||||
pub min_player_name_len: usize,
|
||||
pub max_player_name_len: usize,
|
||||
}
|
||||
|
||||
impl Default for PlayConfiguration {
|
||||
@@ -72,6 +74,8 @@ impl Default for PlayConfiguration {
|
||||
max_boats_number: MAX_BOATS_NUMBER,
|
||||
bot_types: &BOTS_TYPES,
|
||||
ordinate_alphabet: ALPHABET,
|
||||
min_player_name_len: MIN_PLAYER_NAME_LENGTH,
|
||||
max_player_name_len: MAX_PLAYER_NAME_LENGTH,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -89,7 +89,6 @@ impl Handler<CreateInvite> for DispatcherActor {
|
||||
msg.1.do_send(ServerMessage::SetInviteCode {
|
||||
code: invite_code.clone(),
|
||||
});
|
||||
msg.1.do_send(ServerMessage::WaitingForAnotherPlayer);
|
||||
self.with_invite.insert(
|
||||
invite_code,
|
||||
PendingPlayer {
|
||||
|
@@ -22,6 +22,8 @@ pub trait Player {
|
||||
|
||||
fn rejected_boats_layout(&self, errors: Vec<&'static str>);
|
||||
|
||||
fn waiting_for_opponent_boats_layout(&self);
|
||||
|
||||
fn notify_other_player_ready(&self);
|
||||
|
||||
fn notify_game_starting(&self);
|
||||
@@ -68,6 +70,14 @@ enum GameStatus {
|
||||
RematchRejected,
|
||||
}
|
||||
|
||||
impl GameStatus {
|
||||
pub fn can_game_continue_with_bot(&self) -> bool {
|
||||
*self != GameStatus::Finished
|
||||
&& *self != GameStatus::RematchRejected
|
||||
&& *self != GameStatus::RematchRequested
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Game {
|
||||
rules: GameRules,
|
||||
players: Vec<Arc<dyn Player>>,
|
||||
@@ -286,6 +296,8 @@ impl Handler<SetBoatsLayout> for Game {
|
||||
if self.map_0.is_some() && self.map_1.is_some() {
|
||||
self.players.iter().for_each(|p| p.notify_game_starting());
|
||||
self.start_fire_exchanges();
|
||||
} else {
|
||||
self.players[player_index].waiting_for_opponent_boats_layout();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -363,7 +375,9 @@ impl Handler<PlayerLeftGame> for Game {
|
||||
self.players[opponent(offline_player)].opponent_left_game();
|
||||
|
||||
// If the other player is a bot or if the game is not running, stop the game
|
||||
if self.status != GameStatus::Started || self.players[opponent(offline_player)].is_bot() {
|
||||
if !self.status.can_game_continue_with_bot()
|
||||
|| self.players[opponent(offline_player)].is_bot()
|
||||
{
|
||||
ctx.stop();
|
||||
} else {
|
||||
// Replace the player with a bot
|
||||
@@ -371,8 +385,11 @@ impl Handler<PlayerLeftGame> for Game {
|
||||
Arc::new(BotPlayer::new(self.rules.bot_type, ctx.address()));
|
||||
self.players[opponent(offline_player)].opponent_replaced_by_bot();
|
||||
|
||||
if self.turn == offline_player {
|
||||
// Re-do current action
|
||||
if self.status == GameStatus::Started {
|
||||
self.request_fire();
|
||||
} else if self.status == GameStatus::WaitingForBoatsDisposition {
|
||||
self.players[offline_player].query_boats_layout(&self.rules);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -47,6 +47,11 @@ impl Player for HumanPlayer {
|
||||
});
|
||||
}
|
||||
|
||||
fn waiting_for_opponent_boats_layout(&self) {
|
||||
self.player
|
||||
.do_send(ServerMessage::WaitingForOtherPlayerConfiguration);
|
||||
}
|
||||
|
||||
fn notify_other_player_ready(&self) {
|
||||
self.player.do_send(ServerMessage::OpponentReady);
|
||||
}
|
||||
@@ -109,6 +114,7 @@ impl Player for HumanPlayer {
|
||||
|
||||
impl HumanPlayer {
|
||||
pub fn handle_client_message(&self, msg: ClientMessage) {
|
||||
log::debug!("Got message from client: {:?}", msg);
|
||||
match msg {
|
||||
ClientMessage::StopGame => self.game.do_send(PlayerLeftGame(self.uuid)),
|
||||
ClientMessage::BoatsLayout { layout } => {
|
||||
|
@@ -8,6 +8,7 @@ use actix_web_actors::ws::{CloseCode, CloseReason, Message, ProtocolError, Webso
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::bot_player::BotPlayer;
|
||||
use crate::consts::{MAX_PLAYER_NAME_LENGTH, MIN_PLAYER_NAME_LENGTH};
|
||||
use crate::data::{BoatsLayout, Coordinates, CurrentGameStatus, FireResult, GameRules};
|
||||
use crate::dispatcher_actor::{AcceptInvite, CreateInvite, DispatcherActor, PlayRandom};
|
||||
use crate::game::{AddPlayer, Game};
|
||||
@@ -149,6 +150,12 @@ impl Actor for HumanPlayerWS {
|
||||
type Context = WebsocketContext<Self>;
|
||||
|
||||
fn started(&mut self, ctx: &mut Self::Context) {
|
||||
// Check player name length
|
||||
if self.name.len() < MIN_PLAYER_NAME_LENGTH || self.name.len() > MAX_PLAYER_NAME_LENGTH {
|
||||
ctx.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
self.hb(ctx);
|
||||
|
||||
self.send_message(ServerMessage::WaitingForAnotherPlayer, ctx);
|
||||
@@ -222,7 +229,7 @@ impl StreamHandler<Result<ws::Message, ProtocolError>> for HumanPlayerWS {
|
||||
log::warn!("Got unsupported continuation message!");
|
||||
}
|
||||
Ok(Message::Pong(_)) => {
|
||||
log::info!("Got pong message");
|
||||
log::debug!("Got pong message");
|
||||
self.hb = Instant::now();
|
||||
}
|
||||
Ok(Message::Close(reason)) => {
|
||||
@@ -238,6 +245,7 @@ impl Handler<ServerMessage> for HumanPlayerWS {
|
||||
type Result = ();
|
||||
|
||||
fn handle(&mut self, msg: ServerMessage, ctx: &mut Self::Context) -> Self::Result {
|
||||
log::debug!("Send message through WS: {:?}", msg);
|
||||
ctx.text(serde_json::to_string(&msg).unwrap());
|
||||
}
|
||||
}
|
||||
|
@@ -131,6 +131,8 @@ async fn start_random(
|
||||
resp
|
||||
}
|
||||
pub async fn start_server(args: Args) -> std::io::Result<()> {
|
||||
log::info!("Start to listen on {}", args.listen_address);
|
||||
|
||||
let args_clone = args.clone();
|
||||
|
||||
let dispatcher_actor = DispatcherActor::default().start();
|
||||
|
Reference in New Issue
Block a user