aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/db/matches.rs
blob: 813f99880162f68f1ef57ae584fcc63e487fe746 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
pub use crate::db_types::MatchState;
use chrono::NaiveDateTime;
use diesel::associations::BelongsTo;
use diesel::pg::Pg;
use diesel::sql_types::*;
use diesel::{
    BelongingToDsl, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl, RunQueryDsl,
};
use diesel::{Connection, GroupedBy, PgConnection, QueryResult};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

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

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

#[derive(Insertable)]
#[diesel(table_name = matches)]
pub struct NewMatch<'a> {
    pub state: MatchState,
    pub log_path: &'a str,
    pub is_public: bool,
    pub map_id: Option<i32>,
}

#[derive(Insertable)]
#[diesel(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)]
#[diesel(table_name = matches)]
pub struct MatchBase {
    pub id: i32,
    pub state: MatchState,
    pub log_path: String,
    pub created_at: NaiveDateTime,
    pub winner: Option<i32>,
    pub is_public: bool,
    pub map_id: Option<i32>,
}

#[derive(Queryable, Identifiable, Associations, Clone)]
#[diesel(primary_key(match_id, player_id))]
#[diesel(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: &mut PgConnection,
) -> QueryResult<MatchData> {
    conn.transaction(|conn| {
        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>,
}

/// Add player information to MatchBase instances
fn fetch_full_match_data(
    matches: Vec<MatchBase>,
    conn: &mut PgConnection,
) -> QueryResult<Vec<FullMatchData>> {
    let map_ids: HashSet<i32> = matches.iter().filter_map(|m| m.map_id).collect();

    let maps_by_id: HashMap<i32, Map> = maps::table
        .filter(maps::id.eq_any(map_ids))
        .load::<Map>(conn)?
        .into_iter()
        .map(|m| (m.id, m))
        .collect();

    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 {
            match_players: players.into_iter().collect(),
            map: base
                .map_id
                .and_then(|map_id| maps_by_id.get(&map_id).cloned()),
            base,
        })
        .collect();

    Ok(res)
}

// TODO: this method should disappear
pub fn list_matches(amount: i64, conn: &mut PgConnection) -> QueryResult<Vec<FullMatchData>> {
    conn.transaction(|conn| {
        let matches = matches::table
            .filter(matches::state.eq(MatchState::Finished))
            .order_by(matches::created_at.desc())
            .limit(amount)
            .get_results::<MatchBase>(conn)?;

        fetch_full_match_data(matches, conn)
    })
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum BotMatchOutcome {
    Win,
    Loss,
    Tie,
}

pub fn list_public_matches(
    amount: i64,
    before: Option<NaiveDateTime>,
    after: Option<NaiveDateTime>,
    conn: &mut PgConnection,
) -> QueryResult<Vec<FullMatchData>> {
    conn.transaction(|conn| {
        let query = finished_public_matches_query(before, after).limit(amount);
        let matches = query.get_results::<MatchBase>(conn)?;
        fetch_full_match_data(matches, conn)
    })
}

pub fn list_bot_matches(
    bot_id: i32,
    outcome: Option<BotMatchOutcome>,
    amount: i64,
    before: Option<NaiveDateTime>,
    after: Option<NaiveDateTime>,
    conn: &mut PgConnection,
) -> QueryResult<Vec<FullMatchData>> {
    let mut query = finished_public_matches_query(before, after)
        .inner_join(match_players::table)
        .inner_join(
            bot_versions::table.on(match_players::bot_version_id.eq(bot_versions::id.nullable())),
        )
        .filter(bot_versions::bot_id.eq(bot_id))
        .select(matches::all_columns);

    if let Some(outcome) = outcome {
        query = match outcome {
            BotMatchOutcome::Win => {
                query.filter(matches::winner.eq(match_players::player_id.nullable()))
            }
            BotMatchOutcome::Loss => {
                query.filter(matches::winner.ne(match_players::player_id.nullable()))
            }
            BotMatchOutcome::Tie => query.filter(matches::winner.is_null()),
        };
    }

    query = query.limit(amount);

    let matches = query.get_results::<MatchBase>(conn)?;
    fetch_full_match_data(matches, conn)
}

fn finished_public_matches_query(
    before: Option<NaiveDateTime>,
    after: Option<NaiveDateTime>,
) -> matches::BoxedQuery<'static, Pg> {
    let query = matches::table
        .filter(matches::state.eq(MatchState::Finished))
        .filter(matches::is_public.eq(true))
        .into_boxed();

    match (before, after) {
        (None, None) => query.order_by(matches::created_at.desc()),
        (Some(before), None) => query
            .filter(matches::created_at.lt(before))
            .order_by(matches::created_at.desc()),
        (None, Some(after)) => query
            .filter(matches::created_at.gt(after))
            .order_by(matches::created_at.asc()),
        (Some(before), Some(after)) => query
            .filter(matches::created_at.lt(before))
            .filter(matches::created_at.gt(after))
            .order_by(matches::created_at.desc()),
    }
}

// TODO: maybe unify this with matchdata?
pub struct FullMatchData {
    pub base: MatchBase,
    pub map: Option<Map>,
    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: &mut PgConnection) -> QueryResult<FullMatchData> {
    conn.transaction(|conn| {
        let match_base = matches::table.find(id).get_result::<MatchBase>(conn)?;

        let map = match match_base.map_id {
            None => None,
            Some(map_id) => Some(super::maps::find_map(map_id, 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,
            map,
        };

        Ok(res)
    })
}

pub fn find_match_base(id: i32, conn: &mut 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: &mut 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(())
}

#[derive(QueryableByName)]
pub struct BotStatsRecord {
    #[diesel(sql_type = Text)]
    pub opponent: String,
    #[diesel(sql_type = Text)]
    pub map: String,
    #[diesel(sql_type = Nullable<Bool>)]
    pub win: Option<bool>,
    #[diesel(sql_type = Int8)]
    pub count: i64,
}

pub fn fetch_bot_stats(
    bot_name: &str,
    db_conn: &mut PgConnection,
) -> QueryResult<Vec<BotStatsRecord>> {
    diesel::sql_query(
        "
SELECT opponent, map, win, COUNT(*) as count
FROM (
    SELECT
        opponent_bot.name as opponent,
        maps.name as map,
        (matches.winner = bot_player.player_id) as win
    FROM matches
    JOIN maps
        ON matches.map_id = maps.id
    JOIN match_players bot_player
        ON bot_player.match_id = matches.id
    JOIN bot_versions bot_version
        ON bot_version.id = bot_player.bot_version_id
    JOIN bots bot
        ON bot.id = bot_version.bot_id
    JOIN match_players opponent_player
        ON opponent_player.match_id = matches.id
        AND opponent_player.player_id = 1 - bot_player.player_id
    JOIN bot_versions opponent_version
        ON opponent_version.id = opponent_player.bot_version_id
    LEFT OUTER JOIN bots opponent_bot
        ON opponent_version.bot_id = opponent_bot.id
    WHERE
        matches.state = 'finished'
        AND matches.is_public
        AND bot.name = $1
    ORDER BY
        matches.created_at DESC 
    LIMIT 10000
) bot_matches
GROUP BY opponent, map, win
HAVING opponent IS NOT NULL",
    )
    .bind::<Text, _>(bot_name)
    .load(db_conn)
}