From d8bf2165a84b7d3663062a0c20f23dfaace894e6 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sun, 11 Sep 2022 16:51:14 +0200 Subject: [PATCH] Minor improvments --- src/data.rs | 14 ++++++++------ src/game.rs | 4 ++-- src/main.rs | 8 ++++---- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/data.rs b/src/data.rs index 2342b29..bbf4705 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,5 +1,6 @@ use crate::consts::*; +/// Specifies the kind of boat to use #[derive(serde::Serialize, serde::Deserialize, Debug, Copy, Clone)] pub enum BotType { Random, @@ -54,7 +55,7 @@ pub struct GameRules { } impl GameRules { - pub fn multi_players_rules() -> Self { + pub fn random_players_rules() -> Self { Self { map_width: MULTI_PLAYER_MAP_WIDTH, map_height: MULTI_PLAYER_MAP_HEIGHT, @@ -69,7 +70,8 @@ impl GameRules { } } - pub fn boats(&self) -> Vec { + /// Get the list of boats for this configuration + pub fn boats_list(&self) -> Vec { self.boats_str .split(',') .map(|s| s.parse::().unwrap_or_default()) @@ -89,13 +91,13 @@ impl GameRules { errors.push("Map height is outside bounds!"); } - if self.boats().len() < config.min_boats_number - || self.boats().len() > config.max_boats_number + if self.boats_list().len() < config.min_boats_number + || self.boats_list().len() > config.max_boats_number { errors.push("Number of boats is invalid!"); } - for boat in self.boats() { + for boat in self.boats_list() { if boat < config.min_boat_len || boat > config.max_boat_len { errors.push("A boat has an invalid length"); } @@ -111,6 +113,6 @@ mod test { #[test] fn multi_players_config() { - assert!(GameRules::multi_players_rules().get_errors().is_empty()); + assert!(GameRules::random_players_rules().get_errors().is_empty()); } } diff --git a/src/game.rs b/src/game.rs index a8a4373..0c91639 100644 --- a/src/game.rs +++ b/src/game.rs @@ -52,7 +52,7 @@ impl Game { } /// Once the two player has been registered, the game may start - fn start_game(&mut self) { + fn query_boats_disposition(&mut self) { assert_eq!(self.status, GameStatus::Created); self.status = GameStatus::WaitingForBoatsDisposition; self.players[0].query_boats_layout(&self.rules); @@ -77,7 +77,7 @@ impl Handler for Game { self.players.push(msg.0.extract()); if self.players.len() == 2 { - self.start_game(); + self.query_boats_disposition(); } } } diff --git a/src/main.rs b/src/main.rs index 231517d..98585c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,9 +54,9 @@ async fn start_bot_play( resp } -/// Multi-players configuration -async fn multi_players_config() -> impl Responder { - HttpResponse::Ok().json(GameRules::multi_players_rules()) +/// Random play configuration +async fn random_play_config() -> impl Responder { + HttpResponse::Ok().json(GameRules::random_players_rules()) } #[actix_web::main] @@ -78,7 +78,7 @@ async fn main() -> std::io::Result<()> { .wrap(cors) .route("/bot/config", web::get().to(bot_configuration)) .route("/bot/play", web::get().to(start_bot_play)) - .route("/random/config", web::get().to(multi_players_config)) + .route("/random/config", web::get().to(random_play_config)) .route("/", web::get().to(index)) .route("{tail:.*}", web::get().to(not_found)) })