save match winner in database
This commit is contained in:
parent
80c60ac69c
commit
6e1167ee9e
5 changed files with 26 additions and 6 deletions
|
@ -0,0 +1,3 @@
|
||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
ALTER TABLE matches
|
||||||
|
DROP column winner;
|
|
@ -0,0 +1,3 @@
|
||||||
|
-- Your SQL goes here
|
||||||
|
ALTER TABLE matches
|
||||||
|
ADD COLUMN winner integer;
|
|
@ -35,6 +35,7 @@ pub struct MatchBase {
|
||||||
pub state: MatchState,
|
pub state: MatchState,
|
||||||
pub log_path: String,
|
pub log_path: String,
|
||||||
pub created_at: NaiveDateTime,
|
pub created_at: NaiveDateTime,
|
||||||
|
pub winner: Option<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Identifiable, Associations, Clone)]
|
#[derive(Queryable, Identifiable, Associations, Clone)]
|
||||||
|
@ -158,9 +159,15 @@ pub fn find_match_base(id: i32, conn: &PgConnection) -> QueryResult<MatchBase> {
|
||||||
matches::table.find(id).get_result::<MatchBase>(conn)
|
matches::table.find(id).get_result::<MatchBase>(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_match_state(id: i32, match_state: MatchState, conn: &PgConnection) -> QueryResult<()> {
|
pub enum MatchResult {
|
||||||
|
Finished { winner: Option<i32> }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save_match_result(id: i32, result: MatchResult, conn: &PgConnection) -> QueryResult<()> {
|
||||||
|
let MatchResult::Finished { winner } = result;
|
||||||
|
|
||||||
diesel::update(matches::table.find(id))
|
diesel::update(matches::table.find(id))
|
||||||
.set(matches::state.eq(match_state))
|
.set((matches::winner.eq(winner), matches::state.eq(MatchState::Finished)))
|
||||||
.execute(conn)?;
|
.execute(conn)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
|
@ -6,7 +6,10 @@ use runner::MatchOutcome;
|
||||||
use tokio::task::JoinHandle;
|
use tokio::task::JoinHandle;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{self, matches::MatchData},
|
db::{
|
||||||
|
self,
|
||||||
|
matches::{MatchData, MatchResult},
|
||||||
|
},
|
||||||
util::gen_alphanumeric,
|
util::gen_alphanumeric,
|
||||||
ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR,
|
ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR,
|
||||||
};
|
};
|
||||||
|
@ -95,8 +98,11 @@ async fn run_match_task(
|
||||||
.await
|
.await
|
||||||
.expect("could not get database connection");
|
.expect("could not get database connection");
|
||||||
|
|
||||||
db::matches::set_match_state(match_id, db::matches::MatchState::Finished, &conn)
|
let result = MatchResult::Finished {
|
||||||
.expect("could not update match state");
|
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;
|
return outcome;
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,7 @@ table! {
|
||||||
state -> Match_state,
|
state -> Match_state,
|
||||||
log_path -> Text,
|
log_path -> Text,
|
||||||
created_at -> Timestamp,
|
created_at -> Timestamp,
|
||||||
|
winner -> Nullable<Int4>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue