run all bots in python docker for now

This commit is contained in:
Ilion Beyst 2022-01-23 13:23:23 +01:00
parent f62196d983
commit a79b338e90
2 changed files with 21 additions and 11 deletions

View file

@ -42,7 +42,7 @@ pub struct MatchPlayer {
} }
#[async_trait] #[async_trait]
pub trait BotSpec { pub trait BotSpec : Send + Sync{
async fn run_bot( async fn run_bot(
&self, &self,
player_id: u32, player_id: u32,
@ -66,10 +66,7 @@ pub async fn run_match(config: MatchConfig) {
.enumerate() .enumerate()
.map(|(player_id, player)| { .map(|(player_id, player)| {
let player_id = (player_id + 1) as u32; let player_id = (player_id + 1) as u32;
player start_bot(player_id, event_bus.clone(), &player.bot_spec)
.bot_spec
.run_bot(player_id, event_bus.clone())
.map(move |handle| (player_id, handle))
}) })
.collect::<FuturesOrdered<_>>() .collect::<FuturesOrdered<_>>()
// await all results // await all results
@ -101,3 +98,13 @@ pub async fn run_match(config: MatchConfig) {
let match_state = pw_match::PwMatch::create(match_ctx, pw_config); let match_state = pw_match::PwMatch::create(match_ctx, pw_config);
match_state.run().await; match_state.run().await;
} }
// writing this as a closure causes lifetime inference errors
async fn start_bot(
player_id: u32,
event_bus: Arc<Mutex<EventBus>>,
bot_spec: &Box<dyn BotSpec>,
) -> (u32, Box<dyn PlayerHandle>) {
let player_handle = bot_spec.run_bot(player_id, event_bus).await;
(player_id, player_handle)
}

View file

@ -5,7 +5,7 @@ use axum::{
Json, Json,
}; };
use hyper::StatusCode; use hyper::StatusCode;
use planetwars_matchrunner::{run_match, MatchConfig, MatchPlayer}; use planetwars_matchrunner::{docker_runner::DockerBotSpec, run_match, MatchConfig, MatchPlayer};
use rand::{distributions::Alphanumeric, Rng}; use rand::{distributions::Alphanumeric, Rng};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -53,10 +53,13 @@ pub async fn play_match(
players.push(MatchPlayer { players.push(MatchPlayer {
name: bot.name.clone(), name: bot.name.clone(),
path: PathBuf::from(BOTS_DIR).join(code_bundle.path), 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) argv: shlex::split(&bot_config.run_command)
// TODO: this is an user error, should ideally be handled before we get here // TODO: this is an user error, should ideally be handled before we get here
.ok_or_else(|| StatusCode::INTERNAL_SERVER_ERROR)?, .ok_or_else(|| StatusCode::INTERNAL_SERVER_ERROR)?,
}),
}); });
bot_ids.push(matches::MatchPlayerData { bot_id: bot.id }); bot_ids.push(matches::MatchPlayerData { bot_id: bot.id });
@ -66,7 +69,7 @@ pub async fn play_match(
map_name: "hex".to_string(), map_name: "hex".to_string(),
map_path, map_path,
log_path: PathBuf::from(MATCHES_DIR).join(&log_file_name), log_path: PathBuf::from(MATCHES_DIR).join(&log_file_name),
players: players, players,
}; };
tokio::spawn(run_match_task( tokio::spawn(run_match_task(