aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-05-28 11:22:44 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-05-28 11:22:44 +0200
commit80c60ac69c9b0d86a4536eeac82cf266eb4430bc (patch)
treebc2ac0c803be27395c0297123f0794e1b8904676 /planetwars-server/src
parent643c0e7706ab927ef270e4a5b62ada0c38b651b9 (diff)
parentfadcda850332f8adb0a4382da9f04f78db3f6d1a (diff)
downloadplanetwars.dev-80c60ac69c9b0d86a4536eeac82cf266eb4430bc.tar.xz
planetwars.dev-80c60ac69c9b0d86a4536eeac82cf266eb4430bc.zip
Merge branch 'feature/leaderboard'
Diffstat (limited to 'planetwars-server/src')
-rw-r--r--planetwars-server/src/db/bots.rs2
-rw-r--r--planetwars-server/src/db/mod.rs1
-rw-r--r--planetwars-server/src/db/ratings.rs54
-rw-r--r--planetwars-server/src/lib.rs40
-rw-r--r--planetwars-server/src/main.rs11
-rw-r--r--planetwars-server/src/modules/matches.rs102
-rw-r--r--planetwars-server/src/modules/mod.rs2
-rw-r--r--planetwars-server/src/modules/ranking.rs87
-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
-rw-r--r--planetwars-server/src/schema.rs21
12 files changed, 313 insertions, 94 deletions
diff --git a/planetwars-server/src/db/bots.rs b/planetwars-server/src/db/bots.rs
index d99a459..108c692 100644
--- a/planetwars-server/src/db/bots.rs
+++ b/planetwars-server/src/db/bots.rs
@@ -11,7 +11,7 @@ pub struct NewBot<'a> {
pub name: &'a str,
}
-#[derive(Queryable, Debug, PartialEq, Serialize, Deserialize)]
+#[derive(Queryable, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Bot {
pub id: i32,
pub owner_id: Option<i32>,
diff --git a/planetwars-server/src/db/mod.rs b/planetwars-server/src/db/mod.rs
index 7a950c6..84ed2a6 100644
--- a/planetwars-server/src/db/mod.rs
+++ b/planetwars-server/src/db/mod.rs
@@ -1,4 +1,5 @@
pub mod bots;
pub mod matches;
+pub mod ratings;
pub mod sessions;
pub mod users;
diff --git a/planetwars-server/src/db/ratings.rs b/planetwars-server/src/db/ratings.rs
new file mode 100644
index 0000000..8262fed
--- /dev/null
+++ b/planetwars-server/src/db/ratings.rs
@@ -0,0 +1,54 @@
+use diesel::{prelude::*, PgConnection, QueryResult};
+use serde::{Deserialize, Serialize};
+
+use crate::db::bots::Bot;
+use crate::schema::{bots, ratings, users};
+
+#[derive(Queryable, Debug, Insertable, PartialEq, Serialize, Deserialize)]
+pub struct Rating {
+ pub bot_id: i32,
+ pub rating: f64,
+}
+
+pub fn get_rating(bot_id: i32, db_conn: &PgConnection) -> QueryResult<Option<f64>> {
+ ratings::table
+ .filter(ratings::bot_id.eq(bot_id))
+ .select(ratings::rating)
+ .first(db_conn)
+ .optional()
+}
+
+pub fn set_rating(bot_id: i32, rating: f64, db_conn: &PgConnection) -> QueryResult<usize> {
+ diesel::insert_into(ratings::table)
+ .values(Rating { bot_id, rating })
+ .on_conflict(ratings::bot_id)
+ .do_update()
+ .set(ratings::rating.eq(rating))
+ .execute(db_conn)
+}
+
+#[derive(Queryable, Serialize, Deserialize)]
+pub struct Author {
+ id: i32,
+ username: String,
+}
+
+#[derive(Queryable, Serialize, Deserialize)]
+pub struct RankedBot {
+ pub bot: Bot,
+ pub author: Option<Author>,
+ pub rating: f64,
+}
+
+pub fn get_bot_ranking(db_conn: &PgConnection) -> QueryResult<Vec<RankedBot>> {
+ bots::table
+ .left_join(users::table)
+ .inner_join(ratings::table)
+ .select((
+ bots::all_columns,
+ (users::id, users::username).nullable(),
+ ratings::rating,
+ ))
+ .order_by(ratings::rating.desc())
+ .get_results(db_conn)
+}
diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs
index 51d6613..28d7a76 100644
--- a/planetwars-server/src/lib.rs
+++ b/planetwars-server/src/lib.rs
@@ -8,11 +8,14 @@ pub mod routes;
pub mod schema;
pub mod util;
+use std::net::SocketAddr;
use std::ops::Deref;
use bb8::{Pool, PooledConnection};
use bb8_diesel::{self, DieselConnectionManager};
+use config::ConfigError;
use diesel::{Connection, PgConnection};
+use modules::ranking::run_ranker;
use serde::Deserialize;
use axum::{
@@ -55,16 +58,16 @@ pub async fn seed_simplebot(pool: &ConnectionPool) {
});
}
-pub async fn prepare_db(database_url: &str) -> Pool<DieselConnectionManager<PgConnection>> {
+pub type DbPool = Pool<DieselConnectionManager<PgConnection>>;
+
+pub async fn prepare_db(database_url: &str) -> DbPool {
let manager = DieselConnectionManager::<PgConnection>::new(database_url);
let pool = bb8::Pool::builder().build(manager).await.unwrap();
seed_simplebot(&pool).await;
pool
}
-pub async fn api(configuration: Configuration) -> Router {
- let db_pool = prepare_db(&configuration.database_url).await;
-
+pub fn api() -> Router {
Router::new()
.route("/register", post(routes::users::register))
.route("/login", post(routes::users::login))
@@ -88,21 +91,34 @@ pub async fn api(configuration: Configuration) -> Router {
"/matches/:match_id/log",
get(routes::matches::get_match_log),
)
+ .route("/leaderboard", get(routes::bots::get_ranking))
.route("/submit_bot", post(routes::demo::submit_bot))
.route("/save_bot", post(routes::bots::save_bot))
- .layer(AddExtensionLayer::new(db_pool))
}
-pub async fn app() -> Router {
- let configuration = config::Config::builder()
+pub fn get_config() -> Result<Configuration, ConfigError> {
+ config::Config::builder()
.add_source(config::File::with_name("configuration.toml"))
.add_source(config::Environment::with_prefix("PLANETWARS"))
- .build()
- .unwrap()
+ .build()?
.try_deserialize()
- .unwrap();
- let api = api(configuration).await;
- Router::new().nest("/api", api)
+}
+
+pub async fn run_app() {
+ let configuration = get_config().unwrap();
+ let db_pool = prepare_db(&configuration.database_url).await;
+
+ tokio::spawn(run_ranker(db_pool.clone()));
+
+ let api_service = Router::new()
+ .nest("/api", api())
+ .layer(AddExtensionLayer::new(db_pool))
+ .into_make_service();
+
+ // TODO: put in config
+ let addr = SocketAddr::from(([127, 0, 0, 1], 9000));
+
+ axum::Server::bind(&addr).serve(api_service).await.unwrap();
}
#[derive(Deserialize)]
diff --git a/planetwars-server/src/main.rs b/planetwars-server/src/main.rs
index 9bd283e..9ed77b3 100644
--- a/planetwars-server/src/main.rs
+++ b/planetwars-server/src/main.rs
@@ -1,16 +1,7 @@
-use std::net::SocketAddr;
-
extern crate planetwars_server;
extern crate tokio;
#[tokio::main]
async fn main() {
- let app = planetwars_server::app().await;
-
- let addr = SocketAddr::from(([127, 0, 0, 1], 9000));
-
- axum::Server::bind(&addr)
- .serve(app.into_make_service())
- .await
- .unwrap();
+ planetwars_server::run_app().await;
}
diff --git a/planetwars-server/src/modules/matches.rs b/planetwars-server/src/modules/matches.rs
new file mode 100644
index 0000000..201c6d4
--- /dev/null
+++ b/planetwars-server/src/modules/matches.rs
@@ -0,0 +1,102 @@
+use std::path::PathBuf;
+
+use diesel::{PgConnection, QueryResult};
+use planetwars_matchrunner::{self as runner, docker_runner::DockerBotSpec, BotSpec, MatchConfig};
+use runner::MatchOutcome;
+use tokio::task::JoinHandle;
+
+use crate::{
+ db::{self, matches::MatchData},
+ util::gen_alphanumeric,
+ ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR,
+};
+
+const PYTHON_IMAGE: &str = "python:3.10-slim-buster";
+
+pub struct RunMatch<'a> {
+ log_file_name: String,
+ player_code_bundles: Vec<&'a db::bots::CodeBundle>,
+ match_id: Option<i32>,
+}
+
+impl<'a> RunMatch<'a> {
+ pub fn from_players(player_code_bundles: Vec<&'a db::bots::CodeBundle>) -> Self {
+ let log_file_name = format!("{}.log", gen_alphanumeric(16));
+ RunMatch {
+ log_file_name,
+ player_code_bundles,
+ match_id: None,
+ }
+ }
+
+ pub fn runner_config(&self) -> runner::MatchConfig {
+ runner::MatchConfig {
+ map_path: PathBuf::from(MAPS_DIR).join("hex.json"),
+ map_name: "hex".to_string(),
+ log_path: PathBuf::from(MATCHES_DIR).join(&self.log_file_name),
+ players: self
+ .player_code_bundles
+ .iter()
+ .map(|b| runner::MatchPlayer {
+ bot_spec: code_bundle_to_botspec(b),
+ })
+ .collect(),
+ }
+ }
+
+ pub fn store_in_database(&mut self, db_conn: &PgConnection) -> QueryResult<MatchData> {
+ // don't store the same match twice
+ assert!(self.match_id.is_none());
+
+ let new_match_data = db::matches::NewMatch {
+ state: db::matches::MatchState::Playing,
+ log_path: &self.log_file_name,
+ };
+ let new_match_players = self
+ .player_code_bundles
+ .iter()
+ .map(|b| db::matches::MatchPlayerData {
+ code_bundle_id: b.id,
+ })
+ .collect::<Vec<_>>();
+
+ let match_data = db::matches::create_match(&new_match_data, &new_match_players, &db_conn)?;
+ self.match_id = Some(match_data.base.id);
+ Ok(match_data)
+ }
+
+ pub fn spawn(self, pool: ConnectionPool) -> JoinHandle<MatchOutcome> {
+ let match_id = self.match_id.expect("match must be saved before running");
+ let runner_config = self.runner_config();
+ tokio::spawn(run_match_task(pool, runner_config, match_id))
+ }
+}
+
+pub 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()],
+ })
+}
+
+async fn run_match_task(
+ connection_pool: ConnectionPool,
+ match_config: MatchConfig,
+ match_id: i32,
+) -> MatchOutcome {
+ let outcome = runner::run_match(match_config).await;
+
+ // update match state in database
+ let conn = connection_pool
+ .get()
+ .await
+ .expect("could not get database connection");
+
+ db::matches::set_match_state(match_id, db::matches::MatchState::Finished, &conn)
+ .expect("could not update match state");
+
+ return outcome;
+}
diff --git a/planetwars-server/src/modules/mod.rs b/planetwars-server/src/modules/mod.rs
index 57c1ef5..bea28e0 100644
--- a/planetwars-server/src/modules/mod.rs
+++ b/planetwars-server/src/modules/mod.rs
@@ -1,3 +1,5 @@
// This module implements general domain logic, not directly
// tied to the database or API layers.
pub mod bots;
+pub mod matches;
+pub mod ranking;
diff --git a/planetwars-server/src/modules/ranking.rs b/planetwars-server/src/modules/ranking.rs
new file mode 100644
index 0000000..cc81f0f
--- /dev/null
+++ b/planetwars-server/src/modules/ranking.rs
@@ -0,0 +1,87 @@
+use crate::{db::bots::Bot, DbPool};
+
+use crate::db;
+use crate::modules::matches::RunMatch;
+use rand::seq::SliceRandom;
+use std::time::Duration;
+use tokio;
+
+const RANKER_INTERVAL: u64 = 60;
+const START_RATING: f64 = 0.0;
+const SCALE: f64 = 100.0;
+const MAX_UPDATE: f64 = 10.0;
+
+pub async fn run_ranker(db_pool: DbPool) {
+ // TODO: make this configurable
+ // play at most one match every n seconds
+ let mut interval = tokio::time::interval(Duration::from_secs(RANKER_INTERVAL));
+ let db_conn = db_pool
+ .get()
+ .await
+ .expect("could not get database connection");
+ loop {
+ interval.tick().await;
+ let bots = db::bots::find_all_bots(&db_conn).unwrap();
+ if bots.len() < 2 {
+ // not enough bots to play a match
+ continue;
+ }
+ let selected_bots: Vec<Bot> = {
+ let mut rng = &mut rand::thread_rng();
+ bots.choose_multiple(&mut rng, 2).cloned().collect()
+ };
+ play_ranking_match(selected_bots, db_pool.clone()).await;
+ }
+}
+
+async fn play_ranking_match(selected_bots: Vec<Bot>, db_pool: DbPool) {
+ let db_conn = db_pool.get().await.expect("could not get db pool");
+ let mut code_bundles = Vec::new();
+ for bot in &selected_bots {
+ let code_bundle = db::bots::active_code_bundle(bot.id, &db_conn)
+ .expect("could not get active code bundle");
+ code_bundles.push(code_bundle);
+ }
+
+ let code_bundle_refs = code_bundles.iter().map(|b| b).collect::<Vec<_>>();
+
+ let mut run_match = RunMatch::from_players(code_bundle_refs);
+ run_match
+ .store_in_database(&db_conn)
+ .expect("could not store match in db");
+ let outcome = run_match
+ .spawn(db_pool.clone())
+ .await
+ .expect("running match failed");
+
+ let mut ratings = Vec::new();
+ for bot in &selected_bots {
+ let rating = db::ratings::get_rating(bot.id, &db_conn)
+ .expect("could not get bot rating")
+ .unwrap_or(START_RATING);
+ ratings.push(rating);
+ }
+
+ // simple elo rating
+
+ let scores = match outcome.winner {
+ None => vec![0.5; 2],
+ Some(player_num) => {
+ // TODO: please get rid of this offset
+ let player_ix = player_num - 1;
+ let mut scores = vec![0.0; 2];
+ scores[player_ix] = 1.0;
+ scores
+ }
+ };
+
+ for i in 0..2 {
+ let j = 1 - i;
+
+ let scaled_difference = (ratings[j] - ratings[i]) / SCALE;
+ let expected = 1.0 / (1.0 + 10f64.powf(scaled_difference));
+ let new_rating = ratings[i] + MAX_UPDATE * (scores[i] - expected);
+ db::ratings::set_rating(selected_bots[i].id, new_rating, &db_conn)
+ .expect("could not update bot rating");
+ }
+}
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