Cquence is a level-based card game that provides APIs for managing game sessions. The API allows users to start games, retrieve game states, and submit sequences for evaluation. The game ensures data persistence through XML files and supports progression across multiple levels.
CquenceGameplay.mp4
The GameController serves as the API layer for all game-related operations. It exposes the following endpoints:
- Route:
POST /api/Game/start
- Input:
userId
(string) - Output: Returns the initial game state, including the game ID, user ID, and the first level's sequence.
- Functionality:
- Initializes a new game session for the user.
- Calls
IGameService.StartGameAsync
to create and store the initial game state.
- Route:
GET /api/Game/{gameId}/state
- Input:
gameId
(string) - Output: Returns the current state of the game.
- Functionality:
- Fetches the current state of the game using the
gameId
. - Returns a
404
response if the game is not found.
- Fetches the current state of the game using the
- Route:
POST /api/Game/{gameId}/submit
- Input:
gameId
(string),userSequence
(list of strings) - Output: Returns the updated game state or error details for invalid submissions.
- Functionality:
- Evaluates the user-submitted sequence.
- Updates the game state for correct submissions or records errors for incorrect ones.
- Handles validation and exception scenarios gracefully.
The GameService implements the core business logic for game operations.
- Input:
userId
(string) - Output: Returns a
GameState
object initialized for the user. - Functionality:
- Generates a new game ID.
- Initializes the game state with level 1 and a randomly generated sequence.
- Persists the state using
IGameRepository.SaveGameStateAsync
.
- Input:
gameId
(string) - Output: Returns the
GameState
object or throws an error if the game is not found. - Functionality:
- Retrieves the game state using
IGameRepository.GetGameStateAsync
. - Validates the existence of the game state.
- Retrieves the game state using
- Input:
gameId
(string),userSequence
(list of strings) - Output: Updated
GameState
object or error message. - Functionality:
- Validates the user's sequence.
- Updates the game state for correct submissions (progress to the next level).
- Records incorrect submissions and logs details.
- Uses a random sequence generator to create new levels.
The GameRepository manages data persistence for game states using XML files.
- Input:
gameId
(string) - Output:
GameState
object ornull
if the game is not found. - Functionality:
- Reads game state data from an XML file.
- Logs warnings for missing files.
- Input:
gameState
(GameState object) - Output: None
- Functionality:
- Writes game state data to an XML file.
- Ensures the directory structure is created if missing.
-
Start Game:
- User invokes
POST /api/Game/start
withuserId
. GameController
callsStartGameAsync
.GameService
initializes and saves the game state.- The response includes game details and the initial sequence.
- User invokes
-
Retrieve Game State:
- User invokes
GET /api/Game/{gameId}/state
. GameController
callsGetGameStateAsync
.GameService
fetches the state fromGameRepository
.- The response includes the current game state.
- User invokes
-
Submit Sequence:
- User invokes
POST /api/Game/{gameId}/submit
withuserSequence
. GameController
callsSubmitSequenceAsync
.GameService
evaluates the sequence, updates state, and saves it.- The response includes updated state or error details.
- User invokes
-
Game Not Found:
- Returns a
404
response for invalidgameId
during state retrieval.
- Returns a
-
Invalid Sequence:
- Logs the incorrect submission.
- Includes detailed error messages with user and correct sequences.
- Detailed logging for game state retrieval, submissions, and errors.
- Logs include:
gameId
userId
- Sequences
- Error messages
- .NET Core
- Visual Studio Code
- GitHub repository access