This repository has been archived by the owner on Jan 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonResponsePacketSerializer.cpp
172 lines (156 loc) · 5.39 KB
/
JsonResponsePacketSerializer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "JsonResponsePacketSerializer.h"
JsonResponsePacketSerializer::JsonResponsePacketSerializer()
{
}
JsonResponsePacketSerializer::~JsonResponsePacketSerializer()
{
}
/*
This function serialize the error message.
Input:ErrorResponse.
Output:String.
*/
std::string JsonResponsePacketSerializer::serializeResponse(ErrorResponse error_response)
{
json j;
to_json(j, error_response);
return j.dump();
}
/*
This function serialize the LoginResponse message.
Input:LoginResponse.
Output:String.
*/
std::string JsonResponsePacketSerializer::serializeResponse(LoginResponse login_response)
{
json j;
to_json(j, login_response);
return j.dump();
}
/*
This function serialize the LoginResponse message.
Input:LoginResponse.
Output:String.
*/
std::string JsonResponsePacketSerializer::serializeResponse(SignupResponse signup_response)
{
json j;
to_json(j, signup_response);
return j.dump();
}
/*
This function serialize the LogoutResponse message.
Input:LoginResponse.
Output:String.
*/
std::string JsonResponsePacketSerializer::serializeResponse(LogoutResponse logout_response)
{
json j;
to_json(j, logout_response);
return j.dump();
}
std::string JsonResponsePacketSerializer::serializeResponse(GetRoomResponse get_room_response)
{
std::vector<std::string> rooms;
for (RoomData room_data : get_room_response.get_rooms())
{
rooms.push_back((json{ {"name",room_data.get_name()},{"id",room_data.get_id()},{"max_players",room_data.get_max_players()},{"time_per_question",room_data.get_time_per_question()},{"is_active",room_data.get_is_active()} }).dump());
}
return (json{ { "status",get_room_response.get_status()},{"rooms",rooms} }).dump();
}
std::string JsonResponsePacketSerializer::serializeResponse(GetPlayersInRoomResponse get_players_in_room_response)
{
json j = json{ { "players",get_players_in_room_response.get_players() } };
return j.dump();
}
/*
This function serialize the JoinRoomResponse message.
Input:LoginResponse.
Output:String.
*/
std::string JsonResponsePacketSerializer::serializeResponse(JoinRoomResponse join_room_response)
{
json j;
to_json(j, join_room_response);
return j.dump();
}
/*
This function serialize the CreateRoomResponse message.
Input:LoginResponse.
Output:String.
*/
std::string JsonResponsePacketSerializer::serializeResponse(CreateRoomResponse create_room_response)
{
json j;
to_json(j, create_room_response);
return j.dump();
}
/*
This function serialize the HighscoreResponse message.
Input:LoginResponse.
Output:String.
*/
std::string JsonResponsePacketSerializer::serializeResponse(HighscoreResponse highscore_response)
{
Highscores highscores = highscore_response.get_highscores();
json j = json{ { "status",highscore_response.get_status()},{"averag_time_for_answer", highscores.get_averag_time_for_answer()},{"num_games",highscores.get_num_games()},{"right_answers",highscores.get_right_answers()},{"worng_answers",highscores.get_worng_answers()} };
return j.dump();
}
std::string JsonResponsePacketSerializer::serializeResponse(CloseRoomResponse close_room_response)
{
json j;
to_json(j, close_room_response);
return j.dump();
}
std::string JsonResponsePacketSerializer::serializeResponse(StartGameResponse start_game_response)
{
json j;
to_json(j, start_game_response);
return j.dump();
}
std::string JsonResponsePacketSerializer::serializeResponse(GetRoomStateResponse get_room_state_response)
{
json j = json{ {"status",get_room_state_response.get_status()} ,{"has_game_begun",get_room_state_response.get_has_game_begun()},{"question_count",get_room_state_response.get_question_count()},{"answer_timeout",get_room_state_response.get_answer_timeout()},{"players",get_room_state_response.get_players()} };
return j.dump();
}
std::string JsonResponsePacketSerializer::serializeResponse(LeaveRoomResponse leave_room_response)
{
json j;
to_json(j, leave_room_response);
return j.dump();
}
std::string JsonResponsePacketSerializer::serializeResponse(GetQuestionResponse get_question_response)
{
json j = { {"status",get_question_response.get_status()},{"question",get_question_response.get_question()},{"answers",get_question_response.get_answers()} };
return j.dump();
}
std::string JsonResponsePacketSerializer::serializeResponse(SubmitAnswerResponse submit_answer_response)
{
json j = json{ {"status",submit_answer_response.get_status()},{"correct_answer_id",submit_answer_response.get_correct_answer_id()}, {"is_right",submit_answer_response.get_is_right()} };
return j.dump();
}
std::string JsonResponsePacketSerializer::serializeResponse(GetGameResultsResponse get_game_results_response)
{
std::map <std::string, std::vector<unsigned int>> players_results;
std::vector<unsigned int> temp_vector;
for (PlayerResults player_result : get_game_results_response.get_results())
{
temp_vector.push_back(player_result.get_correct_answer_count());
temp_vector.push_back(player_result.get_wrong_answer_count());
temp_vector.push_back(player_result.get_average_answer_time());
players_results.insert(std::make_pair(player_result.get_username(), temp_vector));
temp_vector.clear();
}
json j = json{ {"status",get_game_results_response.get_status()},{"results",players_results} };
return j.dump();
}
std::string JsonResponsePacketSerializer::serializeResponse(LeaveGameResponse leave_game_response)
{
json j;
to_json(j, leave_game_response);
return j.dump();
}
void JsonResponsePacketSerializer::to_json(json & j, ErrorResponse & error_response)
{
j = json{ { "message",error_response.get_message() } };
}