Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
server_response.h
1//
2// Created by Manuel on 15.02.2021.
3//
4// Base class for all messages sent from the server to the client.
5// It offers a function to deserialize a server_response subclass from a valid json.
6
7#ifndef WIZARD_SERVER_RESPONSE_H
8#define WIZARD_SERVER_RESPONSE_H
9
10#include <string>
11#include <unordered_map>
12
13#include "../../serialization/serializable.h"
14
15// Identifier for the different response types.
16// The ResponseType is sent with every server_response to identify the type of server_response
17// during deserialization on the client side.
18enum ResponseType {
19 req_response,
20 state_diff_msg,
21 full_state_msg
22};
23
25private:
26
27 // for deserialization
28 static const std::unordered_map<std::string, ResponseType> _string_to_response_type;
29 // for serialization
30 static const std::unordered_map<ResponseType, std::string> _response_type_to_string;
31
32protected:
33 std::string _game_id;
34 ResponseType _type;
35
37 std::string game_id;
38 ResponseType type;
39 };
40
41 explicit server_response(base_class_properties); // base constructor
42 static base_class_properties create_base_class_properties(ResponseType type, const std::string& game_id);
43 static base_class_properties extract_base_class_properties(const rapidjson::Value& json);
44
45public:
46 ResponseType get_type() const;
47 std::string get_game_id() const;
48
49 // Tries to create the specific server_response from the provided json.
50 // Throws exception if parsing fails -> Use only inside "try{ }catch()" block
51 static server_response* from_json(const rapidjson::Value& json);
52
53 // Serializes the server_response into a json object that can be sent over the network
54 virtual void write_into_json(rapidjson::Value& json, rapidjson::Document::AllocatorType& allocator) const override;
55
56#ifdef WIZARD_CLIENT
57 virtual void Process() const = 0;
58#endif
59};
60
61
62#endif //WIZARD_SERVER_RESPONSE_H
Definition serializable.h:11
Definition server_response.h:24
Definition server_response.h:36