diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-03-03 21:12:16 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-03-03 21:12:18 +0100 |
commit | b70328227e8f01a26f3722dcfbd3c4b82f66629f (patch) | |
tree | 130503ea0820418fda2332061bffb700d0507c59 /planetwars-server/src | |
parent | 0f7c3c3102aff07a85fd4dfc45a1e3f8489851e7 (diff) | |
download | planetwars.dev-b70328227e8f01a26f3722dcfbd3c4b82f66629f.tar.xz planetwars.dev-b70328227e8f01a26f3722dcfbd3c4b82f66629f.zip |
allow selecting opponent in demo
Diffstat (limited to 'planetwars-server/src')
-rw-r--r-- | planetwars-server/src/routes/demo.rs | 11 |
1 files 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<String>, pub code: String, + // TODO: would it be better to pass an ID here? + pub opponent_name: Option<String>, } #[derive(Serialize, Deserialize)] @@ -46,10 +47,14 @@ pub async fn submit_bot( ) -> Result<Json<SubmitBotResponse>, 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? |