diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-10-12 21:08:00 +0200 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-10-12 21:08:00 +0200 |
commit | ed016773b112460ebbf0ff023b0915545229ed41 (patch) | |
tree | 0d98d2af735fd01c3e0634c68e46490e34bacd99 /planetwars-server/src | |
parent | 19b9a6ea1b8a36ae2301ffbc95cf2f54bf7fa77f (diff) | |
download | planetwars.dev-ed016773b112460ebbf0ff023b0915545229ed41.tar.xz planetwars.dev-ed016773b112460ebbf0ff023b0915545229ed41.zip |
filter matches for outcome
Diffstat (limited to 'planetwars-server/src')
-rw-r--r-- | planetwars-server/src/db/matches.rs | 24 | ||||
-rw-r--r-- | planetwars-server/src/routes/matches.rs | 12 |
2 files changed, 33 insertions, 3 deletions
diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index 5e0c5ad..1dded43 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -9,6 +9,7 @@ use diesel::{ BelongingToDsl, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl, RunQueryDsl, }; use diesel::{Connection, GroupedBy, PgConnection, QueryResult}; +use serde::{Deserialize, Serialize}; use std::collections::{HashMap, HashSet}; use crate::schema::{bot_versions, bots, maps, match_players, matches}; @@ -151,6 +152,14 @@ pub fn list_matches(amount: i64, conn: &PgConnection) -> QueryResult<Vec<FullMat }) } +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum BotMatchOutcome { + Win, + Loss, + Tie, +} + pub fn list_public_matches( amount: i64, before: Option<NaiveDateTime>, @@ -172,12 +181,13 @@ pub fn list_public_matches( pub fn list_bot_matches( bot_id: i32, + outcome: Option<BotMatchOutcome>, amount: i64, before: Option<NaiveDateTime>, after: Option<NaiveDateTime>, conn: &PgConnection, ) -> QueryResult<Vec<FullMatchData>> { - let query = matches::table + let mut query = matches::table .filter(matches::state.eq(MatchState::Finished)) .filter(matches::is_public.eq(true)) .order_by(matches::created_at.desc()) @@ -189,6 +199,18 @@ pub fn list_bot_matches( .select(matches::all_columns) .into_boxed(); + if let Some(outcome) = outcome { + query = match outcome { + BotMatchOutcome::Win => { + query.filter(matches::winner.eq(match_players::player_id.nullable())) + } + BotMatchOutcome::Loss => { + query.filter(matches::winner.ne(match_players::player_id.nullable())) + } + BotMatchOutcome::Tie => query.filter(matches::winner.is_null()), + }; + } + let matches = select_matches_page(query, amount, before, after).get_results::<MatchBase>(conn)?; fetch_full_match_data(matches, conn) diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index 10b4507..1d7403c 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -10,7 +10,7 @@ use std::{path::PathBuf, sync::Arc}; use crate::{ db::{ self, - matches::{self, MatchState}, + matches::{self, BotMatchOutcome, MatchState}, }, DatabaseConnection, GlobalConfig, }; @@ -42,6 +42,7 @@ pub struct ListRecentMatchesParams { after: Option<NaiveDateTime>, bot: Option<String>, + outcome: Option<BotMatchOutcome>, } const MAX_NUM_RETURNED_MATCHES: usize = 100; @@ -69,7 +70,14 @@ pub async fn list_recent_matches( Some(bot_name) => { let bot = db::bots::find_bot_by_name(&bot_name, &conn) .map_err(|_| StatusCode::BAD_REQUEST)?; - matches::list_bot_matches(bot.id, count, params.before, params.after, &conn) + matches::list_bot_matches( + bot.id, + params.outcome, + count, + params.before, + params.after, + &conn, + ) } None => matches::list_public_matches(count, params.before, params.after, &conn), }; |