aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/routes
diff options
context:
space:
mode:
Diffstat (limited to 'planetwars-server/src/routes')
-rw-r--r--planetwars-server/src/routes/bots.rs7
-rw-r--r--planetwars-server/src/routes/demo.rs79
-rw-r--r--planetwars-server/src/routes/matches.rs1
3 files changed, 17 insertions, 70 deletions
diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs
index 2cdea2a..3bbaa1a 100644
--- a/planetwars-server/src/routes/bots.rs
+++ b/planetwars-server/src/routes/bots.rs
@@ -12,6 +12,7 @@ use std::path::PathBuf;
use thiserror;
use crate::db::bots::{self, CodeBundle};
+use crate::db::ratings::{RankedBot, self};
use crate::db::users::User;
use crate::modules::bots::save_code_bundle;
use crate::{DatabaseConnection, BOTS_DIR};
@@ -170,6 +171,12 @@ pub async fn list_bots(conn: DatabaseConnection) -> Result<Json<Vec<Bot>>, Statu
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}
+pub async fn get_ranking(conn: DatabaseConnection) -> Result<Json<Vec<RankedBot>>, StatusCode> {
+ ratings::get_bot_ranking(&conn)
+ .map(Json)
+ .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
+}
+
// TODO: currently this only implements the happy flow
pub async fn upload_code_multipart(
conn: DatabaseConnection,
diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs
index eb7f61f..7f7ba71 100644
--- a/planetwars-server/src/routes/demo.rs
+++ b/planetwars-server/src/routes/demo.rs
@@ -1,20 +1,16 @@
use crate::db;
-use crate::db::matches::{FullMatchData, FullMatchPlayerData, MatchPlayerData, MatchState};
+use crate::db::matches::{FullMatchData, FullMatchPlayerData};
use crate::modules::bots::save_code_bundle;
-use crate::util::gen_alphanumeric;
-use crate::{ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR};
+use crate::modules::matches::RunMatch;
+use crate::ConnectionPool;
use axum::extract::Extension;
use axum::Json;
use hyper::StatusCode;
-use planetwars_matchrunner::BotSpec;
-use planetwars_matchrunner::{docker_runner::DockerBotSpec, run_match, MatchConfig, MatchPlayer};
use serde::{Deserialize, Serialize};
-use std::path::PathBuf;
use super::matches::ApiMatch;
-const PYTHON_IMAGE: &str = "python:3.10-slim-buster";
-const OPPONENT_NAME: &str = "simplebot";
+const DEFAULT_OPPONENT_NAME: &str = "simplebot";
#[derive(Serialize, Deserialize, Debug)]
pub struct SubmitBotParams {
@@ -29,16 +25,6 @@ pub struct SubmitBotResponse {
pub match_data: ApiMatch,
}
-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()],
- })
-}
-
/// submit python code for a bot, which will face off
/// with a demo bot. Return a played match.
pub async fn submit_bot(
@@ -49,7 +35,7 @@ pub async fn submit_bot(
let opponent_name = params
.opponent_name
- .unwrap_or_else(|| OPPONENT_NAME.to_string());
+ .unwrap_or_else(|| DEFAULT_OPPONENT_NAME.to_string());
let opponent =
db::bots::find_bot_by_name(&opponent_name, &conn).map_err(|_| StatusCode::BAD_REQUEST)?;
@@ -60,46 +46,11 @@ pub async fn submit_bot(
// TODO: can we recover from this?
.expect("could not save bot code");
- let log_file_name = format!("{}.log", gen_alphanumeric(16));
- // play the match
- let match_config = MatchConfig {
- map_path: PathBuf::from(MAPS_DIR).join("hex.json"),
- map_name: "hex".to_string(),
- log_path: PathBuf::from(MATCHES_DIR).join(&log_file_name),
- players: vec![
- MatchPlayer {
- name: "player".to_string(),
- bot_spec: code_bundle_to_botspec(&player_code_bundle),
- },
- MatchPlayer {
- name: OPPONENT_NAME.to_string(),
- bot_spec: code_bundle_to_botspec(&opponent_code_bundle),
- },
- ],
- };
-
- // store match in database
- let new_match_data = db::matches::NewMatch {
- state: MatchState::Playing,
- log_path: &log_file_name,
- };
-
- let new_match_players = [
- MatchPlayerData {
- code_bundle_id: player_code_bundle.id,
- },
- MatchPlayerData {
- code_bundle_id: opponent_code_bundle.id,
- },
- ];
- let match_data = db::matches::create_match(&new_match_data, &new_match_players, &conn)
- .expect("failed to create match");
-
- tokio::spawn(run_match_task(
- match_data.base.id,
- match_config,
- pool.clone(),
- ));
+ let mut run_match = RunMatch::from_players(vec![&player_code_bundle, &opponent_code_bundle]);
+ let match_data = run_match
+ .store_in_database(&conn)
+ .expect("failed to save match");
+ run_match.spawn(pool.clone());
// TODO: avoid clones
let full_match_data = FullMatchData {
@@ -123,13 +74,3 @@ pub async fn submit_bot(
match_data: api_match,
}))
}
-
-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");
- db::matches::set_match_state(match_id, MatchState::Finished, &conn)
- .expect("failed to update match state");
-}
diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs
index 991a4b5..b61008d 100644
--- a/planetwars-server/src/routes/matches.rs
+++ b/planetwars-server/src/routes/matches.rs
@@ -52,7 +52,6 @@ pub async fn play_match(
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
players.push(MatchPlayer {
- name: bot.name.clone(),
bot_spec: Box::new(DockerBotSpec {
code_path: PathBuf::from(BOTS_DIR).join(code_bundle.path),
image: "python:3.10-slim-buster".to_string(),