filter matches for outcome

This commit is contained in:
Ilion Beyst 2022-10-12 21:08:00 +02:00
parent 19b9a6ea1b
commit ed016773b1
2 changed files with 33 additions and 3 deletions

View file

@ -9,6 +9,7 @@ use diesel::{
BelongingToDsl, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl, RunQueryDsl, BelongingToDsl, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl, RunQueryDsl,
}; };
use diesel::{Connection, GroupedBy, PgConnection, QueryResult}; use diesel::{Connection, GroupedBy, PgConnection, QueryResult};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use crate::schema::{bot_versions, bots, maps, match_players, matches}; 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( pub fn list_public_matches(
amount: i64, amount: i64,
before: Option<NaiveDateTime>, before: Option<NaiveDateTime>,
@ -172,12 +181,13 @@ pub fn list_public_matches(
pub fn list_bot_matches( pub fn list_bot_matches(
bot_id: i32, bot_id: i32,
outcome: Option<BotMatchOutcome>,
amount: i64, amount: i64,
before: Option<NaiveDateTime>, before: Option<NaiveDateTime>,
after: Option<NaiveDateTime>, after: Option<NaiveDateTime>,
conn: &PgConnection, conn: &PgConnection,
) -> QueryResult<Vec<FullMatchData>> { ) -> QueryResult<Vec<FullMatchData>> {
let query = matches::table let mut query = matches::table
.filter(matches::state.eq(MatchState::Finished)) .filter(matches::state.eq(MatchState::Finished))
.filter(matches::is_public.eq(true)) .filter(matches::is_public.eq(true))
.order_by(matches::created_at.desc()) .order_by(matches::created_at.desc())
@ -189,6 +199,18 @@ pub fn list_bot_matches(
.select(matches::all_columns) .select(matches::all_columns)
.into_boxed(); .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 = let matches =
select_matches_page(query, amount, before, after).get_results::<MatchBase>(conn)?; select_matches_page(query, amount, before, after).get_results::<MatchBase>(conn)?;
fetch_full_match_data(matches, conn) fetch_full_match_data(matches, conn)

View file

@ -10,7 +10,7 @@ use std::{path::PathBuf, sync::Arc};
use crate::{ use crate::{
db::{ db::{
self, self,
matches::{self, MatchState}, matches::{self, BotMatchOutcome, MatchState},
}, },
DatabaseConnection, GlobalConfig, DatabaseConnection, GlobalConfig,
}; };
@ -42,6 +42,7 @@ pub struct ListRecentMatchesParams {
after: Option<NaiveDateTime>, after: Option<NaiveDateTime>,
bot: Option<String>, bot: Option<String>,
outcome: Option<BotMatchOutcome>,
} }
const MAX_NUM_RETURNED_MATCHES: usize = 100; const MAX_NUM_RETURNED_MATCHES: usize = 100;
@ -69,7 +70,14 @@ pub async fn list_recent_matches(
Some(bot_name) => { Some(bot_name) => {
let bot = db::bots::find_bot_by_name(&bot_name, &conn) let bot = db::bots::find_bot_by_name(&bot_name, &conn)
.map_err(|_| StatusCode::BAD_REQUEST)?; .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), None => matches::list_public_matches(count, params.before, params.after, &conn),
}; };