list matches for a specific bot

This commit is contained in:
Ilion Beyst 2022-08-04 21:48:25 +02:00
parent 3c2f4977e4
commit 3113f762d8
2 changed files with 47 additions and 3 deletions

View file

@ -142,6 +142,36 @@ pub fn list_public_matches(amount: i64, conn: &PgConnection) -> QueryResult<Vec<
})
}
pub fn list_bot_matches(
bot_id: i32,
amount: i64,
conn: &PgConnection,
) -> QueryResult<Vec<FullMatchData>> {
conn.transaction(|| {
let matches = matches::table
.filter(matches::is_public.eq(true))
.order_by(matches::created_at.desc())
.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))
.limit(amount)
.select((
matches::id,
matches::state,
matches::log_path,
matches::created_at,
matches::winner,
matches::is_public,
))
.get_results::<MatchBase>(conn)?;
fetch_full_match_data(matches, conn)
})
}
// TODO: maybe unify this with matchdata?
pub struct FullMatchData {
pub base: MatchBase,

View file

@ -8,7 +8,10 @@ use serde::{Deserialize, Serialize};
use std::{path::PathBuf, sync::Arc};
use crate::{
db::matches::{self, MatchState},
db::{
self,
matches::{self, MatchState},
},
DatabaseConnection, GlobalConfig,
};
@ -35,6 +38,8 @@ pub struct ListRecentMatchesParams {
// TODO: implement these
before: Option<NaiveDateTime>,
after: Option<NaiveDateTime>,
bot: Option<String>,
}
const MAX_NUM_RETURNED_MATCHES: usize = 100;
@ -47,9 +52,18 @@ pub async fn list_recent_matches(
let count = std::cmp::min(
params.count.unwrap_or(DEFAULT_NUM_RETURNED_MATCHES),
MAX_NUM_RETURNED_MATCHES,
);
) as i64;
matches::list_public_matches(count as i64, &conn)
let matches = match params.bot {
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, &conn)
}
None => matches::list_public_matches(count, &conn),
};
matches
.map_err(|_| StatusCode::BAD_REQUEST)
.map(|matches| Json(matches.into_iter().map(match_data_to_api).collect()))
}