aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/modules/ranking.rs
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-04-28 21:31:49 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-04-28 21:31:49 +0200
commit7b142554d808a494df4ba9e616c58861370ccd93 (patch)
tree91f63584dc5405b57926a8daeaee3c9c49b3234c /planetwars-server/src/modules/ranking.rs
parente7da88c6a5135e78acea3a29040cfbf1e7e0b71f (diff)
downloadplanetwars.dev-7b142554d808a494df4ba9e616c58861370ccd93.tar.xz
planetwars.dev-7b142554d808a494df4ba9e616c58861370ccd93.zip
move match running logic to separate module
Diffstat (limited to 'planetwars-server/src/modules/ranking.rs')
-rw-r--r--planetwars-server/src/modules/ranking.rs33
1 files changed, 30 insertions, 3 deletions
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<Bot> = {
+ 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<Bot>, db_pool: DbPool) {}