Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/database/events.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use chrono::NaiveDateTime;
use rocket::futures::StreamExt as _;
Expand Down Expand Up @@ -572,6 +572,47 @@ pub async fn get_threat_overview(
Ok(threats)
}

pub async fn get_antagonist_overview(
limit: i32,
exclude_round: Option<i32>,
connection: &mut PoolConnection<MySql>,
) -> Result<HashMap<u32, (i32, i32)>, Error> {
let feedbacks = get_feedbacks(
"associative",
"antagonists",
limit,
exclude_round,
connection,
)
.await?;

let mut antagonists = HashMap::new();

for feedback in &feedbacks {
let round_id = match feedback.round_id {
Some(id) => id,
None => continue,
};

if let Some(data_map) = feedback.json.get("data").and_then(|d| d.as_object()) {
let mut total_count = 0;
let mut unique_types = HashSet::new();

for (_key, val) in data_map {
total_count += 1;

if let Some(antag_type) = val.get("antagonist_type").and_then(|t| t.as_str()) {
unique_types.insert(antag_type.to_string());
}
}

antagonists.insert(round_id, (total_count, unique_types.len() as i32));
}
}

Ok(antagonists)
}

#[derive(Debug, Serialize)]
pub struct Overview {
pub round_id: u32,
Expand All @@ -584,6 +625,8 @@ pub struct Overview {
pub players: u32,
pub dynamic_tier: i32,
pub readied_players: i32,
pub antagonist_count: i32,
pub unique_antagonist_count: i32,
}

pub async fn get_overview(
Expand All @@ -601,6 +644,7 @@ pub async fn get_overview(
let crimes = get_crimes_overview(limit, exclude_round, &mut connection).await?;
let players = get_players_overview(limit, exclude_round, &mut connection).await?;
let threat_levels = get_threat_overview(limit, exclude_round, &mut connection).await?;
let antagonist = get_antagonist_overview(limit, exclude_round, &mut connection).await?;

connection.close().await?;

Expand All @@ -615,6 +659,8 @@ pub async fn get_overview(
let crimes = *crimes.get(round_id).unwrap_or(&0);
let players = *players.get(round_id).unwrap_or(&0);
let (dynamic_tier, readied_players) = *threat_levels.get(round_id).unwrap_or(&(0, 0));
let (antagonist_count, unique_antagonist_count) =
*antagonist.get(round_id).unwrap_or(&(0, 0));

let overview_ = Overview {
round_id: *round_id,
Expand All @@ -626,6 +672,8 @@ pub async fn get_overview(
players,
dynamic_tier,
readied_players,
antagonist_count,
unique_antagonist_count,
};

overview.push(overview_);
Expand Down
10 changes: 10 additions & 0 deletions src/database/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct RoundData {
pub nukedisk: Option<Value>,
pub antagonists: Vec<Value>,
pub dynamic_tier: Option<i32>,
pub storyteller: Option<String>,
#[serde(with = "crate::serde::datetime")]
pub initialize_datetime: NaiveDateTime,
#[serde(with = "crate::serde::opt_datetime")]
Expand Down Expand Up @@ -58,6 +59,8 @@ pub async fn get_round(
get_feedback("associative", "dynamic_tier", round_id, &mut connection).await?;
let antagonists_feedback =
get_feedback("associative", "antagonists", round_id, &mut connection).await?;
let storyteller_feedback =
get_feedback("associative", "storyteller", round_id, &mut connection).await?;

let nukedisk: Option<Value> = nukedisk_feedback
.and_then(|fb| fb.json.get("data").cloned())
Expand All @@ -67,6 +70,11 @@ pub async fn get_round(
.and_then(|data| data.get("1").cloned())
.and_then(|data| data.get("tier").cloned())
.and_then(|v| v.as_str()?.parse::<i32>().ok());
let storyteller: Option<String> = storyteller_feedback
.and_then(|fb| fb.json.get("data").cloned())
.and_then(|data| data.get("1").cloned())
.and_then(|data| data.get("name").cloned())
.and_then(|value| value.as_str().map(String::from));
let antagonists_objects: Option<Value> =
antagonists_feedback.and_then(|fb| fb.json.get("data").cloned());
let antagonists: Vec<Value> = match antagonists_objects {
Expand Down Expand Up @@ -106,6 +114,7 @@ pub async fn get_round(
nukedisk,
antagonists,
dynamic_tier,
storyteller,
};

connection.close().await?;
Expand Down Expand Up @@ -236,6 +245,7 @@ pub async fn get_rounds(
antagonists: Vec::new(),
nukedisk: None,
dynamic_tier: None,
storyteller: None,
};

rounds.push(round);
Expand Down