gracefully handle no move left instead of 404

This commit is contained in:
Max Känner 2024-10-06 22:27:47 +02:00
parent e545f09bb4
commit 6751c7e9cd

View File

@ -1,6 +1,6 @@
#![allow(clippy::needless_pass_by_value)] #![allow(clippy::needless_pass_by_value)]
use battlesnake::{logic, Action, GameState}; use battlesnake::{logic, Action, Direction, GameState};
use log::{error, info}; use log::{error, info};
use rocket::{ use rocket::{
fairing::AdHoc, get, http::Status, launch, post, routes, serde::json::Json, tokio::task, fairing::AdHoc, get, http::Status, launch, post, routes, serde::json::Json, tokio::task,
@ -26,7 +26,7 @@ fn handle_start(start_req: Json<GameState>) -> Status {
} }
#[post("/move", format = "json", data = "<move_req>")] #[post("/move", format = "json", data = "<move_req>")]
async fn handle_move(move_req: Json<GameState>) -> Option<Json<Action>> { async fn handle_move(move_req: Json<GameState>) -> Json<Action> {
let start = Instant::now(); let start = Instant::now();
let response = task::spawn_blocking(move || { let response = task::spawn_blocking(move || {
logic::get_move( logic::get_move(
@ -40,9 +40,13 @@ async fn handle_move(move_req: Json<GameState>) -> Option<Json<Action>> {
.await .await
.inspect_err(|e| error!("failed to join compute thread: {e}")) .inspect_err(|e| error!("failed to join compute thread: {e}"))
.ok() .ok()
.flatten()?; .flatten()
.unwrap_or(Action {
r#move: Direction::Up,
shout: Some("I am so dead".to_owned()),
});
Some(Json(response)) Json(response)
} }
#[post("/end", format = "json", data = "<end_req>")] #[post("/end", format = "json", data = "<end_req>")]