refactor: pass on both Bot and BotVersion to MatchPlayer

This commit is contained in:
Ilion Beyst 2022-07-09 20:01:05 +02:00
parent 7eb02a2efc
commit ec1d50f655
4 changed files with 45 additions and 24 deletions

View file

@ -115,7 +115,7 @@ impl pb::bot_api_service_server::BotApiService for BotApiServer {
}); });
let mut run_match = RunMatch::from_players(vec![ let mut run_match = RunMatch::from_players(vec![
MatchPlayer::from_bot_spec(remote_bot_spec), MatchPlayer::from_bot_spec(remote_bot_spec),
MatchPlayer::from_code_bundle(&opponent_code_bundle), MatchPlayer::from_bot_version(&opponent, &opponent_code_bundle),
]); ]);
let created_match = run_match let created_match = run_match
.store_in_database(&conn) .store_in_database(&conn)

View file

@ -24,15 +24,28 @@ pub struct RunMatch {
pub struct MatchPlayer { pub struct MatchPlayer {
bot_spec: Box<dyn BotSpec>, bot_spec: Box<dyn BotSpec>,
// meta that will be passed on to database // metadata that will be passed on to database
code_bundle_id: Option<i32>, code_bundle_id: Option<i32>,
} }
impl MatchPlayer { impl MatchPlayer {
pub fn from_code_bundle(code_bundle: &db::bots::BotVersion) -> Self { pub fn from_bot_version(bot: &db::bots::Bot, version: &db::bots::BotVersion) -> Self {
MatchPlayer { MatchPlayer {
bot_spec: code_bundle_to_botspec(code_bundle), bot_spec: bot_version_to_botspec(bot, version),
code_bundle_id: Some(code_bundle.id), code_bundle_id: Some(version.id),
}
}
/// Construct a MatchPlayer from a BotVersion that certainly contains a code bundle path.
/// Will panic when this is not the case.
pub fn from_code_bundle_version(version: &db::bots::BotVersion) -> Self {
let code_bundle_path = version
.code_bundle_path
.as_ref()
.expect("no code_bundle_path found");
MatchPlayer {
bot_spec: python_docker_bot_spec(code_bundle_path),
code_bundle_id: Some(version.id),
} }
} }
@ -97,12 +110,24 @@ impl RunMatch {
} }
} }
pub fn code_bundle_to_botspec(code_bundle: &db::bots::BotVersion) -> Box<dyn BotSpec> { pub fn bot_version_to_botspec(
// TODO: get rid of this unwrap _bot: &db::bots::Bot,
let bundle_path = PathBuf::from(BOTS_DIR).join(code_bundle.code_bundle_path.as_ref().unwrap()); bot_version: &db::bots::BotVersion,
) -> Box<dyn BotSpec> {
if let Some(code_bundle_path) = &bot_version.code_bundle_path {
python_docker_bot_spec(code_bundle_path)
} else if let Some(_container_digest) = &bot_version.container_digest {
unimplemented!()
} else {
panic!("bad bot version")
}
}
fn python_docker_bot_spec(code_bundle_path: &str) -> Box<dyn BotSpec> {
let code_bundle_abs_path = PathBuf::from(BOTS_DIR).join(code_bundle_path);
Box::new(DockerBotSpec { Box::new(DockerBotSpec {
code_path: bundle_path, code_path: code_bundle_abs_path,
image: PYTHON_IMAGE.to_string(), image: PYTHON_IMAGE.to_string(),
argv: vec!["python".to_string(), "bot.py".to_string()], argv: vec!["python".to_string(), "bot.py".to_string()],
}) })

View file

@ -37,18 +37,14 @@ pub async fn run_ranker(db_pool: DbPool) {
async fn play_ranking_match(selected_bots: Vec<Bot>, db_pool: DbPool) { async fn play_ranking_match(selected_bots: Vec<Bot>, db_pool: DbPool) {
let db_conn = db_pool.get().await.expect("could not get db pool"); let db_conn = db_pool.get().await.expect("could not get db pool");
let mut code_bundles = Vec::new(); let mut players = Vec::new();
for bot in &selected_bots { for bot in &selected_bots {
let code_bundle = db::bots::active_bot_version(bot.id, &db_conn) let version = db::bots::active_bot_version(bot.id, &db_conn)
.expect("could not get active code bundle"); .expect("could not get active bot version");
code_bundles.push(code_bundle); let player = MatchPlayer::from_bot_version(bot, &version);
players.push(player);
} }
let players = code_bundles
.iter()
.map(MatchPlayer::from_code_bundle)
.collect::<Vec<_>>();
let mut run_match = RunMatch::from_players(players); let mut run_match = RunMatch::from_players(players);
run_match run_match
.store_in_database(&db_conn) .store_in_database(&db_conn)

View file

@ -37,18 +37,18 @@ pub async fn submit_bot(
.opponent_name .opponent_name
.unwrap_or_else(|| DEFAULT_OPPONENT_NAME.to_string()); .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)?; db::bots::find_bot_by_name(&opponent_name, &conn).map_err(|_| StatusCode::BAD_REQUEST)?;
let opponent_bot_version = let opponent_bot_version = db::bots::active_bot_version(opponent_bot.id, &conn)
db::bots::active_bot_version(opponent.id, &conn).map_err(|_| StatusCode::BAD_REQUEST)?; .map_err(|_| StatusCode::BAD_REQUEST)?;
let player_bot_version = save_code_string(&params.code, None, &conn) let player_bot_version = save_code_string(&params.code, None, &conn)
// TODO: can we recover from this? // TODO: can we recover from this?
.expect("could not save bot code"); .expect("could not save bot code");
let mut run_match = RunMatch::from_players(vec![ let mut run_match = RunMatch::from_players(vec![
MatchPlayer::from_code_bundle(&player_bot_version), MatchPlayer::from_code_bundle_version(&player_bot_version),
MatchPlayer::from_code_bundle(&opponent_bot_version), MatchPlayer::from_bot_version(&opponent_bot, &opponent_bot_version),
]); ]);
let match_data = run_match let match_data = run_match
.store_in_database(&conn) .store_in_database(&conn)
@ -67,7 +67,7 @@ pub async fn submit_bot(
FullMatchPlayerData { FullMatchPlayerData {
base: match_data.match_players[1].clone(), base: match_data.match_players[1].clone(),
bot_version: Some(opponent_bot_version), bot_version: Some(opponent_bot_version),
bot: Some(opponent), bot: Some(opponent_bot),
}, },
], ],
}; };