server/services/action/play_computer.rs
1use crate::{
2 domain::{GameCommand, GameId, UserId},
3 error::{ServerError, bug},
4 server_state::ServerState,
5};
6
7/// Starts a game against the computer for the specified user.
8///
9/// This function creates a new game where the user plays against an automated
10/// opponent. The game is initialized in the starting phase, and the
11/// returned `GameId` can be used to track 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 play against the computer.
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 play_computer(
23 server_state: ServerState,
24 user_id: UserId,
25) -> Result<GameId, ServerError> {
26 let game_id = GameId::new();
27 let aggregate_id = game_id.value().to_string();
28
29 let command = GameCommand::PlayComputer { user_id, game_id };
30
31 server_state
32 .cqrs
33 .execute(&aggregate_id, command)
34 .await
35 .map_err(bug!())?;
36
37 Ok(game_id)
38}