Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
client_request.h
1//
2// Created by Manuel on 28.01.2021.
3//
4// client_request is the base-class for all requests that are being sent from the client to the server.
5// It offers a function to deserialize a client_request subclass from a valid json.
6
7#ifndef WIZARD_CLIENT_REQUEST_H
8#define WIZARD_CLIENT_REQUEST_H
9
10#include <string>
11#include <unordered_map>
12#include "../../../../rapidjson/include/rapidjson/document.h"
13#include "../../serialization/serializable.h"
14#include "../../exceptions/WizardException.h"
15#include "../../serialization/uuid_generator.h"
16#include "../../serialization/json_utils.h"
17
18// Identifier for the different request types.
19// The RequestType is sent with every client_request to identify the type of client_request
20// during deserialization on the server side.
21enum RequestType {
22 join_game,
23 start_game,
24 play_card,
25 decide_trump_color,
26 estimate_tricks,
27 leave_game
28};
29
31protected:
32
34 RequestType _type;
35 std::string _req_id;
36 std::string _player_id;
37 std::string _game_id;
38 };
39
40 RequestType _type;
41 std::string _req_id;
42 std::string _player_id;
43 std::string _game_id;
44
45 explicit client_request(base_class_properties); // base constructor
46 static base_class_properties create_base_class_properties(RequestType type, std::string req_id, std::string& player_id, std::string& game_id);
47 static base_class_properties extract_base_class_properties(const rapidjson::Value& json);
48
49private:
50
51 // for deserialization
52 static const std::unordered_map<std::string, RequestType> _string_to_request_type;
53 // for serialization
54 static const std::unordered_map<RequestType, std::string> _request_type_to_string;
55
56public:
57 virtual ~client_request() {}
58
59 [[nodiscard]] RequestType get_type() const { return this->_type; }
60 [[nodiscard]] std::string get_req_id() const { return this->_req_id; }
61 [[nodiscard]] std::string get_game_id() const { return this->_game_id; }
62 [[nodiscard]] std::string get_player_id() const { return this->_player_id; }
63
64 // Tries to create the specific client_request from the provided json.
65 // Throws exception if parsing fails -> Use only in "try{ }catch()" block
66 static client_request* from_json(const rapidjson::Value& json);
67
68 // Serializes the client_request into a json object that can be sent over the network
69 void write_into_json(rapidjson::Value& json, rapidjson::Document::AllocatorType& allocator) const override;
70
71 [[nodiscard]] virtual std::string to_string() const;
72};
73
74
75#endif //WIZARD_CLIENT_REQUEST_H
Definition client_request.h:30
Definition serializable.h:11
Definition client_request.h:33