aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/db/matches.rs
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-10-13 17:45:11 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-10-13 17:45:11 +0200
commit2278ecd2584050c28e62f0f5fd8967b81d64cc5b (patch)
tree20ec3117ba9a3c65c988d1d044d7539d0ec02a12 /planetwars-server/src/db/matches.rs
parentce07419bbcff91631ade273cbfbed9808b5432e8 (diff)
downloadplanetwars.dev-2278ecd2584050c28e62f0f5fd8967b81d64cc5b.tar.xz
planetwars.dev-2278ecd2584050c28e62f0f5fd8967b81d64cc5b.zip
implement ListBotMatches, allow querying matches by bot/opponent pair
Diffstat (limited to 'planetwars-server/src/db/matches.rs')
-rw-r--r--planetwars-server/src/db/matches.rs35
1 files changed, 12 insertions, 23 deletions
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<i32>,
outcome: Option<BotMatchOutcome>,
amount: i64,
before: Option<NaiveDateTime>,
after: Option<NaiveDateTime>,
conn: &mut PgConnection,
) -> QueryResult<Vec<FullMatchData>> {
- 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::<MatchBase>(conn)?;
+ let lbm = ListBotMatches {
+ bot_id,
+ opponent_id,
+ outcome,
+ before,
+ after,
+ amount,
+ };
+
+ let matches = lbm.get_results::<MatchBase>(conn)?;
fetch_full_match_data(matches, conn)
}