-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
135 lines (120 loc) · 4.99 KB
/
server.cpp
File metadata and controls
135 lines (120 loc) · 4.99 KB
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
#include <iostream>
#include <string>
#include "utils/httplib.h"
#include "src/Quoridor.cpp"
#include "utils/json.hpp"
using namespace std;
using namespace httplib;
using namespace nlohmann;
// default port is 8080
int running_port = 8080;
int main(int argc, char *argv[]) {
ifstream i("response_codes.json");
json response_codes;
i >> response_codes;
Server srv;
if (argc < 2 || strlen(argv[1]) != 1) {
cout << BOLD(FRED("Invalid players number. (chosse between 2, 3, 4)")) << endl;
cout << "Help command: ./server.out [PLAYER_NUMBER] [PORT]" << endl;
return 0;
}
int players_number = int(argv[1][0] - '0');
if (players_number > 4 || players_number < 2) {
cout << BOLD(FRED("Invalid players number. (chosse between 2, 3, 4)")) << endl;
cout << "Help command: ./server.out [PLAYER_NUMBER] [PORT]" << endl;
return 0;
}
if (argc == 3) {
running_port = atoi(argv[2]);
}
Quoridor quoridor(players_number);
srv.Post("/join", [&](const auto& req, auto& res) {
string result;
if (!req.has_param("name")) {
result = "808";
}
string name = req.get_param_value("name");
result = quoridor.add_player(name);
string message = response_codes[result];
if (result == "800") {
message = "You joined the game successfully!";
}
json j_res = {
{ "message", message },
{ "status", result },
{ "board_size", to_string(BOARD_SIZE) }
};
res.set_content(j_res.dump(), "application/json");
});
srv.Post("/place-wall", [&](const auto& req, auto& res) {
string result = "";
if (!req.has_param("row") && !req.has_param("col") && !req.has_param("wall-type")) {
result = "808";
}
int row = atoi(req.get_param_value("row").c_str());
int col = atoi(req.get_param_value("col").c_str());
string wall_type = req.get_param_value("wall-type");
result = quoridor.place_wall(row, col, wall_type);
json j_res = {
{ "message", response_codes[result] },
{ "status", result }
};
res.set_content(j_res.dump(), "application/json");
});
srv.Post("/move", [&](const auto& req, auto& res) {
string result = "";
if (!req.has_param("direction")) {
result = "808";
}
string dir = req.get_param_value("direction");
if (!req.has_header("name")) {
result = "808";
}
string name = req.get_header_value("name");
int player_index = quoridor.get_player_index(name);
if (player_index == -1) {
result = "811";
}
else {
result = quoridor.move_player(player_index, dir);
}
json j_res = {
{ "message", response_codes[result] },
{ "status", result }
};
res.set_content(j_res.dump(), "application/json");
});
srv.Get("/game-data", [&](const auto& req, auto& res) {
string message = "", status = "800";
if (quoridor.players_index < quoridor.players_number) {
message = "Wait for other players to join...";
status = "801";
}
json j_res = {
{ "current_player", quoridor.players[quoridor.current_player].name },
{ "current_player_name_colored", quoridor.get_colored_name(quoridor.current_player) },
{ "board", quoridor.get_board() },
{ "message", message },
{ "status", status },
{ "player_numbers", quoridor.players_number },
{ "last_action_type", quoridor.last_action_type },
{ "last_action_message", "" }
};
int last_player_index = (quoridor.current_player - 1 + quoridor.players_number) % quoridor.players_number;
if (quoridor.last_action_type == "move") {
j_res["last_action_message"] = "Player " + quoridor.get_colored_name(last_player_index) + " move " + quoridor.last_action;
}
else if (quoridor.last_action_type == "place_wall") {
string wall_type = (quoridor.walls[quoridor.walls.size() - 1].type == "h") ? "horizontal" : "vertical";
j_res["last_action_message"] = "Player " + quoridor.get_colored_name(last_player_index) + " place " + wall_type + " wall at (" + to_string(quoridor.walls[quoridor.walls.size() - 1].x) + ", " + to_string(quoridor.walls[quoridor.walls.size() - 1].y) + ")";
}
if (quoridor.is_game_finished()) {
j_res["is_game_finished"] = true;
j_res["winner_player_name"] = quoridor.get_colored_name(quoridor.winner_player_index);
}
res.set_content(j_res.dump(), "application/json");
});
cout << "Server listening on port " << running_port << "..." << endl;
srv.listen("localhost", running_port);
return 0;
}