Skip to content

Commit d5202e0

Browse files
committed
Fix for non-ladder game modes
1 parent 3dc6870 commit d5202e0

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

‎src/base/clash.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ pub struct ClashBattleArena {
1616
pub struct ClashBattleTeammate {
1717
pub tag: String,
1818
pub name: String,
19-
pub starting_trophies: i64,
20-
pub trophy_change: i64,
19+
// Non-ladder modes (party/PvP variants) omit the trophy and tower fields.
20+
pub starting_trophies: Option<i64>,
21+
pub trophy_change: Option<i64>,
2122
pub crowns: i64,
22-
pub king_tower_hit_points: i64,
23+
pub king_tower_hit_points: Option<i64>,
2324
pub princess_towers_hit_points: Option<Vec<i64>>,
24-
pub elixir_leaked: f64,
25+
pub elixir_leaked: Option<f64>,
2526
pub cards: Vec<ClashCard>,
2627
}
2728

@@ -62,8 +63,13 @@ pub async fn last_battles() -> reqwest::Result<Vec<ClashBattle>> {
6263

6364
let url = format!("https://api.clashroyale.com/v1/players/{tag}/battlelog");
6465
let res: Value = client.get(url).send().await?.json().await?;
65-
dbg!(&res);
66-
let res: Vec<ClashBattle> = serde_json::from_value(res).unwrap();
66+
let res: Vec<ClashBattle> = match serde_json::from_value(res) {
67+
Ok(battles) => battles,
68+
Err(e) => {
69+
eprintln!("failed to parse clash battlelog: {e}");
70+
Vec::new()
71+
}
72+
};
6773
Ok(res)
6874
}
6975

‎src/main.rs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,19 @@ fn start_jobs(state: Arc<RwLock<State>>) {
9595
}
9696

9797
match clash::last_battles().await {
98-
Ok(battles) => state.write().last_clash_battle = battles.first().cloned(),
98+
Ok(battles) => {
99+
// Find the first ladder game (there are so many party modes now...tsk)
100+
let last_ladder_game = battles.iter().find(|b| {
101+
let my_tag = std::env::var("CLASH_ROYALE_TAG")
102+
.expect("an env var named CLASH_ROYALE_TAG");
103+
b.team
104+
.iter()
105+
.find(|t| t.tag == my_tag)
106+
.map(|m| m.trophy_change.is_some())
107+
.unwrap_or(false)
108+
});
109+
state.write().last_clash_battle = last_ladder_game.cloned();
110+
}
99111
Err(e) => eprintln!("failed to fetch clash battles: {e}"),
100112
}
101113

@@ -313,7 +325,7 @@ fn root(request: Request, state: Arc<RwLock<State>>) {
313325

314326
let ago = chrono_humanize::HumanTime::from(battle.battle_time).to_string();
315327

316-
let s = if me.trophy_change.is_positive() {
328+
let s = if me.trophy_change.expect("a None trophy_change should have been caught at the last_clash_battle setting point. It indicates it wasn't a ladder match.").is_positive() {
317329
format!(
318330
"\n{ago} I {}-crowned a {} deck using my {} deck in Clash Royale.\n",
319331
me.crowns,

0 commit comments

Comments
 (0)