Add additional routes for the future clients of SeaBattle
This commit is contained in:
parent
d4223be8b4
commit
3ca6c43c9a
@ -4,7 +4,7 @@ use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
use actix_web_actors::ws;
|
||||
|
||||
use crate::args::Args;
|
||||
use crate::data::{GameRules, PlayConfiguration, VersionInfo};
|
||||
use crate::data::{BoatsLayout, GameRules, PlayConfiguration, VersionInfo};
|
||||
use crate::dispatcher_actor::DispatcherActor;
|
||||
use crate::human_player_ws::{HumanPlayerWS, StartMode};
|
||||
|
||||
@ -29,10 +29,34 @@ async fn game_configuration() -> impl Responder {
|
||||
}
|
||||
|
||||
/// Get default game rules
|
||||
async fn default_rules() -> impl Responder {
|
||||
async fn default_game_rules() -> impl Responder {
|
||||
HttpResponse::Ok().json(GameRules::random_players_rules())
|
||||
}
|
||||
|
||||
/// Validate game rules
|
||||
async fn validate_game_rules(rules: web::Json<GameRules>) -> impl Responder {
|
||||
HttpResponse::Ok().json(rules.get_errors())
|
||||
}
|
||||
|
||||
/// Generate random boats layout
|
||||
async fn gen_boats_layout(rules: web::Json<GameRules>) -> impl Responder {
|
||||
let errors = rules.get_errors();
|
||||
if !errors.is_empty() {
|
||||
return HttpResponse::BadRequest().json(errors);
|
||||
}
|
||||
match BoatsLayout::gen_random_for_rules(&rules) {
|
||||
Ok(l) => HttpResponse::Ok().json(l),
|
||||
Err(e) => {
|
||||
log::error!(
|
||||
"Failed to generate boats layout for valid game rules: {} ! / Rules: {:?}",
|
||||
e,
|
||||
rules
|
||||
);
|
||||
HttpResponse::InternalServerError().json("Failed to generate random layout!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug)]
|
||||
pub struct BotPlayQuery {
|
||||
#[serde(flatten)]
|
||||
@ -140,6 +164,7 @@ async fn start_random(
|
||||
log::info!("New random play");
|
||||
resp
|
||||
}
|
||||
|
||||
pub async fn start_server(args: Args) -> std::io::Result<()> {
|
||||
log::info!("Start to listen on {}", args.listen_address);
|
||||
|
||||
@ -160,7 +185,9 @@ pub async fn start_server(args: Args) -> std::io::Result<()> {
|
||||
.wrap(cors)
|
||||
.route("/version", web::get().to(version_information))
|
||||
.route("/config", web::get().to(game_configuration))
|
||||
.route("/default_rules", web::get().to(default_rules))
|
||||
.route("/game_rules/default", web::get().to(default_game_rules))
|
||||
.route("/game_rules/validate", web::post().to(validate_game_rules))
|
||||
.route("/generate_boats_layout", web::post().to(gen_boats_layout))
|
||||
.route("/play/bot", web::get().to(start_bot_play))
|
||||
.route("/play/create_invite", web::get().to(start_create_invite))
|
||||
.route("/play/accept_invite", web::get().to(start_accept_invite))
|
||||
|
Loading…
Reference in New Issue
Block a user