diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-02-15 19:54:29 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-02-15 19:54:29 +0100 |
commit | ebd01757e8676c081b318e67283dd56089a7db6b (patch) | |
tree | 727de39204bd5433371b77592e6078b3a6abf922 /planetwars-server/src/routes/demo.rs | |
parent | c7f4da07c1bb3b8a52a596e7c2573c7bf3ce7b79 (diff) | |
download | planetwars.dev-ebd01757e8676c081b318e67283dd56089a7db6b.tar.xz planetwars.dev-ebd01757e8676c081b318e67283dd56089a7db6b.zip |
play matches async
Diffstat (limited to 'planetwars-server/src/routes/demo.rs')
-rw-r--r-- | planetwars-server/src/routes/demo.rs | 23 |
1 files changed, 19 insertions, 4 deletions
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) |