From f429adb4f8607ef9b8b8e2c9fa3634c5f39a5602 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sun, 30 Oct 2022 16:20:29 +0100 Subject: allow filtering map in bot matches --- planetwars-server/src/db/match_queries.rs | 5 +++++ planetwars-server/src/db/matches.rs | 2 ++ planetwars-server/src/routes/matches.rs | 11 +++++++++++ 3 files changed, 18 insertions(+) (limited to 'planetwars-server') diff --git a/planetwars-server/src/db/match_queries.rs b/planetwars-server/src/db/match_queries.rs index f9f8140..029a657 100644 --- a/planetwars-server/src/db/match_queries.rs +++ b/planetwars-server/src/db/match_queries.rs @@ -12,6 +12,7 @@ pub struct ListBotMatches { pub outcome: Option, pub opponent_id: Option, + pub map_id: Option, // pagination options pub before: Option, @@ -55,6 +56,10 @@ impl QueryFragment for ListBotMatches { } out.push_sql(" WHERE matches.state = 'finished' AND matches.is_public = true"); + if let Some(map_id) = self.map_id.as_ref() { + out.push_sql(" AND matches.map_id = "); + out.push_bind_param::(map_id)?; + } if let Some(outcome) = self.outcome.as_ref() { match outcome { BotMatchOutcome::Win => { diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index c86f332..2aff44b 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -176,6 +176,7 @@ pub fn list_public_matches( pub fn list_bot_matches( bot_id: i32, opponent_id: Option, + map_id: Option, outcome: Option, had_errors: Option, amount: i64, @@ -188,6 +189,7 @@ pub fn list_bot_matches( outcome, had_errors, opponent_id, + map_id, before, after, amount, diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index 39c153d..1ae8c27 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -45,6 +45,7 @@ pub struct ListRecentMatchesParams { bot: Option, opponent: Option, + map: Option, had_errors: Option, outcome: Option, } @@ -72,6 +73,7 @@ pub async fn list_recent_matches( let matches_result = match params.bot { Some(bot_name) => { + // TODO: do we prefer BAD_REQUEST for invalid parameters, or do we want to return an empty response? let bot = db::bots::find_bot_by_name(&bot_name, &mut conn) .map_err(|_| StatusCode::BAD_REQUEST)?; @@ -83,9 +85,18 @@ pub async fn list_recent_matches( None }; + let map_id = if let Some(ref map_name) = params.map { + let map = db::maps::find_map_by_name(map_name, &mut conn) + .map_err(|_| StatusCode::BAD_REQUEST)?; + Some(map.id) + } else { + None + }; + matches::list_bot_matches( bot.id, opponent_id, + map_id, params.outcome, params.had_errors, count, -- cgit v1.2.3