|
| 1 | +use std::ops::Deref; |
| 2 | + |
| 3 | +use chrono::DateTime; |
| 4 | +use poem::Result; |
| 5 | +use poem_openapi::{payload::Json, types::ToJSON, Object, OpenApi}; |
| 6 | +use reqwest::{Client, ClientBuilder}; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use tracing::{info, warn}; |
| 9 | + |
| 10 | +use super::core::*; |
| 11 | + |
| 12 | +// Payload with the recent servers the user has connected to |
| 13 | +#[derive(Debug, Serialize, Deserialize)] |
| 14 | +pub struct BattleMetricsQuickMatchPayload { |
| 15 | + pub data: Vec<BattleMetricsType>, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Debug, Serialize, Deserialize, Object)] |
| 19 | +pub struct BattleMetricsPlayer { |
| 20 | + // `id` from BattleMetrics |
| 21 | + pub bm_id: String, |
| 22 | + pub name: Option<String>, |
| 23 | + pub private: Option<bool>, |
| 24 | + pub last_seen: Option<String>, |
| 25 | +} |
| 26 | + |
| 27 | +impl From<&BattleMetricsType> for BattleMetricsPlayer { |
| 28 | + fn from(response: &BattleMetricsType) -> Self { |
| 29 | + BattleMetricsPlayer { |
| 30 | + bm_id: response |
| 31 | + .relationships |
| 32 | + .as_ref() |
| 33 | + .and_then(|r| r.player.as_ref().map(|p| p.data.id.clone())) |
| 34 | + .unwrap_or_default(), |
| 35 | + name: response |
| 36 | + .attributes |
| 37 | + .as_ref() |
| 38 | + .and_then(|a| a.extra.get("identifier").map(|v| v.to_string())), |
| 39 | + private: response |
| 40 | + .attributes |
| 41 | + .as_ref() |
| 42 | + .and_then(|a| a.extra.get("private").map(|v| v.as_bool().unwrap_or(false))), |
| 43 | + last_seen: response |
| 44 | + .attributes |
| 45 | + .as_ref() |
| 46 | + .and_then(|a| a.extra.get("lastSeen").map(|v| v.as_str().unwrap_or_default().to_string())), |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[derive(Debug, Serialize, Deserialize)] |
| 52 | +pub struct BattleMetricsPlayerResponse { |
| 53 | + pub data: BattleMetricsPlayer, |
| 54 | +} |
| 55 | + |
| 56 | +impl BattleMetricsPlayerResponse { |
| 57 | + pub fn from(response: BattleMetricsResponse) -> Result<Self> { |
| 58 | + // most last_seen response |
| 59 | + let mut data: Vec<BattleMetricsPlayer> = response |
| 60 | + .data |
| 61 | + .into_iter() |
| 62 | + .map(|x| BattleMetricsPlayer::from(&x)) |
| 63 | + .collect(); |
| 64 | + |
| 65 | + data.sort_by(|a, b| b.last_seen.cmp(&a.last_seen)); |
| 66 | + |
| 67 | + // 5 days ago |
| 68 | + let day_cap = chrono::Duration::days(5); |
| 69 | + let now = chrono::Utc::now(); |
| 70 | + |
| 71 | + info!("length pre filtering: {}", data.len()); |
| 72 | + |
| 73 | + // filter out any players that haven't been seen in the last 5 days |
| 74 | + data.retain(|x| { |
| 75 | + // parse ISO 8601 dates like "2025-03-15T07:23:03.173Z" |
| 76 | + let last_seen_str = match x.last_seen.as_deref() { |
| 77 | + Some(last_seen) => last_seen, |
| 78 | + None => return false, |
| 79 | + }; |
| 80 | + // Remove any surrounding quotes that might have been added |
| 81 | + let cleaned_str = last_seen_str.trim_matches('"'); |
| 82 | + |
| 83 | + info!("cleaned_str: {:?}", cleaned_str); |
| 84 | + |
| 85 | + let last_seen = match chrono::DateTime::parse_from_rfc3339(cleaned_str) { |
| 86 | + Ok(last_seen) => last_seen, |
| 87 | + Err(e) => { |
| 88 | + warn!("Failed to parse last seen: {} - {:?}", cleaned_str, e); |
| 89 | + return false; |
| 90 | + }, |
| 91 | + }; |
| 92 | + |
| 93 | + info!("last_seen: {:?}", last_seen); |
| 94 | + |
| 95 | + last_seen > now - day_cap |
| 96 | + }); |
| 97 | + |
| 98 | + info!("length post filtering: {}", data.len()); |
| 99 | + |
| 100 | + // log all results |
| 101 | + |
| 102 | + info!("data: {:?}", data); |
| 103 | + |
| 104 | + let result_threshold = 4; |
| 105 | + |
| 106 | + // if there are more then threshold results |
| 107 | + if data.len() > result_threshold { |
| 108 | + return Err(poem::Error::from_string( |
| 109 | + format!("Too many results: {}", data.len()), |
| 110 | + poem::http::StatusCode::BAD_REQUEST, |
| 111 | + )); |
| 112 | + } |
| 113 | + |
| 114 | + if data.is_empty() { |
| 115 | + return Err(poem::Error::from_string( |
| 116 | + "No results found".to_string(), |
| 117 | + poem::http::StatusCode::BAD_REQUEST, |
| 118 | + )); |
| 119 | + } |
| 120 | + |
| 121 | + // extract the first result and discard the rest |
| 122 | + let data = data.into_iter().next().unwrap(); |
| 123 | + |
| 124 | + Ok(Self { data }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +pub async fn get_quick_match_players( |
| 129 | + player_name: String, |
| 130 | + auth_token: &String, |
| 131 | +) -> Result<BattleMetricsPlayerResponse> { |
| 132 | + let payload = BattleMetricsQuickMatchPayload { |
| 133 | + data: vec![BattleMetricsType { |
| 134 | + _type: "identifier".to_string(), |
| 135 | + id: None, |
| 136 | + attributes: Some(BattleMetricsAttributes { |
| 137 | + _type: Some("name".to_string()), |
| 138 | + name: None, |
| 139 | + ip: None, |
| 140 | + extra: serde_json::json!({ "identifier": player_name }), |
| 141 | + port: None, |
| 142 | + }), |
| 143 | + relationships: None, |
| 144 | + meta: None, |
| 145 | + }], |
| 146 | + }; |
| 147 | + |
| 148 | + let payload_json = serde_json::to_string(&payload).unwrap(); |
| 149 | + info!("Payload: {}", payload_json); |
| 150 | + |
| 151 | + let client = ClientBuilder::new() |
| 152 | + .timeout(std::time::Duration::from_secs(10)) |
| 153 | + .use_rustls_tls() |
| 154 | + .build() |
| 155 | + .map_err(|e| { |
| 156 | + warn!("Failed to build HTTP client: {}", e); |
| 157 | + poem::Error::from_string( |
| 158 | + format!("Client build error: {}", e), |
| 159 | + poem::http::StatusCode::INTERNAL_SERVER_ERROR, |
| 160 | + ) |
| 161 | + })?; |
| 162 | + |
| 163 | + let url = "https://api.battlemetrics.com/players/quick-match?page[size]=5"; |
| 164 | + |
| 165 | + let response = client |
| 166 | + .post(url) |
| 167 | + .json(&payload) |
| 168 | + .header("Authorization", format!("Bearer {}", auth_token)) |
| 169 | + .timeout(std::time::Duration::from_secs(2)) |
| 170 | + .send() |
| 171 | + .await |
| 172 | + .map_err(|e| { |
| 173 | + warn!("Failed to send request: {}", e); |
| 174 | + poem::Error::from_string( |
| 175 | + format!("Request error: {}", e), |
| 176 | + poem::http::StatusCode::INTERNAL_SERVER_ERROR, |
| 177 | + ) |
| 178 | + })?; |
| 179 | + |
| 180 | + let body = response.text().await.map_err(|e| { |
| 181 | + warn!("Failed to get response text: {}", e); |
| 182 | + poem::Error::from_string( |
| 183 | + format!("Response text error: {}", e), |
| 184 | + poem::http::StatusCode::INTERNAL_SERVER_ERROR, |
| 185 | + ) |
| 186 | + })?; |
| 187 | + |
| 188 | + tracing::info!("Response body: {}", body); |
| 189 | + |
| 190 | + let search_response: BattleMetricsResponse = serde_json::from_str(&body).map_err(|e| { |
| 191 | + warn!("Failed to parse JSON: {}", e); |
| 192 | + poem::Error::from_string( |
| 193 | + format!("JSON parse error: {}", e), |
| 194 | + poem::http::StatusCode::INTERNAL_SERVER_ERROR, |
| 195 | + ) |
| 196 | + })?; |
| 197 | + |
| 198 | + tracing::info!("Search response: {:?}", search_response); |
| 199 | + |
| 200 | + // convert to |
| 201 | + let data = BattleMetricsPlayerResponse::from(search_response)?; |
| 202 | + |
| 203 | + Ok(data) |
| 204 | +} |
0 commit comments