2022-02-27 22:57:06 +01:00
|
|
|
use crate::db;
|
2022-03-10 23:35:42 +01:00
|
|
|
use crate::db::matches::{MatchPlayerData, MatchState};
|
2022-02-27 20:35:22 +01:00
|
|
|
use crate::modules::bots::save_code_bundle;
|
|
|
|
use crate::util::gen_alphanumeric;
|
2022-02-08 20:13:24 +01:00
|
|
|
use crate::{ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR};
|
|
|
|
use axum::extract::Extension;
|
|
|
|
use axum::Json;
|
2022-01-26 20:54:19 +01:00
|
|
|
use hyper::StatusCode;
|
2022-02-27 22:57:06 +01:00
|
|
|
use planetwars_matchrunner::BotSpec;
|
2022-01-26 20:54:19 +01:00
|
|
|
use planetwars_matchrunner::{docker_runner::DockerBotSpec, run_match, MatchConfig, MatchPlayer};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2022-02-08 20:13:24 +01:00
|
|
|
use std::path::PathBuf;
|
2022-01-26 20:54:19 +01:00
|
|
|
|
2022-02-08 20:13:24 +01:00
|
|
|
use super::matches::ApiMatch;
|
2022-01-26 20:54:19 +01:00
|
|
|
|
|
|
|
const PYTHON_IMAGE: &'static str = "python:3.10-slim-buster";
|
2022-02-27 22:57:06 +01:00
|
|
|
const OPPONENT_NAME: &'static str = "simplebot";
|
2022-01-26 20:54:19 +01:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct SubmitBotParams {
|
|
|
|
pub code: String,
|
2022-03-03 21:12:16 +01:00
|
|
|
// TODO: would it be better to pass an ID here?
|
|
|
|
pub opponent_name: Option<String>,
|
2022-01-26 20:54:19 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 20:13:24 +01:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2022-01-26 20:54:19 +01:00
|
|
|
pub struct SubmitBotResponse {
|
2022-02-08 20:13:24 +01:00
|
|
|
#[serde(rename = "match")]
|
|
|
|
pub match_data: ApiMatch,
|
2022-01-26 20:54:19 +01:00
|
|
|
}
|
|
|
|
|
2022-02-27 22:57:06 +01:00
|
|
|
fn code_bundle_to_botspec(code_bundle: &db::bots::CodeBundle) -> Box<dyn BotSpec> {
|
|
|
|
let bundle_path = PathBuf::from(BOTS_DIR).join(&code_bundle.path);
|
|
|
|
|
|
|
|
Box::new(DockerBotSpec {
|
|
|
|
code_path: bundle_path,
|
|
|
|
image: PYTHON_IMAGE.to_string(),
|
|
|
|
argv: vec!["python".to_string(), "bot.py".to_string()],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:54:19 +01:00
|
|
|
/// submit python code for a bot, which will face off
|
|
|
|
/// with a demo bot. Return a played match.
|
|
|
|
pub async fn submit_bot(
|
|
|
|
Json(params): Json<SubmitBotParams>,
|
2022-02-08 20:13:24 +01:00
|
|
|
Extension(pool): Extension<ConnectionPool>,
|
2022-01-26 20:54:19 +01:00
|
|
|
) -> Result<Json<SubmitBotResponse>, StatusCode> {
|
2022-02-08 20:13:24 +01:00
|
|
|
let conn = pool.get().await.expect("could not get database connection");
|
|
|
|
|
2022-03-03 21:12:16 +01:00
|
|
|
let opponent_name = params
|
|
|
|
.opponent_name
|
|
|
|
.unwrap_or_else(|| OPPONENT_NAME.to_string());
|
|
|
|
|
2022-02-27 22:57:06 +01:00
|
|
|
let opponent =
|
2022-03-03 21:12:16 +01:00
|
|
|
db::bots::find_bot_by_name(&opponent_name, &conn).map_err(|_| StatusCode::BAD_REQUEST)?;
|
2022-02-27 22:57:06 +01:00
|
|
|
let opponent_code_bundle =
|
2022-03-03 21:12:16 +01:00
|
|
|
db::bots::active_code_bundle(opponent.id, &conn).map_err(|_| StatusCode::BAD_REQUEST)?;
|
2022-02-27 22:57:06 +01:00
|
|
|
|
|
|
|
let player_code_bundle = save_code_bundle(¶ms.code, None, &conn)
|
2022-02-27 20:35:22 +01:00
|
|
|
// TODO: can we recover from this?
|
|
|
|
.expect("could not save bot code");
|
2022-01-26 20:54:19 +01:00
|
|
|
|
2022-02-27 20:35:22 +01:00
|
|
|
let log_file_name = format!("{}.log", gen_alphanumeric(16));
|
2022-02-08 20:13:24 +01:00
|
|
|
// play the match
|
2022-02-15 19:54:29 +01:00
|
|
|
let match_config = MatchConfig {
|
2022-01-26 20:54:19 +01:00
|
|
|
map_path: PathBuf::from(MAPS_DIR).join("hex.json"),
|
|
|
|
map_name: "hex".to_string(),
|
2022-02-08 20:13:24 +01:00
|
|
|
log_path: PathBuf::from(MATCHES_DIR).join(&log_file_name),
|
2022-01-26 20:54:19 +01:00
|
|
|
players: vec![
|
|
|
|
MatchPlayer {
|
2022-02-08 20:13:24 +01:00
|
|
|
name: "player".to_string(),
|
2022-02-27 22:57:06 +01:00
|
|
|
bot_spec: code_bundle_to_botspec(&player_code_bundle),
|
2022-01-26 20:54:19 +01:00
|
|
|
},
|
|
|
|
MatchPlayer {
|
2022-02-27 22:57:06 +01:00
|
|
|
name: OPPONENT_NAME.to_string(),
|
|
|
|
bot_spec: code_bundle_to_botspec(&opponent_code_bundle),
|
2022-01-26 20:54:19 +01:00
|
|
|
},
|
|
|
|
],
|
2022-02-15 19:54:29 +01:00
|
|
|
};
|
2022-01-26 20:54:19 +01:00
|
|
|
|
2022-02-08 20:13:24 +01:00
|
|
|
// store match in database
|
2022-02-27 22:57:06 +01:00
|
|
|
let new_match_data = db::matches::NewMatch {
|
2022-02-15 19:54:29 +01:00
|
|
|
state: MatchState::Playing,
|
2022-02-08 20:13:24 +01:00
|
|
|
log_path: &log_file_name,
|
|
|
|
};
|
2022-03-10 23:35:42 +01:00
|
|
|
|
|
|
|
let new_match_players = [
|
|
|
|
MatchPlayerData {
|
|
|
|
code_bundle_id: player_code_bundle.id,
|
|
|
|
},
|
|
|
|
MatchPlayerData {
|
|
|
|
code_bundle_id: opponent_code_bundle.id,
|
|
|
|
},
|
|
|
|
];
|
2022-02-08 20:13:24 +01:00
|
|
|
// TODO: set match players
|
2022-03-10 23:35:42 +01:00
|
|
|
let match_data = db::matches::create_match(&new_match_data, &new_match_players, &conn)
|
|
|
|
.expect("failed to create match");
|
2022-01-27 19:22:48 +01:00
|
|
|
|
2022-02-15 19:54:29 +01:00
|
|
|
tokio::spawn(run_match_task(
|
|
|
|
match_data.base.id,
|
|
|
|
match_config,
|
|
|
|
pool.clone(),
|
|
|
|
));
|
|
|
|
|
2022-02-08 20:13:24 +01:00
|
|
|
let api_match = super::matches::match_data_to_api(match_data);
|
|
|
|
Ok(Json(SubmitBotResponse {
|
|
|
|
match_data: api_match,
|
|
|
|
}))
|
|
|
|
}
|
2022-01-27 19:22:48 +01:00
|
|
|
|
2022-02-15 19:54:29 +01:00
|
|
|
async fn run_match_task(match_id: i32, match_config: MatchConfig, connection_pool: ConnectionPool) {
|
|
|
|
run_match(match_config).await;
|
|
|
|
let conn = connection_pool
|
|
|
|
.get()
|
|
|
|
.await
|
|
|
|
.expect("could not get database connection");
|
2022-02-27 22:57:06 +01:00
|
|
|
db::matches::set_match_state(match_id, MatchState::Finished, &conn)
|
2022-02-15 19:54:29 +01:00
|
|
|
.expect("failed to update match state");
|
|
|
|
}
|