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

Commit

Permalink
🩹 Quick fix for vehicles missing in the tankopedia
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Jun 28, 2021
1 parent f6cd96c commit ae1fe51
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
3 changes: 3 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ pub enum TankType {

#[serde(rename = "AT-SPG")]
AT,

#[serde(rename = "unknown")]
Unknown,
}

#[derive(Serialize, Deserialize, Clone)]
Expand Down
51 changes: 25 additions & 26 deletions src/web/player/view.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chrono_humanize::{Accuracy, HumanTime, Tense};
use clap::crate_name;
use maud::{html, Markup, Render, DOCTYPE};
use maud::{html, Markup, PreEscaped, Render, DOCTYPE};
use tide::StatusCode;

use crate::models::Vehicle;
Expand Down Expand Up @@ -233,14 +233,12 @@ pub async fn get(request: tide::Request<State>) -> tide::Result {
}
tbody {
@for snapshot in &model.tank_snapshots {
@if let Some(vehicle) = state.get_vehicle(snapshot.tank_id).await?.as_ref() {
tr {
th { (vehicle) }
td { (snapshot.all_statistics.battles) }
(render_confidence_interval_td(snapshot.all_statistics.battles, snapshot.all_statistics.wins))
td { (snapshot.all_statistics.damage_dealt) }
td { (format!("{:.0}", f64::from(snapshot.all_statistics.damage_dealt) / f64::from(snapshot.all_statistics.battles))) }
}
tr {
th { (state.get_vehicle(snapshot.tank_id).await?.as_ref()) }
td { (snapshot.all_statistics.battles) }
(render_confidence_interval_td(snapshot.all_statistics.battles, snapshot.all_statistics.wins))
td { (snapshot.all_statistics.damage_dealt) }
td { (format!("{:.0}", f64::from(snapshot.all_statistics.damage_dealt) / f64::from(snapshot.all_statistics.battles))) }
}
}
}
Expand Down Expand Up @@ -272,24 +270,25 @@ pub fn get_account_url(account_id: i32) -> String {

impl Render for &Vehicle {
fn render(&self) -> Markup {
let tier = match self.tier {
1 => "â… ",
2 => "â…¡",
3 => "â…¢",
4 => "â…£",
5 => "â…¤",
6 => "â…¥",
7 => "â…¦",
8 => "â…§",
9 => "â…¨",
10 => "â…©",
_ => "?",
let tier = PreEscaped(match self.tier {
1 => "â… &nbsp;",
2 => "â…¡&nbsp;",
3 => "â…¢&nbsp;",
4 => "â…£&nbsp;",
5 => "â…¤&nbsp;",
6 => "â…¥&nbsp;",
7 => "â…¦&nbsp;",
8 => "â…§&nbsp;",
9 => "â…¨&nbsp;",
10 => "â…©&nbsp;",
_ => "",
});
let class = if self.is_premium {
"has-text-warning-dark"
} else {
""
};
html! {
span.(if self.is_premium { "has-text-warning-dark" } else { "" }) title=(self.tank_id) {
(tier) " " (self.name)
}
}
html! { span.(class) title=(self.tank_id) { (tier) (self.name) } }
}
}

Expand Down
22 changes: 18 additions & 4 deletions src/web/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::sync::Arc;
use std::time::Duration;

use async_std::sync::Mutex;
use chrono::Utc;
use lru_time_cache::LruCache;

use crate::database::Database;
use crate::models::Vehicle;
use crate::models::{TankType, Vehicle};
use crate::wargaming::WargamingApi;

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

tankopedia_cache: Arc<Mutex<LruCache<i32, Arc<Option<Vehicle>>>>>,
tankopedia_cache: Arc<Mutex<LruCache<i32, Arc<Vehicle>>>>,
}

impl State {
Expand All @@ -29,12 +30,25 @@ impl State {
}

/// Retrieves cached vehicle information.
pub async fn get_vehicle(&self, tank_id: i32) -> crate::Result<Arc<Option<Vehicle>>> {
pub async fn get_vehicle(&self, tank_id: i32) -> crate::Result<Arc<Vehicle>> {
let mut cache = self.tankopedia_cache.lock().await;
match cache.get(&tank_id) {
Some(vehicle) => Ok(vehicle.clone()),
None => {
let vehicle = Arc::new(self.database.lock().await.retrieve_vehicle(tank_id)?);
let vehicle = match self.database.lock().await.retrieve_vehicle(tank_id)? {
Some(vehicle) => Arc::new(vehicle),
None => {
log::warn!("Tank #{} is not found in the tankopedia.", tank_id);
Arc::new(Vehicle {
tank_id,
name: format!("#{}", tank_id),
tier: 0,
is_premium: false,
type_: TankType::Unknown,
imported_at: Utc::now(),
})
}
};
cache.insert(tank_id, vehicle.clone());
Ok(vehicle)
}
Expand Down

0 comments on commit ae1fe51

Please sign in to comment.