From 523de3ba0692734237b5ed6fa6dce8fbad9d69c8 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Thu, 10 Mar 2022 23:35:42 +0100 Subject: save match players in database --- planetwars-server/src/db/matches.rs | 10 +++++----- planetwars-server/src/routes/demo.rs | 15 ++++++++++++--- planetwars-server/src/routes/matches.rs | 10 +++++++--- planetwars-server/src/schema.rs | 4 ++-- 4 files changed, 26 insertions(+), 13 deletions(-) (limited to 'planetwars-server/src') diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index 6444bf6..d649189 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -17,10 +17,10 @@ pub struct NewMatch<'a> { pub struct NewMatchPlayer { /// id of the match this player is in pub match_id: i32, - /// id of the bot behind this player - pub bot_id: i32, /// player id within the match pub player_id: i32, + /// id of the bot behind this player + pub code_bundle_id: i32, } #[derive(Queryable, Identifiable)] @@ -37,12 +37,12 @@ pub struct MatchBase { #[belongs_to(MatchBase, foreign_key = "match_id")] pub struct MatchPlayer { pub match_id: i32, - pub bot_id: i32, pub player_id: i32, + pub code_bundle_id: i32, } pub struct MatchPlayerData { - pub bot_id: i32, + pub code_bundle_id: i32, } pub fn create_match( @@ -60,8 +60,8 @@ pub fn create_match( .enumerate() .map(|(num, player_data)| NewMatchPlayer { match_id: match_base.id, - bot_id: player_data.bot_id, player_id: num as i32, + code_bundle_id: player_data.code_bundle_id, }) .collect::>(); diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 37312a7..5dbbe92 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -1,5 +1,5 @@ use crate::db; -use crate::db::matches::MatchState; +use crate::db::matches::{MatchPlayerData, MatchState}; use crate::modules::bots::save_code_bundle; use crate::util::gen_alphanumeric; use crate::{ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR}; @@ -83,9 +83,18 @@ pub async fn submit_bot( state: MatchState::Playing, log_path: &log_file_name, }; + + let new_match_players = [ + MatchPlayerData { + code_bundle_id: player_code_bundle.id, + }, + MatchPlayerData { + code_bundle_id: opponent_code_bundle.id, + }, + ]; // TODO: set match players - let match_data = - db::matches::create_match(&new_match_data, &[], &conn).expect("failed to create match"); + let match_data = db::matches::create_match(&new_match_data, &new_match_players, &conn) + .expect("failed to create match"); tokio::spawn(run_match_task( match_data.base.id, diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index f2599cd..a6d8ff2 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -62,7 +62,9 @@ pub async fn play_match( }), }); - bot_ids.push(matches::MatchPlayerData { bot_id: bot.id }); + bot_ids.push(matches::MatchPlayerData { + code_bundle_id: code_bundle.id, + }); } let match_config = MatchConfig { @@ -107,7 +109,9 @@ pub struct ApiMatch { #[derive(Serialize, Deserialize)] pub struct ApiMatchPlayer { - bot_id: i32, + // TODO! +// bot_id: i32, +// code_bundle_id: i32 } pub async fn list_matches(conn: DatabaseConnection) -> Result>, StatusCode> { @@ -124,7 +128,7 @@ pub fn match_data_to_api(data: matches::MatchData) -> ApiMatch { players: data .match_players .iter() - .map(|p| ApiMatchPlayer { bot_id: p.bot_id }) + .map(|_p| ApiMatchPlayer {}) .collect(), } } diff --git a/planetwars-server/src/schema.rs b/planetwars-server/src/schema.rs index 0ebddf3..ae2e60c 100644 --- a/planetwars-server/src/schema.rs +++ b/planetwars-server/src/schema.rs @@ -30,8 +30,8 @@ table! { match_players (match_id, player_id) { match_id -> Int4, - bot_id -> Int4, player_id -> Int4, + code_bundle_id -> Int4, } } @@ -72,7 +72,7 @@ table! { joinable!(bots -> users (owner_id)); joinable!(code_bundles -> bots (bot_id)); -joinable!(match_players -> bots (bot_id)); +joinable!(match_players -> code_bundles (code_bundle_id)); joinable!(match_players -> matches (match_id)); joinable!(sessions -> users (user_id)); -- cgit v1.2.3