From 450d22758ec169ece8f71bc6bd6c79603c136874 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 27 Apr 2022 20:08:48 +0200 Subject: [PATCH 1/8] return winner when running a match --- planetwars-matchrunner/src/lib.rs | 18 ++++++++++++++++-- planetwars-matchrunner/src/pw_match.rs | 6 +++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/planetwars-matchrunner/src/lib.rs b/planetwars-matchrunner/src/lib.rs index b7a9e53..1f1a4c7 100644 --- a/planetwars-matchrunner/src/lib.rs +++ b/planetwars-matchrunner/src/lib.rs @@ -52,7 +52,11 @@ pub trait BotSpec: Send + Sync { ) -> Box; } -pub async fn run_match(config: MatchConfig) { +pub struct MatchOutcome { + pub winner: Option, +} + +pub async fn run_match(config: MatchConfig) -> MatchOutcome { let pw_config = PwConfig { map_file: config.map_path, max_turns: 100, @@ -103,8 +107,18 @@ pub async fn run_match(config: MatchConfig) { // ) // .unwrap(); - let match_state = pw_match::PwMatch::create(match_ctx, pw_config); + let mut match_state = pw_match::PwMatch::create(match_ctx, pw_config); match_state.run().await; + + let final_state = match_state.match_state.state(); + let survivors = final_state.living_players(); + let winner = if survivors.len() == 1 { + Some(survivors[0]) + } else { + None + }; + + MatchOutcome { winner } } // writing this as a closure causes lifetime inference errors diff --git a/planetwars-matchrunner/src/pw_match.rs b/planetwars-matchrunner/src/pw_match.rs index c9a7f7b..f5b803c 100644 --- a/planetwars-matchrunner/src/pw_match.rs +++ b/planetwars-matchrunner/src/pw_match.rs @@ -24,8 +24,8 @@ pub struct MatchConfig { } pub struct PwMatch { - match_ctx: MatchCtx, - match_state: PlanetWars, + pub match_ctx: MatchCtx, + pub match_state: PlanetWars, } impl PwMatch { @@ -39,7 +39,7 @@ impl PwMatch { } } - pub async fn run(mut self) { + pub async fn run(&mut self) { while !self.match_state.is_finished() { let player_messages = self.prompt_players().await; From e7da88c6a5135e78acea3a29040cfbf1e7e0b71f Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 27 Apr 2022 20:43:12 +0200 Subject: [PATCH 2/8] 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 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 +} From 7b142554d808a494df4ba9e616c58861370ccd93 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Thu, 28 Apr 2022 21:31:49 +0200 Subject: [PATCH 3/8] move match running logic to separate module --- planetwars-matchrunner/src/lib.rs | 1 - 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 - 7 files changed, 144 insertions(+), 75 deletions(-) create mode 100644 planetwars-server/src/modules/matches.rs diff --git a/planetwars-matchrunner/src/lib.rs b/planetwars-matchrunner/src/lib.rs index 1f1a4c7..5aff793 100644 --- a/planetwars-matchrunner/src/lib.rs +++ b/planetwars-matchrunner/src/lib.rs @@ -38,7 +38,6 @@ pub struct PlayerInfo { } pub struct MatchPlayer { - pub name: String, pub bot_spec: Box, } 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(), From a7d56ba0f54273920743109fe1a6541030f3c003 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Mon, 9 May 2022 19:41:33 +0200 Subject: [PATCH 4/8] add ratings table --- .../2022-04-28-171349_create_rating/down.sql | 2 ++ .../2022-04-28-171349_create_rating/up.sql | 7 +++++ planetwars-server/src/db/mod.rs | 1 + planetwars-server/src/db/ratings.rs | 27 +++++++++++++++++++ planetwars-server/src/schema.rs | 21 ++++++++++++++- 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 planetwars-server/migrations/2022-04-28-171349_create_rating/down.sql create mode 100644 planetwars-server/migrations/2022-04-28-171349_create_rating/up.sql create mode 100644 planetwars-server/src/db/ratings.rs diff --git a/planetwars-server/migrations/2022-04-28-171349_create_rating/down.sql b/planetwars-server/migrations/2022-04-28-171349_create_rating/down.sql new file mode 100644 index 0000000..8267412 --- /dev/null +++ b/planetwars-server/migrations/2022-04-28-171349_create_rating/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +drop table ratings; \ No newline at end of file diff --git a/planetwars-server/migrations/2022-04-28-171349_create_rating/up.sql b/planetwars-server/migrations/2022-04-28-171349_create_rating/up.sql new file mode 100644 index 0000000..39fe6fe --- /dev/null +++ b/planetwars-server/migrations/2022-04-28-171349_create_rating/up.sql @@ -0,0 +1,7 @@ +-- Your SQL goes here +-- this table could later be expanded to include more information, +-- such as rating state (eg. number of matches played) or scope (eg. map) +create table ratings ( + bot_id integer PRIMARY KEY REFERENCES bots(id), + rating float NOT NULL +) \ No newline at end of file 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, +); From 30de8107b499741808150db61abecd623bf1581b Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 17 May 2022 21:13:29 +0200 Subject: [PATCH 5/8] 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(-) 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, From 17d29c5397f5b2a39d76587722d20467a53014a8 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 18 May 2022 22:03:46 +0200 Subject: [PATCH 6/8] add basic leaderboard view --- .../src/lib/components/Leaderboard.svelte | 72 +++++++++++++++++++ web/pw-server/src/routes/index.svelte | 28 ++++---- 2 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 web/pw-server/src/lib/components/Leaderboard.svelte diff --git a/web/pw-server/src/lib/components/Leaderboard.svelte b/web/pw-server/src/lib/components/Leaderboard.svelte new file mode 100644 index 0000000..75e4807 --- /dev/null +++ b/web/pw-server/src/lib/components/Leaderboard.svelte @@ -0,0 +1,72 @@ + + +
+ + + + + + + {#each leaderboard as entry, index} + + + + + + + {/each} +
+ RatingBotAuthor
{index + 1} + {formatRating(entry)} + {entry["bot"]["name"]} + {#if entry["author"]} + {entry["author"]["username"]} + {/if} +
+
+ + diff --git a/web/pw-server/src/routes/index.svelte b/web/pw-server/src/routes/index.svelte index 32efe69..243f4da 100644 --- a/web/pw-server/src/routes/index.svelte +++ b/web/pw-server/src/routes/index.svelte @@ -13,11 +13,13 @@ import SubmitPane from "$lib/components/SubmitPane.svelte"; import OutputPane from "$lib/components/OutputPane.svelte"; import RulesView from "$lib/components/RulesView.svelte"; + import Leaderboard from "$lib/components/Leaderboard.svelte"; enum ViewMode { Editor, MatchVisualizer, Rules, + Leaderboard, } let matches = []; @@ -111,10 +113,10 @@ return log; } - function selectEditor() { + function setViewMode(viewMode_: ViewMode) { selectedMatchId = undefined; selectedMatchLog = undefined; - viewMode = ViewMode.Editor; + viewMode = viewMode_; } function selectRules() { @@ -140,17 +142,24 @@ +
    {#each matches as match} @@ -175,6 +184,8 @@ {:else if viewMode === ViewMode.Rules} + {:else if viewMode === ViewMode.Leaderboard} + {/if}