diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-07-23 23:40:25 +0200 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-07-23 23:40:25 +0200 |
commit | 4a582e8079178a7ac11f2a492e7988fcdaa210cd (patch) | |
tree | 1403b3e409e184bb53e0349d7e68e673fe7d7b3c /planetwars-server/src/modules | |
parent | f19a70e710b8bf4605625516aa7e4c0cc7ace2e4 (diff) | |
download | planetwars.dev-4a582e8079178a7ac11f2a492e7988fcdaa210cd.tar.xz planetwars.dev-4a582e8079178a7ac11f2a492e7988fcdaa210cd.zip |
store active version id in bots table
Diffstat (limited to 'planetwars-server/src/modules')
-rw-r--r-- | planetwars-server/src/modules/bot_api.rs | 7 | ||||
-rw-r--r-- | planetwars-server/src/modules/bots.rs | 9 | ||||
-rw-r--r-- | planetwars-server/src/modules/ranking.rs | 20 | ||||
-rw-r--r-- | planetwars-server/src/modules/registry.rs | 5 |
4 files changed, 26 insertions, 15 deletions
diff --git a/planetwars-server/src/modules/bot_api.rs b/planetwars-server/src/modules/bot_api.rs index 3bc002c..3efb1c2 100644 --- a/planetwars-server/src/modules/bot_api.rs +++ b/planetwars-server/src/modules/bot_api.rs @@ -104,10 +104,9 @@ impl pb::bot_api_service_server::BotApiService for BotApiServer { let match_request = req.get_ref(); - let opponent_bot = db::bots::find_bot_by_name(&match_request.opponent_name, &conn) - .map_err(|_| Status::not_found("opponent not found"))?; - let opponent_bot_version = db::bots::active_bot_version(opponent_bot.id, &conn) - .map_err(|_| Status::not_found("no opponent version found"))?; + let (opponent_bot, opponent_bot_version) = + db::bots::find_bot_with_version_by_name(&match_request.opponent_name, &conn) + .map_err(|_| Status::not_found("opponent not found"))?; let player_key = gen_alphanumeric(32); diff --git a/planetwars-server/src/modules/bots.rs b/planetwars-server/src/modules/bots.rs index 5513539..4aba168 100644 --- a/planetwars-server/src/modules/bots.rs +++ b/planetwars-server/src/modules/bots.rs @@ -5,6 +5,7 @@ use diesel::{PgConnection, QueryResult}; use crate::{db, util::gen_alphanumeric, GlobalConfig}; /// Save a string containing bot code as a code bundle. +/// If a bot was provided, set the saved bundle as its active version. pub fn save_code_string( bot_code: &str, bot_id: Option<i32>, @@ -22,5 +23,11 @@ pub fn save_code_string( code_bundle_path: Some(&bundle_name), container_digest: None, }; - db::bots::create_bot_version(&new_code_bundle, conn) + let version = db::bots::create_bot_version(&new_code_bundle, conn)?; + // Leave this coupled for now - this is how the behaviour was bevore. + // It would be cleaner to separate version setting and bot selection, though. + if let Some(bot_id) = bot_id { + db::bots::set_active_version(bot_id, Some(version.id), conn)?; + } + Ok(version) } diff --git a/planetwars-server/src/modules/ranking.rs b/planetwars-server/src/modules/ranking.rs index a9f6419..d508d6c 100644 --- a/planetwars-server/src/modules/ranking.rs +++ b/planetwars-server/src/modules/ranking.rs @@ -1,3 +1,4 @@ +use crate::db::bots::BotVersion; use crate::{db::bots::Bot, DbPool, GlobalConfig}; use crate::db; @@ -22,12 +23,12 @@ pub async fn run_ranker(config: Arc<GlobalConfig>, db_pool: DbPool) { .expect("could not get database connection"); loop { interval.tick().await; - let bots = db::bots::find_all_bots(&db_conn).unwrap(); + let bots = db::bots::all_active_bots_with_version(&db_conn).unwrap(); if bots.len() < 2 { // not enough bots to play a match continue; } - let selected_bots: Vec<Bot> = { + let selected_bots: Vec<(Bot, BotVersion)> = { let mut rng = &mut rand::thread_rng(); bots.choose_multiple(&mut rng, 2).cloned().collect() }; @@ -36,15 +37,16 @@ pub async fn run_ranker(config: Arc<GlobalConfig>, db_pool: DbPool) { } } -async fn play_ranking_match(config: Arc<GlobalConfig>, selected_bots: Vec<Bot>, db_pool: DbPool) { - let db_conn = db_pool.get().await.expect("could not get db pool"); +async fn play_ranking_match( + config: Arc<GlobalConfig>, + selected_bots: Vec<(Bot, BotVersion)>, + db_pool: DbPool, +) { let mut players = Vec::new(); - for bot in &selected_bots { - let version = db::bots::active_bot_version(bot.id, &db_conn) - .expect("could not get active bot version"); + for (bot, bot_version) in selected_bots { let player = MatchPlayer::BotVersion { - bot: Some(bot.clone()), - version, + bot: Some(bot), + version: bot_version, }; players.push(player); } diff --git a/planetwars-server/src/modules/registry.rs b/planetwars-server/src/modules/registry.rs index 3f6dad2..297404a 100644 --- a/planetwars-server/src/modules/registry.rs +++ b/planetwars-server/src/modules/registry.rs @@ -397,7 +397,10 @@ async fn put_manifest( code_bundle_path: None, container_digest: Some(&content_digest), }; - db::bots::create_bot_version(&new_version, &db_conn).expect("could not save bot version"); + let version = + db::bots::create_bot_version(&new_version, &db_conn).expect("could not save bot version"); + db::bots::set_active_version(bot.id, Some(version.id), &db_conn) + .expect("could not update bot version"); Ok(Response::builder() .status(StatusCode::CREATED) |