Wizard
Software Engineering Project - Wizard
|
Welcome to Wizard, a C++ implementation of the classic card game. Compete with your friends in exciting multiplayer gameplay!
You can read the game's rules here. The implementation features a client/server architecture for multiplayer scenarios. It uses wxWidgets for the GUI, sockpp for the network interface, rapidjson for object serialization, and googletest for the unit tests.
The game and source files is available on GitLab on the main branch. The game was developed based on the provided LAMA example project game. This project consists of a server and a client, each with their own main.cpp file. Each player can run his client and connect to the server in the same local network.
The client renders the GUI that is presented to the player, whereas the server is a console application without a user interface. Every action a player performs in the client application (for example playing a card) is sent as a formatted message to the server application, which processes the request.
Everything that is passed between client and server are objects of type client_request
and server_response
. Since the underlying network protocol works with TCP, these client_request
and server_response
objects are transformed into a JSON string, which can then be sent over the network. The receiving end reads the JSON string and constructs an object of type client_request
resp. server_response
that reflects the exact parameters that are specified in the JSON string. This process is known as serialization (object to string) and deserialization (string to object). If you want to read more about serialization, read me on Wikipedia.
Both, the client_request
and server_response
base classes, implement the abstract class serializable
with its write_into_json(...)
function. It allows to serialize the object instance into a JSON string. Additionally, they have a static function from_json(...)
, which allows creating an object instance from a JSON string.
When you implement your own specializations of client_request
(and server_response
, if necessary) you will have to implement the write_into_json(...)
functions yourself. Your subclass always has to call the write_into_json(...)
function of its base-class, such that the parameters of the base-class are written into the JSON document:
Here is the base-class implementation:
And here is the subclass implementation (for the play_card_request
class), where an additional field _card_id
is serialized.
The deserialization of client_request
JSONs always goes through the from_json(...)
function of the client_request
class. In this function, the "type" field, stored in the JSON, is inspected to determine, which subclass should be called to perform the deserialization:
Therefore, when you implement your own client_request
subclasses, remember to add a new element into the RequestType
enum to define your new request type. You will also have to add an entry for this new RequestType in the two unordered_maps _string_to_request_type
, resp. _request_type_to_string
in the client_request
base-class. Once this is done, you can add a check for your new RequestType
element in the from_json(...)
function of the client_request
base-class and call the specialized from_json(...)
function of your subclass from there.
Also, don't forget to set the correct RequestType
in the public constructor of your new client_request
subclass, here examplified at the play_card_request
class:
The deserialization in your subclass will look something like this:
There are plenty of examples of subclasses in the network/requests folder, where you can see how the serialization/deserialization scheme works.
All you have to do is use the static class ClientNetworkManager
on the client side and then invoke its sendRequest(const client_request& request)
function with the client_request
that you want to send. The server's response will arrive as an object of type request_response
and the ClientNetworkManager
will invoke the Process()
function of that request_response
object automatically.
All messages arriving at the server are being deserialized and then passed on to the handle_request(client_request* req)
function of the request_handler
singleton class. This function returns a pointer to an object of type request_response
(a subclass of server_response
), which is then automatically sent back to the requesting client. In your game implementation you should extend the handle_request(client_request* req)
function of the request_handler
, such that it can handle the client_request
that you add to your game and return an object of type request_response
with all parameters you want to send.
If the client_request
causes an update of the game_state you should also update all other players of that game about the game_state change. This happens in the game_instance
class, here examplified at the case where a start_game_request
calls the start_game(...)
function on the respective game_instance
on the server side:
By default, the server (specifically, the server_network_manager) will print every valid message that it receives to the console. In order for this to work in your project as well, you have to make sure that your CMake file contains a line, where the preprocessor variable PRINT_NETWORK_MESSAGES is defined for your server executable.
If a wrongly formatted message arrives at the server, it will print an error message with the received message string to the console, no matter if PRINT_NETWORK_MESSAGES is defined or not.
If you want to manually print one of your serialized messages (or any other serialized object for that matter), you can use the helper function json_utils::to_string(const rapidjson::Value* json)
as follows.
The game_state
class stores all parameters that are required to represent the game on the client (resp. server) side. In order to synchronize this game_state
among all players, the game_state
can also be serialized and deserialized. If a client_request
was successfully executed on the server, then the request_response
that is sent back to the client contains a serialized version of the updated game_state
. All other players receive the updated game_state
at the same time through a full_state_response
.
To serialize the game_state
, the same write_into_json(...)
function is used as for the client_request
.
The game_state
inherits from unique_serializable
, which essentially requires the write_into_json()
function and adds a unique id
to the object, such that it can be uniquely identified. Similarly, each parameter nested inside the game_state
(e.g. players, draw_pile, etc.) also inherit from unique_serializable
and therefore have their own id
and serialization, resp. deserialization functions.
On the client side, the new game_state
is then passed to the updateGameState(game_state*)
function of the GameController
class, which performs a redraw of the GUI.
Since you will have to add your own properties to the game_state
class (and probably create other classes that inherit from unique_serializable
to add to your game_state), we want to shortly elaborate how the serialization and deserialization works for subclasses of unique_serializable
. It's very similar to the client_request
class discussed earlier. Here is how the write_into_json(...)
function is implemented in the game_state
class of Wizard. Don't be shocked by the lengthy code. It's only a lot of repetition for each class property :
For deserialization, the from_json(...)
function is used, which is implemented as follows:
A similar scheme is applied in all other objects that inherit from unique_serializable
. Namely, these are:
player
hand
card
draw_pile
discard_pile
serializable_value
The GUI of the example project was built using the cross-platform GUI library wxWidgets. In order to build a project using wxWidget elements, you will first need to install wxWidgets on your system (see Section 1.1 above).
Here is a list of the most important elements that you will need to create your GUI. This is just meant as an overview, you will need to look up their correct usage in wxWidget's documentation.
wxIMPLEMENT_APP()
: In order to properly interact with the operating system's GUI, wxWidgets takes over the control flow of your application. wxWidgets therefore has its own main()
function, that you can reference with the macro wxIMPLEMENT_APP(wxApp*)
.wxApp
: The core class of your application must inherit from the wxApp
class. wxWidgets will call the OnInit()
function when starting the application. You can find the example project's implementation in src/client/app/Wizard
.wxFrame
: Each window of your application must inherit from the wxFrame
class. The example project has one window which you can find here: src/client/windows/GameWindow
wxPanel
: Panels serve as containers for elements within a window. All panels must instantiate or inherit from the wxPanel
class. A panel can contain one or more subpanels.wxBoxSizer
: Box sizers allow you to layout your panels within a window, either horizontally or vertically. By nesting box sizers, you can create complex layouts. Have a look at src/client/panels/ConnectionPanel
for an example.wxStaticText
: This class displays text in your GUI.wxButton
: This class creates a clickable button in your GUI.wxMessageBox()
: You can use this function to display a small pop-up window with text in front of the your current main window. This is useful to display error or status messages.Like in most GUI environments, objects in wxWidgets trigger events when they are interacted with by the user. For instance, a button will trigger a wxEVT_BUTTON
event when clicked. Similarly, a panel will trigger a wxEVT_LEFT_UP
event when clicked. There are many other events that can be triggered - for example when a keyboard key is pressed, when a window is resized, or when the cursor moves over an element.
In order to make the GUI interactive, we must specify the effect of an event. The easiest way is to bind an event to a lambda function. A lambda function is an unnamed function that can be used as an r-value. In C++, lambda functions have the following syntax:
Here is an example which binds a lambda function to a button click event:
In C++, we need to specify which variables from outside the lambda function's scope should be accessible within it. In the example above, myVariable
is declared outside of the lambda function but is used by the doSomething()
function call within the lambda function. We must therefore list myVariable
within the square brackets at the beginning of the lambda function definition, in order to make it accessible from within the lambda function's scope.