added faction peace treaties and territory wars
This commit is contained in:
parent
e589b7b438
commit
f28c390394
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "torn-api"
|
name = "torn-api"
|
||||||
version = "0.5.12"
|
version = "0.5.13"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Pyrit [2111649]"]
|
authors = ["Pyrit [2111649]"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#![allow(unused)]
|
#![allow(unused)]
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||||
use serde::de::{Deserialize, Deserializer, Error, Unexpected, Visitor};
|
use serde::de::{Deserialize, Deserializer, Error, Unexpected, Visitor};
|
||||||
|
|
||||||
|
@ -92,6 +94,47 @@ where
|
||||||
deserializer.deserialize_any(DumbVisitor)
|
deserializer.deserialize_any(DumbVisitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn datetime_map<'de, D>(
|
||||||
|
deserializer: D,
|
||||||
|
) -> Result<BTreeMap<i32, chrono::DateTime<chrono::Utc>>, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
struct UnixTimestamp(
|
||||||
|
#[serde(with = "chrono::serde::ts_seconds")] chrono::DateTime<chrono::Utc>,
|
||||||
|
);
|
||||||
|
|
||||||
|
struct MapVisitor;
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for MapVisitor {
|
||||||
|
type Value = BTreeMap<i32, chrono::DateTime<chrono::Utc>>;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
write!(formatter, "map of unix timestamps")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
||||||
|
where
|
||||||
|
A: serde::de::MapAccess<'de>,
|
||||||
|
{
|
||||||
|
let mut result = BTreeMap::new();
|
||||||
|
while let Some(key) = map.next_key::<&'de str>()? {
|
||||||
|
let id = key
|
||||||
|
.parse()
|
||||||
|
.map_err(|_e| A::Error::invalid_value(Unexpected::Str(key), &"integer"))?;
|
||||||
|
|
||||||
|
let ts: UnixTimestamp = map.next_value()?;
|
||||||
|
result.insert(id, ts.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deserializer.deserialize_map(MapVisitor)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "decimal")]
|
#[cfg(feature = "decimal")]
|
||||||
pub(crate) fn string_or_decimal<'de, D>(deserializer: D) -> Result<rust_decimal::Decimal, D::Error>
|
pub(crate) fn string_or_decimal<'de, D>(deserializer: D) -> Result<rust_decimal::Decimal, D::Error>
|
||||||
where
|
where
|
||||||
|
|
|
@ -35,6 +35,21 @@ pub struct Member<'a> {
|
||||||
pub last_action: LastAction,
|
pub last_action: LastAction,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
pub struct FactionTerritoryWar<'a> {
|
||||||
|
pub territory: &'a str,
|
||||||
|
pub assaulting_faction: i32,
|
||||||
|
pub defending_faction: i32,
|
||||||
|
pub score: i32,
|
||||||
|
pub required_score: i32,
|
||||||
|
|
||||||
|
#[serde(with = "chrono::serde::ts_seconds")]
|
||||||
|
pub start_time: DateTime<Utc>,
|
||||||
|
|
||||||
|
#[serde(with = "chrono::serde::ts_seconds")]
|
||||||
|
pub end_time: DateTime<Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
pub struct Basic<'a> {
|
pub struct Basic<'a> {
|
||||||
#[serde(rename = "ID")]
|
#[serde(rename = "ID")]
|
||||||
|
@ -49,6 +64,12 @@ pub struct Basic<'a> {
|
||||||
|
|
||||||
#[serde(borrow)]
|
#[serde(borrow)]
|
||||||
pub members: BTreeMap<i32, Member<'a>>,
|
pub members: BTreeMap<i32, Member<'a>>,
|
||||||
|
|
||||||
|
#[serde(deserialize_with = "de_util::datetime_map")]
|
||||||
|
pub peace: BTreeMap<i32, DateTime<Utc>>,
|
||||||
|
|
||||||
|
#[serde(borrow)]
|
||||||
|
pub territory_wars: Vec<FactionTerritoryWar<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Deserialize)]
|
#[derive(Debug, Clone, Copy, Deserialize)]
|
||||||
|
|
|
@ -852,6 +852,45 @@ pub(crate) mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
async fn test_concurrent_spread() {
|
||||||
|
let storage = Arc::new(setup().await.0);
|
||||||
|
|
||||||
|
for i in 0..24 {
|
||||||
|
storage
|
||||||
|
.store_key(1, format!("{}", i), vec![Domain::All])
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
for _ in 0..10 {
|
||||||
|
let mut set = tokio::task::JoinSet::new();
|
||||||
|
|
||||||
|
for _ in 0..50 {
|
||||||
|
let storage = storage.clone();
|
||||||
|
set.spawn(async move {
|
||||||
|
storage.acquire_key(Domain::All).await.unwrap();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for _ in 0..50 {
|
||||||
|
set.join_next().await.unwrap().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
let keys = storage.read_user_keys(1).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(keys.len(), 25);
|
||||||
|
|
||||||
|
for key in keys {
|
||||||
|
assert_eq!(key.uses, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlx::query("update api_keys set uses=0")
|
||||||
|
.execute(&storage.pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
// HACK: this test is time sensitive and will fail if runs at the top of the minute
|
// HACK: this test is time sensitive and will fail if runs at the top of the minute
|
||||||
#[test]
|
#[test]
|
||||||
async fn test_concurrent_many() {
|
async fn test_concurrent_many() {
|
||||||
|
|
Loading…
Reference in a new issue