1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
use std::path::PathBuf;
use axum::Json;
use hyper::StatusCode;
use planetwars_matchrunner::{docker_runner::DockerBotSpec, run_match, MatchConfig, MatchPlayer};
use rand::{distributions::Alphanumeric, Rng};
use serde::{Deserialize, Serialize};
use crate::{DatabaseConnection, BOTS_DIR, MAPS_DIR, MATCHES_DIR};
const PYTHON_IMAGE: &'static str = "python:3.10-slim-buster";
const SIMPLEBOT_PATH: &'static str = "../simplebot";
#[derive(Serialize, Deserialize, Debug)]
pub struct SubmitBotParams {
pub code: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SubmitBotResponse {
pub match_id: String,
}
pub fn gen_alphanumeric(length: usize) -> String {
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(length)
.map(char::from)
.collect()
}
/// 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>,
) -> Result<Json<SubmitBotResponse>, StatusCode> {
let uploaded_bot_id: String = gen_alphanumeric(16);
let match_id = gen_alphanumeric(16);
let uploaded_bot_dir = PathBuf::from(BOTS_DIR).join(&uploaded_bot_id);
std::fs::create_dir(&uploaded_bot_dir).unwrap();
std::fs::write(uploaded_bot_dir.join("bot.py"), params.code.as_bytes()).unwrap();
run_match(MatchConfig {
map_path: PathBuf::from(MAPS_DIR).join("hex.json"),
map_name: "hex".to_string(),
log_path: PathBuf::from(MATCHES_DIR).join(format!("{}.log", match_id)),
players: vec![
MatchPlayer {
name: "bot".to_string(),
bot_spec: Box::new(DockerBotSpec {
code_path: uploaded_bot_dir,
image: PYTHON_IMAGE.to_string(),
argv: vec!["python".to_string(), "bot.py".to_string()],
}),
},
MatchPlayer {
name: "simplebot".to_string(),
bot_spec: Box::new(DockerBotSpec {
code_path: PathBuf::from(SIMPLEBOT_PATH),
image: PYTHON_IMAGE.to_string(),
argv: vec!["python".to_string(), "simplebot.py".to_string()],
}),
},
],
})
.await;
Ok(Json(SubmitBotResponse { match_id }))
}
|