Handle bug that happens when a player leaves the game in an early stage

This commit is contained in:
2022-10-16 18:25:59 +02:00
parent 04ee20dac2
commit 70d70c2851
5 changed files with 21 additions and 3 deletions

View File

@@ -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);
}
}
}