store demo matches in database
This commit is contained in:
parent
69f33763ab
commit
fc10359997
7 changed files with 54 additions and 42 deletions
|
@ -1,2 +1,2 @@
|
||||||
DROP INDEX users_username_index
|
DROP INDEX users_username_index;
|
||||||
DROP TABLE users;
|
DROP TABLE users;
|
|
@ -1,4 +1,4 @@
|
||||||
CREATE TYPE match_state AS ENUM ('playing', 'ended');
|
CREATE TYPE match_state AS ENUM ('playing', 'finished');
|
||||||
|
|
||||||
CREATE TABLE matches (
|
CREATE TABLE matches (
|
||||||
id SERIAL PRIMARY KEY NOT NULL,
|
id SERIAL PRIMARY KEY NOT NULL,
|
||||||
|
|
|
@ -46,16 +46,16 @@ pub struct MatchPlayerData {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_match(
|
pub fn create_match(
|
||||||
match_data: &NewMatch,
|
new_match_base: &NewMatch,
|
||||||
match_players: &[MatchPlayerData],
|
new_match_players: &[MatchPlayerData],
|
||||||
conn: &PgConnection,
|
conn: &PgConnection,
|
||||||
) -> QueryResult<i32> {
|
) -> QueryResult<MatchData> {
|
||||||
conn.transaction(|| {
|
conn.transaction(|| {
|
||||||
let match_base = diesel::insert_into(matches::table)
|
let match_base = diesel::insert_into(matches::table)
|
||||||
.values(match_data)
|
.values(new_match_base)
|
||||||
.get_result::<MatchBase>(conn)?;
|
.get_result::<MatchBase>(conn)?;
|
||||||
|
|
||||||
let match_players = match_players
|
let new_match_players = new_match_players
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(num, player_data)| NewMatchPlayer {
|
.map(|(num, player_data)| NewMatchPlayer {
|
||||||
|
@ -65,11 +65,14 @@ pub fn create_match(
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
diesel::insert_into(match_players::table)
|
let match_players = diesel::insert_into(match_players::table)
|
||||||
.values(&match_players)
|
.values(&new_match_players)
|
||||||
.execute(conn)?;
|
.get_results::<MatchPlayer>(conn)?;
|
||||||
|
|
||||||
Ok(match_base.id)
|
Ok(MatchData {
|
||||||
|
base: match_base,
|
||||||
|
match_players,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ use crate::schema::users;
|
||||||
use argon2;
|
use argon2;
|
||||||
use diesel::{prelude::*, PgConnection};
|
use diesel::{prelude::*, PgConnection};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Credentials<'a> {
|
pub struct Credentials<'a> {
|
||||||
|
|
|
@ -55,10 +55,6 @@ pub async fn api() -> Router {
|
||||||
)
|
)
|
||||||
.route("/matches/:match_id", get(routes::matches::get_match_log))
|
.route("/matches/:match_id", get(routes::matches::get_match_log))
|
||||||
.route("/submit_bot", post(routes::demo::submit_bot))
|
.route("/submit_bot", post(routes::demo::submit_bot))
|
||||||
.route(
|
|
||||||
"/submission_match_log/:match_id",
|
|
||||||
get(routes::demo::get_submission_match_log),
|
|
||||||
)
|
|
||||||
.layer(AddExtensionLayer::new(pool));
|
.layer(AddExtensionLayer::new(pool));
|
||||||
api
|
api
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
use std::path::PathBuf;
|
use crate::db::matches::{self, MatchState};
|
||||||
|
use crate::{ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR};
|
||||||
use axum::{extract::Path, Json};
|
use axum::extract::Extension;
|
||||||
|
use axum::Json;
|
||||||
use hyper::StatusCode;
|
use hyper::StatusCode;
|
||||||
use planetwars_matchrunner::{docker_runner::DockerBotSpec, 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};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::{DatabaseConnection, BOTS_DIR, MAPS_DIR, MATCHES_DIR};
|
use super::matches::ApiMatch;
|
||||||
|
|
||||||
const PYTHON_IMAGE: &'static str = "python:3.10-slim-buster";
|
const PYTHON_IMAGE: &'static str = "python:3.10-slim-buster";
|
||||||
const SIMPLEBOT_PATH: &'static str = "../simplebot";
|
const SIMPLEBOT_PATH: &'static str = "../simplebot";
|
||||||
|
@ -16,38 +18,36 @@ pub struct SubmitBotParams {
|
||||||
pub code: String,
|
pub code: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct SubmitBotResponse {
|
pub struct SubmitBotResponse {
|
||||||
pub match_id: String,
|
#[serde(rename = "match")]
|
||||||
}
|
pub match_data: ApiMatch,
|
||||||
|
|
||||||
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
|
/// submit python code for a bot, which will face off
|
||||||
/// with a demo bot. Return a played match.
|
/// with a demo bot. Return a played match.
|
||||||
pub async fn submit_bot(
|
pub async fn submit_bot(
|
||||||
Json(params): Json<SubmitBotParams>,
|
Json(params): Json<SubmitBotParams>,
|
||||||
|
Extension(pool): Extension<ConnectionPool>,
|
||||||
) -> Result<Json<SubmitBotResponse>, StatusCode> {
|
) -> Result<Json<SubmitBotResponse>, StatusCode> {
|
||||||
let uploaded_bot_id: String = gen_alphanumeric(16);
|
let conn = pool.get().await.expect("could not get database connection");
|
||||||
let match_id = gen_alphanumeric(16);
|
|
||||||
|
|
||||||
let uploaded_bot_dir = PathBuf::from(BOTS_DIR).join(&uploaded_bot_id);
|
let uploaded_bot_uuid: String = gen_alphanumeric(16);
|
||||||
|
let log_file_name = format!("{}.log", gen_alphanumeric(16));
|
||||||
|
|
||||||
|
// store uploaded bot
|
||||||
|
let uploaded_bot_dir = PathBuf::from(BOTS_DIR).join(&uploaded_bot_uuid);
|
||||||
std::fs::create_dir(&uploaded_bot_dir).unwrap();
|
std::fs::create_dir(&uploaded_bot_dir).unwrap();
|
||||||
std::fs::write(uploaded_bot_dir.join("bot.py"), params.code.as_bytes()).unwrap();
|
std::fs::write(uploaded_bot_dir.join("bot.py"), params.code.as_bytes()).unwrap();
|
||||||
|
|
||||||
|
// play the match
|
||||||
run_match(MatchConfig {
|
run_match(MatchConfig {
|
||||||
map_path: PathBuf::from(MAPS_DIR).join("hex.json"),
|
map_path: PathBuf::from(MAPS_DIR).join("hex.json"),
|
||||||
map_name: "hex".to_string(),
|
map_name: "hex".to_string(),
|
||||||
log_path: PathBuf::from(MATCHES_DIR).join(format!("{}.log", match_id)),
|
log_path: PathBuf::from(MATCHES_DIR).join(&log_file_name),
|
||||||
players: vec![
|
players: vec![
|
||||||
MatchPlayer {
|
MatchPlayer {
|
||||||
name: "bot".to_string(),
|
name: "player".to_string(),
|
||||||
bot_spec: Box::new(DockerBotSpec {
|
bot_spec: Box::new(DockerBotSpec {
|
||||||
code_path: uploaded_bot_dir,
|
code_path: uploaded_bot_dir,
|
||||||
image: PYTHON_IMAGE.to_string(),
|
image: PYTHON_IMAGE.to_string(),
|
||||||
|
@ -66,12 +66,25 @@ pub async fn submit_bot(
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
Ok(Json(SubmitBotResponse { match_id }))
|
// store match in database
|
||||||
|
let new_match_data = matches::NewMatch {
|
||||||
|
state: MatchState::Finished,
|
||||||
|
log_path: &log_file_name,
|
||||||
|
};
|
||||||
|
// TODO: set match players
|
||||||
|
let match_data =
|
||||||
|
matches::create_match(&new_match_data, &[], &conn).expect("failed to create match");
|
||||||
|
|
||||||
|
let api_match = super::matches::match_data_to_api(match_data);
|
||||||
|
Ok(Json(SubmitBotResponse {
|
||||||
|
match_data: api_match,
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: unify this with existing match API
|
pub fn gen_alphanumeric(length: usize) -> String {
|
||||||
pub async fn get_submission_match_log(Path(match_id): Path<String>) -> Result<String, StatusCode> {
|
rand::thread_rng()
|
||||||
let log_path = PathBuf::from(MATCHES_DIR).join(format!("{}.log", match_id));
|
.sample_iter(&Alphanumeric)
|
||||||
|
.take(length)
|
||||||
std::fs::read_to_string(&log_path).map_err(|_| StatusCode::NOT_FOUND)
|
.map(char::from)
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,7 +115,7 @@ pub async fn list_matches(conn: DatabaseConnection) -> Result<Json<Vec<ApiMatch>
|
||||||
.map(|matches| Json(matches.into_iter().map(match_data_to_api).collect()))
|
.map(|matches| Json(matches.into_iter().map(match_data_to_api).collect()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn match_data_to_api(data: matches::MatchData) -> ApiMatch {
|
pub fn match_data_to_api(data: matches::MatchData) -> ApiMatch {
|
||||||
ApiMatch {
|
ApiMatch {
|
||||||
id: data.base.id,
|
id: data.base.id,
|
||||||
timestamp: data.base.created_at,
|
timestamp: data.base.created_at,
|
||||||
|
|
Loading…
Reference in a new issue