2022-01-03 23:33:00 +01:00
|
|
|
use std::path::PathBuf;
|
2022-01-01 16:32:55 +01:00
|
|
|
|
2022-01-02 17:56:52 +01:00
|
|
|
use axum::{
|
|
|
|
extract::{Extension, Path},
|
|
|
|
Json,
|
|
|
|
};
|
2022-01-01 16:32:55 +01:00
|
|
|
use hyper::StatusCode;
|
2022-01-23 13:23:23 +01:00
|
|
|
use planetwars_matchrunner::{docker_runner::DockerBotSpec, run_match, MatchConfig, MatchPlayer};
|
2022-01-01 16:32:55 +01:00
|
|
|
use rand::{distributions::Alphanumeric, Rng};
|
2022-01-01 11:26:49 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2022-01-01 16:32:55 +01:00
|
|
|
use crate::{
|
2022-01-03 23:33:00 +01:00
|
|
|
db::{
|
|
|
|
bots,
|
|
|
|
matches::{self, MatchState},
|
|
|
|
users::User,
|
|
|
|
},
|
2022-01-02 16:14:03 +01:00
|
|
|
ConnectionPool, DatabaseConnection, BOTS_DIR, MAPS_DIR, MATCHES_DIR,
|
2022-01-01 16:32:55 +01:00
|
|
|
};
|
|
|
|
|
2022-01-01 11:26:49 +01:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct MatchParams {
|
|
|
|
// Just bot ids for now
|
|
|
|
players: Vec<i32>,
|
|
|
|
}
|
|
|
|
|
2022-01-01 16:32:55 +01:00
|
|
|
pub async fn play_match(
|
2022-01-02 16:14:03 +01:00
|
|
|
_user: User,
|
|
|
|
Extension(pool): Extension<ConnectionPool>,
|
2022-01-01 16:32:55 +01:00
|
|
|
Json(params): Json<MatchParams>,
|
|
|
|
) -> Result<(), StatusCode> {
|
2022-01-02 16:14:03 +01:00
|
|
|
let conn = pool.get().await.expect("could not get database connection");
|
2022-01-01 16:32:55 +01:00
|
|
|
let map_path = PathBuf::from(MAPS_DIR).join("hex.json");
|
|
|
|
|
|
|
|
let slug: String = rand::thread_rng()
|
|
|
|
.sample_iter(&Alphanumeric)
|
|
|
|
.take(16)
|
|
|
|
.map(char::from)
|
|
|
|
.collect();
|
2022-01-02 17:56:52 +01:00
|
|
|
let log_file_name = format!("{}.log", slug);
|
2022-01-01 16:32:55 +01:00
|
|
|
|
|
|
|
let mut players = Vec::new();
|
2022-01-02 16:14:03 +01:00
|
|
|
let mut bot_ids = Vec::new();
|
2022-01-01 16:32:55 +01:00
|
|
|
for bot_name in params.players {
|
|
|
|
let bot = bots::find_bot(bot_name, &conn).map_err(|_| StatusCode::BAD_REQUEST)?;
|
|
|
|
let code_bundle =
|
|
|
|
bots::active_code_bundle(bot.id, &conn).map_err(|_| StatusCode::BAD_REQUEST)?;
|
|
|
|
|
|
|
|
let bundle_path = PathBuf::from(BOTS_DIR).join(&code_bundle.path);
|
|
|
|
let bot_config: BotConfig = std::fs::read_to_string(bundle_path.join("botconfig.toml"))
|
|
|
|
.and_then(|config_str| toml::from_str(&config_str).map_err(|e| e.into()))
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
|
|
|
|
|
|
players.push(MatchPlayer {
|
2022-01-23 13:23:23 +01:00
|
|
|
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)
|
2022-03-13 15:20:03 +01:00
|
|
|
.ok_or(StatusCode::INTERNAL_SERVER_ERROR)?,
|
2022-01-23 13:23:23 +01:00
|
|
|
}),
|
2022-01-01 16:32:55 +01:00
|
|
|
});
|
2022-01-02 16:14:03 +01:00
|
|
|
|
2022-03-10 23:35:42 +01:00
|
|
|
bot_ids.push(matches::MatchPlayerData {
|
|
|
|
code_bundle_id: code_bundle.id,
|
|
|
|
});
|
2022-01-01 16:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let match_config = MatchConfig {
|
|
|
|
map_name: "hex".to_string(),
|
|
|
|
map_path,
|
2022-01-02 17:56:52 +01:00
|
|
|
log_path: PathBuf::from(MATCHES_DIR).join(&log_file_name),
|
2022-01-23 13:23:23 +01:00
|
|
|
players,
|
2022-01-01 16:32:55 +01:00
|
|
|
};
|
|
|
|
|
2022-01-02 17:56:52 +01:00
|
|
|
tokio::spawn(run_match_task(
|
|
|
|
match_config,
|
|
|
|
log_file_name,
|
|
|
|
bot_ids,
|
|
|
|
pool.clone(),
|
|
|
|
));
|
2022-01-01 16:32:55 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-02 16:14:03 +01:00
|
|
|
async fn run_match_task(
|
|
|
|
config: MatchConfig,
|
2022-01-02 17:56:52 +01:00
|
|
|
log_file_name: String,
|
2022-01-02 16:14:03 +01:00
|
|
|
match_players: Vec<matches::MatchPlayerData>,
|
|
|
|
pool: ConnectionPool,
|
|
|
|
) {
|
|
|
|
let match_data = matches::NewMatch {
|
2022-01-03 23:33:00 +01:00
|
|
|
state: MatchState::Finished,
|
2022-01-02 17:56:52 +01:00
|
|
|
log_path: &log_file_name,
|
2022-01-02 16:14:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
run_match(config).await;
|
|
|
|
let conn = pool.get().await.expect("could not get database connection");
|
|
|
|
matches::create_match(&match_data, &match_players, &conn).expect("could not create match");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct ApiMatch {
|
|
|
|
id: i32,
|
|
|
|
timestamp: chrono::NaiveDateTime,
|
2022-02-16 18:40:32 +01:00
|
|
|
state: MatchState,
|
2022-01-02 16:14:03 +01:00
|
|
|
players: Vec<ApiMatchPlayer>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct ApiMatchPlayer {
|
2022-03-11 00:41:18 +01:00
|
|
|
code_bundle_id: i32,
|
|
|
|
bot_id: Option<i32>,
|
|
|
|
bot_name: Option<String>,
|
2022-01-02 16:14:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn list_matches(conn: DatabaseConnection) -> Result<Json<Vec<ApiMatch>>, StatusCode> {
|
|
|
|
matches::list_matches(&conn)
|
|
|
|
.map_err(|_| StatusCode::BAD_REQUEST)
|
|
|
|
.map(|matches| Json(matches.into_iter().map(match_data_to_api).collect()))
|
|
|
|
}
|
|
|
|
|
2022-03-11 00:41:18 +01:00
|
|
|
pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch {
|
2022-01-02 16:14:03 +01:00
|
|
|
ApiMatch {
|
|
|
|
id: data.base.id,
|
|
|
|
timestamp: data.base.created_at,
|
2022-02-16 18:40:32 +01:00
|
|
|
state: data.base.state,
|
2022-01-02 16:14:03 +01:00
|
|
|
players: data
|
|
|
|
.match_players
|
|
|
|
.iter()
|
2022-03-11 00:41:18 +01:00
|
|
|
.map(|_p| ApiMatchPlayer {
|
|
|
|
code_bundle_id: _p.code_bundle.id,
|
|
|
|
bot_id: _p.bot.as_ref().map(|b| b.id),
|
|
|
|
bot_name: _p.bot.as_ref().map(|b| b.name.clone()),
|
|
|
|
})
|
2022-01-02 16:14:03 +01:00
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-01 16:32:55 +01:00
|
|
|
// TODO: this is duplicated from planetwars-cli
|
|
|
|
// clean this up and move to matchrunner crate
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct BotConfig {
|
|
|
|
pub name: String,
|
|
|
|
pub run_command: String,
|
|
|
|
pub build_command: Option<String>,
|
2022-01-01 11:26:49 +01:00
|
|
|
}
|
2022-01-02 17:56:52 +01:00
|
|
|
|
2022-02-12 23:52:45 +01:00
|
|
|
pub async fn get_match_data(
|
|
|
|
Path(match_id): Path<i32>,
|
|
|
|
conn: DatabaseConnection,
|
|
|
|
) -> Result<Json<ApiMatch>, StatusCode> {
|
|
|
|
let match_data = matches::find_match(match_id, &conn)
|
|
|
|
.map_err(|_| StatusCode::NOT_FOUND)
|
2022-03-13 15:20:03 +01:00
|
|
|
.map(match_data_to_api)?;
|
2022-02-12 23:52:45 +01:00
|
|
|
Ok(Json(match_data))
|
|
|
|
}
|
|
|
|
|
2022-01-02 17:56:52 +01:00
|
|
|
pub async fn get_match_log(
|
|
|
|
Path(match_id): Path<i32>,
|
|
|
|
conn: DatabaseConnection,
|
|
|
|
) -> Result<Vec<u8>, StatusCode> {
|
2022-03-12 09:04:12 +01:00
|
|
|
let match_base =
|
|
|
|
matches::find_match_base(match_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
|
2022-01-02 17:56:52 +01:00
|
|
|
let log_path = PathBuf::from(MATCHES_DIR).join(&match_base.log_path);
|
|
|
|
let log_contents = std::fs::read(log_path).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
|
|
Ok(log_contents)
|
|
|
|
}
|