aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/db/matches.rs
blob: 39b7d9ba7e96af560e58df0e8bf8ff79e5bcf399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
pub use crate::db_types::MatchState;
use chrono::NaiveDateTime;
use diesel::associations::BelongsTo;
use diesel::{
    BelongingToDsl, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl, RunQueryDsl,
};
use diesel::{Connection, GroupedBy, PgConnection, QueryResult};

use crate::schema::{bot_versions, bots, match_players, matches};

use super::bots::{Bot, BotVersion};

#[derive(Insertable)]
#[table_name = "matches"]
pub struct NewMatch<'a> {
    pub state: MatchState,
    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,
    /// id of the bot behind this player
    pub bot_version_id: Option<i32>,
}

#[derive(Queryable, Identifiable)]
#[table_name = "matches"]
pub struct MatchBase {
    pub id: i32,
    pub state: MatchState,
    pub log_path: String,
    pub created_at: NaiveDateTime,
    pub winner: Option<i32>,
}

#[derive(Queryable, Identifiable, Associations, Clone)]
#[primary_key(match_id, player_id)]
#[belongs_to(MatchBase, foreign_key = "match_id")]
pub struct MatchPlayer {
    pub match_id: i32,
    pub player_id: i32,
    pub code_bundle_id: Option<i32>,
}

pub struct MatchPlayerData {
    pub code_bundle_id: Option<i32>,
}

pub fn create_match(
    new_match_base: &NewMatch,
    new_match_players: &[MatchPlayerData],
    conn: &PgConnection,
) -> QueryResult<MatchData> {
    conn.transaction(|| {
        let match_base = diesel::insert_into(matches::table)
            .values(new_match_base)
            .get_result::<MatchBase>(conn)?;

        let new_match_players = new_match_players
            .iter()
            .enumerate()
            .map(|(num, player_data)| NewMatchPlayer {
                match_id: match_base.id,
                player_id: num as i32,
                bot_version_id: player_data.code_bundle_id,
            })
            .collect::<Vec<_>>();

        let match_players = diesel::insert_into(match_players::table)
            .values(&new_match_players)
            .get_results::<MatchPlayer>(conn)?;

        Ok(MatchData {
            base: match_base,
            match_players,
        })
    })
}

pub struct MatchData {
    pub base: MatchBase,
    pub match_players: Vec<MatchPlayer>,
}

pub fn list_matches(conn: &PgConnection) -> QueryResult<Vec<FullMatchData>> {
    conn.transaction(|| {
        let matches = matches::table.get_results::<MatchBase>(conn)?;

        let match_players = MatchPlayer::belonging_to(&matches)
            .left_join(
                bot_versions::table
                    .on(match_players::bot_version_id.eq(bot_versions::id.nullable())),
            )
            .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable())))
            .order_by((
                match_players::match_id.asc(),
                match_players::player_id.asc(),
            ))
            .load::<FullMatchPlayerData>(conn)?
            .grouped_by(&matches);

        let res = matches
            .into_iter()
            .zip(match_players.into_iter())
            .map(|(base, players)| FullMatchData {
                base,
                match_players: players.into_iter().collect(),
            })
            .collect();

        Ok(res)
    })
}

// 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,
    pub bot_version: Option<BotVersion>,
    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> {
    conn.transaction(|| {
        let match_base = matches::table.find(id).get_result::<MatchBase>(conn)?;

        let match_players = MatchPlayer::belonging_to(&match_base)
            .left_join(
                bot_versions::table
                    .on(match_players::bot_version_id.eq(bot_versions::id.nullable())),
            )
            .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable())))
            .order_by(match_players::player_id.asc())
            .load::<FullMatchPlayerData>(conn)?;

        let res = FullMatchData {
            base: match_base,
            match_players,
        };

        Ok(res)
    })
}

pub fn find_match_base(id: i32, conn: &PgConnection) -> QueryResult<MatchBase> {
    matches::table.find(id).get_result::<MatchBase>(conn)
}

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))
        .set((
            matches::winner.eq(winner),
            matches::state.eq(MatchState::Finished),
        ))
        .execute(conn)?;
    Ok(())
}