diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-01-02 17:56:52 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-01-02 17:56:52 +0100 |
commit | 69331eb08a6199bfa8378e08cf378803b076eaae (patch) | |
tree | 3a84474e81c2f2d553369e96244e2a62f601509d /planetwars-server/src/routes | |
parent | 85dcf3ba2fd0cf907610625399db691b274118bb (diff) | |
download | planetwars.dev-69331eb08a6199bfa8378e08cf378803b076eaae.tar.xz planetwars.dev-69331eb08a6199bfa8378e08cf378803b076eaae.zip |
serve match logs
Diffstat (limited to 'planetwars-server/src/routes')
-rw-r--r-- | planetwars-server/src/routes/matches.rs | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index f2c5186..4dfd44e 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -1,6 +1,9 @@ -use std::path::PathBuf; +use std::{io::Read, path::PathBuf}; -use axum::{extract::Extension, Json}; +use axum::{ + extract::{Extension, Path}, + Json, +}; use hyper::StatusCode; use planetwars_matchrunner::{run_match, MatchConfig, MatchPlayer}; use rand::{distributions::Alphanumeric, Rng}; @@ -30,7 +33,7 @@ pub async fn play_match( .take(16) .map(char::from) .collect(); - let log_path = PathBuf::from(MATCHES_DIR).join(&format!("{}.log", slug)); + let log_file_name = format!("{}.log", slug); let mut players = Vec::new(); let mut bot_ids = Vec::new(); @@ -58,22 +61,27 @@ pub async fn play_match( let match_config = MatchConfig { map_name: "hex".to_string(), map_path, - log_path: log_path.clone(), + log_path: PathBuf::from(MATCHES_DIR).join(&log_file_name), players: players, }; - tokio::spawn(run_match_task(match_config, bot_ids, pool.clone())); + tokio::spawn(run_match_task( + match_config, + log_file_name, + bot_ids, + pool.clone(), + )); Ok(()) } async fn run_match_task( config: MatchConfig, + log_file_name: String, match_players: Vec<matches::MatchPlayerData>, pool: ConnectionPool, ) { - let log_path = config.log_path.as_os_str().to_str().unwrap().to_string(); let match_data = matches::NewMatch { - log_path: &log_path, + log_path: &log_file_name, }; run_match(config).await; @@ -119,3 +127,13 @@ pub struct BotConfig { pub run_command: String, pub build_command: Option<String>, } + +pub async fn get_match_log( + Path(match_id): Path<i32>, + conn: DatabaseConnection, +) -> Result<Vec<u8>, StatusCode> { + let match_base = matches::find_mach_base(match_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?; + let log_path = PathBuf::from(MATCHES_DIR).join(&match_base.log_path); + let log_contents = std::fs::read(log_path).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + Ok(log_contents) +} |