save match players in database

This commit is contained in:
Ilion Beyst 2022-03-10 23:35:42 +01:00
parent 95b733ba4d
commit 523de3ba06
5 changed files with 27 additions and 14 deletions

View file

@ -11,7 +11,7 @@ CREATE INDEX match_created_at ON matches(created_at);
CREATE TABLE match_players ( CREATE TABLE match_players (
match_id integer REFERENCES matches(id) NOT NULL, match_id integer REFERENCES matches(id) NOT NULL,
bot_id integer REFERENCES bots(id) NOT NULL,
player_id integer NOT NULL, player_id integer NOT NULL,
code_bundle_id integer REFERENCES code_bundles(id) NOT NULL,
PRIMARY KEY (match_id, player_id) PRIMARY KEY (match_id, player_id)
); );

View file

@ -17,10 +17,10 @@ pub struct NewMatch<'a> {
pub struct NewMatchPlayer { pub struct NewMatchPlayer {
/// id of the match this player is in /// id of the match this player is in
pub match_id: i32, pub match_id: i32,
/// id of the bot behind this player
pub bot_id: i32,
/// player id within the match /// player id within the match
pub player_id: i32, pub player_id: i32,
/// id of the bot behind this player
pub code_bundle_id: i32,
} }
#[derive(Queryable, Identifiable)] #[derive(Queryable, Identifiable)]
@ -37,12 +37,12 @@ pub struct MatchBase {
#[belongs_to(MatchBase, foreign_key = "match_id")] #[belongs_to(MatchBase, foreign_key = "match_id")]
pub struct MatchPlayer { pub struct MatchPlayer {
pub match_id: i32, pub match_id: i32,
pub bot_id: i32,
pub player_id: i32, pub player_id: i32,
pub code_bundle_id: i32,
} }
pub struct MatchPlayerData { pub struct MatchPlayerData {
pub bot_id: i32, pub code_bundle_id: i32,
} }
pub fn create_match( pub fn create_match(
@ -60,8 +60,8 @@ pub fn create_match(
.enumerate() .enumerate()
.map(|(num, player_data)| NewMatchPlayer { .map(|(num, player_data)| NewMatchPlayer {
match_id: match_base.id, match_id: match_base.id,
bot_id: player_data.bot_id,
player_id: num as i32, player_id: num as i32,
code_bundle_id: player_data.code_bundle_id,
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();

View file

@ -1,5 +1,5 @@
use crate::db; use crate::db;
use crate::db::matches::MatchState; use crate::db::matches::{MatchPlayerData, MatchState};
use crate::modules::bots::save_code_bundle; use crate::modules::bots::save_code_bundle;
use crate::util::gen_alphanumeric; use crate::util::gen_alphanumeric;
use crate::{ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR}; use crate::{ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR};
@ -83,9 +83,18 @@ pub async fn submit_bot(
state: MatchState::Playing, state: MatchState::Playing,
log_path: &log_file_name, 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 // TODO: set match players
let match_data = let match_data = db::matches::create_match(&new_match_data, &new_match_players, &conn)
db::matches::create_match(&new_match_data, &[], &conn).expect("failed to create match"); .expect("failed to create match");
tokio::spawn(run_match_task( tokio::spawn(run_match_task(
match_data.base.id, match_data.base.id,

View file

@ -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 { let match_config = MatchConfig {
@ -107,7 +109,9 @@ pub struct ApiMatch {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct ApiMatchPlayer { pub struct ApiMatchPlayer {
bot_id: i32, // TODO!
// bot_id: i32,
// code_bundle_id: i32
} }
pub async fn list_matches(conn: DatabaseConnection) -> Result<Json<Vec<ApiMatch>>, StatusCode> { pub async fn list_matches(conn: DatabaseConnection) -> Result<Json<Vec<ApiMatch>>, StatusCode> {
@ -124,7 +128,7 @@ pub fn match_data_to_api(data: matches::MatchData) -> ApiMatch {
players: data players: data
.match_players .match_players
.iter() .iter()
.map(|p| ApiMatchPlayer { bot_id: p.bot_id }) .map(|_p| ApiMatchPlayer {})
.collect(), .collect(),
} }
} }

View file

@ -30,8 +30,8 @@ table! {
match_players (match_id, player_id) { match_players (match_id, player_id) {
match_id -> Int4, match_id -> Int4,
bot_id -> Int4,
player_id -> Int4, player_id -> Int4,
code_bundle_id -> Int4,
} }
} }
@ -72,7 +72,7 @@ table! {
joinable!(bots -> users (owner_id)); joinable!(bots -> users (owner_id));
joinable!(code_bundles -> bots (bot_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!(match_players -> matches (match_id));
joinable!(sessions -> users (user_id)); joinable!(sessions -> users (user_id));