Minor improvments
This commit is contained in:
parent
6e7a0799eb
commit
d8bf2165a8
14
src/data.rs
14
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<usize> {
|
||||
/// Get the list of boats for this configuration
|
||||
pub fn boats_list(&self) -> Vec<usize> {
|
||||
self.boats_str
|
||||
.split(',')
|
||||
.map(|s| s.parse::<usize>().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());
|
||||
}
|
||||
}
|
||||
|
@ -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<AddPlayer> for Game {
|
||||
self.players.push(msg.0.extract());
|
||||
|
||||
if self.players.len() == 2 {
|
||||
self.start_game();
|
||||
self.query_boats_disposition();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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))
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user