From 881bece87beff62b1eeab0ca0004ca78dfb06b5f Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Sat, 16 Mar 2019 18:45:59 +0100 Subject: [PATCH] Some nice presentation --- src/lib.rs | 102 ++++++++++++++++++++++++++++++++++---- templates/base.html.tera | 3 +- templates/index.html.tera | 53 ++++++++++++++++++-- 3 files changed, 142 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 47a26c6..9df9a19 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, @@ -15,9 +15,9 @@ pub struct Data { pub battles: HashMap, } -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 { - 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, + + /// The battles. + /// + /// This hashmap is symmetric. + pub battles: HashMap, + + /// The ias sorted by victory count. + pub sorted_ias: Vec, +} + +impl Data { + /// Creates some test data. + pub fn test() -> Data { + JsonData::test().into() + } +} + +impl From 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::>(); + + 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, } } } diff --git a/templates/base.html.tera b/templates/base.html.tera index 5cb65a9..71cde33 100644 --- a/templates/base.html.tera +++ b/templates/base.html.tera @@ -29,7 +29,7 @@ - {% block content %}{% endblock content %} + {% block content %}{% endblock %} + {% block extrajs %}{% endblock %} diff --git a/templates/index.html.tera b/templates/index.html.tera index 23cdbad..1a5bcdd 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -2,10 +2,55 @@ {% block content %}
-

Welcome to Pytron

- {% for ia in ias %} -
{{ ia }}
- {% endfor %} +
+
+ + + + {% for ia in ias %} + + {% endfor %} + + + + + {% for ia1 in ias %} + + + {% for ia2 in ias %} + {% if ia1.name == ia2.name %} + + {% else %} + {% set key = ia1.name ~ "/" ~ ia2.name %} + {% set value = battles | get(key=key) %} + {% if value > 0.5 %} + + {% elif value < 0.5 %} + + {% else %} + + {% endif %} + {% endif %} + {% endfor %} + + + + {% endfor %} +
{{ ia.name }}
{{ ia1.name }}{{ value }}{{ value }}{{ value }}{{ ia1.victory_count }}{{ ia1.defeat_count }}
+
+
+ + + {% for ia in sorted_ias %} + + + + + {% endfor %} +
AIScore
{{ ia.name }}{{ ia.victory_count - ia.defeat_count }}
+
+
{% endblock %} +