2022-01-03 23:33:00 +01:00
|
|
|
pub use crate::db_types::MatchState;
|
2022-01-02 16:14:03 +01:00
|
|
|
use chrono::NaiveDateTime;
|
2022-03-11 00:41:18 +01:00
|
|
|
use diesel::associations::BelongsTo;
|
|
|
|
use diesel::{
|
|
|
|
BelongingToDsl, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl, RunQueryDsl,
|
|
|
|
};
|
2022-01-02 16:14:03 +01:00
|
|
|
use diesel::{Connection, GroupedBy, PgConnection, QueryResult};
|
|
|
|
|
2022-07-05 20:34:20 +02:00
|
|
|
use crate::schema::{bot_versions, bots, match_players, matches};
|
2022-03-11 00:41:18 +01:00
|
|
|
|
2022-07-06 22:41:27 +02:00
|
|
|
use super::bots::{Bot, BotVersion};
|
2022-01-02 16:14:03 +01:00
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "matches"]
|
|
|
|
pub struct NewMatch<'a> {
|
2022-01-03 23:33:00 +01:00
|
|
|
pub state: MatchState,
|
2022-01-02 16:14:03 +01:00
|
|
|
pub log_path: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "match_players"]
|
|
|
|
pub struct NewMatchPlayer {
|
|
|
|
/// id of the match this player is in
|
|
|
|
pub match_id: i32,
|
|
|
|
/// player id within the match
|
|
|
|
pub player_id: i32,
|
2022-03-10 23:35:42 +01:00
|
|
|
/// id of the bot behind this player
|
2022-07-06 22:41:27 +02:00
|
|
|
pub bot_version_id: Option<i32>,
|
2022-01-02 16:14:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Queryable, Identifiable)]
|
|
|
|
#[table_name = "matches"]
|
|
|
|
pub struct MatchBase {
|
|
|
|
pub id: i32,
|
2022-01-03 23:33:00 +01:00
|
|
|
pub state: MatchState,
|
2022-01-02 16:14:03 +01:00
|
|
|
pub log_path: String,
|
|
|
|
pub created_at: NaiveDateTime,
|
2022-05-29 11:41:52 +02:00
|
|
|
pub winner: Option<i32>,
|
2022-01-02 16:14:03 +01:00
|
|
|
}
|
|
|
|
|
2022-03-11 00:41:18 +01:00
|
|
|
#[derive(Queryable, Identifiable, Associations, Clone)]
|
2022-01-02 16:14:03 +01:00
|
|
|
#[primary_key(match_id, player_id)]
|
|
|
|
#[belongs_to(MatchBase, foreign_key = "match_id")]
|
|
|
|
pub struct MatchPlayer {
|
|
|
|
pub match_id: i32,
|
|
|
|
pub player_id: i32,
|
2022-06-10 21:09:33 +02:00
|
|
|
pub code_bundle_id: Option<i32>,
|
2022-01-02 16:14:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MatchPlayerData {
|
2022-06-10 21:49:32 +02:00
|
|
|
pub code_bundle_id: Option<i32>,
|
2022-01-02 16:14:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_match(
|
2022-02-08 20:13:24 +01:00
|
|
|
new_match_base: &NewMatch,
|
|
|
|
new_match_players: &[MatchPlayerData],
|
2022-01-02 16:14:03 +01:00
|
|
|
conn: &PgConnection,
|
2022-02-08 20:13:24 +01:00
|
|
|
) -> QueryResult<MatchData> {
|
2022-01-02 16:14:03 +01:00
|
|
|
conn.transaction(|| {
|
|
|
|
let match_base = diesel::insert_into(matches::table)
|
2022-02-08 20:13:24 +01:00
|
|
|
.values(new_match_base)
|
2022-01-02 16:14:03 +01:00
|
|
|
.get_result::<MatchBase>(conn)?;
|
|
|
|
|
2022-02-08 20:13:24 +01:00
|
|
|
let new_match_players = new_match_players
|
2022-01-02 16:14:03 +01:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(num, player_data)| NewMatchPlayer {
|
|
|
|
match_id: match_base.id,
|
|
|
|
player_id: num as i32,
|
2022-07-06 22:41:27 +02:00
|
|
|
bot_version_id: player_data.code_bundle_id,
|
2022-01-02 16:14:03 +01:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2022-02-08 20:13:24 +01:00
|
|
|
let match_players = diesel::insert_into(match_players::table)
|
|
|
|
.values(&new_match_players)
|
|
|
|
.get_results::<MatchPlayer>(conn)?;
|
2022-01-02 16:14:03 +01:00
|
|
|
|
2022-02-08 20:13:24 +01:00
|
|
|
Ok(MatchData {
|
|
|
|
base: match_base,
|
|
|
|
match_players,
|
|
|
|
})
|
2022-01-02 16:14:03 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MatchData {
|
|
|
|
pub base: MatchBase,
|
|
|
|
pub match_players: Vec<MatchPlayer>,
|
|
|
|
}
|
|
|
|
|
2022-03-11 00:41:18 +01:00
|
|
|
pub fn list_matches(conn: &PgConnection) -> QueryResult<Vec<FullMatchData>> {
|
2022-01-02 16:14:03 +01:00
|
|
|
conn.transaction(|| {
|
|
|
|
let matches = matches::table.get_results::<MatchBase>(conn)?;
|
|
|
|
|
|
|
|
let match_players = MatchPlayer::belonging_to(&matches)
|
2022-06-10 21:09:33 +02:00
|
|
|
.left_join(
|
2022-07-05 20:34:20 +02:00
|
|
|
bot_versions::table
|
2022-07-06 22:41:27 +02:00
|
|
|
.on(match_players::bot_version_id.eq(bot_versions::id.nullable())),
|
2022-06-10 21:09:33 +02:00
|
|
|
)
|
2022-07-05 20:34:20 +02:00
|
|
|
.left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable())))
|
2022-07-23 14:47:24 +02:00
|
|
|
.order_by((
|
|
|
|
match_players::match_id.asc(),
|
|
|
|
match_players::player_id.asc(),
|
|
|
|
))
|
2022-03-11 00:41:18 +01:00
|
|
|
.load::<FullMatchPlayerData>(conn)?
|
2022-01-02 16:14:03 +01:00
|
|
|
.grouped_by(&matches);
|
|
|
|
|
|
|
|
let res = matches
|
|
|
|
.into_iter()
|
|
|
|
.zip(match_players.into_iter())
|
2022-03-11 00:41:18 +01:00
|
|
|
.map(|(base, players)| FullMatchData {
|
2022-01-02 16:14:03 +01:00
|
|
|
base,
|
|
|
|
match_players: players.into_iter().collect(),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
})
|
|
|
|
}
|
2022-01-02 17:56:52 +01:00
|
|
|
|
2022-03-11 00:41:18 +01:00
|
|
|
// TODO: maybe unify this with matchdata?
|
|
|
|
pub struct FullMatchData {
|
|
|
|
pub base: MatchBase,
|
|
|
|
pub match_players: Vec<FullMatchPlayerData>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Queryable)]
|
|
|
|
// #[primary_key(base.match_id, base::player_id)]
|
|
|
|
pub struct FullMatchPlayerData {
|
|
|
|
pub base: MatchPlayer,
|
2022-07-06 22:41:27 +02:00
|
|
|
pub bot_version: Option<BotVersion>,
|
2022-03-11 00:41:18 +01:00
|
|
|
pub bot: Option<Bot>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BelongsTo<MatchBase> for FullMatchPlayerData {
|
|
|
|
type ForeignKey = i32;
|
|
|
|
type ForeignKeyColumn = match_players::match_id;
|
|
|
|
|
|
|
|
fn foreign_key(&self) -> Option<&Self::ForeignKey> {
|
|
|
|
Some(&self.base.match_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foreign_key_column() -> Self::ForeignKeyColumn {
|
|
|
|
match_players::match_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_match(id: i32, conn: &PgConnection) -> QueryResult<FullMatchData> {
|
2022-01-02 17:56:52 +01:00
|
|
|
conn.transaction(|| {
|
|
|
|
let match_base = matches::table.find(id).get_result::<MatchBase>(conn)?;
|
|
|
|
|
2022-03-11 00:41:18 +01:00
|
|
|
let match_players = MatchPlayer::belonging_to(&match_base)
|
2022-06-10 21:09:33 +02:00
|
|
|
.left_join(
|
2022-07-05 20:34:20 +02:00
|
|
|
bot_versions::table
|
2022-07-06 22:41:27 +02:00
|
|
|
.on(match_players::bot_version_id.eq(bot_versions::id.nullable())),
|
2022-06-10 21:09:33 +02:00
|
|
|
)
|
2022-07-05 20:34:20 +02:00
|
|
|
.left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable())))
|
2022-07-23 14:47:24 +02:00
|
|
|
.order_by(match_players::player_id.asc())
|
2022-03-11 00:41:18 +01:00
|
|
|
.load::<FullMatchPlayerData>(conn)?;
|
2022-01-02 17:56:52 +01:00
|
|
|
|
2022-03-11 00:41:18 +01:00
|
|
|
let res = FullMatchData {
|
2022-01-02 17:56:52 +01:00
|
|
|
base: match_base,
|
|
|
|
match_players,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-12 09:04:12 +01:00
|
|
|
pub fn find_match_base(id: i32, conn: &PgConnection) -> QueryResult<MatchBase> {
|
2022-01-02 17:56:52 +01:00
|
|
|
matches::table.find(id).get_result::<MatchBase>(conn)
|
|
|
|
}
|
2022-02-15 19:54:29 +01:00
|
|
|
|
2022-05-29 11:41:52 +02:00
|
|
|
pub enum MatchResult {
|
2022-06-10 21:09:33 +02:00
|
|
|
Finished { winner: Option<i32> },
|
2022-05-29 11:41:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save_match_result(id: i32, result: MatchResult, conn: &PgConnection) -> QueryResult<()> {
|
|
|
|
let MatchResult::Finished { winner } = result;
|
|
|
|
|
2022-02-15 19:54:29 +01:00
|
|
|
diesel::update(matches::table.find(id))
|
2022-06-10 21:09:33 +02:00
|
|
|
.set((
|
|
|
|
matches::winner.eq(winner),
|
|
|
|
matches::state.eq(MatchState::Finished),
|
|
|
|
))
|
2022-02-15 19:54:29 +01:00
|
|
|
.execute(conn)?;
|
|
|
|
Ok(())
|
2022-06-10 21:09:33 +02:00
|
|
|
}
|