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
|
use axum::{
extract::{Path, Query},
Extension, Json,
};
use chrono::NaiveDateTime;
use hyper::StatusCode;
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, sync::Arc};
use crate::{
db::{
self,
matches::{self, BotMatchOutcome, MatchState},
},
DatabaseConnection, GlobalConfig,
};
use super::maps::ApiMap;
#[derive(Serialize, Deserialize)]
pub struct ApiMatch {
id: i32,
timestamp: chrono::NaiveDateTime,
state: MatchState,
players: Vec<ApiMatchPlayer>,
winner: Option<i32>,
map: Option<ApiMap>,
}
#[derive(Serialize, Deserialize)]
pub struct ApiMatchPlayer {
bot_version_id: Option<i32>,
bot_id: Option<i32>,
bot_name: Option<String>,
owner_id: Option<i32>,
had_errors: Option<bool>,
}
#[derive(Serialize, Deserialize)]
pub struct ListRecentMatchesParams {
count: Option<usize>,
// TODO: should timezone be specified here?
before: Option<NaiveDateTime>,
after: Option<NaiveDateTime>,
bot: Option<String>,
opponent: Option<String>,
had_errors: Option<bool>,
outcome: Option<BotMatchOutcome>,
}
const MAX_NUM_RETURNED_MATCHES: usize = 100;
const DEFAULT_NUM_RETURNED_MATCHES: usize = 50;
#[derive(Serialize, Deserialize)]
pub struct ListMatchesResponse {
matches: Vec<ApiMatch>,
has_next: bool,
}
pub async fn list_recent_matches(
Query(params): Query<ListRecentMatchesParams>,
mut conn: DatabaseConnection,
) -> Result<Json<ListMatchesResponse>, StatusCode> {
let requested_count = std::cmp::min(
params.count.unwrap_or(DEFAULT_NUM_RETURNED_MATCHES),
MAX_NUM_RETURNED_MATCHES,
);
// fetch one additional record to check whether a next page exists
let count = (requested_count + 1) as i64;
let matches_result = match params.bot {
Some(bot_name) => {
let bot = db::bots::find_bot_by_name(&bot_name, &mut conn)
.map_err(|_| StatusCode::BAD_REQUEST)?;
let opponent_id = if let Some(opponent_name) = params.opponent {
let opponent = db::bots::find_bot_by_name(&opponent_name, &mut conn)
.map_err(|_| StatusCode::BAD_REQUEST)?;
Some(opponent.id)
} else {
None
};
matches::list_bot_matches(
bot.id,
opponent_id,
params.outcome,
params.had_errors,
count,
params.before,
params.after,
&mut conn,
)
}
None => matches::list_public_matches(count, params.before, params.after, &mut conn),
};
let mut matches = matches_result.expect("failed to get matches"); //.map_err(|_| StatusCode::BAD_REQUEST)?;
let mut has_next = false;
if matches.len() > requested_count {
has_next = true;
matches.truncate(requested_count);
}
let api_matches = matches.into_iter().map(match_data_to_api).collect();
Ok(Json(ListMatchesResponse {
matches: api_matches,
has_next,
}))
}
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()),
owner_id: p.bot.as_ref().and_then(|b| b.owner_id),
had_errors: p.base.had_errors,
})
.collect(),
winner: data.base.winner,
map: data.map.map(|m| ApiMap { name: m.name }),
}
}
pub async fn get_match_data(
Path(match_id): Path<i32>,
mut conn: DatabaseConnection,
) -> Result<Json<ApiMatch>, StatusCode> {
let match_data = matches::find_match(match_id, &mut 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>,
mut conn: DatabaseConnection,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<Vec<u8>, StatusCode> {
let match_base =
matches::find_match_base(match_id, &mut conn).map_err(|_| StatusCode::NOT_FOUND)?;
let log_path = PathBuf::from(&config.match_logs_directory).join(&match_base.log_path);
let log_contents = std::fs::read(log_path).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(log_contents)
}
|