Handle bug that happens when a player leaves the game in an early stage
This commit is contained in:
		@@ -68,6 +68,14 @@ enum GameStatus {
 | 
			
		||||
    RematchRejected,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl GameStatus {
 | 
			
		||||
    pub fn can_game_continue_with_bot(&self) -> bool {
 | 
			
		||||
        *self != GameStatus::Finished
 | 
			
		||||
            && *self != GameStatus::RematchRejected
 | 
			
		||||
            && *self != GameStatus::RematchRejected
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct Game {
 | 
			
		||||
    rules: GameRules,
 | 
			
		||||
    players: Vec<Arc<dyn Player>>,
 | 
			
		||||
@@ -363,7 +371,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 +381,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);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -109,6 +109,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 } => {
 | 
			
		||||
 
 | 
			
		||||
@@ -222,7 +222,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 +238,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