Add strike timeout setting
This commit is contained in:
		@@ -24,3 +24,6 @@ pub const INVITE_CODE_LENGTH: usize = 5;
 | 
			
		||||
 | 
			
		||||
pub const MIN_PLAYER_NAME_LENGTH: usize = 1;
 | 
			
		||||
pub const MAX_PLAYER_NAME_LENGTH: usize = 10;
 | 
			
		||||
 | 
			
		||||
pub const MIN_STRIKE_TIMEOUT: usize = 5;
 | 
			
		||||
pub const MAX_STRIKE_TIMEOUT: usize = 90;
 | 
			
		||||
 
 | 
			
		||||
@@ -489,6 +489,7 @@ mod test {
 | 
			
		||||
            boats_str: "1,1".to_string(),
 | 
			
		||||
            boats_can_touch: false,
 | 
			
		||||
            player_continue_on_hit: false,
 | 
			
		||||
            strike_timeout: None,
 | 
			
		||||
            bot_type: BotType::Random,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
use crate::consts::*;
 | 
			
		||||
use crate::data::{BotType, PlayConfiguration};
 | 
			
		||||
use serde_with::{serde_as, DisplayFromStr};
 | 
			
		||||
use serde_with::{serde_as, DisplayFromStr, NoneAsEmptyString};
 | 
			
		||||
 | 
			
		||||
#[serde_as]
 | 
			
		||||
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Eq, PartialEq)]
 | 
			
		||||
@@ -15,6 +15,8 @@ pub struct GameRules {
 | 
			
		||||
    pub boats_can_touch: bool,
 | 
			
		||||
    #[serde_as(as = "DisplayFromStr")]
 | 
			
		||||
    pub player_continue_on_hit: bool,
 | 
			
		||||
    #[serde_as(as = "NoneAsEmptyString")]
 | 
			
		||||
    pub strike_timeout: Option<usize>,
 | 
			
		||||
    pub bot_type: BotType,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -36,6 +38,7 @@ impl GameRules {
 | 
			
		||||
                .join(","),
 | 
			
		||||
            boats_can_touch: MULTI_PLAYER_BOATS_CAN_TOUCH,
 | 
			
		||||
            player_continue_on_hit: MULTI_PLAYER_PLAYER_CAN_CONTINUE_AFTER_HIT,
 | 
			
		||||
            strike_timeout: Some(30),
 | 
			
		||||
            bot_type: BotType::Smart,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -121,6 +124,16 @@ impl GameRules {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if let Some(timeout) = self.strike_timeout {
 | 
			
		||||
            if timeout < config.min_strike_timeout {
 | 
			
		||||
                errors.push("Strike timeout is too short!");
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if timeout > config.max_strike_timeout {
 | 
			
		||||
                errors.push("Strike timeout is too long!");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        errors
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -59,6 +59,8 @@ pub struct PlayConfiguration {
 | 
			
		||||
    pub ordinate_alphabet: &'static str,
 | 
			
		||||
    pub min_player_name_len: usize,
 | 
			
		||||
    pub max_player_name_len: usize,
 | 
			
		||||
    pub min_strike_timeout: usize,
 | 
			
		||||
    pub max_strike_timeout: usize,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Default for PlayConfiguration {
 | 
			
		||||
@@ -76,6 +78,8 @@ impl Default for PlayConfiguration {
 | 
			
		||||
            ordinate_alphabet: ALPHABET,
 | 
			
		||||
            min_player_name_len: MIN_PLAYER_NAME_LENGTH,
 | 
			
		||||
            max_player_name_len: MAX_PLAYER_NAME_LENGTH,
 | 
			
		||||
            min_strike_timeout: MIN_STRIKE_TIMEOUT,
 | 
			
		||||
            max_strike_timeout: MAX_STRIKE_TIMEOUT,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -163,6 +163,7 @@ pub async fn start_server(args: Args) -> std::io::Result<()> {
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod test {
 | 
			
		||||
    use crate::data::GameRules;
 | 
			
		||||
    use crate::server::BotPlayQuery;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
@@ -177,4 +178,20 @@ mod test {
 | 
			
		||||
 | 
			
		||||
        assert_eq!(query, des)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn simple_bot_request_serialize_deserialize_no_timeout() {
 | 
			
		||||
        let query = BotPlayQuery {
 | 
			
		||||
            rules: GameRules {
 | 
			
		||||
                strike_timeout: None,
 | 
			
		||||
                ..Default::default()
 | 
			
		||||
            },
 | 
			
		||||
            player_name: "Player".to_string(),
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        let string = serde_urlencoded::to_string(&query).unwrap();
 | 
			
		||||
        let des = serde_urlencoded::from_str(&string).unwrap();
 | 
			
		||||
 | 
			
		||||
        assert_eq!(query, des)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user