From ed016773b112460ebbf0ff023b0915545229ed41 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 12 Oct 2022 21:08:00 +0200 Subject: filter matches for outcome --- planetwars-server/src/db/matches.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'planetwars-server/src/db') 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, @@ -172,12 +181,13 @@ pub fn list_public_matches( pub fn list_bot_matches( bot_id: i32, + outcome: Option, amount: i64, before: Option, after: Option, conn: &PgConnection, ) -> QueryResult> { - 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::(conn)?; fetch_full_match_data(matches, conn) -- cgit v1.2.3