From ced894ef173e0f13eca375e7d2a146b32af5c0db Mon Sep 17 00:00:00 2001
From: Jacobtread <jacobtread@gmail.com>
Date: Sat, 11 May 2024 15:46:18 +1200
Subject: [PATCH 1/2] feat: add total players count to game snapshot

---
 src/services/game/mod.rs | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/services/game/mod.rs b/src/services/game/mod.rs
index 96e649e4..2c90ea48 100644
--- a/src/services/game/mod.rs
+++ b/src/services/game/mod.rs
@@ -69,6 +69,8 @@ pub struct GameSnapshot {
     pub attributes: AttrMap,
     /// Snapshots of the game players
     pub players: Option<Box<[GamePlayerSnapshot]>>,
+    /// The total number of players in the game
+    pub total_players: usize,
 }
 
 /// Attributes map type
@@ -424,6 +426,7 @@ impl Game {
     }
 
     pub fn snapshot(&self, include_net: bool, include_players: bool) -> GameSnapshot {
+        let total_players: usize = self.players.len();
         let players = if include_players {
             let players = self
                 .players
@@ -434,12 +437,14 @@ impl Game {
         } else {
             None
         };
+
         GameSnapshot {
             id: self.id,
             state: self.state,
             setting: self.settings.bits(),
             attributes: self.attributes.clone(),
             players,
+            total_players,
         }
     }
 

From 9366109c7067172cefca0f86cb729d921e1e01ed Mon Sep 17 00:00:00 2001
From: Jacobtread <jacobtread@gmail.com>
Date: Sat, 11 May 2024 15:54:36 +1200
Subject: [PATCH 2/2] feat: store game creation time and include in api

---
 src/services/game/manager.rs |  3 +++
 src/services/game/mod.rs     | 14 ++++++++++----
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/services/game/manager.rs b/src/services/game/manager.rs
index e06c817c..5250504d 100644
--- a/src/services/game/manager.rs
+++ b/src/services/game/manager.rs
@@ -15,6 +15,7 @@ use crate::{
         types::{GameID, PlayerID},
     },
 };
+use chrono::Utc;
 use log::{debug, warn};
 use std::{
     collections::VecDeque,
@@ -209,10 +210,12 @@ impl GameManager {
         setting: GameSettings,
     ) -> (GameRef, GameID) {
         let id = self.next_id.fetch_add(1, Ordering::AcqRel);
+        let created_at = Utc::now();
         let game = Game::new(
             id,
             attributes,
             setting,
+            created_at,
             self.clone(),
             self.tunnel_service.clone(),
         );
diff --git a/src/services/game/mod.rs b/src/services/game/mod.rs
index 2c90ea48..f80d9c4d 100644
--- a/src/services/game/mod.rs
+++ b/src/services/game/mod.rs
@@ -22,6 +22,7 @@ use crate::{
         types::{GameID, PlayerID},
     },
 };
+use chrono::{DateTime, Utc};
 use log::{debug, warn};
 use serde::Serialize;
 use std::sync::{Arc, Weak};
@@ -40,19 +41,19 @@ pub type WeakGameRef = Weak<RwLock<Game>>;
 pub struct Game {
     /// Unique ID for this game
     pub id: GameID,
-
     /// The current game state
     pub state: GameState,
     /// The current game setting
     pub settings: GameSettings,
     /// The game attributes
     pub attributes: AttrMap,
-
+    /// When the game was started
+    pub created_at: DateTime<Utc>,
+    /// Players currently in the game
     pub players: Vec<GamePlayer>,
-
     /// Services access
     pub game_manager: Arc<GameManager>,
-
+    /// Access to the tunneling service
     pub tunnel_service: Arc<TunnelService>,
 }
 
@@ -71,6 +72,8 @@ pub struct GameSnapshot {
     pub players: Option<Box<[GamePlayerSnapshot]>>,
     /// The total number of players in the game
     pub total_players: usize,
+    /// When the game was created
+    pub created_at: DateTime<Utc>,
 }
 
 /// Attributes map type
@@ -217,6 +220,7 @@ impl Game {
         id: GameID,
         attributes: AttrMap,
         settings: GameSettings,
+        created_at: DateTime<Utc>,
         game_manager: Arc<GameManager>,
         tunnel_service: Arc<TunnelService>,
     ) -> Game {
@@ -226,6 +230,7 @@ impl Game {
             settings,
             state: Default::default(),
             players: Default::default(),
+            created_at,
             game_manager,
             tunnel_service,
         }
@@ -445,6 +450,7 @@ impl Game {
             attributes: self.attributes.clone(),
             players,
             total_players,
+            created_at: self.created_at,
         }
     }
 
