aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/routes
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-03-11 00:41:18 +0100
committerIlion Beyst <ilion.beyst@gmail.com>2022-03-11 00:41:18 +0100
commit63695c299c023bd61d2a45cc22e509ac3717ec8d (patch)
tree3a1c4a4907a728b3a5eeddbe55f7e4e8a6411051 /planetwars-server/src/routes
parent523de3ba0692734237b5ed6fa6dce8fbad9d69c8 (diff)
downloadplanetwars.dev-63695c299c023bd61d2a45cc22e509ac3717ec8d.tar.xz
planetwars.dev-63695c299c023bd61d2a45cc22e509ac3717ec8d.zip
add information about match players to match API object
Diffstat (limited to 'planetwars-server/src/routes')
-rw-r--r--planetwars-server/src/routes/demo.rs22
-rw-r--r--planetwars-server/src/routes/matches.rs14
2 files changed, 28 insertions, 8 deletions
diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs
index 5dbbe92..749c0ca 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::{MatchPlayerData, MatchState};
+use crate::db::matches::{FullMatchData, FullMatchPlayerData, MatchPlayerData, MatchState};
use crate::modules::bots::save_code_bundle;
use crate::util::gen_alphanumeric;
use crate::{ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR};
@@ -92,7 +92,6 @@ pub async fn submit_bot(
code_bundle_id: opponent_code_bundle.id,
},
];
- // TODO: set match players
let match_data = db::matches::create_match(&new_match_data, &new_match_players, &conn)
.expect("failed to create match");
@@ -102,7 +101,24 @@ pub async fn submit_bot(
pool.clone(),
));
- let api_match = super::matches::match_data_to_api(match_data);
+ // TODO: avoid clones
+ let full_match_data = FullMatchData {
+ base: match_data.base,
+ match_players: vec![
+ FullMatchPlayerData {
+ base: match_data.match_players[0].clone(),
+ code_bundle: player_code_bundle,
+ bot: None,
+ },
+ FullMatchPlayerData {
+ base: match_data.match_players[1].clone(),
+ code_bundle: opponent_code_bundle,
+ bot: Some(opponent),
+ },
+ ],
+ };
+
+ let api_match = super::matches::match_data_to_api(full_match_data);
Ok(Json(SubmitBotResponse {
match_data: api_match,
}))
diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs
index a6d8ff2..fc09551 100644
--- a/planetwars-server/src/routes/matches.rs
+++ b/planetwars-server/src/routes/matches.rs
@@ -109,9 +109,9 @@ pub struct ApiMatch {
#[derive(Serialize, Deserialize)]
pub struct ApiMatchPlayer {
- // TODO!
-// bot_id: i32,
-// code_bundle_id: i32
+ code_bundle_id: i32,
+ bot_id: Option<i32>,
+ bot_name: Option<String>,
}
pub async fn list_matches(conn: DatabaseConnection) -> Result<Json<Vec<ApiMatch>>, StatusCode> {
@@ -120,7 +120,7 @@ pub async fn list_matches(conn: DatabaseConnection) -> Result<Json<Vec<ApiMatch>
.map(|matches| Json(matches.into_iter().map(match_data_to_api).collect()))
}
-pub fn match_data_to_api(data: matches::MatchData) -> ApiMatch {
+pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch {
ApiMatch {
id: data.base.id,
timestamp: data.base.created_at,
@@ -128,7 +128,11 @@ pub fn match_data_to_api(data: matches::MatchData) -> ApiMatch {
players: data
.match_players
.iter()
- .map(|_p| ApiMatchPlayer {})
+ .map(|_p| ApiMatchPlayer {
+ code_bundle_id: _p.code_bundle.id,
+ bot_id: _p.bot.as_ref().map(|b| b.id),
+ bot_name: _p.bot.as_ref().map(|b| b.name.clone()),
+ })
.collect(),
}
}