Add boats configuration

This commit is contained in:
Pierre HUBERT 2022-09-10 15:02:45 +02:00
parent 481bfe14f4
commit f4d6fdf146
6 changed files with 133 additions and 1 deletions

15
Cargo.lock generated
View File

@ -943,6 +943,7 @@ dependencies = [
"clap", "clap",
"env_logger", "env_logger",
"log", "log",
"serde",
] ]
[[package]] [[package]]
@ -956,6 +957,20 @@ name = "serde"
version = "1.0.144" version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "serde_json" name = "serde_json"

View File

@ -9,5 +9,6 @@ edition = "2021"
clap = { version = "3.2.17", features = ["derive"] } clap = { version = "3.2.17", features = ["derive"] }
log = "0.4.17" log = "0.4.17"
env_logger = "0.9.0" env_logger = "0.9.0"
serde = { version = "1.0.144", features = ["derive"] }
actix-web = "4.1.0" actix-web = "4.1.0"
actix-cors = "0.6.2" actix-cors = "0.6.2"

19
src/consts.rs Normal file
View File

@ -0,0 +1,19 @@
//! # Project constants
pub const MIN_BOATS_NUMBER: usize = 1;
pub const MAX_BOATS_NUMBER: usize = 10;
pub const MIN_BOATS_LENGTH: usize = 1;
pub const MAX_BOATS_LENGTH: usize = 6;
pub const MIN_MAP_WIDTH: usize = 5;
pub const MAX_MAP_WIDTH: usize = 26;
pub const MIN_MAP_HEIGHT: usize = 5;
pub const MAX_MAP_HEIGHT: usize = 26;
pub const MULTI_PLAYER_MAP_WIDTH: usize = 10;
pub const MULTI_PLAYER_MAP_HEIGHT: usize = 10;
pub const MULTI_PLAYER_BOATS_CAN_TOUCH: bool = true;
pub const MULTI_PLAYER_PLAYER_CAN_CONTINUE_AFTER_HIT: bool = true;
pub const MULTI_PLAYER_PLAYER_BOATS: [usize; 5] = [2, 3, 3, 4, 5];

79
src/data.rs Normal file
View File

@ -0,0 +1,79 @@
use crate::consts::*;
#[derive(serde::Serialize)]
pub struct PlayConfiguration {
min_boat_len: usize,
max_boat_len: usize,
min_map_width: usize,
max_map_width: usize,
min_map_height: usize,
max_map_height: usize,
min_boats_number: usize,
max_boats_number: usize,
}
impl Default for PlayConfiguration {
fn default() -> Self {
Self {
min_boat_len: MIN_BOATS_LENGTH,
max_boat_len: MAX_BOATS_LENGTH,
min_map_width: MIN_MAP_WIDTH,
max_map_width: MAX_MAP_WIDTH,
min_map_height: MIN_MAP_HEIGHT,
max_map_height: MAX_MAP_HEIGHT,
min_boats_number: MIN_BOATS_NUMBER,
max_boats_number: MAX_BOATS_NUMBER,
}
}
}
#[derive(serde::Serialize, serde::Deserialize)]
pub struct GameRules {
pub map_width: usize,
pub map_height: usize,
pub boats: Vec<usize>,
pub boats_can_touch: bool,
pub player_continue_on_hit: bool,
}
impl GameRules {
pub fn multi_players_rules() -> Self {
Self {
map_width: MULTI_PLAYER_MAP_WIDTH,
map_height: MULTI_PLAYER_MAP_HEIGHT,
boats: MULTI_PLAYER_PLAYER_BOATS.to_vec(),
boats_can_touch: MULTI_PLAYER_BOATS_CAN_TOUCH,
player_continue_on_hit: MULTI_PLAYER_PLAYER_CAN_CONTINUE_AFTER_HIT,
}
}
pub fn get_errors(&self) -> Vec<&str> {
let config = PlayConfiguration::default();
let mut errors = vec![];
if self.map_width < config.min_map_width || self.map_width > config.max_map_width {
errors.push("Map width is outside bounds!");
}
if self.map_height < config.min_map_height || self.map_height > config.max_map_height {
errors.push("Map height is outside bounds!");
}
if self.boats.len() < config.min_boat_len || self.boats.len() > config.max_boat_len {
errors.push("Number of boats is invalid!");
}
errors
}
}
#[cfg(test)]
mod test {
use crate::data::GameRules;
#[test]
fn multi_players_config() {
assert!(GameRules::multi_players_rules().get_errors().is_empty());
}
}

2
src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod consts;
pub mod data;

View File

@ -2,6 +2,8 @@ use actix_web::{App, HttpResponse, HttpServer, Responder, web};
use clap::Parser; use clap::Parser;
use env_logger::Env; use env_logger::Env;
use sea_battle_backend::data::{GameRules, PlayConfiguration};
/// Simple sea battle server /// Simple sea battle server
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
@ -25,9 +27,19 @@ async fn not_found() -> impl Responder {
HttpResponse::NotFound().json("You missed your strike lol") HttpResponse::NotFound().json("You missed your strike lol")
} }
/// Get bot configuration
async fn bot_configuration() -> impl Responder {
HttpResponse::Ok().json(PlayConfiguration::default())
}
/// Multi-players configuration
async fn multi_players_config() -> impl Responder {
HttpResponse::Ok().json(GameRules::multi_players_rules())
}
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()>{ async fn main() -> std::io::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let args: Args = Args::parse(); let args: Args = Args::parse();
@ -35,6 +47,10 @@ async fn main() -> std::io::Result<()>{
HttpServer::new(|| { HttpServer::new(|| {
App::new() App::new()
.route("/bot/config", web::get().to(bot_configuration))
.route("/random/config", web::get().to(multi_players_config))
.route("/", web::get().to(index)) .route("/", web::get().to(index))
.route("{tail:.*}", web::get().to(not_found)) .route("{tail:.*}", web::get().to(not_found))
}) })