From a38437f059c465fcf5b0e23e09b36510591c9384 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 3 May 2021 18:10:45 +0100 Subject: [PATCH] bundles: Show in web UI Signed-off-by: Ian Jackson --- daemon/session.rs | 2 ++ nwtemplates/bundles-info-pane.tera | 10 ++++++++ src/bundles.rs | 41 +++++++++++++++++++++++++++++- src/prelude.rs | 2 +- src/updates.rs | 12 +++++++++ templates/macros.tera | 10 ++++++++ templates/script.ts | 7 +++++ 7 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 nwtemplates/bundles-info-pane.tera diff --git a/daemon/session.rs b/daemon/session.rs index 174e25f5..b53c8976 100644 --- a/daemon/session.rs +++ b/daemon/session.rs @@ -23,6 +23,7 @@ struct SessionRenderContext { sse_url_prefix: String, links: Html, player_info_pane: Html, + bundles_info_pane: Html, fake_rng: bool, } @@ -235,6 +236,7 @@ fn session_inner(form: Json, nick, sse_url_prefix, player_info_pane, + bundles_info_pane: ig.bundle_list.info_pane()?, ptoken: form.ptoken.clone(), links: (&*ig.links).into(), fake_rng: config().game_rng.is_fake(), diff --git a/nwtemplates/bundles-info-pane.tera b/nwtemplates/bundles-info-pane.tera new file mode 100644 index 00000000..14361fe7 --- /dev/null +++ b/nwtemplates/bundles-info-pane.tera @@ -0,0 +1,10 @@ +{# -*- HTML -*- -#} + +{% for b in bundles %} +
+#{{ b.id }} +{{ b.title }} +
+{% endfor %} diff --git a/src/bundles.rs b/src/bundles.rs index bf066c9e..1af062f4 100644 --- a/src/bundles.rs +++ b/src/bundles.rs @@ -42,6 +42,7 @@ impl FromStr for Index { #[throws(Self::Err)] fn from_str(s: &str) -> Index { Index(u16::from_str(s)?) } } +hformat_as_display!{Id} const BUNDLES_MAX: Index = Index(64); @@ -214,7 +215,18 @@ impl InstanceBundles { fn updated(&self, ig: &mut Instance) { ig.bundle_list = self.iter().map(|(id, state)| { (id, state.clone()) - }).collect() + }).collect(); + + let new_info_pane = ig.bundle_list.info_pane().unwrap_or_else(|e|{ + let m = "error rendering bundle list"; + error!("{}: {}", m, e); + Html::from_txt(m) + }); + let new_info_pane = Arc::new(new_info_pane); + let mut prepub = PrepareUpdatesBuffer::new(ig, Some(1)); + prepub.raw_updates(vec![ + PUE::UpdateBundles { new_info_pane } + ]); } #[throws(IE)] @@ -291,6 +303,33 @@ impl Uploading { } } +#[ext(pub)] +impl MgmtBundleList { + #[throws(IE)] + fn info_pane(&self) -> Html { + #[derive(Serialize,Debug)] + struct RenderPane { + bundles: Vec, + } + #[derive(Serialize,Debug)] + struct RenderBundle { + id: Html, + title: Html, + } + let bundles = self.iter().filter_map(|(&id, state)| { + if_let!{ State::Loaded(Loaded { meta }) = state; else return None; } + let BundleMeta { title } = meta; + let id = hformat!("{}", id); + let title = Html::from_txt(title); + Some(RenderBundle { id, title }) + }).collect(); + + Html::from_html_string( + nwtemplates::render("bundles-info-pane.tera", &RenderPane { bundles })? + ) + } +} + impl InstanceBundles { #[throws(MgmtError)] pub fn finish_upload(&mut self, ig: &mut Instance, diff --git a/src/prelude.rs b/src/prelude.rs index 177220af..dce8bf08 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -122,7 +122,7 @@ pub use crate::accounts::loaded_acl::{self, EffectiveACL, LoadedAcl, PermSet}; pub use crate::accounts::*; pub use crate::authproofs::{self, Authorisation, Unauthorised}; pub use crate::authproofs::AuthorisationSuperuser; -pub use crate::bundles::{self, InstanceBundles}; +pub use crate::bundles::{self, InstanceBundles, MgmtBundleListExt}; pub use crate::commands::{AccessTokenInfo, AccessTokenReport, MgmtError}; pub use crate::commands::{MgmtCommand, MgmtResponse}; pub use crate::commands::{MgmtGameInstruction, MgmtGameResponse}; diff --git a/src/updates.rs b/src/updates.rs index ebfb9373..53b69871 100644 --- a/src/updates.rs +++ b/src/updates.rs @@ -70,6 +70,9 @@ pub enum PreparedUpdateEntry { player: PlayerId, new_info_pane: Arc, }, + UpdateBundles { + new_info_pane: Arc, + }, Log(Arc), Error(ErrorSignaledViaUpdate), } @@ -246,6 +249,9 @@ enum TransmitUpdateEntry<'u> { player: PlayerId, new_info_pane: &'u Html, }, + UpdateBundles { + new_info_pane: &'u Arc, + }, SetLinks(Html), #[serde(serialize_with="serialize_logentry")] Log(TransmitUpdateLogEntry<'u>), @@ -438,6 +444,9 @@ impl PreparedUpdateEntry { RemovePlayer { player:_, new_info_pane } => { new_info_pane.json_len() + 100 } + UpdateBundles { new_info_pane } => { + new_info_pane.json_len() + 100 + } SetTableColour(colour) => { colour.json_len() + 50 } @@ -953,6 +962,9 @@ impl PreparedUpdate { &PUE::RemovePlayer { player, ref new_info_pane } => { TUE::RemovePlayer { player, new_info_pane } } + &PUE::UpdateBundles { ref new_info_pane } => { + TUE::UpdateBundles { new_info_pane } + } PUE::Error(e) => { match *e { ESVU::InternalError => TUE::Error(ESVU::InternalError), diff --git a/templates/macros.tera b/templates/macros.tera index eaf65f49..4ca17ce7 100644 --- a/templates/macros.tera +++ b/templates/macros.tera @@ -110,6 +110,7 @@ Hi {{nick | escape}}
Show H this help U players + B bundles
@@ -123,6 +124,15 @@ Hi {{nick | escape}} + + {% endmacro infopane %} diff --git a/templates/script.ts b/templates/script.ts index d5c9d309..4df5d069 100644 --- a/templates/script.ts +++ b/templates/script.ts @@ -153,6 +153,7 @@ type PaneName = string; const pane_keys : { [key: string]: PaneName } = { "H" : "help", "U" : "players", + "B" : "bundles", }; const uo_kind_prec : { [kind: string]: number } = { @@ -1360,6 +1361,12 @@ function player_info_pane_set(j: PlayersUpdate) { .innerHTML = j.new_info_pane; } +messages.UpdateBundles = function +(j: { new_info_pane: string }) { + document.getElementById('bundle_list')! + .innerHTML = j.new_info_pane; +} + messages.SetTableSize = function ([x, y]: [number, number]) { function set_attrs(elem: Element, l: [string,string][]) { -- 2.30.2