From b70328227e8f01a26f3722dcfbd3c4b82f66629f Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Thu, 3 Mar 2022 21:12:16 +0100 Subject: [PATCH] allow selecting opponent in demo --- planetwars-server/src/routes/demo.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 8642577..37312a7 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -18,8 +18,9 @@ const OPPONENT_NAME: &'static str = "simplebot"; #[derive(Serialize, Deserialize, Debug)] pub struct SubmitBotParams { - pub bot_name: Option, pub code: String, + // TODO: would it be better to pass an ID here? + pub opponent_name: Option, } #[derive(Serialize, Deserialize)] @@ -46,10 +47,14 @@ pub async fn submit_bot( ) -> Result, StatusCode> { let conn = pool.get().await.expect("could not get database connection"); + let opponent_name = params + .opponent_name + .unwrap_or_else(|| OPPONENT_NAME.to_string()); + let opponent = - db::bots::find_bot_by_name(OPPONENT_NAME, &conn).expect("could not find opponent bot"); + db::bots::find_bot_by_name(&opponent_name, &conn).map_err(|_| StatusCode::BAD_REQUEST)?; let opponent_code_bundle = - db::bots::active_code_bundle(opponent.id, &conn).expect("opponent bot has no code bundles"); + db::bots::active_code_bundle(opponent.id, &conn).map_err(|_| StatusCode::BAD_REQUEST)?; let player_code_bundle = save_code_bundle(¶ms.code, None, &conn) // TODO: can we recover from this?