refactor: delay BotSpec construction in RunMatch
This commit is contained in:
parent
0b9a9f0eaa
commit
e69bd14f1d
5 changed files with 45 additions and 47 deletions
|
@ -51,7 +51,7 @@ pub struct NewBotVersion<'a> {
|
||||||
pub container_digest: Option<&'a str>,
|
pub container_digest: Option<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Serialize, Deserialize, Debug)]
|
#[derive(Queryable, Serialize, Deserialize, Clone, Debug)]
|
||||||
pub struct BotVersion {
|
pub struct BotVersion {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub bot_id: Option<i32>,
|
pub bot_id: Option<i32>,
|
||||||
|
|
|
@ -102,10 +102,10 @@ impl pb::bot_api_service_server::BotApiService for BotApiServer {
|
||||||
|
|
||||||
let match_request = req.get_ref();
|
let match_request = req.get_ref();
|
||||||
|
|
||||||
let opponent = db::bots::find_bot_by_name(&match_request.opponent_name, &conn)
|
let opponent_bot = db::bots::find_bot_by_name(&match_request.opponent_name, &conn)
|
||||||
.map_err(|_| Status::not_found("opponent not found"))?;
|
.map_err(|_| Status::not_found("opponent not found"))?;
|
||||||
let opponent_code_bundle = db::bots::active_bot_version(opponent.id, &conn)
|
let opponent_bot_version = db::bots::active_bot_version(opponent_bot.id, &conn)
|
||||||
.map_err(|_| Status::not_found("opponent has no code"))?;
|
.map_err(|_| Status::not_found("no opponent version found"))?;
|
||||||
|
|
||||||
let player_key = gen_alphanumeric(32);
|
let player_key = gen_alphanumeric(32);
|
||||||
|
|
||||||
|
@ -114,8 +114,13 @@ impl pb::bot_api_service_server::BotApiService for BotApiServer {
|
||||||
router: self.router.clone(),
|
router: self.router.clone(),
|
||||||
});
|
});
|
||||||
let mut run_match = RunMatch::from_players(vec![
|
let mut run_match = RunMatch::from_players(vec![
|
||||||
MatchPlayer::from_bot_spec(remote_bot_spec),
|
MatchPlayer::BotSpec {
|
||||||
MatchPlayer::from_bot_version(&opponent, &opponent_code_bundle),
|
spec: remote_bot_spec,
|
||||||
|
},
|
||||||
|
MatchPlayer::BotVersion {
|
||||||
|
bot: Some(opponent_bot),
|
||||||
|
version: opponent_bot_version,
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
let created_match = run_match
|
let created_match = run_match
|
||||||
.store_in_database(&conn)
|
.store_in_database(&conn)
|
||||||
|
|
|
@ -22,39 +22,14 @@ pub struct RunMatch {
|
||||||
match_id: Option<i32>,
|
match_id: Option<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MatchPlayer {
|
pub enum MatchPlayer {
|
||||||
bot_spec: Box<dyn BotSpec>,
|
BotVersion {
|
||||||
// metadata that will be passed on to database
|
bot: Option<db::bots::Bot>,
|
||||||
code_bundle_id: Option<i32>,
|
version: db::bots::BotVersion,
|
||||||
}
|
},
|
||||||
|
BotSpec {
|
||||||
impl MatchPlayer {
|
spec: Box<dyn BotSpec>,
|
||||||
pub fn from_bot_version(bot: &db::bots::Bot, version: &db::bots::BotVersion) -> Self {
|
},
|
||||||
MatchPlayer {
|
|
||||||
bot_spec: bot_version_to_botspec(bot, version),
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_bot_spec(bot_spec: Box<dyn BotSpec>) -> Self {
|
|
||||||
MatchPlayer {
|
|
||||||
bot_spec,
|
|
||||||
code_bundle_id: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RunMatch {
|
impl RunMatch {
|
||||||
|
@ -76,7 +51,12 @@ impl RunMatch {
|
||||||
.players
|
.players
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|player| runner::MatchPlayer {
|
.map(|player| runner::MatchPlayer {
|
||||||
bot_spec: player.bot_spec,
|
bot_spec: match player {
|
||||||
|
MatchPlayer::BotVersion { bot, version } => {
|
||||||
|
bot_version_to_botspec(bot.as_ref(), &version)
|
||||||
|
}
|
||||||
|
MatchPlayer::BotSpec { spec } => spec,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
}
|
}
|
||||||
|
@ -94,11 +74,14 @@ impl RunMatch {
|
||||||
.players
|
.players
|
||||||
.iter()
|
.iter()
|
||||||
.map(|p| db::matches::MatchPlayerData {
|
.map(|p| db::matches::MatchPlayerData {
|
||||||
code_bundle_id: p.code_bundle_id,
|
code_bundle_id: match p {
|
||||||
|
MatchPlayer::BotVersion { version, .. } => Some(version.id),
|
||||||
|
MatchPlayer::BotSpec { .. } => None,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let match_data = db::matches::create_match(&new_match_data, &new_match_players, &db_conn)?;
|
let match_data = db::matches::create_match(&new_match_data, &new_match_players, db_conn)?;
|
||||||
self.match_id = Some(match_data.base.id);
|
self.match_id = Some(match_data.base.id);
|
||||||
Ok(match_data)
|
Ok(match_data)
|
||||||
}
|
}
|
||||||
|
@ -111,12 +94,12 @@ impl RunMatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bot_version_to_botspec(
|
pub fn bot_version_to_botspec(
|
||||||
bot: &db::bots::Bot,
|
bot: Option<&db::bots::Bot>,
|
||||||
bot_version: &db::bots::BotVersion,
|
bot_version: &db::bots::BotVersion,
|
||||||
) -> Box<dyn BotSpec> {
|
) -> Box<dyn BotSpec> {
|
||||||
if let Some(code_bundle_path) = &bot_version.code_bundle_path {
|
if let Some(code_bundle_path) = &bot_version.code_bundle_path {
|
||||||
python_docker_bot_spec(code_bundle_path)
|
python_docker_bot_spec(code_bundle_path)
|
||||||
} else if let Some(container_digest) = &bot_version.container_digest {
|
} else if let (Some(container_digest), Some(bot)) = (&bot_version.container_digest, bot) {
|
||||||
// TODO: put this in config
|
// TODO: put this in config
|
||||||
let registry_url = "localhost:9001";
|
let registry_url = "localhost:9001";
|
||||||
Box::new(DockerBotSpec {
|
Box::new(DockerBotSpec {
|
||||||
|
@ -126,6 +109,7 @@ pub fn bot_version_to_botspec(
|
||||||
working_dir: None,
|
working_dir: None,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
// TODO: ideally this would not be possible
|
||||||
panic!("bad bot version")
|
panic!("bad bot version")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,10 @@ async fn play_ranking_match(selected_bots: Vec<Bot>, db_pool: DbPool) {
|
||||||
for bot in &selected_bots {
|
for bot in &selected_bots {
|
||||||
let version = 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 bot version");
|
.expect("could not get active bot version");
|
||||||
let player = MatchPlayer::from_bot_version(bot, &version);
|
let player = MatchPlayer::BotVersion {
|
||||||
|
bot: Some(bot.clone()),
|
||||||
|
version,
|
||||||
|
};
|
||||||
players.push(player);
|
players.push(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,8 +47,14 @@ pub async fn submit_bot(
|
||||||
.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_version(&player_bot_version),
|
MatchPlayer::BotVersion {
|
||||||
MatchPlayer::from_bot_version(&opponent_bot, &opponent_bot_version),
|
bot: None,
|
||||||
|
version: player_bot_version.clone(),
|
||||||
|
},
|
||||||
|
MatchPlayer::BotVersion {
|
||||||
|
bot: Some(opponent_bot.clone()),
|
||||||
|
version: opponent_bot_version.clone(),
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
let match_data = run_match
|
let match_data = run_match
|
||||||
.store_in_database(&conn)
|
.store_in_database(&conn)
|
||||||
|
|
Loading…
Reference in a new issue