Commit Graph

95 Commits

Author SHA1 Message Date
Kieran Kihn
5196de460a refactor(game): simplify card play logic
- Replaced iterator-based `play` methods with a single card-based method.
- Streamlined logic in `ServerPlayerState`, `ClientGameState`, and `HandCard`.
2025-12-07 13:57:54 +08:00
Kieran Kihn
ea87fca1fd fix(network): ensure write continuation upon successful async_write
- Added a check in `Session::doWrite` to invoke subsequent writes when the message queue is not empty.
2025-12-04 17:17:32 +08:00
Kieran Kihn
8335769cc8 feat(network): enhance NetworkClient with run and stop methods
- Added `run` and `stop` methods to manage the IO context lifecycle.
- Updated `send` to post messages to the IO context.
2025-12-04 17:17:24 +08:00
Kieran Kihn
024f99fc01 feat(network): add PlayerPublicState to InitGamePayload
- Introduced `PlayerPublicState` structure to represent player state.
- Updated `InitGamePayload` to include `PlayerPublicState` for all players.
- Enhanced `MessageSerializer` to handle serialization and deserialization of `PlayerPublicState`.
2025-12-04 17:15:49 +08:00
Kieran Kihn
a136e309e6 chore: include Session.cpp in build configuration 2025-12-04 16:40:03 +08:00
Kieran Kihn
0e9f5bc924 test(network): simplify and consolidate NetworkClientTest
- Removed redundant tests and merged similar cases in `NetworkClientTest`.
- Updated `Connect` and `Send` tests to streamline functionality.
- Enhanced `MessageSerializerTest` to validate `playerId` in `InitGamePayload`.
2025-12-04 16:39:47 +08:00
Kieran Kihn
b22f88aa60 fix(server): include playerId in InitGamePayload initialization 2025-12-04 16:39:24 +08:00
Kieran Kihn
731ed5a890 feat(network): add playerId to InitGamePayload
- Updated `InitGamePayload` to include `playerId`.
- Modified `MessageSerializer` to handle serialization and deserialization of `playerId`.
- Added validation for `playerId` in INIT_GAME payload.
2025-12-04 16:21:11 +08:00
Kieran Kihn
1d2e77aca3 refactor(network): integrate NetworkClient with Session
- Moved `Session` implementation to `Session.cpp` and `Session.h`.
- Updated `Session` to use a `message` queue for sending data, improving handling of writes.
- Adjusted `NetworkClient` and `NetworkServer` to integrate with the new `Session` logic.
2025-12-04 14:42:22 +08:00
Kieran Kihn
7363556ad5 feat(game): add init method to ClientGameState
- Introduced `init` method to initialize `discardPile` in `ClientGameState`.
- Added `<ranges>` header to support future range-based operations.
2025-12-04 13:28:56 +08:00
Kieran Kihn
766e9f9a21 feat(server): add game reset and end game handling
- Introduced `reset` method in `ServerGameState` to allow game state reinitialization.
- Added `handleEndGame` in `UnoServer` to detect and process game conclusion.
2025-12-04 13:28:36 +08:00
Kieran Kihn
535e62c852 feat(server): add argument parsing for server port
- Integrated `argparse` to parse command-line arguments for setting the server port.
- Default port set to `10001`.
2025-12-04 10:22:37 +08:00
Kieran Kihn
5f3a4083fd chore: update CMake library visibility for dependencies
- Changed `uno-game-lib` dependencies to `PUBLIC` visibility for `ftxui` components, `nlohmann_json`, and `asio`.
- Modified `uno-game-test` to use `PRIVATE` visibility for `uno-game-lib`.
2025-12-02 12:31:01 +08:00
Kieran Kihn
0d5a187cba feat(server): implement Uno server functionality
- Added `UnoServer` class to handle player actions, game state, and networking.
- Supported message handling for joining, starting, drawing, and playing cards.
- Integrated `ServerGameState` with `NetworkServer` for multiplayer game logic.
- Created `main.cpp` to initialize and run the Uno server.
2025-12-02 12:28:59 +08:00
Kieran Kihn
4b498e3b4a test(network): update tests to reflect InitGamePayload changes
- Modified `MessageSerializerTest` to pass `handCard.getCards()` instead of `handCard` in `InitGamePayload` construction.
2025-12-02 12:28:12 +08:00
Kieran Kihn
271d89df0d refactor(network): change DrawCardPayload::drawCount to size_t 2025-12-02 12:17:28 +08:00
Kieran Kihn
016e87288d refactor(network): replace HandCard with std::multiset<Card> in InitGamePayload
- Updated `InitGamePayload` to use `std::multiset<Card>` for `handCard`.
- Removed `serializeHandCard` method and replaced its usage with `serializeCards`.
- Adjusted `deserializeHandCard` to return `std::multiset<Card>` and simplified insertion logic.
- Cleaned up unused declarations in `MessageSerializer`.
2025-12-02 12:06:21 +08:00
Kieran Kihn
8a00b66047 refactor(game): return drawn cards in updateStateByDraw
- Updated `updateStateByDraw` to return a `std::vector<Card>` instead of `void`.
- Adjusted `ServerGameState`, `GameState`, and `ClientGameState` implementations to reflect the change.
2025-12-02 12:06:06 +08:00
Kieran Kihn
e5304b8c6c refactor(game): simplify GameState construction and initialization
- Removed `GameStatus` dependency from `GameState` constructors.
- Adjusted `ClientGameState` and `ServerGameState` constructors accordingly.
- Simplified `ServerGameState::init()` to handle card initialization and player actions.
- Updated tests to reflect these changes.
2025-12-02 11:08:42 +08:00
Kieran Kihn
a650b61610 chore: split client and server executables
- Added `uno-client` and `uno-server` executables.
- Updated `CMakeLists.txt` to reflect new structure.
- Removed duplicate requirement for `ftxui`.
2025-12-01 23:34:22 +08:00
Kieran Kihn
e3821e9f6d chore: update .gitignore to exclude cmake-build-debug-with-coverage/ 2025-12-01 15:07:35 +08:00
Kieran Kihn
a8ed0f6397 chore: Removed /utf-8 option from ftxui compile options for Clang to improve compatibility. 2025-12-01 15:07:22 +08:00
Kieran Kihn
a10674fbd3 test(network): add unit tests for NetworkClient and NetworkServer
- Introduced comprehensive test cases for `NetworkClient` and `NetworkServer` functionality.
- Added tests for constructor, `connect`, `send`, `read`, and round-trip scenarios in `NetworkClientTest`.
- Added tests for player management, message sending, and concurrent access in `NetworkServerTest`.
- Updated `CMakeLists.txt` to include `NetworkClientTest` and `NetworkServerTest` in the build configuration.
2025-11-29 18:52:08 +08:00
Kieran Kihn
c2ccaaf17e fix(network): ensure disconnect before connect in NetworkClient
- Added `disconnect` method to gracefully close existing connections before establishing a new one.
- Updated `connect` to call `disconnect` to prevent resource conflicts.
2025-11-29 18:51:58 +08:00
Kieran Kihn
12d1d6c93f fix(network): call accept on NetworkServer initialization and after player addition
- Ensure `accept` is called during `NetworkServer` initialization and after successfully adding a player to handle new connections continuously.
2025-11-29 18:51:41 +08:00
Kieran Kihn
b6144d33e9 fix(network): reattempt read in NetworkServer after message handling
- Added a call to `read()` after successfully processing an incoming message in `NetworkServer`.
2025-11-29 18:51:30 +08:00
Kieran Kihn
27cf988ee5 fix(network): validate and reattempt read if message length is invalid
- Updated `NetworkServer` to validate `messageLength` and reattempt `read` if it exceeds the maximum allowed size.
2025-11-29 18:51:18 +08:00
Kieran Kihn
3dd07e4b20 fix(network): use shared pointer for message length in Session::read
- Updated `Session::read` to use `std::shared_ptr` for message length to ensure proper memory handling in async operations.
2025-11-29 18:50:44 +08:00
Kieran Kihn
f4f66bb397 fix(network): send message length before body 2025-11-29 18:50:17 +08:00
Kieran Kihn
b0b6d78630 feat(network): add stop method to NetworkServer
- Implemented `stop` method in `NetworkServer` to close the acceptor and stop the IO context.
- Updated `NetworkServer.h` with corresponding method declaration and documentation.
2025-11-29 18:47:33 +08:00
Kieran Kihn
042ab95dda feat(network): add NetworkClient for TCP communication
- Introduced `NetworkClient` class for client-side TCP communication.
- Implemented `connect`, `send`, and `read` methods for message handling.
- Updated `CMakeLists.txt` to include `NetworkClient.cpp` in the build configuration.
2025-11-28 22:22:03 +08:00
Kieran Kihn
84c470859b feat(network): add NetworkServer for managing player sessions and communication
- Implemented `NetworkServer` and `Session` classes for handling TCP communication with players.
- Added `NetworkServer.cpp` and `NetworkServer.h` to the project.
- Integrated `asio` library into the build configuration in `CMakeLists.txt`.
2025-11-28 20:40:14 +08:00
Kieran Kihn
def62a18c6 test(network): enhance MessageSerializerTest with MessageStatus coverage
- Added `status_code` expectations in serialization and deserialization tests.
- Introduced tests for invalid `status_code` handling in `MessageSerializer`.
- Updated test cases to include `MessageStatus::OK` for payload validation and round-trip scenarios.
2025-11-23 15:03:09 +08:00
Kieran Kihn
a341b7330b test(game): fix CardTest expectations for colorToString output
- Updated `colorToString` expectations in `CardTest` to match corrected behavior for Red cards.
2025-11-23 15:02:49 +08:00
Kieran Kihn
db69b399ec refactor(game): remove redundant colorToString logic for wild card types
- Eliminated unnecessary wild card handling in `Card::colorToString`.
2025-11-23 15:02:30 +08:00
Kieran Kihn
7300de3fe4 feat(network): refactor MessageSerializer for modular serialization and validation
- Added reusable `serializeCards` function for card list serialization.
- Enhanced `DrawCardPayload` with a `cards` field.
- Introduced `serializeMessageStatus` and `deserializeMessageStatus` for message status handling.
- Updated deserialization logic to validate and include `status_code` field.
- Improved error messaging and validation for payload deserialization methods.
2025-11-23 15:02:14 +08:00
Kieran Kihn
7301a2c787 feat(network): enhance Message validation and add EMPTY payload type
- Introduced `MessageStatus` for message validation and tracking.
- Added `EMPTY` payload type to `MessagePayloadType` for invalid or empty messages.
- Updated `Message` constructor to validate payload type and status.
- Implemented `getMessageStatus` to retrieve the message status.
2025-11-23 15:01:08 +08:00
Kieran Kihn
6c50c600bf chore: update .clang-format configuration
- Disallow short `if` statements on a single line by setting `AllowShortIfStatementsOnASingleLine` to `Never`.
- Enable insertion of braces for control statements with `InsertBraces`.
2025-11-23 14:58:55 +08:00
Kieran Kihn
09f252e443 chore: update .gitignore to exclude build/ directory 2025-11-21 22:53:49 +08:00
Kieran Kihn
6757f559ea test(network): add MessageSerializerTest for serialization and deserialization coverage
- Added comprehensive unit tests for `MessageSerializer` in `MessageSerializerTest.cpp`.
- Included `MessageSerializerTest.cpp` in `test/CMakeLists.txt`.
2025-11-21 22:53:35 +08:00
Kieran Kihn
6ea7ad71c2 feat(network): add MessageSerializer for message serialization/deserialization
- Implemented `MessageSerializer` class to handle JSON serialization and deserialization of game messages and payloads.
- Added `MessageSerializer.cpp` and `MessageSerializer.h` to `CMakeLists.txt`.
- Linked `nlohmann_json` library to the project.
2025-11-21 22:50:31 +08:00
Kieran Kihn
820358e0a3 feat(network): add Message class with payload types
- Introduced `Message` class in `src/network` for handling game-related message payloads (e.g., `JOIN_GAME`, `START_GAME`, etc.).
- Added `Message.cpp` and `Message.h` to `CMakeLists.txt`.
2025-11-21 22:49:23 +08:00
Kieran Kihn
6393a5b311 chore: update CMakeList.txt
- link `uno-game-lib` to `uno-game` instead of add sources to `uno-game`
- styled `CMakedList.txt` and `test/CMakeList.txt`
2025-11-20 13:32:15 +08:00
Kieran Kihn
65b932fc47 chore: update .gitignore
- add `.vscode`, `.idea` and `.cache` to `.gitignore`
- exclude `.idea` from VCS
2025-11-20 13:30:27 +08:00
Kieran Kihn
e78741bd9d refactor(game): encapsulate Player and ClientGameState state management
- Replaced public `Player::handCard` pointer with private `HandCard` member.
- Introduced `draw`, `play`, and `getCards` methods in `Player` and `ClientGameState`.
- Modified `ClientGameState` constructor to accept player name instead of `Player` object.
- Updated tests to reflect new `Player` and `ClientGameState` structure.
2025-11-18 17:15:07 +08:00
Kieran Kihn
23cd94e656 test(game): refine GameStateTest while-loop logic and assertions
- Adjusted while-loop conditions to streamline test execution.
- Enhanced `canBePlayedOn` validation by passing `drawCount`.
- Added assertion to validate card count consistency during gameplay.
2025-11-17 22:11:14 +08:00
Kieran Kihn
423c237a39 fix(game): remove redundant PlayerState::play call in GameState::play
- Eliminated unnecessary `PlayerState::play` method invocation to streamline card play logic.
2025-11-17 22:10:16 +08:00
Kieran Kihn
b027e5c4db fix(game): update Card::canBePlayedOn to include draw count validation
- Modified `canBePlayedOn` method to consider `drawCount` for additional play restrictions.
- Updated `GameState::updateStateByCard` to pass `drawCount` when validating playable cards.
2025-11-17 21:33:36 +08:00
Kieran Kihn
b7dcaabc28 test(game): add GameStateTest for client and server game state validation
- Added new unit tests in `GameStateTest.cpp` to validate `ClientGameState` and `ServerGameState` behavior.
- Updated `CMakeLists.txt` to include the new test file.
2025-11-17 21:17:56 +08:00
Kieran Kihn
e5f9666ff0 test(game): refactor PlayerTest to use HandCard::draw method instead of array constructor 2025-11-17 21:17:37 +08:00