From bbc7cda33f9dc5f562f7456e1cb3c9417423bc61 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Sat, 7 Feb 2026 23:49:51 +0100 Subject: [PATCH] index should almost be done --- froxy-templates/examples/index.rs | 86 ++++++++++++++ froxy-templates/src/index.rs | 110 ++++++++++++++++++ froxy-templates/src/templates/index.html | 136 ++++++++++++----------- 3 files changed, 269 insertions(+), 63 deletions(-) create mode 100644 froxy-templates/examples/index.rs diff --git a/froxy-templates/examples/index.rs b/froxy-templates/examples/index.rs new file mode 100644 index 0000000..ab9bdd5 --- /dev/null +++ b/froxy-templates/examples/index.rs @@ -0,0 +1,86 @@ +use std::collections::HashMap; + +use froxy_templates::index::{self, ClusterData, IndexData, LocationData, MapPos, PcIssue}; +use smol_str::SmolStr; + +fn main() { + let mut env = minijinja::Environment::new(); + index::add_to_context(&mut env); + + let data = index::render( + &env, + IndexData { + clusters: HashMap::from([( + SmolStr::new_static("F1"), + ClusterData { + users: 80, + max_pc: 100, + dead_pc: 1, + silent: false, + piscine: true, + issues: [ + (SmolStr::new_static("F1r1s1"), PcIssue::Dead), + (SmolStr::new_static("F1r2s1"), PcIssue::Attention), + ] + .into_iter() + .collect(), + map: index::Map { + rows: [ + index::MapRow { + range: SmolStr::new_static("R1"), + col: [ + MapPos::Empty, + MapPos::Flex, + MapPos::Wall, + MapPos::Door, + MapPos::Empty, + ] + .to_vec(), + }, + index::MapRow { + range: SmolStr::new_static("R2"), + col: [ + MapPos::Empty, + MapPos::Flex, + MapPos::Pc(SmolStr::new_static("F1r1s1")), + MapPos::Pc(SmolStr::new_static("F1r2s1")), + MapPos::Pc(SmolStr::new_static("F1r3s1")), + MapPos::Pc(SmolStr::new_static("F1r4s1")), + MapPos::Pc(SmolStr::new_static("F1r5s1")), + MapPos::Empty, + ] + .to_vec(), + }, + index::MapRow { + range: SmolStr::new_static("R3"), + col: [ + MapPos::Empty, + MapPos::Flex, + MapPos::Wall, + MapPos::Door, + MapPos::Empty, + ] + .to_vec(), + }, + ] + .to_vec(), + }, + }, + )]), + locations: [(SmolStr::new_static("F1r5s1"), LocationData { + login: "maiboyer".into(), + image: "https://friends.42paris.fr/proxy/resize/512/03c61af252becbca11aac5ff49a2e61c/maiboyer.jpg".into(), + me: false, + friend: false, + close_friend: false, + pooled: false, + })] + .into_iter() + .collect(), + current_cluster: SmolStr::new_static("F1"), + }, + ) + .unwrap(); + + println!("{data}"); +} diff --git a/froxy-templates/src/index.rs b/froxy-templates/src/index.rs index 9ed0fc2..7d4c090 100644 --- a/froxy-templates/src/index.rs +++ b/froxy-templates/src/index.rs @@ -1,3 +1,7 @@ +use std::collections::HashMap; + +use smol_str::SmolStr; + pub use crate::templates::INDEX; pub fn add_to_context(env: &mut minijinja::Environment) { @@ -8,4 +12,110 @@ pub fn add_to_context(env: &mut minijinja::Environment) { crate::open_modal::add_to_context(env); } env.add_template("index.html", INDEX).unwrap(); + env.add_function("max", |a: i32, b: i32| -> i32 { a.max(b) }); +} + +pub fn render(env: &minijinja::Environment, data: IndexData) -> Result { + let template = env.get_template("index.html")?; + template.render(data) +} + +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct LocationData { + pub login: SmolStr, + pub image: String, + pub me: bool, + pub friend: bool, + pub close_friend: bool, + pub pooled: bool, +} + +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct ClusterData { + pub users: u32, + pub dead_pc: u32, + pub max_pc: u32, + pub silent: bool, + pub piscine: bool, + pub map: Map, + pub issues: HashMap, +} + +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)] +#[serde(try_from = "SmolStr", into = "SmolStr")] +pub enum PcIssue { + #[default] + Dead, + Attention, +} + +impl TryFrom for PcIssue { + type Error = &'static str; + fn try_from(value: SmolStr) -> Result { + Ok(match value.as_str() { + "dead" => Self::Dead, + "attention" => Self::Attention, + _ => return Err("Invalid string"), + }) + } +} + +impl From for SmolStr { + fn from(val: PcIssue) -> Self { + match val { + PcIssue::Dead => SmolStr::new_static("dead"), + PcIssue::Attention => SmolStr::new_static("attention"), + } + } +} + +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +#[serde(from = "SmolStr", into = "SmolStr")] +pub enum MapPos { + Wall, + Flex, + Empty, + Door, + Pc(SmolStr), +} + +impl From for MapPos { + fn from(value: SmolStr) -> Self { + match value.as_str() { + "x" => Self::Wall, + "f" => Self::Flex, + "" => Self::Empty, + "d" => Self::Door, + _ => Self::Pc(value), + } + } +} +impl From for SmolStr { + fn from(val: MapPos) -> Self { + match val { + MapPos::Wall => SmolStr::new_static("x"), + MapPos::Flex => SmolStr::new_static("f"), + MapPos::Door => SmolStr::new_static("d"), + MapPos::Empty => SmolStr::new_static(""), + MapPos::Pc(s) => s, + } + } +} + +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct MapRow { + pub range: SmolStr, + pub col: Vec, +} + +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct Map { + pub rows: Vec, +} + +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct IndexData { + pub locations: HashMap, + pub clusters: HashMap, + pub current_cluster: SmolStr, } diff --git a/froxy-templates/src/templates/index.html b/froxy-templates/src/templates/index.html index 3606e7b..e023ffb 100644 --- a/froxy-templates/src/templates/index.html +++ b/froxy-templates/src/templates/index.html @@ -58,13 +58,48 @@
- {% for cluster in clusters %} + {% for name in clusters %} - + data-cluster="{{ name }}" id="radio{{ name }}" + autocomplete="off"> + {% set cluster = clusters[name] %} + {% set piscine = "" %} + {% set silent = "" %} + {% set spacer = "" %} + + {%- if cluster.piscine -%} + {% set piscine = '' %} + {%- endif -%} + {%- if cluster.silent -%} + {% set piscine = '' %} + {%- endif -%} + + {%- if cluster.piscine and cluster.silent -%} + {% set spacer = " " %} + {%- endif -%} + + {% set u = cluster.users %} + {% set m = cluster.max_pc - cluster.dead_pc %} + + {%- if m < 0 -%} + {% set m = 0 %} + {%- endif -%} + + {% set percent = 0 %} + {%- if m > 0 -%} + {% set percent = (u * 100) / m %} + {%- endif -%} + + {% set fill_level = 'primary' %} + {%- if percent > 75 -%} + {% set fill_level = 'danger' %} + {%- elif percent > 65 -%} + {% set fill_level = 'warning' %} + {% endif%} + + {%- endfor -%}
@@ -72,86 +107,61 @@ - {% for index, x in enumerate(map[1:], 0) %} + {% set map = clusters[current_cluster].map %} + {% set cluster = clusters[current_cluster] %} + {% for row in map.rows %} - {%- set countB = namespace(value=1) -%} - - {%- for y in x -%} - {%- if y == 'x' -%} + {%- set countB = 0 -%} + + {%- for col in row.col -%} + {%- if col == 'x' -%} - {%- elif y == '|' -%} + {%- elif col == '|' -%} - {%- elif y == 'h' -%} + {%- elif col == 'h' -%} - {%- elif y == 'd' -%} + {%- elif col == 'd' -%} - {%- elif y == 'f' -%} + {%- elif col == 'f' -%} - {%- elif exrypz(y) == False -%} + {%- elif col == '' -%} {%- else -%} {%- set text_color = '' -%} {%- set img = '' -%} - {%- if y in locations -%} - {%- set img = '' -%} - {%- elif actual_cluster in piscine and y in tutor_station -%} - {%- set img = '' -%} + {%- if col in locations -%} + {%- set img = '' -%} {%- endif -%} - {%- if actual_cluster in piscine and y in tutor_station -%} - {%- set text_color = 'tutor-shield' -%} - {%- endif -%} - {%- set relation = '' -%} - {%- set currently_in_piscine = y in locations and (locations[y]['user']['pool_month'], locations[y]['user']['pool_year']) in piscine_date -%} - {%- set in_piscine_cluster = actual_cluster in piscine -%} - {%- if y in locations and locations[y]['user']['id'] not in tutors and currently_in_piscine != in_piscine_cluster -%} - {%- set relation = 'wrong-cluster' -%} - {%- endif -%} - {%- if focus and focus == y %} + {%- if focus and focus == col %} {%- set relation = 'focus' -%} - {%- elif y in locations and locations[y]['me'] -%} + {%- elif col in locations and locations[col].me -%} {%- set relation = 'me' -%} - {%- elif y in locations and locations[y]['whitelist'] -%} - {%- set relation = 'whitelist' -%} - {%- elif y in locations and locations[y]['close_friend'] -%} + {%- elif col in locations and locations[col].close_friend -%} {%- set relation = 'close_friend' -%} - {%- elif y in locations and locations[y]['friend'] -%} + {%- elif col in locations and locations[col].friend -%} {%- set relation = 'friend' -%} - {%- elif y in issues_map and issues_map[y]['count'] >= 1 and issues_map[y]['issue'] == 1 -%} - {%- set relation = 'dead' -%} - {%- elif y in issues_map and issues_map[y]['count'] >= 1 and issues_map[y]['issue'] == 2 -%} - {%- set relation = 'attention' -%} - {%- elif y in locations and locations[y]['pool'] and actual_cluster not in piscine -%} + {%- elif col in cluster.issues -%} + {%- set relation = cluster.issues[col] -%} + {%- elif col in locations and locations[col]['pool'] and not cluster.piscine -%} {%- set relation = 'pooled' -%} {%- endif -%} - {%- if actual_cluster in piscine and y in locations and locations[y]['user']['id'] in tutors -%} - {%- set relation = 'tutor' -%} - {%- set text_color = '' -%} - {%- elif actual_cluster in piscine and y in locations and not locations[y]['user']['tutor'] in tutors and y in tutor_station -%} - {%- set relation = 'tutor-spot' -%} - {%- set text_color = '' -%} - {%- endif -%} - {%- if y in locations and locations[y]['admin'] -%} - {%- set relation = 'admin' -%} - {%- endif -%} - - {%- if countB.value % 2 == 0 -%} - - {%- else -%} - - {%- endif -%} {%- endif -%} - {%- set countB.value = countB.value + 1 -%} + {%- set countB = (countB + 1) % 2 -%} {%- endfor -%} - + {%- endfor -%}
{{ map[0][index] }}{{ row.range }} {{ img|safe }}
{{ y.replace(actual_cluster, '') }} + {% set loc_name = col|replace(current_cluster, "") %} +
+ {%- if countB % 2 == 0 -%} + {{ img|safe }}
{{ loc_name }} + {%- else -%} + {{ loc_name }}
{{ img|safe }} + {%- endif -%}
{{ y.replace(actual_cluster, '') }}
{{ img|safe }} -
{{ map[0][index] }}{{ row.range }}