Skip to content
This repository was archived by the owner on Mar 18, 2023. It is now read-only.

Commit ae1fe51

Browse files
committed
🩹 Quick fix for vehicles missing in the tankopedia
1 parent f6cd96c commit ae1fe51

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

‎src/models.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ pub enum TankType {
118118

119119
#[serde(rename = "AT-SPG")]
120120
AT,
121+
122+
#[serde(rename = "unknown")]
123+
Unknown,
121124
}
122125

123126
#[derive(Serialize, Deserialize, Clone)]

‎src/web/player/view.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use chrono_humanize::{Accuracy, HumanTime, Tense};
22
use clap::crate_name;
3-
use maud::{html, Markup, Render, DOCTYPE};
3+
use maud::{html, Markup, PreEscaped, Render, DOCTYPE};
44
use tide::StatusCode;
55

66
use crate::models::Vehicle;
@@ -233,14 +233,12 @@ pub async fn get(request: tide::Request<State>) -> tide::Result {
233233
}
234234
tbody {
235235
@for snapshot in &model.tank_snapshots {
236-
@if let Some(vehicle) = state.get_vehicle(snapshot.tank_id).await?.as_ref() {
237-
tr {
238-
th { (vehicle) }
239-
td { (snapshot.all_statistics.battles) }
240-
(render_confidence_interval_td(snapshot.all_statistics.battles, snapshot.all_statistics.wins))
241-
td { (snapshot.all_statistics.damage_dealt) }
242-
td { (format!("{:.0}", f64::from(snapshot.all_statistics.damage_dealt) / f64::from(snapshot.all_statistics.battles))) }
243-
}
236+
tr {
237+
th { (state.get_vehicle(snapshot.tank_id).await?.as_ref()) }
238+
td { (snapshot.all_statistics.battles) }
239+
(render_confidence_interval_td(snapshot.all_statistics.battles, snapshot.all_statistics.wins))
240+
td { (snapshot.all_statistics.damage_dealt) }
241+
td { (format!("{:.0}", f64::from(snapshot.all_statistics.damage_dealt) / f64::from(snapshot.all_statistics.battles))) }
244242
}
245243
}
246244
}
@@ -272,24 +270,25 @@ pub fn get_account_url(account_id: i32) -> String {
272270

273271
impl Render for &Vehicle {
274272
fn render(&self) -> Markup {
275-
let tier = match self.tier {
276-
1 => "â… ",
277-
2 => "â…¡",
278-
3 => "â…¢",
279-
4 => "â…£",
280-
5 => "â…¤",
281-
6 => "â…¥",
282-
7 => "â…¦",
283-
8 => "â…§",
284-
9 => "â…¨",
285-
10 => "â…©",
286-
_ => "?",
273+
let tier = PreEscaped(match self.tier {
274+
1 => "â… &nbsp;",
275+
2 => "â…¡&nbsp;",
276+
3 => "â…¢&nbsp;",
277+
4 => "â…£&nbsp;",
278+
5 => "â…¤&nbsp;",
279+
6 => "â…¥&nbsp;",
280+
7 => "â…¦&nbsp;",
281+
8 => "â…§&nbsp;",
282+
9 => "â…¨&nbsp;",
283+
10 => "â…©&nbsp;",
284+
_ => "",
285+
});
286+
let class = if self.is_premium {
287+
"has-text-warning-dark"
288+
} else {
289+
""
287290
};
288-
html! {
289-
span.(if self.is_premium { "has-text-warning-dark" } else { "" }) title=(self.tank_id) {
290-
(tier) " " (self.name)
291-
}
292-
}
291+
html! { span.(class) title=(self.tank_id) { (tier) (self.name) } }
293292
}
294293
}
295294

‎src/web/state.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use std::sync::Arc;
22
use std::time::Duration;
33

44
use async_std::sync::Mutex;
5+
use chrono::Utc;
56
use lru_time_cache::LruCache;
67

78
use crate::database::Database;
8-
use crate::models::Vehicle;
9+
use crate::models::{TankType, Vehicle};
910
use crate::wargaming::WargamingApi;
1011

1112
/// Web application global state.
@@ -14,7 +15,7 @@ pub struct State {
1415
pub api: WargamingApi,
1516
pub database: Arc<Mutex<Database>>,
1617

17-
tankopedia_cache: Arc<Mutex<LruCache<i32, Arc<Option<Vehicle>>>>>,
18+
tankopedia_cache: Arc<Mutex<LruCache<i32, Arc<Vehicle>>>>,
1819
}
1920

2021
impl State {
@@ -29,12 +30,25 @@ impl State {
2930
}
3031

3132
/// Retrieves cached vehicle information.
32-
pub async fn get_vehicle(&self, tank_id: i32) -> crate::Result<Arc<Option<Vehicle>>> {
33+
pub async fn get_vehicle(&self, tank_id: i32) -> crate::Result<Arc<Vehicle>> {
3334
let mut cache = self.tankopedia_cache.lock().await;
3435
match cache.get(&tank_id) {
3536
Some(vehicle) => Ok(vehicle.clone()),
3637
None => {
37-
let vehicle = Arc::new(self.database.lock().await.retrieve_vehicle(tank_id)?);
38+
let vehicle = match self.database.lock().await.retrieve_vehicle(tank_id)? {
39+
Some(vehicle) => Arc::new(vehicle),
40+
None => {
41+
log::warn!("Tank #{} is not found in the tankopedia.", tank_id);
42+
Arc::new(Vehicle {
43+
tank_id,
44+
name: format!("#{}", tank_id),
45+
tier: 0,
46+
is_premium: false,
47+
type_: TankType::Unknown,
48+
imported_at: Utc::now(),
49+
})
50+
}
51+
};
3852
cache.insert(tank_id, vehicle.clone());
3953
Ok(vehicle)
4054
}

0 commit comments

Comments
 (0)