add map info to matches

This commit is contained in:
Ilion Beyst 2022-08-25 20:47:00 +02:00
parent f727613efb
commit 624fa99fad
2 changed files with 25 additions and 2 deletions

View file

@ -8,10 +8,12 @@ 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 std::collections::{HashMap, HashSet};
use crate::schema::{bot_versions, bots, match_players, matches}; use crate::schema::{bot_versions, bots, maps, match_players, matches};
use super::bots::{Bot, BotVersion}; use super::bots::{Bot, BotVersion};
use super::maps::Map;
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "matches"] #[table_name = "matches"]
@ -98,6 +100,15 @@ fn fetch_full_match_data(
matches: Vec<MatchBase>, matches: Vec<MatchBase>,
conn: &PgConnection, conn: &PgConnection,
) -> QueryResult<Vec<FullMatchData>> { ) -> QueryResult<Vec<FullMatchData>> {
let map_ids: HashSet<i32> = matches.iter().filter_map(|m| m.map_id).collect();
let maps_by_id: HashMap<i32, Map> = maps::table
.filter(maps::id.eq_any(map_ids))
.load::<Map>(conn)?
.into_iter()
.map(|m| (m.id, m))
.collect();
let match_players = MatchPlayer::belonging_to(&matches) let match_players = MatchPlayer::belonging_to(&matches)
.left_join( .left_join(
bot_versions::table.on(match_players::bot_version_id.eq(bot_versions::id.nullable())), bot_versions::table.on(match_players::bot_version_id.eq(bot_versions::id.nullable())),
@ -114,8 +125,11 @@ fn fetch_full_match_data(
.into_iter() .into_iter()
.zip(match_players.into_iter()) .zip(match_players.into_iter())
.map(|(base, players)| FullMatchData { .map(|(base, players)| FullMatchData {
base,
match_players: players.into_iter().collect(), match_players: players.into_iter().collect(),
map: base
.map_id
.and_then(|map_id| maps_by_id.get(&map_id).cloned()),
base,
}) })
.collect(); .collect();
@ -204,6 +218,7 @@ where
// TODO: maybe unify this with matchdata? // TODO: maybe unify this with matchdata?
pub struct FullMatchData { pub struct FullMatchData {
pub base: MatchBase, pub base: MatchBase,
pub map: Option<Map>,
pub match_players: Vec<FullMatchPlayerData>, pub match_players: Vec<FullMatchPlayerData>,
} }
@ -232,6 +247,11 @@ pub fn find_match(id: i32, conn: &PgConnection) -> QueryResult<FullMatchData> {
conn.transaction(|| { conn.transaction(|| {
let match_base = matches::table.find(id).get_result::<MatchBase>(conn)?; let match_base = matches::table.find(id).get_result::<MatchBase>(conn)?;
let map = match match_base.map_id {
None => None,
Some(map_id) => Some(super::maps::find_map(map_id, conn)?),
};
let match_players = MatchPlayer::belonging_to(&match_base) let match_players = MatchPlayer::belonging_to(&match_base)
.left_join( .left_join(
bot_versions::table bot_versions::table
@ -244,6 +264,7 @@ pub fn find_match(id: i32, conn: &PgConnection) -> QueryResult<FullMatchData> {
let res = FullMatchData { let res = FullMatchData {
base: match_base, base: match_base,
match_players, match_players,
map,
}; };
Ok(res) Ok(res)

View file

@ -82,6 +82,8 @@ pub async fn submit_bot(
bot: Some(opponent_bot), bot: Some(opponent_bot),
}, },
], ],
// TODO!
map: None,
}; };
let api_match = super::matches::match_data_to_api(full_match_data); let api_match = super::matches::match_data_to_api(full_match_data);