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