From e8dbb019337287040891231701b7e50593c37187 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sat, 30 Jul 2022 19:49:08 +0200 Subject: list only public matches in API --- planetwars-server/src/db/matches.rs | 64 +++++++++++++++++++++------------ planetwars-server/src/lib.rs | 2 +- planetwars-server/src/routes/matches.rs | 6 ++-- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index be2a8be..c884a63 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -89,6 +89,36 @@ pub struct MatchData { pub match_players: Vec, } +/// Add player information to MatchBase instances +fn fetch_full_match_data( + matches: Vec, + conn: &PgConnection, +) -> QueryResult> { + let match_players = MatchPlayer::belonging_to(&matches) + .left_join( + bot_versions::table.on(match_players::bot_version_id.eq(bot_versions::id.nullable())), + ) + .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable()))) + .order_by(( + match_players::match_id.asc(), + match_players::player_id.asc(), + )) + .load::(conn)? + .grouped_by(&matches); + + let res = matches + .into_iter() + .zip(match_players.into_iter()) + .map(|(base, players)| FullMatchData { + base, + match_players: players.into_iter().collect(), + }) + .collect(); + + Ok(res) +} + +// TODO: this method should disappear pub fn list_matches(amount: i64, conn: &PgConnection) -> QueryResult> { conn.transaction(|| { let matches = matches::table @@ -96,29 +126,19 @@ pub fn list_matches(amount: i64, conn: &PgConnection) -> QueryResult(conn)?; - let match_players = MatchPlayer::belonging_to(&matches) - .left_join( - bot_versions::table - .on(match_players::bot_version_id.eq(bot_versions::id.nullable())), - ) - .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable()))) - .order_by(( - match_players::match_id.asc(), - match_players::player_id.asc(), - )) - .load::(conn)? - .grouped_by(&matches); - - let res = matches - .into_iter() - .zip(match_players.into_iter()) - .map(|(base, players)| FullMatchData { - base, - match_players: players.into_iter().collect(), - }) - .collect(); + fetch_full_match_data(matches, conn) + }) +} - Ok(res) +pub fn list_public_matches(amount: i64, conn: &PgConnection) -> QueryResult> { + conn.transaction(|| { + let matches = matches::table + .filter(matches::is_public.eq(true)) + .order_by(matches::created_at.desc()) + .limit(amount) + .get_results::(conn)?; + + fetch_full_match_data(matches, conn) }) } diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs index e16b232..9983dda 100644 --- a/planetwars-server/src/lib.rs +++ b/planetwars-server/src/lib.rs @@ -129,7 +129,7 @@ pub fn api() -> Router { "/bots/:bot_name/upload", post(routes::bots::upload_code_multipart), ) - .route("/matches", get(routes::matches::list_matches)) + .route("/matches", get(routes::matches::list_public_matches)) .route("/matches/:match_id", get(routes::matches::get_match_data)) .route( "/matches/:match_id/log", diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index 58ca478..0a4b56a 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -23,8 +23,10 @@ pub struct ApiMatchPlayer { bot_name: Option, } -pub async fn list_matches(conn: DatabaseConnection) -> Result>, StatusCode> { - matches::list_matches(100, &conn) +pub async fn list_public_matches( + conn: DatabaseConnection, +) -> Result>, StatusCode> { + matches::list_public_matches(100, &conn) .map_err(|_| StatusCode::BAD_REQUEST) .map(|matches| Json(matches.into_iter().map(match_data_to_api).collect())) } -- cgit v1.2.3