Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improved games api #68

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading