chiark / gitweb /
timestamps in log in state
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 14 Oct 2020 19:16:13 +0000 (20:16 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 14 Oct 2020 19:17:11 +0000 (20:17 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/gamestate.rs
src/session.rs
src/updates.rs
templates/session.tera

index 9446a4e57fce8d5b3a0820cbe0f80fe65f091d22..bebb4b168c4e7dacce35dfd85eedcc40db293d39 100644 (file)
@@ -23,6 +23,10 @@ visible_slotmap_key!{ VisiblePieceId('.') }
 #[serde(transparent)]
 pub struct Html (pub String);
 
+#[derive(Copy,Clone,Debug,Serialize,Deserialize,Eq,Ord,PartialEq,PartialOrd)]
+#[serde(transparent)]
+pub struct Timestamp(pub u64); /* time_t */
+
 pub const DEFAULT_TABLE_SIZE : Pos = PosC([ 400, 200 ]);
 
 // ---------- general data types ----------
@@ -41,7 +45,7 @@ pub struct GameState {
   pub pieces : Pieces,
   pub players : PlayerMap,
   pub gen : Generation,
-  pub log : Vec<(Generation,Arc<LogEntry>)>, // xxx expiry
+  pub log : Vec<(Generation, Timestamp, Arc<LogEntry>)>, // xxx expiry
   pub max_z : ZCoord,
 }
 
@@ -149,6 +153,18 @@ impl Display for Generation {
   }
 }
 
+impl Timestamp {
+  /// Always >= previously
+  pub fn now() -> Timestamp {
+    use std::time::SystemTime;
+    let now = SystemTime::now()
+      .duration_since(SystemTime::UNIX_EPOCH)
+      .unwrap()
+      .as_secs();
+    Timestamp(now)
+  }
+}
+
 pub trait ClampTable : Sized {
   fn clamped(self, range: Self) -> (Self, bool);
 }
index a940e2dc074a0159913f0fe243d9997e6ad7463f..f9b959ecfe73a5b453b9336feae0f2b73e890b3b 100644 (file)
@@ -14,7 +14,7 @@ struct SessionRenderContext {
   defs : Vec<(VisiblePieceId,Html)>,
   nick : String,
   load : String,
-  log : Vec<(Generation,Arc<LogEntry>)>,
+  log : Vec<(Generation, Timestamp, Arc<LogEntry>)>,
 }
 
 #[derive(Serialize,Debug)]
index 45113316f6eb14989f86cc07ce7faed2ef9f3c22..1984d333800270030870b5e67f74336a9eae4556 100644 (file)
@@ -402,7 +402,11 @@ impl<'r> PrepareUpdatesBuffer<'r> {
     for logentry in logents {
       let logentry = Arc::new(logentry);
       let gen = self.gen();
-      self.g.gs.log.push((gen, logentry.clone()));
+      let now = Timestamp::now();
+      let when = iter::once(now).chain(
+        self.g.gs.log.last().map(|l| l.1)
+      ).max().unwrap();
+      self.g.gs.log.push((gen, when, logentry.clone()));
       self.us.push(PreparedUpdateEntry::Log(logentry));
     }
   }
index 9c43090a4f25b52611fb13a6d886013191b0cc4c..edd152f90c276111f07abd8c74f32d0deec4ab4c 100644 (file)
@@ -50,7 +50,7 @@
 <p>
   <div id="log" style="overflow-y: scroll; height: 200px;">
 {% for ent in log %}
-    <div>{{ ent.1.html | safe }}</div>
+    <div>{{ ent.2.html | safe }}</div>
 {%- endfor %}
   </div>