Skip to content

Commit

Permalink
Merge pull request #68 from PocketRelay/feature-improved-games-api
Browse files Browse the repository at this point in the history
feat: improved games api
  • Loading branch information
jacobtread authored May 11, 2024
2 parents 74fa9c1 + 9366109 commit c926ee7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/services/game/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
types::{GameID, PlayerID},
},
};
use chrono::Utc;
use log::{debug, warn};
use std::{
collections::VecDeque,
Expand Down Expand Up @@ -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(),
);
Expand Down
19 changes: 15 additions & 4 deletions src/services/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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>,
}

Expand All @@ -69,6 +70,10 @@ 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,
/// When the game was created
pub created_at: DateTime<Utc>,
}

/// Attributes map type
Expand Down Expand Up @@ -215,6 +220,7 @@ impl Game {
id: GameID,
attributes: AttrMap,
settings: GameSettings,
created_at: DateTime<Utc>,
game_manager: Arc<GameManager>,
tunnel_service: Arc<TunnelService>,
) -> Game {
Expand All @@ -224,6 +230,7 @@ impl Game {
settings,
state: Default::default(),
players: Default::default(),
created_at,
game_manager,
tunnel_service,
}
Expand Down Expand Up @@ -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
Expand All @@ -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,
}
}

Expand Down

0 comments on commit c926ee7

Please sign in to comment.