From 6e1167ee9e4ce2bef3c4a7f38efc362be1d750cd Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sun, 29 May 2022 11:41:52 +0200 Subject: save match winner in database --- .../2022-05-29-085905_add_winner_to_matches/down.sql | 3 +++ .../2022-05-29-085905_add_winner_to_matches/up.sql | 3 +++ planetwars-server/src/db/matches.rs | 13 ++++++++++--- planetwars-server/src/modules/matches.rs | 12 +++++++++--- planetwars-server/src/schema.rs | 1 + 5 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 planetwars-server/migrations/2022-05-29-085905_add_winner_to_matches/down.sql create mode 100644 planetwars-server/migrations/2022-05-29-085905_add_winner_to_matches/up.sql (limited to 'planetwars-server') diff --git a/planetwars-server/migrations/2022-05-29-085905_add_winner_to_matches/down.sql b/planetwars-server/migrations/2022-05-29-085905_add_winner_to_matches/down.sql new file mode 100644 index 0000000..1deada2 --- /dev/null +++ b/planetwars-server/migrations/2022-05-29-085905_add_winner_to_matches/down.sql @@ -0,0 +1,3 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE matches +DROP column winner; \ No newline at end of file diff --git a/planetwars-server/migrations/2022-05-29-085905_add_winner_to_matches/up.sql b/planetwars-server/migrations/2022-05-29-085905_add_winner_to_matches/up.sql new file mode 100644 index 0000000..6212ebd --- /dev/null +++ b/planetwars-server/migrations/2022-05-29-085905_add_winner_to_matches/up.sql @@ -0,0 +1,3 @@ +-- Your SQL goes here +ALTER TABLE matches +ADD COLUMN winner integer; \ No newline at end of file diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index 42689d1..ee25e85 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -35,6 +35,7 @@ pub struct MatchBase { pub state: MatchState, pub log_path: String, pub created_at: NaiveDateTime, + pub winner: Option, } #[derive(Queryable, Identifiable, Associations, Clone)] @@ -158,9 +159,15 @@ pub fn find_match_base(id: i32, conn: &PgConnection) -> QueryResult { matches::table.find(id).get_result::(conn) } -pub fn set_match_state(id: i32, match_state: MatchState, conn: &PgConnection) -> QueryResult<()> { +pub enum MatchResult { + Finished { winner: Option } +} + +pub fn save_match_result(id: i32, result: MatchResult, conn: &PgConnection) -> QueryResult<()> { + let MatchResult::Finished { winner } = result; + diesel::update(matches::table.find(id)) - .set(matches::state.eq(match_state)) + .set((matches::winner.eq(winner), matches::state.eq(MatchState::Finished))) .execute(conn)?; Ok(()) -} +} \ No newline at end of file diff --git a/planetwars-server/src/modules/matches.rs b/planetwars-server/src/modules/matches.rs index 201c6d4..a254bac 100644 --- a/planetwars-server/src/modules/matches.rs +++ b/planetwars-server/src/modules/matches.rs @@ -6,7 +6,10 @@ use runner::MatchOutcome; use tokio::task::JoinHandle; use crate::{ - db::{self, matches::MatchData}, + db::{ + self, + matches::{MatchData, MatchResult}, + }, util::gen_alphanumeric, ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR, }; @@ -95,8 +98,11 @@ async fn run_match_task( .await .expect("could not get database connection"); - db::matches::set_match_state(match_id, db::matches::MatchState::Finished, &conn) - .expect("could not update match state"); + let result = MatchResult::Finished { + winner: outcome.winner.map(|w| (w - 1) as i32), // player numbers in matchrunner start at 1 + }; + + db::matches::save_match_result(match_id, result, &conn).expect("could not save match result"); return outcome; } diff --git a/planetwars-server/src/schema.rs b/planetwars-server/src/schema.rs index 812e05e..be3e858 100644 --- a/planetwars-server/src/schema.rs +++ b/planetwars-server/src/schema.rs @@ -44,6 +44,7 @@ table! { state -> Match_state, log_path -> Text, created_at -> Timestamp, + winner -> Nullable, } } -- cgit v1.2.3