Some nice presentation

This commit is contained in:
2019-03-16 18:45:59 +01:00
parent 2a164d5e72
commit 881bece87b
3 changed files with 142 additions and 16 deletions
+91 -11
View File
@@ -7,7 +7,7 @@ use std::io::Read;
#[derive(Serialize, Deserialize)]
/// The data contained from the json file generated by python.
pub struct Data {
pub struct JsonData {
/// The names of the IAs.
pub ias: Vec<String>,
@@ -15,9 +15,9 @@ pub struct Data {
pub battles: HashMap<String, f64>,
}
impl Data {
impl JsonData {
/// Creates some test data.
pub fn test() -> Data {
pub fn test() -> JsonData {
// Open the file
let mut file = File::open("./assets/test.json")
@@ -30,15 +30,95 @@ impl Data {
serde_json::from_str(&content)
.expect("Couldn't parse json")
}
}
/// Returns the result for a certain battle.
pub fn get(&self, ia1: &str, ia2: &str) -> Option<f64> {
if let Some(value) = self.battles.get(&format!("{}/{}", ia1, ia2)) {
Some(*value)
} else if let Some(value) = self.battles.get(&format!("{}/{}", ia2, ia1)) {
Some(1.0 - *value)
} else {
None
#[derive(Serialize, Deserialize, Clone)]
/// An IA.
pub struct Ia {
/// The name of the ia.
pub name: String,
/// The number of battles won by the ia.
pub victory_count: usize,
/// The number of battles lost by the ia.
pub defeat_count: usize,
}
#[derive(Serialize, Deserialize)]
pub struct Data {
/// The IAs
pub ias: Vec<Ia>,
/// The battles.
///
/// This hashmap is symmetric.
pub battles: HashMap<String, f64>,
/// The ias sorted by victory count.
pub sorted_ias: Vec<Ia>,
}
impl Data {
/// Creates some test data.
pub fn test() -> Data {
JsonData::test().into()
}
}
impl From<JsonData> for Data {
fn from(d: JsonData) -> Data {
let mut ias = vec![];
for ia in d.ias {
ias.push(Ia {
name: ia,
victory_count: 0,
defeat_count: 0,
})
}
let mut battles = HashMap::new();
for (key, val) in d.battles {
let playing_ias = key.split("/")
.collect::<Vec<_>>();
let key1 = format!("{}/{}", playing_ias[0], playing_ias[1]);
let key2 = format!("{}/{}", playing_ias[1], playing_ias[0]);
battles.insert(key1, val);
battles.insert(key2, val);
if val != 0.5 {
let (winner, loser) = if val > 0.5 {
(playing_ias[0], playing_ias[1])
} else {
(playing_ias[1], playing_ias[0])
};
for ia in &mut ias {
if ia.name == winner {
ia.victory_count += 1;
}
if ia.name == loser {
ia.defeat_count += 1;
}
}
}
}
let mut sorted_ias = ias.clone();
sorted_ias.sort_by_key(|ia| {
(std::usize::MAX - ia.victory_count, ia.defeat_count)
});
Data {
ias,
sorted_ias,
battles,
}
}
}