aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/routes
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-10-11 22:58:42 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-10-11 22:58:42 +0200
commit19b9a6ea1b8a36ae2301ffbc95cf2f54bf7fa77f (patch)
tree95809a4813381520abdcdedfeb5caf61c975482f /planetwars-server/src/routes
parent8651f1d8f196a61aff1b4eef0b475649b2c58444 (diff)
downloadplanetwars.dev-19b9a6ea1b8a36ae2301ffbc95cf2f54bf7fa77f.tar.xz
planetwars.dev-19b9a6ea1b8a36ae2301ffbc95cf2f54bf7fa77f.zip
add new bot stats endpoint
Diffstat (limited to 'planetwars-server/src/routes')
-rw-r--r--planetwars-server/src/routes/bots.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs
index 4ab1b4e..f8087fd 100644
--- a/planetwars-server/src/routes/bots.rs
+++ b/planetwars-server/src/routes/bots.rs
@@ -7,6 +7,7 @@ use rand::distributions::Alphanumeric;
use rand::Rng;
use serde::{Deserialize, Serialize};
use serde_json::{self, json, value::Value as JsonValue};
+use std::collections::HashMap;
use std::io::Cursor;
use std::path::PathBuf;
use std::sync::Arc;
@@ -275,3 +276,41 @@ pub async fn get_code(
std::fs::read(full_bundle_path).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(bot_code)
}
+
+#[derive(Default, Serialize, Deserialize)]
+pub struct MatchupStats {
+ win: i64,
+ loss: i64,
+ tie: i64,
+}
+
+impl MatchupStats {
+ fn update(&mut self, win: Option<bool>, count: i64) {
+ match win {
+ Some(true) => self.win += count,
+ Some(false) => self.loss += count,
+ None => self.tie += count,
+ }
+ }
+}
+
+type BotStats = HashMap<String, HashMap<String, MatchupStats>>;
+
+pub async fn get_bot_stats(
+ conn: DatabaseConnection,
+ Path(bot_name): Path<String>,
+) -> Result<Json<BotStats>, StatusCode> {
+ let stats_records = db::matches::fetch_bot_stats(&bot_name, &conn)
+ .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
+ let mut bot_stats: BotStats = HashMap::new();
+ for record in stats_records {
+ bot_stats
+ .entry(record.opponent)
+ .or_default()
+ .entry(record.map)
+ .or_default()
+ .update(record.win, record.count);
+ }
+
+ Ok(Json(bot_stats))
+}