diff --git a/src/services/game/manager.rs b/src/services/game/manager.rs index e06c817..5250504 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 96e649e..f80d9c4 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>; 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, + /// Players currently in the game pub players: Vec, - /// Services access pub game_manager: Arc, - + /// Access to the tunneling service pub tunnel_service: Arc, } @@ -69,6 +70,10 @@ pub struct GameSnapshot { pub attributes: AttrMap, /// Snapshots of the game players pub players: Option>, + /// The total number of players in the game + pub total_players: usize, + /// When the game was created + pub created_at: DateTime, } /// Attributes map type @@ -215,6 +220,7 @@ impl Game { id: GameID, attributes: AttrMap, settings: GameSettings, + created_at: DateTime, game_manager: Arc, tunnel_service: Arc, ) -> Game { @@ -224,6 +230,7 @@ impl Game { settings, state: Default::default(), players: Default::default(), + created_at, game_manager, tunnel_service, } @@ -424,6 +431,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 +442,15 @@ impl Game { } else { None }; + GameSnapshot { id: self.id, state: self.state, setting: self.settings.bits(), attributes: self.attributes.clone(), players, + total_players, + created_at: self.created_at, } }