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

use diesel::{PgConnection, QueryResult};
use planetwars_matchrunner::{self as runner, docker_runner::DockerBotSpec, BotSpec, MatchConfig};
use runner::MatchOutcome;
use tokio::task::JoinHandle;

use crate::{
    db::{
        self,
        matches::{MatchData, MatchResult},
    },
    util::gen_alphanumeric,
    ConnectionPool, BOTS_DIR, MAPS_DIR, MATCHES_DIR,
};

const PYTHON_IMAGE: &str = "python:3.10-slim-buster";

pub struct RunMatch {
    log_file_name: String,
    players: Vec<MatchPlayer>,
    match_id: Option<i32>,
}

pub struct MatchPlayer {
    bot_spec: Box<dyn BotSpec>,
    // meta that will be passed on to database
    code_bundle_id: Option<i32>,
}

impl MatchPlayer {
    pub fn from_code_bundle(code_bundle: &db::bots::BotVersion) -> Self {
        MatchPlayer {
            bot_spec: code_bundle_to_botspec(code_bundle),
            code_bundle_id: Some(code_bundle.id),
        }
    }

    pub fn from_bot_spec(bot_spec: Box<dyn BotSpec>) -> Self {
        MatchPlayer {
            bot_spec,
            code_bundle_id: None,
        }
    }
}

impl RunMatch {
    pub fn from_players(players: Vec<MatchPlayer>) -> Self {
        let log_file_name = format!("{}.log", gen_alphanumeric(16));
        RunMatch {
            log_file_name,
            players,
            match_id: None,
        }
    }

    pub fn into_runner_config(self) -> runner::MatchConfig {
        runner::MatchConfig {
            map_path: PathBuf::from(MAPS_DIR).join("hex.json"),
            map_name: "hex".to_string(),
            log_path: PathBuf::from(MATCHES_DIR).join(&self.log_file_name),
            players: self
                .players
                .into_iter()
                .map(|player| runner::MatchPlayer {
                    bot_spec: player.bot_spec,
                })
                .collect(),
        }
    }

    pub fn store_in_database(&mut self, db_conn: &PgConnection) -> QueryResult<MatchData> {
        // don't store the same match twice
        assert!(self.match_id.is_none());

        let new_match_data = db::matches::NewMatch {
            state: db::matches::MatchState::Playing,
            log_path: &self.log_file_name,
        };
        let new_match_players = self
            .players
            .iter()
            .map(|p| db::matches::MatchPlayerData {
                code_bundle_id: p.code_bundle_id,
            })
            .collect::<Vec<_>>();

        let match_data = db::matches::create_match(&new_match_data, &new_match_players, &db_conn)?;
        self.match_id = Some(match_data.base.id);
        Ok(match_data)
    }

    pub fn spawn(self, pool: ConnectionPool) -> JoinHandle<MatchOutcome> {
        let match_id = self.match_id.expect("match must be saved before running");
        let runner_config = self.into_runner_config();
        tokio::spawn(run_match_task(pool, runner_config, match_id))
    }
}

pub fn code_bundle_to_botspec(code_bundle: &db::bots::BotVersion) -> Box<dyn BotSpec> {
    // TODO: get rid of this unwrap
    let bundle_path = PathBuf::from(BOTS_DIR).join(code_bundle.code_bundle_path.as_ref().unwrap());

    Box::new(DockerBotSpec {
        code_path: bundle_path,
        image: PYTHON_IMAGE.to_string(),
        argv: vec!["python".to_string(), "bot.py".to_string()],
    })
}

async fn run_match_task(
    connection_pool: ConnectionPool,
    match_config: MatchConfig,
    match_id: i32,
) -> MatchOutcome {
    let outcome = runner::run_match(match_config).await;

    // update match state in database
    let conn = connection_pool
        .get()
        .await
        .expect("could not get database connection");

    let result = MatchResult::Finished {
        winner: outcome.winner.map(|w| (w - 1) as i32), // player numbers in matchrunner start at 1
    };

    db::matches::save_match_result(match_id, result, &conn).expect("could not save match result");

    outcome
}