From 74810675f62fd175c4a5173e5a33f20ae1cefbaf Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 3 Jan 2021 22:54:48 +0000 Subject: [PATCH] wip links Signed-off-by: Ian Jackson --- daemon/cmdlistener.rs | 9 +++++---- daemon/session.rs | 5 ++--- src/global.rs | 7 +++++-- src/sse.rs | 3 +-- src/updates.rs | 20 ++++++++------------ templates/landscape.tera | 15 +++------------ templates/session.tera | 3 +++ 7 files changed, 27 insertions(+), 35 deletions(-) diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index d018d107..30debbef 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -441,22 +441,23 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>( Ok(Resp::Info(info)) })?, - Insn::SetLinks(spec_links) => { + Insn::SetLinks(mut spec_links) => { let ig = cs.check_acl(ag, ig, PCH::Instance, &[TP::SetLinks])?.0; let mut new_links : LinksTable = default(); for (k,v) in spec_links.drain() { let url : Url = (&v).try_into()?; new_links[k] = Some(url.into_string()); } - ig.links = new_links; + let new_links = Arc::new(new_links); + ig.links = new_links.clone(); let log = vec![LogEntry { html: Html(format!("{} set the links to off-server game resources", &who.0)), }]; (U{ log, pcs: vec![], - raw: vec![ PreparedUpdateEntry::SetLinks ]}, - Fine, eg) + raw: Some(vec![ PreparedUpdateEntry::SetLinks(new_links) ])}, + Fine, ig) } ResetPlayerAccess(player) => { diff --git a/daemon/session.rs b/daemon/session.rs index 3f740651..e52bf99a 100644 --- a/daemon/session.rs +++ b/daemon/session.rs @@ -18,7 +18,7 @@ struct SessionRenderContext { load: String, log: Vec, sse_url_prefix: String, - links: Vec<(LinkKind, Html)>, + links: Html, } #[derive(Debug,Serialize)] @@ -187,8 +187,7 @@ fn session_inner(form : Json, nick : gpl.nick.clone(), sse_url_prefix, ptoken: form.ptoken.clone(), - links: ig.links.iter() - .filter_map(|(k,v)| Some((k.clone(), v.clone()?))).collect(), + links: (&*ig.links).into(), load : serde_json::to_string(&DataLoad { players: load_players, last_log_ts: timestamp_abbrev.unwrap_or_default(), diff --git a/src/global.rs b/src/global.rs index c9288eb0..10f13bd4 100644 --- a/src/global.rs +++ b/src/global.rs @@ -51,7 +51,7 @@ pub struct Instance { pub tokens_players: TokenRegistry, pub tokens_clients: TokenRegistry, pub acl: LoadedAcl, - pub links: LinksTable, + pub links: Arc, } pub struct PlayerRecord { @@ -218,7 +218,7 @@ struct InstanceSaveAccesses { tokens_players: Vec<(RawTokenStr, PlayerId)>, aplayers: SecondarySlotMap, acl: Acl, - #[serde(default)] pub links: LinksTable, + #[serde(default)] pub links: Arc, } display_as_debug!{InstanceLockError} @@ -459,6 +459,9 @@ impl Deref for LinksTable { type Target = EnumMap>; fn deref(&self) -> &Self::Target { &self.0 } } +impl DerefMut for LinksTable { + fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } +} impl From<&LinksTable> for Html { fn from(links: &LinksTable) -> Html { diff --git a/src/sse.rs b/src/sse.rs index 01482159..497171a2 100644 --- a/src/sse.rs +++ b/src/sse.rs @@ -92,7 +92,6 @@ impl Read for UpdateReader { } let g = &mut *ig; - let for_json_len = InstanceForJsonLen { links: &g.links }; let iplayer = &mut match g.iplayers.get_mut(self.player) { Some(x) => x, None => { @@ -133,7 +132,7 @@ impl Read for UpdateReader { break } }; - let next_len = UPDATE_MAX_FRAMING_SIZE + next.json_len(&for_json_len); + let next_len = UPDATE_MAX_FRAMING_SIZE + next.json_len(); if next_len > buf.len() { if buf.len() != orig_wanted { break } diff --git a/src/updates.rs b/src/updates.rs index a979c085..4c38a646 100644 --- a/src/updates.rs +++ b/src/updates.rs @@ -48,7 +48,7 @@ pub enum PreparedUpdateEntry { }, SetTableSize(Pos), SetTableColour(Colour), - SetLinks, // we use whatever the most recent is + SetLinks(Arc), AddPlayer { player: PlayerId, data: DataLoadPlayer }, RemovePlayer { player: PlayerId }, Log (Arc), @@ -147,10 +147,6 @@ impl PlayerUpdatesBuildContext { } } -pub struct InstanceForJsonLen<'i> { - links: &'i LinksTable, -} - impl PlayerUpdates { pub fn new_begin(gs: &GameState) -> PlayerUpdatesBuildContext { let u1 = Arc::new(PreparedUpdate { @@ -187,13 +183,13 @@ impl PlayerUpdates { } impl PreparedUpdate { - pub fn json_len(&self, ig: &InstanceForJsonLen) -> usize { - self.us.iter().map(|u| 20 + u.json_len(ig)).sum() + pub fn json_len(&self) -> usize { + self.us.iter().map(|u| 20 + u.json_len()).sum() } } impl PreparedUpdateEntry { - pub fn json_len(&self, ig: &InstanceForJsonLen) -> usize { + pub fn json_len(&self) -> usize { use PreparedUpdateEntry::*; match self { Piece { ref op, .. } => { @@ -209,8 +205,8 @@ impl PreparedUpdateEntry { SetTableColour(colour) => { colour.0.as_bytes().len() + 50 } - SetLinks => { - ig.links.iter().filter_map( + SetLinks(links) => { + links.iter().filter_map( |(_k,v)| Some(50 + v.as_ref()?.len()) ).sum::() + 50 } @@ -563,8 +559,8 @@ impl PreparedUpdate { continue } } - PUE::SetLinks => { - + PUE::SetLinks(links) => { + TUE::SetLinks((&**links).into()) } }; ents.push(ue); diff --git a/templates/landscape.tera b/templates/landscape.tera index 288fc391..676163d7 100644 --- a/templates/landscape.tera +++ b/templates/landscape.tera @@ -36,18 +36,9 @@ {{ m::nick() }} | {{ m::status() }}
-{%- for l in links -%} -{%- if l.first -%} -
-{%- else -%} -| -{%- endif -%} -{{ l.0 }} -xxx core should be in some other .tera file so we can reuse it on update -{%- if l.last -%} -
-{%- endif -%} -{%- endfor -%} +{%- if links != "" -%} + +{%- endif -%}
switch to portrait view | {{ m::zoom() }} diff --git a/templates/session.tera b/templates/session.tera index dc0a7e1f..33218232 100644 --- a/templates/session.tera +++ b/templates/session.tera @@ -37,6 +37,9 @@ g{{ m::wresting() }} {{ m::status() }} | switch to landscape view | +{%- if links != "" -%} +| {{ links }} +{%- endif -%} {{ m::zoom() }} {{ m::errors() }} -- 2.30.2