aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/db
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/db
parent643c0e7706ab927ef270e4a5b62ada0c38b651b9 (diff)
parentfadcda850332f8adb0a4382da9f04f78db3f6d1a (diff)
downloadplanetwars.dev-80c60ac69c9b0d86a4536eeac82cf266eb4430bc.tar.xz
planetwars.dev-80c60ac69c9b0d86a4536eeac82cf266eb4430bc.zip
Merge branch 'feature/leaderboard'
Diffstat (limited to 'planetwars-server/src/db')
-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
3 files changed, 56 insertions, 1 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)
+}