-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
123 lines (106 loc) · 3.62 KB
/
server.cpp
File metadata and controls
123 lines (106 loc) · 3.62 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
#include <httplib.h>
#include <iostream>
#include <vector>
#include<string>
#include "server.h"
//map
//Player
//wall: walls are represented by * in the map.
// Every wall has lenght of three that can either horizental or vertical
//1, 2, 3, 4 are four players that place in the corner of the map at the begging of the game
using namespace httplib;
using namespace std;
int main(void)
{
Server svr;
int n;
cout<<"WELCOME"<<endl;
cout<<"----------------------------------------------------"<<endl;
cout<<"1.Two player"<<endl
<<"2.Three Platers"<<endl
<<"3.Four Platers"<<endl;
cout<<"Enter the game mode(1, 2, 3): \n";
cin>>n;
n += 1;
int nn = n;
int turn = 1;
Game Corridor(n);
string msg = "";
string masg = "";
svr.Post("/hi", [&](const Request &req, Response& res) {
if(n == 0){
msg = "Sorry. This game is full right now, maybe next game.";
res.set_content(msg, "text/plain");
}else{
cout<<req.body<<endl;
//res.set_content(req.body, "text/plain");
if(n == 1)
cout<<"All players are ready! Let's start the game. \n";
masg = req.body;
masg = Corridor.addPlayer(masg);
res.set_content(masg, "text/plain");
n -= 1;
}
});
svr.Post("/move", [&](const Request &req, Response& res) {
cout<<req.body<<endl<<endl;
msg = req.body;
string player_name = to_string(msg[0]);
char move = msg[2];
cout<<"turn: "<< n<<endl;
cout<<"player name: "<< stoi(player_name)-48<<endl;
cout<<"Move: "<< move<<endl;
if(turn == (stoi(player_name)-48) ){
masg = Corridor.playerMovementHandler(stoi(player_name)-48, move);
if(masg != "Invalid move. Try another one.%"){
if(turn == nn)
turn = 1;
else
turn++;
}
}else{
masg = "It's not your turn to move.%";
}
cout<<masg<<endl;
res.set_content(masg, "text/plain");
});
svr.Get("/renderMap", [&](const Request &req, Response& res) {
masg = Corridor.renderMap();
res.set_content(masg, "text/plain");
});
svr.Post("/wallPlacement", [&](const Request &req, Response& res) {
cout<<req.body<<endl<<endl;
msg = req.body;
string player_name = to_string(msg[0]);
string wall_x = to_string(msg[2]);
string wall_y = to_string(msg[4]);
string vertical = to_string(msg[6]);
if(turn == (stoi(player_name)-48) ){
masg = Corridor.playerWallPlacementHandler(stoi(wall_x)-48, stoi(wall_y)-48, stoi(vertical)-48);
if(masg != "can't place a wall there!%"){
if(turn == nn)
turn = 1;
else
turn++;
}
}else{
masg = "It's not your turn to move.";
}
cout<<masg<<endl;
res.set_content(masg, "text/plain");
});
svr.Get("/win", [&](const Request& req, Response& res) {
res.set_content(to_string(Corridor.winner_flag), "text/plain");
});
svr.Get("/ready", [&](const Request& req, Response& res) {
int ready_flag = 0;
if(n == 0)
ready_flag = 1;
res.set_content(to_string(ready_flag), "text/plain");
});
svr.Get("/stop", [&](const Request& req, Response& res) {
Corridor.stop_flag = 1;
res.set_content(to_string(Corridor.stop_flag), "text/plain");
});
svr.listen("localhost", 8080);
}