From 5ee66c9c9b4156692c739a861c9cdbaf0c65aec8 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Fri, 10 Jun 2022 21:09:33 +0200 Subject: allow match_player code_bundle_id to be null --- planetwars-server/src/routes/bots.rs | 2 +- planetwars-server/src/routes/demo.rs | 4 ++-- planetwars-server/src/routes/matches.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index 3bbaa1a..df0c4d0 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -12,7 +12,7 @@ use std::path::PathBuf; use thiserror; use crate::db::bots::{self, CodeBundle}; -use crate::db::ratings::{RankedBot, self}; +use crate::db::ratings::{self, RankedBot}; use crate::db::users::User; use crate::modules::bots::save_code_bundle; use crate::{DatabaseConnection, BOTS_DIR}; diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 7f7ba71..3318dfd 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -58,12 +58,12 @@ pub async fn submit_bot( match_players: vec![ FullMatchPlayerData { base: match_data.match_players[0].clone(), - code_bundle: player_code_bundle, + code_bundle: Some(player_code_bundle), bot: None, }, FullMatchPlayerData { base: match_data.match_players[1].clone(), - code_bundle: opponent_code_bundle, + code_bundle: Some(opponent_code_bundle), bot: Some(opponent), }, ], diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index b61008d..7169ebe 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -107,7 +107,7 @@ pub struct ApiMatch { #[derive(Serialize, Deserialize)] pub struct ApiMatchPlayer { - code_bundle_id: i32, + code_bundle_id: Option, bot_id: Option, bot_name: Option, } @@ -127,7 +127,7 @@ pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch { .match_players .iter() .map(|_p| ApiMatchPlayer { - code_bundle_id: _p.code_bundle.id, + code_bundle_id: _p.code_bundle.as_ref().map(|cb| cb.id), bot_id: _p.bot.as_ref().map(|b| b.id), bot_name: _p.bot.as_ref().map(|b| b.name.clone()), }) -- cgit v1.2.3 From a3766980735851e9aa4b56a80e91c0b77cf63adb Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Fri, 10 Jun 2022 21:49:32 +0200 Subject: update RunMatch helper to allow for remote bots --- planetwars-server/src/routes/demo.rs | 7 +++++-- planetwars-server/src/routes/matches.rs | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 3318dfd..33dc02d 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -1,7 +1,7 @@ use crate::db; use crate::db::matches::{FullMatchData, FullMatchPlayerData}; use crate::modules::bots::save_code_bundle; -use crate::modules::matches::RunMatch; +use crate::modules::matches::{MatchPlayer, RunMatch}; use crate::ConnectionPool; use axum::extract::Extension; use axum::Json; @@ -46,7 +46,10 @@ pub async fn submit_bot( // TODO: can we recover from this? .expect("could not save bot code"); - let mut run_match = RunMatch::from_players(vec![&player_code_bundle, &opponent_code_bundle]); + let mut run_match = RunMatch::from_players(vec![ + MatchPlayer::from_code_bundle(&player_code_bundle), + MatchPlayer::from_code_bundle(&opponent_code_bundle), + ]); let match_data = run_match .store_in_database(&conn) .expect("failed to save match"); diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index 7169ebe..874c775 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -61,7 +61,7 @@ pub async fn play_match( }); bot_ids.push(matches::MatchPlayerData { - code_bundle_id: code_bundle.id, + code_bundle_id: Some(code_bundle.id), }); } -- cgit v1.2.3 From 951cb293111db9ea0947cd65872da744bce92d31 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Mon, 20 Jun 2022 22:01:26 +0200 Subject: upgrade to axum 0.5 --- planetwars-server/src/routes/bots.rs | 2 +- planetwars-server/src/routes/users.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index 3bbaa1a..df0c4d0 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -12,7 +12,7 @@ use std::path::PathBuf; use thiserror; use crate::db::bots::{self, CodeBundle}; -use crate::db::ratings::{RankedBot, self}; +use crate::db::ratings::{self, RankedBot}; use crate::db::users::User; use crate::modules::bots::save_code_bundle; use crate::{DatabaseConnection, BOTS_DIR}; diff --git a/planetwars-server/src/routes/users.rs b/planetwars-server/src/routes/users.rs index 54ddd09..1989904 100644 --- a/planetwars-server/src/routes/users.rs +++ b/planetwars-server/src/routes/users.rs @@ -5,7 +5,7 @@ use axum::extract::{FromRequest, RequestParts, TypedHeader}; use axum::headers::authorization::Bearer; use axum::headers::Authorization; use axum::http::StatusCode; -use axum::response::{Headers, IntoResponse, Response}; +use axum::response::{IntoResponse, Response}; use axum::{async_trait, Json}; use serde::{Deserialize, Serialize}; use serde_json::json; @@ -163,9 +163,9 @@ pub async fn login(conn: DatabaseConnection, params: Json) -> Respo Some(user) => { let session = sessions::create_session(&user, &conn); let user_data: UserData = user.into(); - let headers = Headers(vec![("Token", &session.token)]); + let headers = [("Token", &session.token)]; - (headers, Json(user_data)).into_response() + (StatusCode::OK, headers, Json(user_data)).into_response() } } } -- cgit v1.2.3 From ea05674b4473d9399f5aa6dca982ae73aad0ebcf Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Mon, 4 Jul 2022 22:33:35 +0200 Subject: remove obsolete create match route --- planetwars-server/src/routes/matches.rs | 103 +------------------------------- 1 file changed, 3 insertions(+), 100 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index 874c775..5f95ce9 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -1,102 +1,13 @@ use std::path::PathBuf; - -use axum::{ - extract::{Extension, Path}, - Json, -}; +use axum::{extract::Path, Json}; use hyper::StatusCode; -use planetwars_matchrunner::{docker_runner::DockerBotSpec, run_match, MatchConfig, MatchPlayer}; -use rand::{distributions::Alphanumeric, Rng}; use serde::{Deserialize, Serialize}; use crate::{ - db::{ - bots, - matches::{self, MatchState}, - users::User, - }, - ConnectionPool, DatabaseConnection, BOTS_DIR, MAPS_DIR, MATCHES_DIR, + db::matches::{self, MatchState}, + DatabaseConnection, MATCHES_DIR, }; -#[derive(Serialize, Deserialize, Debug)] -pub struct MatchParams { - // Just bot ids for now - players: Vec, -} - -pub async fn play_match( - _user: User, - Extension(pool): Extension, - Json(params): Json, -) -> Result<(), StatusCode> { - let conn = pool.get().await.expect("could not get database connection"); - let map_path = PathBuf::from(MAPS_DIR).join("hex.json"); - - let slug: String = rand::thread_rng() - .sample_iter(&Alphanumeric) - .take(16) - .map(char::from) - .collect(); - let log_file_name = format!("{}.log", slug); - - let mut players = Vec::new(); - let mut bot_ids = Vec::new(); - for bot_name in params.players { - let bot = bots::find_bot(bot_name, &conn).map_err(|_| StatusCode::BAD_REQUEST)?; - let code_bundle = - bots::active_code_bundle(bot.id, &conn).map_err(|_| StatusCode::BAD_REQUEST)?; - - let bundle_path = PathBuf::from(BOTS_DIR).join(&code_bundle.path); - let bot_config: BotConfig = std::fs::read_to_string(bundle_path.join("botconfig.toml")) - .and_then(|config_str| toml::from_str(&config_str).map_err(|e| e.into())) - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; - - players.push(MatchPlayer { - bot_spec: Box::new(DockerBotSpec { - code_path: PathBuf::from(BOTS_DIR).join(code_bundle.path), - image: "python:3.10-slim-buster".to_string(), - argv: shlex::split(&bot_config.run_command) - .ok_or(StatusCode::INTERNAL_SERVER_ERROR)?, - }), - }); - - bot_ids.push(matches::MatchPlayerData { - code_bundle_id: Some(code_bundle.id), - }); - } - - let match_config = MatchConfig { - map_name: "hex".to_string(), - map_path, - log_path: PathBuf::from(MATCHES_DIR).join(&log_file_name), - players, - }; - - 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, - pool: ConnectionPool, -) { - let match_data = matches::NewMatch { - state: MatchState::Finished, - log_path: &log_file_name, - }; - - run_match(config).await; - let conn = pool.get().await.expect("could not get database connection"); - matches::create_match(&match_data, &match_players, &conn).expect("could not create match"); -} - #[derive(Serialize, Deserialize)] pub struct ApiMatch { id: i32, @@ -135,14 +46,6 @@ pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch { } } -// TODO: this is duplicated from planetwars-cli -// clean this up and move to matchrunner crate -#[derive(Serialize, Deserialize)] -pub struct BotConfig { - pub name: String, - pub run_command: String, - pub build_command: Option, -} pub async fn get_match_data( Path(match_id): Path, -- cgit v1.2.3 From b3df5c6f8cc59e099a2f1db3df8089af4abca02e Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 5 Jul 2022 20:34:20 +0200 Subject: migrate code_bundles to bot_versions --- planetwars-server/src/routes/bots.rs | 2 +- planetwars-server/src/routes/matches.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index df0c4d0..6d5d7df 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -215,7 +215,7 @@ pub async fn upload_code_multipart( let bundle = bots::NewCodeBundle { bot_id: Some(bot.id), - path: &folder_name, + code_bundle_path: &folder_name, }; let code_bundle = bots::create_code_bundle(&bundle, &conn).expect("Failed to create code bundle"); diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index 5f95ce9..0c1bee4 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -1,7 +1,7 @@ -use std::path::PathBuf; use axum::{extract::Path, Json}; use hyper::StatusCode; use serde::{Deserialize, Serialize}; +use std::path::PathBuf; use crate::{ db::matches::{self, MatchState}, @@ -46,7 +46,6 @@ pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch { } } - pub async fn get_match_data( Path(match_id): Path, conn: DatabaseConnection, -- cgit v1.2.3 From d7b7585dd70f9d41184cf88c2ecbd88341898c38 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 6 Jul 2022 22:41:27 +0200 Subject: rename code_bundle to bot_version --- planetwars-server/src/routes/bots.rs | 8 ++++---- planetwars-server/src/routes/demo.rs | 14 +++++++------- planetwars-server/src/routes/matches.rs | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index 6d5d7df..1ffedef 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -11,7 +11,7 @@ use std::io::Cursor; use std::path::PathBuf; use thiserror; -use crate::db::bots::{self, CodeBundle}; +use crate::db::bots::{self, BotVersion}; use crate::db::ratings::{self, RankedBot}; use crate::db::users::User; use crate::modules::bots::save_code_bundle; @@ -148,8 +148,8 @@ pub async fn get_bot( Path(bot_id): Path, ) -> Result, StatusCode> { let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?; - let bundles = bots::find_bot_code_bundles(bot.id, &conn) - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + let bundles = + bots::find_bot_versions(bot.id, &conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; Ok(Json(json!({ "bot": bot, "bundles": bundles, @@ -183,7 +183,7 @@ pub async fn upload_code_multipart( user: User, Path(bot_id): Path, mut multipart: Multipart, -) -> Result, StatusCode> { +) -> Result, StatusCode> { let bots_dir = PathBuf::from(BOTS_DIR); let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?; diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 33dc02d..1747bfe 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -39,16 +39,16 @@ pub async fn submit_bot( let opponent = 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).map_err(|_| StatusCode::BAD_REQUEST)?; + let opponent_bot_version = + db::bots::active_bot_version(opponent.id, &conn).map_err(|_| StatusCode::BAD_REQUEST)?; - let player_code_bundle = save_code_bundle(¶ms.code, None, &conn) + let player_bot_version = save_code_bundle(¶ms.code, None, &conn) // TODO: can we recover from this? .expect("could not save bot code"); let mut run_match = RunMatch::from_players(vec![ - MatchPlayer::from_code_bundle(&player_code_bundle), - MatchPlayer::from_code_bundle(&opponent_code_bundle), + MatchPlayer::from_code_bundle(&player_bot_version), + MatchPlayer::from_code_bundle(&opponent_bot_version), ]); let match_data = run_match .store_in_database(&conn) @@ -61,12 +61,12 @@ pub async fn submit_bot( match_players: vec![ FullMatchPlayerData { base: match_data.match_players[0].clone(), - code_bundle: Some(player_code_bundle), + bot_version: Some(player_bot_version), bot: None, }, FullMatchPlayerData { base: match_data.match_players[1].clone(), - code_bundle: Some(opponent_code_bundle), + bot_version: Some(opponent_bot_version), bot: Some(opponent), }, ], diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index 0c1bee4..f33a5f1 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -18,7 +18,7 @@ pub struct ApiMatch { #[derive(Serialize, Deserialize)] pub struct ApiMatchPlayer { - code_bundle_id: Option, + bot_version_id: Option, bot_id: Option, bot_name: Option, } @@ -38,7 +38,7 @@ pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch { .match_players .iter() .map(|_p| ApiMatchPlayer { - code_bundle_id: _p.code_bundle.as_ref().map(|cb| cb.id), + bot_version_id: _p.bot_version.as_ref().map(|cb| cb.id), bot_id: _p.bot.as_ref().map(|b| b.id), bot_name: _p.bot.as_ref().map(|b| b.name.clone()), }) -- cgit v1.2.3 From 6ec792e3bd633a0b3971e401d29b2f8671f38b14 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Thu, 7 Jul 2022 18:57:46 +0200 Subject: NewBotVersion --- planetwars-server/src/routes/bots.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index 1ffedef..54c0d36 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -213,12 +213,13 @@ pub async fn upload_code_multipart( .extract(bots_dir.join(&folder_name)) .map_err(|_| StatusCode::BAD_REQUEST)?; - let bundle = bots::NewCodeBundle { + let bot_version = bots::NewBotVersion { bot_id: Some(bot.id), - code_bundle_path: &folder_name, + code_bundle_path: Some(&folder_name), + container_digest: None, }; let code_bundle = - bots::create_code_bundle(&bundle, &conn).expect("Failed to create code bundle"); + bots::create_bot_version(&bot_version, &conn).expect("Failed to create code bundle"); Ok(Json(code_bundle)) } -- cgit v1.2.3 From 0f14dee499f48b11fc329164c30cd475400a9f4d Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Thu, 7 Jul 2022 19:13:55 +0200 Subject: refactor: rename save_code_bundle to save_code_string --- planetwars-server/src/routes/bots.rs | 4 ++-- planetwars-server/src/routes/demo.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index 54c0d36..edb68ae 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -14,7 +14,7 @@ use thiserror; use crate::db::bots::{self, BotVersion}; use crate::db::ratings::{self, RankedBot}; use crate::db::users::User; -use crate::modules::bots::save_code_bundle; +use crate::modules::bots::save_code_string; use crate::{DatabaseConnection, BOTS_DIR}; use bots::Bot; @@ -120,7 +120,7 @@ pub async fn save_bot( } }; let _code_bundle = - save_code_bundle(¶ms.code, Some(bot.id), &conn).expect("failed to save code bundle"); + save_code_string(¶ms.code, Some(bot.id), &conn).expect("failed to save code bundle"); Ok(Json(bot)) } diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 1747bfe..4f83de0 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -1,6 +1,6 @@ use crate::db; use crate::db::matches::{FullMatchData, FullMatchPlayerData}; -use crate::modules::bots::save_code_bundle; +use crate::modules::bots::save_code_string; use crate::modules::matches::{MatchPlayer, RunMatch}; use crate::ConnectionPool; use axum::extract::Extension; @@ -42,7 +42,7 @@ pub async fn submit_bot( let opponent_bot_version = db::bots::active_bot_version(opponent.id, &conn).map_err(|_| StatusCode::BAD_REQUEST)?; - let player_bot_version = save_code_bundle(¶ms.code, None, &conn) + let player_bot_version = save_code_string(¶ms.code, None, &conn) // TODO: can we recover from this? .expect("could not save bot code"); -- cgit v1.2.3 From ec1d50f655c05d9dec0c4b01fd1039e9c5525f34 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sat, 9 Jul 2022 20:01:05 +0200 Subject: refactor: pass on both Bot and BotVersion to MatchPlayer --- planetwars-server/src/routes/demo.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 4f83de0..1a6ae9a 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -37,18 +37,18 @@ pub async fn submit_bot( .opponent_name .unwrap_or_else(|| DEFAULT_OPPONENT_NAME.to_string()); - let opponent = + let opponent_bot = db::bots::find_bot_by_name(&opponent_name, &conn).map_err(|_| StatusCode::BAD_REQUEST)?; - let opponent_bot_version = - db::bots::active_bot_version(opponent.id, &conn).map_err(|_| StatusCode::BAD_REQUEST)?; + let opponent_bot_version = db::bots::active_bot_version(opponent_bot.id, &conn) + .map_err(|_| StatusCode::BAD_REQUEST)?; let player_bot_version = save_code_string(¶ms.code, None, &conn) // TODO: can we recover from this? .expect("could not save bot code"); let mut run_match = RunMatch::from_players(vec![ - MatchPlayer::from_code_bundle(&player_bot_version), - MatchPlayer::from_code_bundle(&opponent_bot_version), + MatchPlayer::from_code_bundle_version(&player_bot_version), + MatchPlayer::from_bot_version(&opponent_bot, &opponent_bot_version), ]); let match_data = run_match .store_in_database(&conn) @@ -67,7 +67,7 @@ pub async fn submit_bot( FullMatchPlayerData { base: match_data.match_players[1].clone(), bot_version: Some(opponent_bot_version), - bot: Some(opponent), + bot: Some(opponent_bot), }, ], }; -- cgit v1.2.3 From e69bd14f1d64b0d8b2438a40a069d3647c1edd73 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 12 Jul 2022 20:54:00 +0200 Subject: refactor: delay BotSpec construction in RunMatch --- planetwars-server/src/routes/demo.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 1a6ae9a..f9929f7 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -47,8 +47,14 @@ pub async fn submit_bot( .expect("could not save bot code"); let mut run_match = RunMatch::from_players(vec![ - MatchPlayer::from_code_bundle_version(&player_bot_version), - MatchPlayer::from_bot_version(&opponent_bot, &opponent_bot_version), + MatchPlayer::BotVersion { + bot: None, + version: player_bot_version.clone(), + }, + MatchPlayer::BotVersion { + bot: Some(opponent_bot.clone()), + version: opponent_bot_version.clone(), + }, ]); let match_data = run_match .store_in_database(&conn) -- cgit v1.2.3 From 668409e76d8cc7797fe627b2e2c3d0223b3db684 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 13 Jul 2022 19:36:07 +0200 Subject: refactor: unify match save and spawn --- planetwars-server/src/routes/demo.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index f9929f7..5ff02c7 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -46,7 +46,7 @@ pub async fn submit_bot( // TODO: can we recover from this? .expect("could not save bot code"); - let mut run_match = RunMatch::from_players(vec![ + let run_match = RunMatch::from_players(vec![ MatchPlayer::BotVersion { bot: None, version: player_bot_version.clone(), @@ -56,10 +56,10 @@ pub async fn submit_bot( version: opponent_bot_version.clone(), }, ]); - let match_data = run_match - .store_in_database(&conn) - .expect("failed to save match"); - run_match.spawn(pool.clone()); + let (match_data, _) = run_match + .run(pool.clone()) + .await + .expect("failed to run match"); // TODO: avoid clones let full_match_data = FullMatchData { -- cgit v1.2.3 From 00459f9e3d818f0fb84160862f02898d64f98110 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Thu, 14 Jul 2022 20:53:08 +0200 Subject: create a configuration to hold docker registry url --- planetwars-server/src/routes/demo.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 5ff02c7..6f2d5e6 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -1,7 +1,9 @@ +use std::sync::Arc; + use crate::db; use crate::db::matches::{FullMatchData, FullMatchPlayerData}; use crate::modules::bots::save_code_string; -use crate::modules::matches::{MatchPlayer, RunMatch}; +use crate::modules::matches::{MatchPlayer, MatchRunnerConfig, RunMatch}; use crate::ConnectionPool; use axum::extract::Extension; use axum::Json; @@ -30,6 +32,7 @@ pub struct SubmitBotResponse { pub async fn submit_bot( Json(params): Json, Extension(pool): Extension, + Extension(runner_config): Extension>, ) -> Result, StatusCode> { let conn = pool.get().await.expect("could not get database connection"); @@ -46,16 +49,19 @@ pub async fn submit_bot( // TODO: can we recover from this? .expect("could not save bot code"); - let run_match = RunMatch::from_players(vec![ - MatchPlayer::BotVersion { - bot: None, - version: player_bot_version.clone(), - }, - MatchPlayer::BotVersion { - bot: Some(opponent_bot.clone()), - version: opponent_bot_version.clone(), - }, - ]); + let run_match = RunMatch::from_players( + runner_config, + vec![ + MatchPlayer::BotVersion { + bot: None, + version: player_bot_version.clone(), + }, + MatchPlayer::BotVersion { + bot: Some(opponent_bot.clone()), + version: opponent_bot_version.clone(), + }, + ], + ); let (match_data, _) = run_match .run(pool.clone()) .await -- cgit v1.2.3 From ec5c91d37b46cb3cec4878176469c66d2304dadd Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Thu, 14 Jul 2022 21:50:42 +0200 Subject: change runnerconfig to globalconfig --- planetwars-server/src/routes/demo.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 6f2d5e6..77f9e8d 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -3,8 +3,9 @@ use std::sync::Arc; use crate::db; use crate::db::matches::{FullMatchData, FullMatchPlayerData}; use crate::modules::bots::save_code_string; -use crate::modules::matches::{MatchPlayer, MatchRunnerConfig, RunMatch}; +use crate::modules::matches::{MatchPlayer, RunMatch}; use crate::ConnectionPool; +use crate::GlobalConfig; use axum::extract::Extension; use axum::Json; use hyper::StatusCode; @@ -32,7 +33,7 @@ pub struct SubmitBotResponse { pub async fn submit_bot( Json(params): Json, Extension(pool): Extension, - Extension(runner_config): Extension>, + Extension(config): Extension>, ) -> Result, StatusCode> { let conn = pool.get().await.expect("could not get database connection"); @@ -50,7 +51,7 @@ pub async fn submit_bot( .expect("could not save bot code"); let run_match = RunMatch::from_players( - runner_config, + config, vec![ MatchPlayer::BotVersion { bot: None, -- cgit v1.2.3 From d13d131130ab53fb8ee7d49d2b40718622a4ab11 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sat, 16 Jul 2022 21:22:03 +0200 Subject: move storage paths to GlobalConfig --- planetwars-server/src/routes/bots.rs | 13 ++++++++----- planetwars-server/src/routes/demo.rs | 2 +- planetwars-server/src/routes/matches.rs | 9 +++++---- 3 files changed, 14 insertions(+), 10 deletions(-) (limited to 'planetwars-server/src/routes') diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index edb68ae..9ddb109 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -1,7 +1,7 @@ use axum::extract::{Multipart, Path}; use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; -use axum::{body, Json}; +use axum::{body, Extension, Json}; use diesel::OptionalExtension; use rand::distributions::Alphanumeric; use rand::Rng; @@ -9,13 +9,14 @@ use serde::{Deserialize, Serialize}; use serde_json::{self, json, value::Value as JsonValue}; use std::io::Cursor; use std::path::PathBuf; +use std::sync::Arc; use thiserror; use crate::db::bots::{self, BotVersion}; use crate::db::ratings::{self, RankedBot}; use crate::db::users::User; use crate::modules::bots::save_code_string; -use crate::{DatabaseConnection, BOTS_DIR}; +use crate::{DatabaseConnection, GlobalConfig}; use bots::Bot; #[derive(Serialize, Deserialize, Debug)] @@ -96,6 +97,7 @@ pub async fn save_bot( Json(params): Json, user: User, conn: DatabaseConnection, + Extension(config): Extension>, ) -> Result, SaveBotError> { let res = bots::find_bot_by_name(¶ms.bot_name, &conn) .optional() @@ -119,8 +121,8 @@ pub async fn save_bot( bots::create_bot(&new_bot, &conn).expect("could not create bot") } }; - let _code_bundle = - save_code_string(¶ms.code, Some(bot.id), &conn).expect("failed to save code bundle"); + let _code_bundle = save_code_string(¶ms.code, Some(bot.id), &conn, &config) + .expect("failed to save code bundle"); Ok(Json(bot)) } @@ -183,8 +185,9 @@ pub async fn upload_code_multipart( user: User, Path(bot_id): Path, mut multipart: Multipart, + Extension(config): Extension>, ) -> Result, StatusCode> { - let bots_dir = PathBuf::from(BOTS_DIR); + let bots_dir = PathBuf::from(&config.bots_directory); let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?; diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 77f9e8d..69838f3 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -46,7 +46,7 @@ pub async fn submit_bot( let opponent_bot_version = db::bots::active_bot_version(opponent_bot.id, &conn) .map_err(|_| StatusCode::BAD_REQUEST)?; - let player_bot_version = save_code_string(¶ms.code, None, &conn) + let player_bot_version = save_code_string(¶ms.code, None, &conn, &config) // TODO: can we recover from this? .expect("could not save bot code"); diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index f33a5f1..a980daa 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -1,11 +1,11 @@ -use axum::{extract::Path, Json}; +use axum::{extract::Path, Extension, Json}; use hyper::StatusCode; use serde::{Deserialize, Serialize}; -use std::path::PathBuf; +use std::{path::PathBuf, sync::Arc}; use crate::{ db::matches::{self, MatchState}, - DatabaseConnection, MATCHES_DIR, + DatabaseConnection, GlobalConfig, }; #[derive(Serialize, Deserialize)] @@ -59,10 +59,11 @@ pub async fn get_match_data( pub async fn get_match_log( Path(match_id): Path, conn: DatabaseConnection, + Extension(config): Extension>, ) -> Result, StatusCode> { let match_base = matches::find_match_base(match_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?; - let log_path = PathBuf::from(MATCHES_DIR).join(&match_base.log_path); + let log_path = PathBuf::from(&config.match_logs_directory).join(&match_base.log_path); let log_contents = std::fs::read(log_path).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; Ok(log_contents) } -- cgit v1.2.3