aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/routes/matches.rs
blob: f33a5f1999ed8e6ee07864ddb00924d8efd5c6c2 (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
use axum::{extract::Path, Json};
use hyper::StatusCode;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

use crate::{
    db::matches::{self, MatchState},
    DatabaseConnection, MATCHES_DIR,
};

#[derive(Serialize, Deserialize)]
pub struct ApiMatch {
    id: i32,
    timestamp: chrono::NaiveDateTime,
    state: MatchState,
    players: Vec<ApiMatchPlayer>,
}

#[derive(Serialize, Deserialize)]
pub struct ApiMatchPlayer {
    bot_version_id: Option<i32>,
    bot_id: Option<i32>,
    bot_name: Option<String>,
}

pub async fn list_matches(conn: DatabaseConnection) -> Result<Json<Vec<ApiMatch>>, StatusCode> {
    matches::list_matches(&conn)
        .map_err(|_| StatusCode::BAD_REQUEST)
        .map(|matches| Json(matches.into_iter().map(match_data_to_api).collect()))
}

pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch {
    ApiMatch {
        id: data.base.id,
        timestamp: data.base.created_at,
        state: data.base.state,
        players: data
            .match_players
            .iter()
            .map(|_p| ApiMatchPlayer {
                bot_version_id: _p.bot_version.as_ref().map(|cb| cb.id),
                bot_id: _p.bot.as_ref().map(|b| b.id),
                bot_name: _p.bot.as_ref().map(|b| b.name.clone()),
            })
            .collect(),
    }
}

pub async fn get_match_data(
    Path(match_id): Path<i32>,
    conn: DatabaseConnection,
) -> Result<Json<ApiMatch>, StatusCode> {
    let match_data = matches::find_match(match_id, &conn)
        .map_err(|_| StatusCode::NOT_FOUND)
        .map(match_data_to_api)?;
    Ok(Json(match_data))
}

pub async fn get_match_log(
    Path(match_id): Path<i32>,
    conn: DatabaseConnection,
) -> Result<Vec<u8>, StatusCode> {
    let match_base =
        matches::find_match_base(match_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
    let log_path = PathBuf::from(MATCHES_DIR).join(&match_base.log_path);
    let log_contents = std::fs::read(log_path).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    Ok(log_contents)
}