aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/routes/matches.rs
blob: 9fa532e321e8326bb201b2dc8ee93efa5a0af186 (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
use std::path::PathBuf;

use axum::{
    extract::{Extension, Path},
    Json,
};
use hyper::StatusCode;
use planetwars_matchrunner::{docker_runner::DockerBotSpec, run_match, MatchConfig, MatchPlayer};
use rand::{distributions::Alphanumeric, Rng};
use serde::{Deserialize, Serialize};

use crate::{
    db::{
        bots,
        matches::{self, MatchState},
        users::User,
    },
    ConnectionPool, DatabaseConnection, BOTS_DIR, MAPS_DIR, MATCHES_DIR,
};

#[derive(Serialize, Deserialize, Debug)]
pub struct MatchParams {
    // Just bot ids for now
    players: Vec<i32>,
}

pub async fn play_match(
    _user: User,
    Extension(pool): Extension<ConnectionPool>,
    Json(params): Json<MatchParams>,
) -> Result<(), StatusCode> {
    let conn = pool.get().await.expect("could not get database connection");
    let map_path = PathBuf::from(MAPS_DIR).join("hex.json");

    let slug: String = rand::thread_rng()
        .sample_iter(&Alphanumeric)
        .take(16)
        .map(char::from)
        .collect();
    let log_file_name = format!("{}.log", slug);

    let mut players = Vec::new();
    let mut bot_ids = Vec::new();
    for bot_name in params.players {
        let bot = bots::find_bot(bot_name, &conn).map_err(|_| StatusCode::BAD_REQUEST)?;
        let code_bundle =
            bots::active_code_bundle(bot.id, &conn).map_err(|_| StatusCode::BAD_REQUEST)?;

        let bundle_path = PathBuf::from(BOTS_DIR).join(&code_bundle.path);
        let bot_config: BotConfig = std::fs::read_to_string(bundle_path.join("botconfig.toml"))
            .and_then(|config_str| toml::from_str(&config_str).map_err(|e| e.into()))
            .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

        players.push(MatchPlayer {
            name: bot.name.clone(),
            bot_spec: Box::new(DockerBotSpec {
                code_path: PathBuf::from(BOTS_DIR).join(code_bundle.path),
                image: "python:3.10-slim-buster".to_string(),
                argv: shlex::split(&bot_config.run_command)
                    // TODO: this is an user error, should ideally be handled before we get here
                    .ok_or_else(|| StatusCode::INTERNAL_SERVER_ERROR)?,
            }),
        });

        bot_ids.push(matches::MatchPlayerData {
            code_bundle_id: code_bundle.id,
        });
    }

    let match_config = MatchConfig {
        map_name: "hex".to_string(),
        map_path,
        log_path: PathBuf::from(MATCHES_DIR).join(&log_file_name),
        players,
    };

    tokio::spawn(run_match_task(
        match_config,
        log_file_name,
        bot_ids,
        pool.clone(),
    ));
    Ok(())
}

async fn run_match_task(
    config: MatchConfig,
    log_file_name: String,
    match_players: Vec<matches::MatchPlayerData>,
    pool: ConnectionPool,
) {
    let match_data = matches::NewMatch {
        state: MatchState::Finished,
        log_path: &log_file_name,
    };

    run_match(config).await;
    let conn = pool.get().await.expect("could not get database connection");
    matches::create_match(&match_data, &match_players, &conn).expect("could not create match");
}

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

#[derive(Serialize, Deserialize)]
pub struct ApiMatchPlayer {
    code_bundle_id: 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 {
                code_bundle_id: _p.code_bundle.id,
                bot_id: _p.bot.as_ref().map(|b| b.id),
                bot_name: _p.bot.as_ref().map(|b| b.name.clone()),
            })
            .collect(),
    }
}

// TODO: this is duplicated from planetwars-cli
// clean this up and move to matchrunner crate
#[derive(Serialize, Deserialize)]
pub struct BotConfig {
    pub name: String,
    pub run_command: String,
    pub build_command: Option<String>,
}

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(|data| match_data_to_api(data))?;
    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)
}