From 2278ecd2584050c28e62f0f5fd8967b81d64cc5b Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Thu, 13 Oct 2022 17:45:11 +0200 Subject: implement ListBotMatches, allow querying matches by bot/opponent pair --- planetwars-server/src/db/matches.rs | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) (limited to 'planetwars-server/src/db/matches.rs') diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index 813f998..d628b14 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -14,6 +14,7 @@ use crate::schema::{bot_versions, bots, maps, match_players, matches}; use super::bots::{Bot, BotVersion}; use super::maps::Map; +use super::match_queries::ListBotMatches; #[derive(Insertable)] #[diesel(table_name = matches)] @@ -173,35 +174,23 @@ pub fn list_public_matches( pub fn list_bot_matches( bot_id: i32, + opponent_id: Option, outcome: Option, amount: i64, before: Option, after: Option, conn: &mut PgConnection, ) -> QueryResult> { - let mut query = finished_public_matches_query(before, after) - .inner_join(match_players::table) - .inner_join( - bot_versions::table.on(match_players::bot_version_id.eq(bot_versions::id.nullable())), - ) - .filter(bot_versions::bot_id.eq(bot_id)) - .select(matches::all_columns); - - 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()), - }; - } - - query = query.limit(amount); - - let matches = query.get_results::(conn)?; + let lbm = ListBotMatches { + bot_id, + opponent_id, + outcome, + before, + after, + amount, + }; + + let matches = lbm.get_results::(conn)?; fetch_full_match_data(matches, conn) } -- cgit v1.2.3