去重一下card-event,防止彩盘乱发。
This commit is contained in:
+25
-8
@@ -1,8 +1,9 @@
|
|||||||
use crate::data::CurrentVersion;
|
use crate::data::{CurrentVersion, EventCard};
|
||||||
use crate::{ending_point, OPT_CONFIG, STORE, UA};
|
use crate::{OPT_CONFIG, STORE, UA, ending_point};
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::sync::OnceLock;
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
const COMMIT_URL: &str =
|
const COMMIT_URL: &str =
|
||||||
@@ -59,6 +60,24 @@ pub fn raw_url(path: &str) -> String {
|
|||||||
+ path
|
+ path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn deduplicate_event_cards(cards: Vec<EventCard>) -> Vec<EventCard> {
|
||||||
|
let mut map: HashMap<u64, EventCard> = HashMap::new();
|
||||||
|
|
||||||
|
for card in cards {
|
||||||
|
map.entry(card.card_id)
|
||||||
|
.and_modify(|existing| {
|
||||||
|
if card.id < existing.id {
|
||||||
|
*existing = card.clone();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.or_insert(card);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut res: Vec<EventCard> = map.into_values().collect();
|
||||||
|
res.sort_by_key(|c| c.id);
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn sync_data() -> anyhow::Result<()> {
|
pub async fn sync_data() -> anyhow::Result<()> {
|
||||||
let client = http_client()?;
|
let client = http_client()?;
|
||||||
let (events_cards, event_stories, cards) = tokio::try_join!(
|
let (events_cards, event_stories, cards) = tokio::try_join!(
|
||||||
@@ -68,15 +87,13 @@ pub async fn sync_data() -> anyhow::Result<()> {
|
|||||||
.send()
|
.send()
|
||||||
.await?
|
.await?
|
||||||
.text(),
|
.text(),
|
||||||
client
|
client.get(raw_url("cards.json")).send().await?.text()
|
||||||
.get(raw_url("cards.json"))
|
|
||||||
.send()
|
|
||||||
.await?
|
|
||||||
.text()
|
|
||||||
)?;
|
)?;
|
||||||
ending_point!(ok);
|
ending_point!(ok);
|
||||||
let mut store = STORE.write().map_err(|e| anyhow!("{}", e))?;
|
let mut store = STORE.write().map_err(|e| anyhow!("{}", e))?;
|
||||||
store.set_event_cards(serde_json::from_str(&events_cards)?);
|
store.set_event_cards(deduplicate_event_cards(serde_json::from_str(
|
||||||
|
&events_cards,
|
||||||
|
)?));
|
||||||
store.set_event_stories(serde_json::from_str(&event_stories)?);
|
store.set_event_stories(serde_json::from_str(&event_stories)?);
|
||||||
store.set_cards(serde_json::from_str(&cards)?);
|
store.set_cards(serde_json::from_str(&cards)?);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user