-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.cpp
194 lines (185 loc) · 5.6 KB
/
room.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "room.h"
void sendSize(int fd, uint16_t size)
{
size = htons(size);
send(fd, &size, sizeof(uint16_t), 0);
}
// void sendMessage(int fd, const char *message, int size)
// {
// int n = send(fd, message, size, 0);
// if (n < 0)
// printf("send message error:");
// if (n < sizeof(message))
// printf("not whole message sent\n");
// }
void Room::sendGameState(int fd, char *message, int size)
{
unsigned long int n = send(fd, message, size, 0);
if (n <= 0)
printf("send message error:");
if (n < sizeof(message))
printf("not whole message sent\n");
}
Room::Room()
{
num_clients = 0;
waitTime = 30;
ingame = false;
memset(clientsFd, 0, sizeof(clientsFd));
memset(health, 0, sizeof(health));
}
void Room::addClient(int fd, std::string name)
{
std::unique_lock<std::mutex> lock(clientFdsMutex);
this->clientsFd[state.addPlayer(name)] = fd;
num_clients++;
// if(num_clients==1)
// set as admin
}
void Room::removeClient(int id)
{
if (state.isActive(id))
{
shutdown(this->clientsFd[id], SHUT_RDWR);
close(this->clientsFd[id]);
this->clientsFd[id] = 0;
state.removePlayer(id);
num_clients--;
}
}
void Room::roomLoop()
{
int h;
float x, y;
char names[9 * 6];
char statemessage[14 * 6 + 1 + 4], playerState[15];
state.startNewGame();
while (true)
{
// waitng room
while (waitTime >= 0)
{
// check on message queue with mutex if new clients arrived
// send information abaout time left to all clients
if (num_clients < 1)
{
// for (int i = 0; i < 6; i++)
// {
// if (state.isActive(i))
// {
// removeClient(i);
// }
// // TODO: sent information to serwer that its shutdown so that it will be destroyed
// }
// // destroyme.append(id_t);
// return;
waitTime=30;
}
else
{
std::unique_lock<std::mutex> lock(clientFdsMutex);
memset(statemessage, 0, sizeof(statemessage));
state.createGameStateMessage(statemessage);
state.getPlayersNames(names);
for (int i = 0; i < 6; i++)
{
if (state.isActive(i))
{
int f = clientsFd[i];
sendSize(f, num_clients);
send(f, statemessage, 89, 0);
sendSize(f, waitTime);
send(f, names, 54, 0);
if (recv(f, playerState, 15, 0) <= 0)
{
health[i]++;
printf("Client not responding\n");
if (health[i] > 0)
removeClient(i);
}
else
health[i] = 0;
std::stringstream iss(playerState);
iss >> x >> y >> h;
if (h == 5)
{
// TODO rozłącz i usun gracza z gry
removeClient(i);
}
else if (h == 3 && i == 0)
{
waitTime = 1;
}
}
}
waitTime--;
}
sleep(1);
}
ingame = true;
// ingame
while (!state.game_over)
{
std::unique_lock<std::mutex> lock(clientFdsMutex);
state.createGameStateMessage(statemessage);
for (int i = 0; i < 6; i++)
{
if (state.isActive(i))
// broken pipe exception if clients disconnects
send(clientsFd[i], statemessage, 89, 0);
}
// recieve && apply forces by using poll; if n== 0 in the player state close the connection and delte player
// CHANGE TO POLL
for (int i = 0; i < 6; i++)
{
if (state.isActive(i))
{
if (recv(clientsFd[i], playerState, 15, 0) <= 0)
{
health[i]++;
printf("Client not responding\n");
if (health[i] > 3)
removeClient(i);
}
else
health[i] = 0;
// printf("%s\n",playerState);
std::stringstream iss(playerState);
iss >> x >> y >> h;
if (h == 1)
{
state.updatePlayerPosition(i, x, y);
}
if (h == 5)
{
// rozłącz i usun gracza z gry
removeClient(i);
}
}
}
state.Step();
if (num_clients <= 0)
{
break;
}
}
ingame = false;
waitTime = 30;
state.startNewGame();
}
}
std::string Room::getStateName()
{
std::string sname;
if (ingame)
{
sname.append("in game ");
}
else
{
sname.append("in lobby ");
}
sname.append(1, '0' + num_clients);
sname.append("/6");
return sname;
}