From ebd01757e8676c081b318e67283dd56089a7db6b Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 15 Feb 2022 19:54:29 +0100 Subject: play matches async --- planetwars-server/src/routes/demo.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'planetwars-server/src/routes/demo.rs') diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 11d8d72..dbcdb64 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -41,7 +41,7 @@ pub async fn submit_bot( std::fs::write(uploaded_bot_dir.join("bot.py"), params.code.as_bytes()).unwrap(); // play the match - run_match(MatchConfig { + let match_config = MatchConfig { map_path: PathBuf::from(MAPS_DIR).join("hex.json"), map_name: "hex".to_string(), log_path: PathBuf::from(MATCHES_DIR).join(&log_file_name), @@ -63,24 +63,39 @@ pub async fn submit_bot( }), }, ], - }) - .await; + }; // store match in database let new_match_data = matches::NewMatch { - state: MatchState::Finished, + state: MatchState::Playing, log_path: &log_file_name, }; // TODO: set match players let match_data = matches::create_match(&new_match_data, &[], &conn).expect("failed to create match"); + tokio::spawn(run_match_task( + match_data.base.id, + match_config, + pool.clone(), + )); + let api_match = super::matches::match_data_to_api(match_data); Ok(Json(SubmitBotResponse { match_data: api_match, })) } +async fn run_match_task(match_id: i32, match_config: MatchConfig, connection_pool: ConnectionPool) { + run_match(match_config).await; + let conn = connection_pool + .get() + .await + .expect("could not get database connection"); + matches::set_match_state(match_id, MatchState::Finished, &conn) + .expect("failed to update match state"); +} + pub fn gen_alphanumeric(length: usize) -> String { rand::thread_rng() .sample_iter(&Alphanumeric) -- cgit v1.2.3