From e7da88c6a5135e78acea3a29040cfbf1e7e0b71f Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 27 Apr 2022 20:43:12 +0200 Subject: restructure server entrypoint for ranker --- planetwars-server/src/lib.rs | 39 ++++++++++++++++++++++---------- planetwars-server/src/main.rs | 11 +-------- planetwars-server/src/modules/mod.rs | 1 + planetwars-server/src/modules/ranking.rs | 5 ++++ 4 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 planetwars-server/src/modules/ranking.rs (limited to 'planetwars-server/src') diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs index 51d6613..3f6caa9 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> { +pub type DbPool = Pool>; + +pub async fn prepare_db(database_url: &str) -> DbPool { let manager = DieselConnectionManager::::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)) @@ -90,19 +93,31 @@ pub async fn api(configuration: Configuration) -> Router { ) .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 { + 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/mod.rs b/planetwars-server/src/modules/mod.rs index 57c1ef5..2efce4e 100644 --- a/planetwars-server/src/modules/mod.rs +++ b/planetwars-server/src/modules/mod.rs @@ -1,3 +1,4 @@ // This module implements general domain logic, not directly // tied to the database or API layers. pub mod bots; +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..739e6a6 --- /dev/null +++ b/planetwars-server/src/modules/ranking.rs @@ -0,0 +1,5 @@ +use crate::DbPool; + +pub async fn run_ranker(_db_pool: DbPool) { + // do nothing, for now +} -- cgit v1.2.3 From 7b142554d808a494df4ba9e616c58861370ccd93 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Thu, 28 Apr 2022 21:31:49 +0200 Subject: move match running logic to separate module --- planetwars-server/src/db/bots.rs | 2 +- planetwars-server/src/modules/matches.rs | 102 +++++++++++++++++++++++++++++++ planetwars-server/src/modules/mod.rs | 1 + planetwars-server/src/modules/ranking.rs | 33 +++++++++- planetwars-server/src/routes/demo.rs | 79 +++--------------------- planetwars-server/src/routes/matches.rs | 1 - 6 files changed, 144 insertions(+), 74 deletions(-) create mode 100644 planetwars-server/src/modules/matches.rs (limited to 'planetwars-server/src') 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, 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, +} + +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 { + // 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::>(); + + 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 { + 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 { + 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 2efce4e..bea28e0 100644 --- a/planetwars-server/src/modules/mod.rs +++ b/planetwars-server/src/modules/mod.rs @@ -1,4 +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 index 739e6a6..53e1e4e 100644 --- a/planetwars-server/src/modules/ranking.rs +++ b/planetwars-server/src/modules/ranking.rs @@ -1,5 +1,32 @@ -use crate::DbPool; +use crate::{db::bots::Bot, DbPool}; -pub async fn run_ranker(_db_pool: DbPool) { - // do nothing, for now +use crate::db; +use diesel::PgConnection; +use rand::seq::SliceRandom; +use std::time::Duration; +use tokio; + +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(10)); + 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 = { + let mut rng = &mut rand::thread_rng(); + bots.choose_multiple(&mut rng, 2).cloned().collect() + }; + play_match(selected_bots, db_pool.clone()).await; + } } + +async fn play_match(selected_bots: Vec, db_pool: DbPool) {} 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 { - 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(), -- cgit v1.2.3 From a7d56ba0f54273920743109fe1a6541030f3c003 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Mon, 9 May 2022 19:41:33 +0200 Subject: add ratings table --- planetwars-server/src/db/mod.rs | 1 + planetwars-server/src/db/ratings.rs | 27 +++++++++++++++++++++++++++ planetwars-server/src/schema.rs | 21 ++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 planetwars-server/src/db/ratings.rs (limited to 'planetwars-server/src') 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..bee8548 --- /dev/null +++ b/planetwars-server/src/db/ratings.rs @@ -0,0 +1,27 @@ +use diesel::{prelude::*, PgConnection, QueryResult}; +use serde::{Deserialize, Serialize}; + +use crate::schema::{bots, ratings}; + +#[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> { + 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 { + 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) +} diff --git a/planetwars-server/src/schema.rs b/planetwars-server/src/schema.rs index ae2e60c..812e05e 100644 --- a/planetwars-server/src/schema.rs +++ b/planetwars-server/src/schema.rs @@ -47,6 +47,16 @@ table! { } } +table! { + use diesel::sql_types::*; + use crate::db_types::*; + + ratings (bot_id) { + bot_id -> Int4, + rating -> Float8, + } +} + table! { use diesel::sql_types::*; use crate::db_types::*; @@ -74,6 +84,15 @@ joinable!(bots -> users (owner_id)); joinable!(code_bundles -> bots (bot_id)); joinable!(match_players -> code_bundles (code_bundle_id)); joinable!(match_players -> matches (match_id)); +joinable!(ratings -> bots (bot_id)); joinable!(sessions -> users (user_id)); -allow_tables_to_appear_in_same_query!(bots, code_bundles, match_players, matches, sessions, users,); +allow_tables_to_appear_in_same_query!( + bots, + code_bundles, + match_players, + matches, + ratings, + sessions, + users, +); -- cgit v1.2.3 From 30de8107b499741808150db61abecd623bf1581b Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 17 May 2022 21:13:29 +0200 Subject: implement leaderboard endpoint --- planetwars-server/src/db/ratings.rs | 29 ++++++++++++++++++++++++++++- planetwars-server/src/lib.rs | 1 + planetwars-server/src/routes/bots.rs | 7 +++++++ 3 files changed, 36 insertions(+), 1 deletion(-) (limited to 'planetwars-server/src') diff --git a/planetwars-server/src/db/ratings.rs b/planetwars-server/src/db/ratings.rs index bee8548..8262fed 100644 --- a/planetwars-server/src/db/ratings.rs +++ b/planetwars-server/src/db/ratings.rs @@ -1,7 +1,8 @@ use diesel::{prelude::*, PgConnection, QueryResult}; use serde::{Deserialize, Serialize}; -use crate::schema::{bots, ratings}; +use crate::db::bots::Bot; +use crate::schema::{bots, ratings, users}; #[derive(Queryable, Debug, Insertable, PartialEq, Serialize, Deserialize)] pub struct Rating { @@ -25,3 +26,29 @@ pub fn set_rating(bot_id: i32, rating: f64, db_conn: &PgConnection) -> QueryResu .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, + pub rating: f64, +} + +pub fn get_bot_ranking(db_conn: &PgConnection) -> QueryResult> { + 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 3f6caa9..28d7a76 100644 --- a/planetwars-server/src/lib.rs +++ b/planetwars-server/src/lib.rs @@ -91,6 +91,7 @@ pub fn api() -> 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)) } 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>, Statu .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR) } +pub async fn get_ranking(conn: DatabaseConnection) -> Result>, 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, -- cgit v1.2.3 From 06ac7a370ce763c2f997b9606ca880bf3782f926 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 17 May 2022 21:57:44 +0200 Subject: implement simple elo rating --- planetwars-server/src/modules/ranking.rs | 63 ++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 4 deletions(-) (limited to 'planetwars-server/src') diff --git a/planetwars-server/src/modules/ranking.rs b/planetwars-server/src/modules/ranking.rs index 53e1e4e..3892383 100644 --- a/planetwars-server/src/modules/ranking.rs +++ b/planetwars-server/src/modules/ranking.rs @@ -1,15 +1,20 @@ use crate::{db::bots::Bot, DbPool}; use crate::db; -use diesel::PgConnection; +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 = 5.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(10)); + let mut interval = tokio::time::interval(Duration::from_secs(RANKER_INTERVAL)); let db_conn = db_pool .get() .await @@ -25,8 +30,58 @@ pub async fn run_ranker(db_pool: DbPool) { let mut rng = &mut rand::thread_rng(); bots.choose_multiple(&mut rng, 2).cloned().collect() }; - play_match(selected_bots, db_pool.clone()).await; + play_ranking_match(selected_bots, db_pool.clone()).await; } } -async fn play_match(selected_bots: Vec, db_pool: DbPool) {} +async fn play_ranking_match(selected_bots: Vec, 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::>(); + + 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[i] - ratings[j]) / 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"); + } +} -- cgit v1.2.3 From fadcda850332f8adb0a4382da9f04f78db3f6d1a Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sat, 28 May 2022 11:22:00 +0200 Subject: fix order of arguments in ranker --- planetwars-server/src/modules/ranking.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'planetwars-server/src') diff --git a/planetwars-server/src/modules/ranking.rs b/planetwars-server/src/modules/ranking.rs index 3892383..cc81f0f 100644 --- a/planetwars-server/src/modules/ranking.rs +++ b/planetwars-server/src/modules/ranking.rs @@ -9,7 +9,7 @@ use tokio; const RANKER_INTERVAL: u64 = 60; const START_RATING: f64 = 0.0; const SCALE: f64 = 100.0; -const MAX_UPDATE: f64 = 5.0; +const MAX_UPDATE: f64 = 10.0; pub async fn run_ranker(db_pool: DbPool) { // TODO: make this configurable @@ -78,7 +78,7 @@ async fn play_ranking_match(selected_bots: Vec, db_pool: DbPool) { for i in 0..2 { let j = 1 - i; - let scaled_difference = (ratings[i] - ratings[j]) / SCALE; + 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) -- cgit v1.2.3