From 3e3909bb6c9be8b7bb77f7e38cc377d5718c78ea Mon Sep 17 00:00:00 2001 From: TotallyNot <44345987+TotallyNot@users.noreply.github.com> Date: Tue, 16 May 2023 11:49:52 +0200 Subject: [PATCH] added user->attacks --- torn-api/Cargo.toml | 2 +- torn-api/src/common.rs | 105 ++++++++++++++++++++++++++++++++++++++ torn-api/src/faction.rs | 109 +--------------------------------------- torn-api/src/user.rs | 10 +++- 4 files changed, 117 insertions(+), 109 deletions(-) diff --git a/torn-api/Cargo.toml b/torn-api/Cargo.toml index 7b3cdf1..59e280b 100644 --- a/torn-api/Cargo.toml +++ b/torn-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "torn-api" -version = "0.5.17" +version = "0.5.18" edition = "2021" authors = ["Pyrit [2111649]"] license = "MIT" diff --git a/torn-api/src/common.rs b/torn-api/src/common.rs index 3cc948a..91789ed 100644 --- a/torn-api/src/common.rs +++ b/torn-api/src/common.rs @@ -56,3 +56,108 @@ pub struct Territory { #[serde(deserialize_with = "de_util::string_or_decimal")] pub coordinate_y: rust_decimal::Decimal, } + +#[derive(Debug, Clone, Copy, Deserialize)] +pub enum AttackResult { + Attacked, + Mugged, + Hospitalized, + Lost, + Arrested, + Escape, + Interrupted, + Assist, + Timeout, + Stalemate, + Special, + Looted, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct Attack<'a> { + pub code: &'a str, + #[serde(with = "ts_seconds")] + pub timestamp_started: DateTime, + #[serde(with = "ts_seconds")] + pub timestamp_ended: DateTime, + + #[serde(deserialize_with = "de_util::empty_string_int_option")] + pub attacker_id: Option, + #[serde(deserialize_with = "de_util::empty_string_int_option")] + pub attacker_faction: Option, + pub defender_id: i32, + #[serde(deserialize_with = "de_util::empty_string_int_option")] + pub defender_faction: Option, + pub result: AttackResult, + + #[serde(deserialize_with = "de_util::int_is_bool")] + pub stealthed: bool, + + #[cfg(feature = "decimal")] + pub respect: rust_decimal::Decimal, + + #[cfg(not(feature = "decimal"))] + pub respect: f32, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct RespectModifiers { + pub fair_fight: f32, + pub war: f32, + pub retaliation: f32, + pub group_attack: f32, + pub overseas: f32, + pub chain_bonus: f32, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct AttackFull<'a> { + pub code: &'a str, + #[serde(with = "ts_seconds")] + pub timestamp_started: DateTime, + #[serde(with = "ts_seconds")] + pub timestamp_ended: DateTime, + + #[serde(deserialize_with = "de_util::empty_string_int_option")] + pub attacker_id: Option, + #[serde(deserialize_with = "de_util::empty_string_is_none")] + pub attacker_name: Option<&'a str>, + #[serde(deserialize_with = "de_util::empty_string_int_option")] + pub attacker_faction: Option, + #[serde( + deserialize_with = "de_util::empty_string_is_none", + rename = "attacker_factionname" + )] + pub attacker_faction_name: Option<&'a str>, + + pub defender_id: i32, + pub defender_name: &'a str, + #[serde(deserialize_with = "de_util::empty_string_int_option")] + pub defender_faction: Option, + #[serde( + deserialize_with = "de_util::empty_string_is_none", + rename = "defender_factionname" + )] + pub defender_faction_name: Option<&'a str>, + + pub result: AttackResult, + + #[serde(deserialize_with = "de_util::int_is_bool")] + pub stealthed: bool, + #[serde(deserialize_with = "de_util::int_is_bool")] + pub raid: bool, + #[serde(deserialize_with = "de_util::int_is_bool")] + pub ranked_war: bool, + + #[cfg(feature = "decimal")] + pub respect: rust_decimal::Decimal, + #[cfg(feature = "decimal")] + pub respect_loss: rust_decimal::Decimal, + + #[cfg(not(feature = "decimal"))] + pub respect: f32, + #[cfg(not(feature = "decimal"))] + pub respect_loss: f32, + + pub modifiers: RespectModifiers, +} diff --git a/torn-api/src/faction.rs b/torn-api/src/faction.rs index d4b611f..298b00e 100644 --- a/torn-api/src/faction.rs +++ b/torn-api/src/faction.rs @@ -1,13 +1,13 @@ use std::collections::{BTreeMap, HashMap}; -use chrono::{serde::ts_seconds, DateTime, Utc}; +use chrono::{DateTime, Utc}; use serde::Deserialize; use torn_api_macros::ApiCategory; use crate::de_util::{self, null_is_empty_dict}; -pub use crate::common::{LastAction, Status, Territory}; +pub use crate::common::{Attack, AttackFull, LastAction, Status, Territory}; #[derive(Debug, Clone, Copy, ApiCategory)] #[api(category = "faction")] @@ -76,111 +76,6 @@ pub struct Basic<'a> { pub territory_wars: Vec>, } -#[derive(Debug, Clone, Copy, Deserialize)] -pub enum AttackResult { - Attacked, - Mugged, - Hospitalized, - Lost, - Arrested, - Escape, - Interrupted, - Assist, - Timeout, - Stalemate, - Special, - Looted, -} - -#[derive(Debug, Clone, Deserialize)] -pub struct Attack<'a> { - pub code: &'a str, - #[serde(with = "ts_seconds")] - pub timestamp_started: DateTime, - #[serde(with = "ts_seconds")] - pub timestamp_ended: DateTime, - - #[serde(deserialize_with = "de_util::empty_string_int_option")] - pub attacker_id: Option, - #[serde(deserialize_with = "de_util::empty_string_int_option")] - pub attacker_faction: Option, - pub defender_id: i32, - #[serde(deserialize_with = "de_util::empty_string_int_option")] - pub defender_faction: Option, - pub result: AttackResult, - - #[serde(deserialize_with = "de_util::int_is_bool")] - pub stealthed: bool, - - #[cfg(feature = "decimal")] - pub respect: rust_decimal::Decimal, - - #[cfg(not(feature = "decimal"))] - pub respect: f32, -} - -#[derive(Debug, Clone, Deserialize)] -pub struct RespectModifiers { - pub fair_fight: f32, - pub war: f32, - pub retaliation: f32, - pub group_attack: f32, - pub overseas: f32, - pub chain_bonus: f32, -} - -#[derive(Debug, Clone, Deserialize)] -pub struct AttackFull<'a> { - pub code: &'a str, - #[serde(with = "ts_seconds")] - pub timestamp_started: DateTime, - #[serde(with = "ts_seconds")] - pub timestamp_ended: DateTime, - - #[serde(deserialize_with = "de_util::empty_string_int_option")] - pub attacker_id: Option, - #[serde(deserialize_with = "de_util::empty_string_is_none")] - pub attacker_name: Option<&'a str>, - #[serde(deserialize_with = "de_util::empty_string_int_option")] - pub attacker_faction: Option, - #[serde( - deserialize_with = "de_util::empty_string_is_none", - rename = "attacker_factionname" - )] - pub attacker_faction_name: Option<&'a str>, - - pub defender_id: i32, - pub defender_name: &'a str, - #[serde(deserialize_with = "de_util::empty_string_int_option")] - pub defender_faction: Option, - #[serde( - deserialize_with = "de_util::empty_string_is_none", - rename = "defender_factionname" - )] - pub defender_faction_name: Option<&'a str>, - - pub result: AttackResult, - - #[serde(deserialize_with = "de_util::int_is_bool")] - pub stealthed: bool, - #[serde(deserialize_with = "de_util::int_is_bool")] - pub raid: bool, - #[serde(deserialize_with = "de_util::int_is_bool")] - pub ranked_war: bool, - - #[cfg(feature = "decimal")] - pub respect: rust_decimal::Decimal, - #[cfg(feature = "decimal")] - pub respect_loss: rust_decimal::Decimal, - - #[cfg(not(feature = "decimal"))] - pub respect: f32, - #[cfg(not(feature = "decimal"))] - pub respect_loss: f32, - - pub modifiers: RespectModifiers, -} - #[cfg(test)] mod tests { use super::*; diff --git a/torn-api/src/user.rs b/torn-api/src/user.rs index 52053ab..7f1f1b7 100644 --- a/torn-api/src/user.rs +++ b/torn-api/src/user.rs @@ -2,12 +2,13 @@ use serde::{ de::{self, MapAccess, Visitor}, Deserialize, Deserializer, }; +use std::collections::BTreeMap; use torn_api_macros::ApiCategory; use crate::de_util; -pub use crate::common::{LastAction, Status}; +pub use crate::common::{Attack, AttackFull, LastAction, Status}; #[derive(Debug, Clone, Copy, ApiCategory)] #[api(category = "user")] @@ -22,6 +23,10 @@ pub enum Selection { PersonalStats, #[api(type = "CriminalRecord", field = "criminalrecord")] Crimes, + #[api(type = "BTreeMap", field = "attacks")] + AttacksFull, + #[api(type = "BTreeMap", field = "attacks")] + Attacks, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] @@ -404,6 +409,7 @@ mod tests { Selection::Profile, Selection::PersonalStats, Selection::Crimes, + Selection::Attacks, ]) }) .await @@ -414,6 +420,8 @@ mod tests { response.profile().unwrap(); response.personal_stats().unwrap(); response.crimes().unwrap(); + response.attacks().unwrap(); + response.attacks_full().unwrap(); } #[async_test]