server/services/action/host_game.rs
1use crate::{
2 domain::{GameCommand, GameId, UserId},
3 error::{ServerError, bug},
4 server_state::ServerState,
5};
6
7/// Hosts a new game for the specified user.
8///
9/// This function creates a new game with the user as the host. The game
10/// is initialized in the starting phase, and the game ID is returned
11/// so it can be used to join or manage the game.
12///
13/// # Parameters
14///
15/// - `server_state`: The shared server state containing the game and database.
16/// - `user_id`: The ID of the user who will host the new game.
17///
18/// # Returns
19///
20/// Returns `Ok(GameId)` containing the ID of the newly created game.
21/// Returns a `ServerError` if the game cannot be created due to internal issues.
22pub async fn host_game(server_state: ServerState, user_id: UserId) -> Result<GameId, ServerError> {
23 let game_id = GameId::new();
24 let aggregate_id = game_id.value().to_string();
25
26 let command = GameCommand::HostGame { user_id, game_id };
27
28 server_state
29 .cqrs
30 .execute(&aggregate_id, command)
31 .await
32 .map_err(bug!())?;
33
34 Ok(game_id)
35}