allow match_player code_bundle_id to be null

This commit is contained in:
Ilion Beyst 2022-06-10 21:09:33 +02:00
parent d1977b95c8
commit 5ee66c9c9b
7 changed files with 24 additions and 13 deletions

View file

@ -0,0 +1 @@
ALTER TABLE match_players ALTER COLUMN code_bundle_id SET NOT NULL;

View file

@ -0,0 +1 @@
ALTER TABLE match_players ALTER COLUMN code_bundle_id DROP NOT NULL;

View file

@ -44,7 +44,7 @@ pub struct MatchBase {
pub struct MatchPlayer { pub struct MatchPlayer {
pub match_id: i32, pub match_id: i32,
pub player_id: i32, pub player_id: i32,
pub code_bundle_id: i32, pub code_bundle_id: Option<i32>,
} }
pub struct MatchPlayerData { pub struct MatchPlayerData {
@ -92,7 +92,10 @@ pub fn list_matches(conn: &PgConnection) -> QueryResult<Vec<FullMatchData>> {
let matches = matches::table.get_results::<MatchBase>(conn)?; let matches = matches::table.get_results::<MatchBase>(conn)?;
let match_players = MatchPlayer::belonging_to(&matches) let match_players = MatchPlayer::belonging_to(&matches)
.inner_join(code_bundles::table) .left_join(
code_bundles::table
.on(match_players::code_bundle_id.eq(code_bundles::id.nullable())),
)
.left_join(bots::table.on(code_bundles::bot_id.eq(bots::id.nullable()))) .left_join(bots::table.on(code_bundles::bot_id.eq(bots::id.nullable())))
.load::<FullMatchPlayerData>(conn)? .load::<FullMatchPlayerData>(conn)?
.grouped_by(&matches); .grouped_by(&matches);
@ -120,7 +123,7 @@ pub struct FullMatchData {
// #[primary_key(base.match_id, base::player_id)] // #[primary_key(base.match_id, base::player_id)]
pub struct FullMatchPlayerData { pub struct FullMatchPlayerData {
pub base: MatchPlayer, pub base: MatchPlayer,
pub code_bundle: CodeBundle, pub code_bundle: Option<CodeBundle>,
pub bot: Option<Bot>, pub bot: Option<Bot>,
} }
@ -142,7 +145,10 @@ pub fn find_match(id: i32, conn: &PgConnection) -> QueryResult<FullMatchData> {
let match_base = matches::table.find(id).get_result::<MatchBase>(conn)?; let match_base = matches::table.find(id).get_result::<MatchBase>(conn)?;
let match_players = MatchPlayer::belonging_to(&match_base) let match_players = MatchPlayer::belonging_to(&match_base)
.inner_join(code_bundles::table) .left_join(
code_bundles::table
.on(match_players::code_bundle_id.eq(code_bundles::id.nullable())),
)
.left_join(bots::table.on(code_bundles::bot_id.eq(bots::id.nullable()))) .left_join(bots::table.on(code_bundles::bot_id.eq(bots::id.nullable())))
.load::<FullMatchPlayerData>(conn)?; .load::<FullMatchPlayerData>(conn)?;
@ -160,14 +166,17 @@ pub fn find_match_base(id: i32, conn: &PgConnection) -> QueryResult<MatchBase> {
} }
pub enum MatchResult { pub enum MatchResult {
Finished { winner: Option<i32> } Finished { winner: Option<i32> },
} }
pub fn save_match_result(id: i32, result: MatchResult, conn: &PgConnection) -> QueryResult<()> { pub fn save_match_result(id: i32, result: MatchResult, conn: &PgConnection) -> QueryResult<()> {
let MatchResult::Finished { winner } = result; let MatchResult::Finished { winner } = result;
diesel::update(matches::table.find(id)) diesel::update(matches::table.find(id))
.set((matches::winner.eq(winner), matches::state.eq(MatchState::Finished))) .set((
matches::winner.eq(winner),
matches::state.eq(MatchState::Finished),
))
.execute(conn)?; .execute(conn)?;
Ok(()) Ok(())
} }

View file

@ -12,7 +12,7 @@ use std::path::PathBuf;
use thiserror; use thiserror;
use crate::db::bots::{self, CodeBundle}; use crate::db::bots::{self, CodeBundle};
use crate::db::ratings::{RankedBot, self}; use crate::db::ratings::{self, RankedBot};
use crate::db::users::User; use crate::db::users::User;
use crate::modules::bots::save_code_bundle; use crate::modules::bots::save_code_bundle;
use crate::{DatabaseConnection, BOTS_DIR}; use crate::{DatabaseConnection, BOTS_DIR};

View file

@ -58,12 +58,12 @@ pub async fn submit_bot(
match_players: vec![ match_players: vec![
FullMatchPlayerData { FullMatchPlayerData {
base: match_data.match_players[0].clone(), base: match_data.match_players[0].clone(),
code_bundle: player_code_bundle, code_bundle: Some(player_code_bundle),
bot: None, bot: None,
}, },
FullMatchPlayerData { FullMatchPlayerData {
base: match_data.match_players[1].clone(), base: match_data.match_players[1].clone(),
code_bundle: opponent_code_bundle, code_bundle: Some(opponent_code_bundle),
bot: Some(opponent), bot: Some(opponent),
}, },
], ],

View file

@ -107,7 +107,7 @@ pub struct ApiMatch {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct ApiMatchPlayer { pub struct ApiMatchPlayer {
code_bundle_id: i32, code_bundle_id: Option<i32>,
bot_id: Option<i32>, bot_id: Option<i32>,
bot_name: Option<String>, bot_name: Option<String>,
} }
@ -127,7 +127,7 @@ pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch {
.match_players .match_players
.iter() .iter()
.map(|_p| ApiMatchPlayer { .map(|_p| ApiMatchPlayer {
code_bundle_id: _p.code_bundle.id, code_bundle_id: _p.code_bundle.as_ref().map(|cb| cb.id),
bot_id: _p.bot.as_ref().map(|b| b.id), bot_id: _p.bot.as_ref().map(|b| b.id),
bot_name: _p.bot.as_ref().map(|b| b.name.clone()), bot_name: _p.bot.as_ref().map(|b| b.name.clone()),
}) })

View file

@ -31,7 +31,7 @@ table! {
match_players (match_id, player_id) { match_players (match_id, player_id) {
match_id -> Int4, match_id -> Int4,
player_id -> Int4, player_id -> Int4,
code_bundle_id -> Int4, code_bundle_id -> Nullable<Int4>,
} }
} }