1 | #include <cstdlib>
|
---|
2 | #include <cstdio>
|
---|
3 | #include <unistd.h>
|
---|
4 | #include <string>
|
---|
5 | #include <iostream>
|
---|
6 | #include <sstream>
|
---|
7 | #include <cstring>
|
---|
8 | #include <cmath>
|
---|
9 |
|
---|
10 | #include <vector>
|
---|
11 | #include <map>
|
---|
12 |
|
---|
13 | #include <sys/time.h>
|
---|
14 |
|
---|
15 | #include <sys/socket.h>
|
---|
16 | #include <netdb.h>
|
---|
17 | #include <netinet/in.h>
|
---|
18 | #include <arpa/inet.h>
|
---|
19 |
|
---|
20 | #include <crypt.h>
|
---|
21 |
|
---|
22 | /*
|
---|
23 | #include <openssl/bio.h>
|
---|
24 | #include <openssl/ssl.h>
|
---|
25 | #include <openssl/err.h>
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include "../common/Compiler.h"
|
---|
29 | #include "../common/Common.h"
|
---|
30 | #include "../common/Message.h"
|
---|
31 | #include "../common/WorldMap.h"
|
---|
32 | #include "../common/Player.h"
|
---|
33 |
|
---|
34 | #include "DataAccess.h"
|
---|
35 |
|
---|
36 | using namespace std;
|
---|
37 |
|
---|
38 | // from used to be const. Removed that so I could take a reference
|
---|
39 | // and use it to send messages
|
---|
40 | bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg, int sock);
|
---|
41 |
|
---|
42 | void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
|
---|
43 |
|
---|
44 | // this should probably go somewhere in the common folder
|
---|
45 | void error(const char *msg)
|
---|
46 | {
|
---|
47 | perror(msg);
|
---|
48 | exit(0);
|
---|
49 | }
|
---|
50 |
|
---|
51 | Player *findPlayerByName(map<unsigned int, Player> &m, string name)
|
---|
52 | {
|
---|
53 | map<unsigned int, Player>::iterator it;
|
---|
54 |
|
---|
55 | for (it = m.begin(); it != m.end(); it++)
|
---|
56 | {
|
---|
57 | if ( it->second.name.compare(name) == 0 )
|
---|
58 | return &(it->second);
|
---|
59 | }
|
---|
60 |
|
---|
61 | return NULL;
|
---|
62 | }
|
---|
63 |
|
---|
64 | Player *findPlayerByAddr(map<unsigned int, Player> &m, const sockaddr_in &addr)
|
---|
65 | {
|
---|
66 | map<unsigned int, Player>::iterator it;
|
---|
67 |
|
---|
68 | for (it = m.begin(); it != m.end(); it++)
|
---|
69 | {
|
---|
70 | if ( it->second.addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
---|
71 | it->second.addr.sin_port == addr.sin_port )
|
---|
72 | return &(it->second);
|
---|
73 | }
|
---|
74 |
|
---|
75 | return NULL;
|
---|
76 | }
|
---|
77 |
|
---|
78 | int main(int argc, char *argv[])
|
---|
79 | {
|
---|
80 | int sock, length, n;
|
---|
81 | struct sockaddr_in server;
|
---|
82 | struct sockaddr_in from; // info of client sending the message
|
---|
83 | NETWORK_MSG clientMsg, serverMsg;
|
---|
84 | map<unsigned int, Player> mapPlayers;
|
---|
85 | unsigned int unusedId = 1;
|
---|
86 |
|
---|
87 | //SSL_load_error_strings();
|
---|
88 | //ERR_load_BIO_strings();
|
---|
89 | //OpenSSL_add_all_algorithms();
|
---|
90 |
|
---|
91 | if (argc < 2) {
|
---|
92 | cerr << "ERROR, no port provided" << endl;
|
---|
93 | exit(1);
|
---|
94 | }
|
---|
95 |
|
---|
96 | WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
|
---|
97 |
|
---|
98 | // add some items to the map. They will be sent out
|
---|
99 | // to players when they login
|
---|
100 | for (int y=0; y<gameMap->height; y++) {
|
---|
101 | for (int x=0; x<gameMap->width; x++) {
|
---|
102 | switch (gameMap->getStructure(x, y)) {
|
---|
103 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
104 | gameMap->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
|
---|
105 | break;
|
---|
106 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
107 | gameMap->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
|
---|
108 | break;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
114 | if (sock < 0) error("Opening socket");
|
---|
115 | length = sizeof(server);
|
---|
116 | bzero(&server,length);
|
---|
117 | server.sin_family=AF_INET;
|
---|
118 | server.sin_port=htons(atoi(argv[1]));
|
---|
119 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
120 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
121 | error("binding");
|
---|
122 |
|
---|
123 | set_nonblock(sock);
|
---|
124 |
|
---|
125 | bool broadcastResponse;
|
---|
126 | timespec ts;
|
---|
127 | int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0;
|
---|
128 | while (true) {
|
---|
129 |
|
---|
130 | usleep(5000);
|
---|
131 |
|
---|
132 | clock_gettime(CLOCK_REALTIME, &ts);
|
---|
133 | // make the number smaller so millis can fit in an int
|
---|
134 | ts.tv_sec -= 1368000000;
|
---|
135 | curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
|
---|
136 |
|
---|
137 | if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50) {
|
---|
138 | timeLastUpdated = curTime;
|
---|
139 |
|
---|
140 | // maybe put this in a separate method
|
---|
141 | map<unsigned int, Player>::iterator it;
|
---|
142 | FLOAT_POSITION oldPos;
|
---|
143 | bool broadcastMove = false;
|
---|
144 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
|
---|
145 | oldPos = it->second.pos;
|
---|
146 | if (it->second.move(gameMap)) {
|
---|
147 |
|
---|
148 | // check if the move needs to be canceled
|
---|
149 | switch(gameMap->getElement(it->second.pos.x/25, it->second.pos.y/25)) {
|
---|
150 | case WorldMap::TERRAIN_NONE:
|
---|
151 | case WorldMap::TERRAIN_OCEAN:
|
---|
152 | case WorldMap::TERRAIN_ROCK:
|
---|
153 | {
|
---|
154 | it->second.pos = oldPos;
|
---|
155 | it->second.target.x = it->second.pos.x;
|
---|
156 | it->second.target.y = it->second.pos.y;
|
---|
157 | broadcastMove = true;
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | default:
|
---|
161 | // if there are no obstacles, do nothing
|
---|
162 | break;
|
---|
163 | }
|
---|
164 |
|
---|
165 | WorldMap::ObjectType flagType;
|
---|
166 | POSITION pos;
|
---|
167 | bool flagTurnedIn = false;
|
---|
168 |
|
---|
169 | switch(gameMap->getStructure(it->second.pos.x/25, it->second.pos.y/25)) {
|
---|
170 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
171 | {
|
---|
172 | if (it->second.team == 0 && it->second.hasRedFlag)
|
---|
173 | {
|
---|
174 | it->second.hasRedFlag = false;
|
---|
175 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
176 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
177 | flagTurnedIn = true;
|
---|
178 | }
|
---|
179 |
|
---|
180 | break;
|
---|
181 | }
|
---|
182 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
183 | {
|
---|
184 | if (it->second.team == 1 && it->second.hasBlueFlag)
|
---|
185 | {
|
---|
186 | it->second.hasBlueFlag = false;
|
---|
187 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
188 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
189 | flagTurnedIn = true;
|
---|
190 | }
|
---|
191 |
|
---|
192 | break;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (flagTurnedIn) {
|
---|
197 | // send an OBJECT message to add the flag back to its spawn point
|
---|
198 | pos.x = pos.x*25+12;
|
---|
199 | pos.y = pos.y*25+12;
|
---|
200 | gameMap->addObject(flagType, pos.x, pos.y);
|
---|
201 |
|
---|
202 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
203 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
204 |
|
---|
205 | map<unsigned int, Player>::iterator it2;
|
---|
206 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
207 | {
|
---|
208 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
209 | error("sendMessage");
|
---|
210 | }
|
---|
211 |
|
---|
212 | // this means a PLAYER message will be sent
|
---|
213 | broadcastMove = true;
|
---|
214 | }
|
---|
215 |
|
---|
216 | if (broadcastMove) {
|
---|
217 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
218 | it->second.serialize(serverMsg.buffer);
|
---|
219 |
|
---|
220 | cout << "about to broadcast move" << endl;
|
---|
221 | map<unsigned int, Player>::iterator it2;
|
---|
222 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
223 | {
|
---|
224 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
225 | error("sendMessage");
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
233 |
|
---|
234 | if (n >= 0) {
|
---|
235 | broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg, sock);
|
---|
236 |
|
---|
237 | // probably replace this with a function that prints based on the
|
---|
238 | // message type
|
---|
239 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
240 | if (broadcastResponse)
|
---|
241 | {
|
---|
242 | cout << "Should be broadcasting the message" << endl;
|
---|
243 |
|
---|
244 | map<unsigned int, Player>::iterator it;
|
---|
245 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
246 | {
|
---|
247 | cout << "Sent message back to " << it->second.name << endl;
|
---|
248 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
249 | error("sendMessage");
|
---|
250 | }
|
---|
251 | }
|
---|
252 | else
|
---|
253 | {
|
---|
254 | cout << "Should be sending back the message" << endl;
|
---|
255 |
|
---|
256 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
257 | error("sendMessage");
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | return 0;
|
---|
263 | }
|
---|
264 |
|
---|
265 | bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg, int sock)
|
---|
266 | {
|
---|
267 | DataAccess da;
|
---|
268 |
|
---|
269 | cout << "Received message" << endl;
|
---|
270 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
271 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
272 |
|
---|
273 | // maybe we should make a message class and have this be a member
|
---|
274 | bool broadcastResponse = false;
|
---|
275 |
|
---|
276 | // Check that if an invalid message is sent, the client will correctly
|
---|
277 | // receive and display the response. Maybe make a special error msg type
|
---|
278 | switch(clientMsg.type)
|
---|
279 | {
|
---|
280 | case MSG_TYPE_REGISTER:
|
---|
281 | {
|
---|
282 | string username(clientMsg.buffer);
|
---|
283 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
284 |
|
---|
285 | cout << "username: " << username << endl;
|
---|
286 | cout << "password: " << password << endl;
|
---|
287 |
|
---|
288 | int error = da.insertPlayer(username, password);
|
---|
289 |
|
---|
290 | if (!error)
|
---|
291 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
292 | else
|
---|
293 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
294 |
|
---|
295 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
296 |
|
---|
297 | break;
|
---|
298 | }
|
---|
299 | case MSG_TYPE_LOGIN:
|
---|
300 | {
|
---|
301 | cout << "Got login message" << endl;
|
---|
302 |
|
---|
303 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
304 |
|
---|
305 | string username(clientMsg.buffer);
|
---|
306 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
307 |
|
---|
308 | Player* p = da.getPlayer(username);
|
---|
309 |
|
---|
310 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
311 | {
|
---|
312 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
313 | }
|
---|
314 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
315 | {
|
---|
316 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
317 | }
|
---|
318 | else
|
---|
319 | {
|
---|
320 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
321 |
|
---|
322 | updateUnusedId(unusedId, mapPlayers);
|
---|
323 | p->id = unusedId;
|
---|
324 | cout << "new player id: " << p->id << endl;
|
---|
325 | p->setAddr(from);
|
---|
326 |
|
---|
327 | // choose a random team (either 0 or 1)
|
---|
328 | p->team = rand() % 2;
|
---|
329 |
|
---|
330 | // tell the new player about all the existing players
|
---|
331 | cout << "Sending other players to new player" << endl;
|
---|
332 |
|
---|
333 | map<unsigned int, Player>::iterator it;
|
---|
334 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
335 | {
|
---|
336 | it->second.serialize(serverMsg.buffer);
|
---|
337 |
|
---|
338 | cout << "sending info about " << it->second.name << endl;
|
---|
339 | cout << "sending id " << it->second.id << endl;
|
---|
340 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
341 | error("sendMessage");
|
---|
342 | }
|
---|
343 |
|
---|
344 | // tell the new player about all map objects
|
---|
345 | // (currently just the flags)
|
---|
346 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
347 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
348 | vector<WorldMap::Object>::iterator itObjects;
|
---|
349 | cout << "sending items" << endl;
|
---|
350 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
351 | itObjects->serialize(serverMsg.buffer);
|
---|
352 | cout << "sending item id " << itObjects->id << endl;
|
---|
353 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
354 | error("sendMessage");
|
---|
355 | }
|
---|
356 |
|
---|
357 | p->serialize(serverMsg.buffer);
|
---|
358 | cout << "Should be broadcasting the message" << endl;
|
---|
359 |
|
---|
360 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
361 | {
|
---|
362 | cout << "Sent message back to " << it->second.name << endl;
|
---|
363 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
364 | error("sendMessage");
|
---|
365 | }
|
---|
366 |
|
---|
367 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
368 | mapPlayers[unusedId] = *p;
|
---|
369 | }
|
---|
370 |
|
---|
371 | delete(p);
|
---|
372 |
|
---|
373 | break;
|
---|
374 | }
|
---|
375 | case MSG_TYPE_LOGOUT:
|
---|
376 | {
|
---|
377 | string name(clientMsg.buffer);
|
---|
378 | cout << "Player logging out: " << name << endl;
|
---|
379 |
|
---|
380 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
381 |
|
---|
382 | if (p == NULL)
|
---|
383 | {
|
---|
384 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
385 | cout << "Player not logged in" << endl;
|
---|
386 | }
|
---|
387 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
388 | p->addr.sin_port != from.sin_port )
|
---|
389 | {
|
---|
390 | strcpy(serverMsg.buffer, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server.");
|
---|
391 | cout << "Player logged in using a different connection" << endl;
|
---|
392 | }
|
---|
393 | else
|
---|
394 | {
|
---|
395 | if (p->id < unusedId)
|
---|
396 | unusedId = p->id;
|
---|
397 | mapPlayers.erase(p->id);
|
---|
398 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
---|
399 | }
|
---|
400 |
|
---|
401 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
402 |
|
---|
403 | break;
|
---|
404 | }
|
---|
405 | case MSG_TYPE_CHAT:
|
---|
406 | {
|
---|
407 | cout << "Got a chat message" << endl;
|
---|
408 |
|
---|
409 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
410 |
|
---|
411 | if (p == NULL)
|
---|
412 | {
|
---|
413 | strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
|
---|
414 | }
|
---|
415 | else
|
---|
416 | {
|
---|
417 | broadcastResponse = true;
|
---|
418 |
|
---|
419 | ostringstream oss;
|
---|
420 | oss << p->name << ": " << clientMsg.buffer;
|
---|
421 |
|
---|
422 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
423 | }
|
---|
424 |
|
---|
425 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
426 |
|
---|
427 | break;
|
---|
428 | }
|
---|
429 | case MSG_TYPE_PLAYER_MOVE:
|
---|
430 | {
|
---|
431 | cout << "PLAYER_MOVE" << endl;
|
---|
432 |
|
---|
433 | int id, x, y;
|
---|
434 |
|
---|
435 | memcpy(&id, clientMsg.buffer, 4);
|
---|
436 | memcpy(&x, clientMsg.buffer+4, 4);
|
---|
437 | memcpy(&y, clientMsg.buffer+8, 4);
|
---|
438 |
|
---|
439 | cout << "x: " << x << endl;
|
---|
440 | cout << "y: " << y << endl;
|
---|
441 | cout << "id: " << id << endl;
|
---|
442 |
|
---|
443 | if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
---|
444 | mapPlayers[id].addr.sin_port == from.sin_port )
|
---|
445 | {
|
---|
446 | // we need to make sure the player can move here
|
---|
447 | if (0 <= x && x < 300 && 0 <= y && y < 300 &&
|
---|
448 | gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
|
---|
449 | {
|
---|
450 | cout << "valid terrain" << endl;
|
---|
451 |
|
---|
452 | mapPlayers[id].target.x = x;
|
---|
453 | mapPlayers[id].target.y = y;
|
---|
454 |
|
---|
455 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
456 |
|
---|
457 | memcpy(serverMsg.buffer, &id, 4);
|
---|
458 | memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
|
---|
459 | memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
|
---|
460 |
|
---|
461 | broadcastResponse = true;
|
---|
462 | }
|
---|
463 | else
|
---|
464 | cout << "Bad terrain detected" << endl;
|
---|
465 | }
|
---|
466 | else // nned to send back a message indicating failure
|
---|
467 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
---|
468 |
|
---|
469 | break;
|
---|
470 | }
|
---|
471 | case MSG_TYPE_PICKUP_FLAG:
|
---|
472 | {
|
---|
473 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
474 | cout << "PICKUP_FLAG" << endl;
|
---|
475 |
|
---|
476 | int id;
|
---|
477 |
|
---|
478 | memcpy(&id, clientMsg.buffer, 4);
|
---|
479 | cout << "id: " << id << endl;
|
---|
480 |
|
---|
481 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
482 | vector<WorldMap::Object>::iterator itObjects;
|
---|
483 |
|
---|
484 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end();) {
|
---|
485 | POSITION pos = itObjects->pos;
|
---|
486 | bool gotFlag = false;
|
---|
487 |
|
---|
488 | if (posDistance(mapPlayers[id].pos, pos.toFloat()) < 10) {
|
---|
489 | switch (itObjects->type) {
|
---|
490 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
491 | if (mapPlayers[id].team == 1) {
|
---|
492 | gotFlag = true;
|
---|
493 | mapPlayers[id].hasBlueFlag = true;
|
---|
494 | broadcastResponse = true;
|
---|
495 | }
|
---|
496 | break;
|
---|
497 | case WorldMap::OBJECT_RED_FLAG:
|
---|
498 | if (mapPlayers[id].team == 0) {
|
---|
499 | gotFlag = true;
|
---|
500 | mapPlayers[id].hasRedFlag = true;
|
---|
501 | broadcastResponse = true;
|
---|
502 | }
|
---|
503 | break;
|
---|
504 | }
|
---|
505 |
|
---|
506 | if (gotFlag) {
|
---|
507 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
---|
508 | memcpy(serverMsg.buffer, &itObjects->id, 4);
|
---|
509 |
|
---|
510 | map<unsigned int, Player>::iterator it;
|
---|
511 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
512 | {
|
---|
513 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
514 | error("sendMessage");
|
---|
515 | }
|
---|
516 |
|
---|
517 | // remove the object from the server-side map
|
---|
518 | cout << "size before: " << gameMap->getObjects()->size() << endl;
|
---|
519 | itObjects = vctObjects->erase(itObjects);
|
---|
520 | cout << "size after: " << gameMap->getObjects()->size() << endl;
|
---|
521 | }
|
---|
522 | }
|
---|
523 |
|
---|
524 | if (!gotFlag)
|
---|
525 | itObjects++;
|
---|
526 | }
|
---|
527 |
|
---|
528 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
529 | mapPlayers[id].serialize(serverMsg.buffer);
|
---|
530 |
|
---|
531 | break;
|
---|
532 | }
|
---|
533 | case MSG_TYPE_DROP_FLAG:
|
---|
534 | {
|
---|
535 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
536 | cout << "DROP_FLAG" << endl;
|
---|
537 |
|
---|
538 | int id;
|
---|
539 |
|
---|
540 | memcpy(&id, clientMsg.buffer, 4);
|
---|
541 | cout << "id: " << id << endl;
|
---|
542 |
|
---|
543 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
544 | if (mapPlayers[id].hasBlueFlag)
|
---|
545 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
546 | else if (mapPlayers[id].hasRedFlag)
|
---|
547 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
548 |
|
---|
549 | gameMap->addObject(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y);
|
---|
550 |
|
---|
551 | // need to send the OBJECT message too
|
---|
552 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
553 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
554 |
|
---|
555 | map<unsigned int, Player>::iterator it;
|
---|
556 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
557 | {
|
---|
558 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
559 | error("sendMessage");
|
---|
560 | }
|
---|
561 |
|
---|
562 | mapPlayers[id].hasBlueFlag = false;
|
---|
563 | mapPlayers[id].hasRedFlag = false;
|
---|
564 |
|
---|
565 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
566 | mapPlayers[id].serialize(serverMsg.buffer);
|
---|
567 |
|
---|
568 | map<unsigned int, Player>::iterator it2;
|
---|
569 | broadcastResponse = true;
|
---|
570 |
|
---|
571 | break;
|
---|
572 | }
|
---|
573 | default:
|
---|
574 | {
|
---|
575 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
576 |
|
---|
577 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
578 |
|
---|
579 | break;
|
---|
580 | }
|
---|
581 | }
|
---|
582 |
|
---|
583 | return broadcastResponse;
|
---|
584 | }
|
---|
585 |
|
---|
586 | void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
|
---|
587 | {
|
---|
588 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
589 | id++;
|
---|
590 | }
|
---|