1 | #include <cstdlib>
|
---|
2 | #include <cstdio>
|
---|
3 | #include <unistd.h>
|
---|
4 | #include <string>
|
---|
5 | #include <iostream>
|
---|
6 | #include <sstream>
|
---|
7 | #include <fstream>
|
---|
8 | #include <cstring>
|
---|
9 |
|
---|
10 | #include <vector>
|
---|
11 | #include <map>
|
---|
12 |
|
---|
13 | #include <csignal>
|
---|
14 |
|
---|
15 | #include <sys/time.h>
|
---|
16 |
|
---|
17 | #include <sys/socket.h>
|
---|
18 | #include <netdb.h>
|
---|
19 | #include <netinet/in.h>
|
---|
20 | #include <arpa/inet.h>
|
---|
21 |
|
---|
22 | #include <crypt.h>
|
---|
23 |
|
---|
24 | /*
|
---|
25 | #include <openssl/bio.h>
|
---|
26 | #include <openssl/ssl.h>
|
---|
27 | #include <openssl/err.h>
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include "../common/Compiler.h"
|
---|
31 | #include "../common/Common.h"
|
---|
32 | #include "../common/MessageProcessor.h"
|
---|
33 | #include "../common/WorldMap.h"
|
---|
34 | #include "../common/Player.h"
|
---|
35 | #include "../common/Projectile.h"
|
---|
36 | #include "../common/Game.h"
|
---|
37 | #include "../common/GameSummary.h"
|
---|
38 |
|
---|
39 | #include "DataAccess.h"
|
---|
40 |
|
---|
41 | using namespace std;
|
---|
42 |
|
---|
43 | // from used to be const. Removed that so I could take a reference
|
---|
44 | // and use it to send messages
|
---|
45 | void processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, MessageProcessor& msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, ofstream& outputLog);
|
---|
46 |
|
---|
47 | Player *findPlayerByName(map<unsigned int, Player*> &m, string name);
|
---|
48 | Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr);
|
---|
49 |
|
---|
50 | void quit(int sig);
|
---|
51 |
|
---|
52 | bool done;
|
---|
53 |
|
---|
54 | int main(int argc, char *argv[])
|
---|
55 | {
|
---|
56 | int sock, length;
|
---|
57 | struct sockaddr_in server;
|
---|
58 | struct sockaddr_in from; // info of client sending the message
|
---|
59 | NETWORK_MSG clientMsg, serverMsg;
|
---|
60 | MessageProcessor msgProcessor;
|
---|
61 | map<unsigned int, Player*> mapPlayers;
|
---|
62 | map<unsigned int, Projectile> mapProjectiles;
|
---|
63 | map<string, Game*> mapGames;
|
---|
64 | ofstream outputLog;
|
---|
65 |
|
---|
66 | done = false;
|
---|
67 |
|
---|
68 | signal(SIGINT, quit);
|
---|
69 |
|
---|
70 | //SSL_load_error_strings();
|
---|
71 | //ERR_load_BIO_strings();
|
---|
72 | //OpenSSL_add_all_algorithms();
|
---|
73 |
|
---|
74 | if (argc != 2)
|
---|
75 | {
|
---|
76 | cerr << "ERROR, expected server [domain] [port]" << endl;
|
---|
77 | exit(1);
|
---|
78 | }
|
---|
79 |
|
---|
80 | outputLog.open("server.log", ios::app);
|
---|
81 | outputLog << "Started server on " << getCurrentDateTimeString() << endl;
|
---|
82 |
|
---|
83 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
84 | if (sock < 0)
|
---|
85 | error("Opening socket");
|
---|
86 | length = sizeof(server);
|
---|
87 | bzero(&server,length);
|
---|
88 | server.sin_family=AF_INET;
|
---|
89 | server.sin_port=htons(atoi(argv[1]));
|
---|
90 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
91 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
92 | error("binding");
|
---|
93 |
|
---|
94 | set_nonblock(sock);
|
---|
95 |
|
---|
96 | msgProcessor = MessageProcessor(sock, &outputLog);
|
---|
97 |
|
---|
98 | timespec ts;
|
---|
99 | int timeLastUpdated = 0, curTime = 0;
|
---|
100 | while (!done)
|
---|
101 | {
|
---|
102 | usleep(5000);
|
---|
103 |
|
---|
104 | clock_gettime(CLOCK_REALTIME, &ts);
|
---|
105 | // make the number smaller so millis can fit in an int
|
---|
106 | ts.tv_sec -= 1368000000;
|
---|
107 | curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
|
---|
108 |
|
---|
109 | if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50)
|
---|
110 | {
|
---|
111 | timeLastUpdated = curTime;
|
---|
112 |
|
---|
113 | msgProcessor.cleanAckedMessages();
|
---|
114 | msgProcessor.resendUnackedMessages();
|
---|
115 |
|
---|
116 | map<unsigned int, Player*>::iterator it;
|
---|
117 |
|
---|
118 | // set targets for all chasing players (or make them attack if they're close enough)
|
---|
119 | // this should be moved into the games loop
|
---|
120 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
121 | {
|
---|
122 | Player* p = it->second;
|
---|
123 |
|
---|
124 | // check if it's time to revive dead players
|
---|
125 | if (p->isDead)
|
---|
126 | {
|
---|
127 | cout << "Player is dead" << endl;
|
---|
128 |
|
---|
129 | if (getCurrentMillis() - p->timeDied >= 10000)
|
---|
130 | {
|
---|
131 | p->isDead = false;
|
---|
132 |
|
---|
133 | POSITION spawnPos;
|
---|
134 |
|
---|
135 | switch (p->team)
|
---|
136 | {
|
---|
137 | case 0:// blue team
|
---|
138 | spawnPos = p->currentGame->getMap()->getStructureLocation(STRUCTURE_BLUE_FLAG);
|
---|
139 | break;
|
---|
140 | case 1:// red team
|
---|
141 | spawnPos = p->currentGame->getMap()->getStructureLocation(STRUCTURE_RED_FLAG);
|
---|
142 | break;
|
---|
143 | default:
|
---|
144 | // should never go here
|
---|
145 | cout << "Error: Invalid team" << endl;
|
---|
146 | break;
|
---|
147 | }
|
---|
148 |
|
---|
149 | // spawn the player to the right of their flag location
|
---|
150 | spawnPos.x = (spawnPos.x+1) * 25 + 12;
|
---|
151 | spawnPos.y = spawnPos.y * 25 + 12;
|
---|
152 |
|
---|
153 | p->pos = spawnPos.toFloat();
|
---|
154 | p->target = spawnPos;
|
---|
155 | p->health = p->maxHealth;
|
---|
156 |
|
---|
157 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
158 | p->serialize(serverMsg.buffer);
|
---|
159 |
|
---|
160 | msgProcessor.broadcastMessage(serverMsg, p->currentGame->getPlayers());
|
---|
161 | }
|
---|
162 |
|
---|
163 | continue;
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (p->currentGame != NULL) {
|
---|
167 | map<unsigned int, Player*> playersInGame = p->currentGame->getPlayers();
|
---|
168 | if (p->updateTarget(playersInGame))
|
---|
169 | {
|
---|
170 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
171 | p->serialize(serverMsg.buffer);
|
---|
172 | msgProcessor.broadcastMessage(serverMsg, playersInGame);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | // process players currently in a game
|
---|
178 | map<string, Game*>::iterator itGames;
|
---|
179 | Game* game = NULL;
|
---|
180 |
|
---|
181 | for (itGames = mapGames.begin(); itGames != mapGames.end();) {
|
---|
182 | game = itGames->second;
|
---|
183 | if (game->handleGameEvents()) {
|
---|
184 | // save game record
|
---|
185 | int winningTeam = -1;
|
---|
186 | if (game->getBlueScore() == 3)
|
---|
187 | winningTeam = 0;
|
---|
188 | else if (game->getRedScore() == 3)
|
---|
189 | winningTeam = 1;
|
---|
190 |
|
---|
191 | if (winningTeam == -1)
|
---|
192 | cout << "Error: Game ended, but neither team has a score of 3" << endl;
|
---|
193 | else {
|
---|
194 | map<unsigned int, Player*>::iterator it;
|
---|
195 | DataAccess da;
|
---|
196 |
|
---|
197 | for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++) {
|
---|
198 | da.saveGameHistory(it->second->getId(), winningTeam, game->getBlueScore(), game->getRedScore());
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | // send a GAME_INFO message with 0 players to force clients to delete the game
|
---|
203 | int numPlayers = 0;
|
---|
204 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
205 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
206 | strcpy(serverMsg.buffer+4, game->getName().c_str());
|
---|
207 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
208 |
|
---|
209 | delete itGames->second;
|
---|
210 | mapGames.erase(itGames++);
|
---|
211 | }else
|
---|
212 | itGames++;
|
---|
213 | }
|
---|
214 |
|
---|
215 | // move all projectiles
|
---|
216 | // see if this can be moved inside the game class
|
---|
217 | // this method can be moved when I add a MessageProcessor to the Game class
|
---|
218 | map<unsigned int, Projectile>::iterator itProj;
|
---|
219 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) {
|
---|
220 | game = itGames->second;
|
---|
221 | for (itProj = game->getProjectiles().begin(); itProj != game->getProjectiles().end(); itProj++)
|
---|
222 | {
|
---|
223 | cout << "About to call projectile move" << endl;
|
---|
224 | if (itProj->second.move(game->getPlayers()))
|
---|
225 | {
|
---|
226 | // send a REMOVE_PROJECTILE message
|
---|
227 | cout << "send a REMOVE_PROJECTILE message" << endl;
|
---|
228 | serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
|
---|
229 | memcpy(serverMsg.buffer, &itProj->second.id, 4);
|
---|
230 | game->removeProjectile(itProj->second.id);
|
---|
231 | msgProcessor.broadcastMessage(serverMsg, game->getPlayers());
|
---|
232 |
|
---|
233 | Player* target = game->getPlayers()[itProj->second.target];
|
---|
234 | game->dealDamageToPlayer(target, itProj->second.damage);
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | if (msgProcessor.receiveMessage(&clientMsg, &from) >= 0)
|
---|
241 | {
|
---|
242 | processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, outputLog);
|
---|
243 |
|
---|
244 | cout << "Finished processing the message" << endl;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | outputLog << "Stopped server on " << getCurrentDateTimeString() << endl;
|
---|
249 | outputLog.close();
|
---|
250 |
|
---|
251 | // delete all games
|
---|
252 | map<string, Game*>::iterator itGames;
|
---|
253 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
|
---|
254 | {
|
---|
255 | delete itGames->second;
|
---|
256 | }
|
---|
257 |
|
---|
258 | map<unsigned int, Player*>::iterator itPlayers;
|
---|
259 | for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++)
|
---|
260 | {
|
---|
261 | delete itPlayers->second;
|
---|
262 | }
|
---|
263 |
|
---|
264 | return 0;
|
---|
265 | }
|
---|
266 |
|
---|
267 | void processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, ofstream& outputLog)
|
---|
268 | {
|
---|
269 | NETWORK_MSG serverMsg;
|
---|
270 | DataAccess da;
|
---|
271 |
|
---|
272 | cout << "Inside processMessage" << endl;
|
---|
273 |
|
---|
274 | cout << "Received message" << endl;
|
---|
275 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
276 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
277 |
|
---|
278 | // Check that if an invalid message is sent, the client will correctly
|
---|
279 | // receive and display the response. Maybe make a special error msg type
|
---|
280 | switch(clientMsg.type)
|
---|
281 | {
|
---|
282 | case MSG_TYPE_REGISTER:
|
---|
283 | {
|
---|
284 | string username(clientMsg.buffer);
|
---|
285 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
286 | Player::PlayerClass playerClass;
|
---|
287 |
|
---|
288 | memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4);
|
---|
289 |
|
---|
290 | cout << "username: " << username << endl;
|
---|
291 | cout << "password: " << password << endl;
|
---|
292 |
|
---|
293 | bool validClass = false;
|
---|
294 |
|
---|
295 | switch(playerClass) {
|
---|
296 | case Player::CLASS_WARRIOR:
|
---|
297 | case Player::CLASS_RANGER:
|
---|
298 | validClass = true;
|
---|
299 | break;
|
---|
300 | case Player::CLASS_NONE:
|
---|
301 | validClass = false;
|
---|
302 | break;
|
---|
303 | }
|
---|
304 |
|
---|
305 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
306 |
|
---|
307 | if (validClass) {
|
---|
308 | int error = da.insertPlayer(username, password, playerClass);
|
---|
309 |
|
---|
310 | if (error)
|
---|
311 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
312 | else
|
---|
313 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
314 | }else
|
---|
315 | strcpy(serverMsg.buffer, "You didn't select a class");
|
---|
316 |
|
---|
317 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
318 |
|
---|
319 | break;
|
---|
320 | }
|
---|
321 | case MSG_TYPE_LOGIN:
|
---|
322 | {
|
---|
323 | cout << "Got login message" << endl;
|
---|
324 |
|
---|
325 | string username(clientMsg.buffer);
|
---|
326 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
327 |
|
---|
328 | Player* p = da.getPlayer(username);
|
---|
329 |
|
---|
330 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
331 | {
|
---|
332 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
333 | if (p != NULL)
|
---|
334 | delete(p);
|
---|
335 | }
|
---|
336 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
337 | {
|
---|
338 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
339 | delete(p);
|
---|
340 | }
|
---|
341 | else
|
---|
342 | {
|
---|
343 | cout << "new player id: " << p->getId() << endl;
|
---|
344 | p->setAddr(from);
|
---|
345 |
|
---|
346 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
347 | // tell the new player about all the existing players
|
---|
348 | cout << "Sending other players to new player" << endl;
|
---|
349 |
|
---|
350 | map<unsigned int, Player*>::iterator it;
|
---|
351 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
352 | {
|
---|
353 | it->second->serialize(serverMsg.buffer);
|
---|
354 |
|
---|
355 | cout << "sending info about " << it->second->name << endl;
|
---|
356 | cout << "sending id " << it->second->getId() << endl;
|
---|
357 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
358 | }
|
---|
359 |
|
---|
360 | // send info about existing games to new player
|
---|
361 | map<string, Game*>::iterator itGames;
|
---|
362 | Game* g;
|
---|
363 | int numPlayers;
|
---|
364 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
365 |
|
---|
366 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
|
---|
367 | {
|
---|
368 | g = itGames->second;
|
---|
369 | numPlayers = g->getNumPlayers();
|
---|
370 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
371 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
372 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
373 | }
|
---|
374 |
|
---|
375 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
376 | p->serialize(serverMsg.buffer);
|
---|
377 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
378 |
|
---|
379 | mapPlayers[p->getId()] = p;
|
---|
380 | }
|
---|
381 |
|
---|
382 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
383 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
384 |
|
---|
385 | break;
|
---|
386 | }
|
---|
387 | case MSG_TYPE_LOGOUT:
|
---|
388 | {
|
---|
389 | string name(clientMsg.buffer);
|
---|
390 | cout << "Player logging out: " << name << endl;
|
---|
391 |
|
---|
392 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
393 |
|
---|
394 | if (p == NULL)
|
---|
395 | {
|
---|
396 | strcpy(serverMsg.buffer+4, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
397 | cout << "Player not logged in" << endl;
|
---|
398 | }
|
---|
399 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
400 | p->addr.sin_port != from.sin_port )
|
---|
401 | {
|
---|
402 | strcpy(serverMsg.buffer+4, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server.");
|
---|
403 | cout << "Player logged in using a different connection" << endl;
|
---|
404 | }
|
---|
405 | else
|
---|
406 | {
|
---|
407 | // broadcast to all players before deleting p from the map
|
---|
408 | unsigned int playerId = p->getId();
|
---|
409 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
410 | memcpy(serverMsg.buffer, &playerId, 4);
|
---|
411 |
|
---|
412 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
413 |
|
---|
414 | mapPlayers.erase(p->getId());
|
---|
415 | delete p;
|
---|
416 |
|
---|
417 | strcpy(serverMsg.buffer+4, "You have successfully logged out.");
|
---|
418 | }
|
---|
419 |
|
---|
420 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
421 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
422 |
|
---|
423 | break;
|
---|
424 | }
|
---|
425 | case MSG_TYPE_CHAT:
|
---|
426 | {
|
---|
427 | cout << "Got a chat message" << endl;
|
---|
428 |
|
---|
429 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
430 |
|
---|
431 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
432 |
|
---|
433 | if (p == NULL)
|
---|
434 | {
|
---|
435 | strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
|
---|
436 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
437 | }
|
---|
438 | else
|
---|
439 | {
|
---|
440 | ostringstream oss;
|
---|
441 | oss << p->name << ": " << clientMsg.buffer;
|
---|
442 |
|
---|
443 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
444 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
445 | }
|
---|
446 |
|
---|
447 | break;
|
---|
448 | }
|
---|
449 | case MSG_TYPE_PLAYER_MOVE:
|
---|
450 | {
|
---|
451 | cout << "PLAYER_MOVE" << endl;
|
---|
452 |
|
---|
453 | unsigned int id;
|
---|
454 | int x, y;
|
---|
455 |
|
---|
456 | memcpy(&id, clientMsg.buffer, 4);
|
---|
457 | memcpy(&x, clientMsg.buffer+4, 4);
|
---|
458 | memcpy(&y, clientMsg.buffer+8, 4);
|
---|
459 |
|
---|
460 | cout << "x: " << x << endl;
|
---|
461 | cout << "y: " << y << endl;
|
---|
462 | cout << "id: " << id << endl;
|
---|
463 |
|
---|
464 | Player* p = mapPlayers[id];
|
---|
465 | bool validMessage = false;
|
---|
466 |
|
---|
467 | if ( p->addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
---|
468 | p->addr.sin_port == from.sin_port )
|
---|
469 | {
|
---|
470 | if (p->currentGame->startPlayerMovement(id, x, y)) {
|
---|
471 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
472 |
|
---|
473 | memcpy(serverMsg.buffer, &id, 4);
|
---|
474 | memcpy(serverMsg.buffer+4, &p->target.x, 4);
|
---|
475 | memcpy(serverMsg.buffer+8, &p->target.y, 4);
|
---|
476 |
|
---|
477 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
478 |
|
---|
479 | validMessage = true;
|
---|
480 | }
|
---|
481 | else
|
---|
482 | cout << "Bad terrain detected" << endl;
|
---|
483 | }
|
---|
484 | else
|
---|
485 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
---|
486 |
|
---|
487 | if (!validMessage)
|
---|
488 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
489 |
|
---|
490 | break;
|
---|
491 | }
|
---|
492 | case MSG_TYPE_PICKUP_FLAG:
|
---|
493 | {
|
---|
494 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
495 | cout << "PICKUP_FLAG" << endl;
|
---|
496 |
|
---|
497 | unsigned int id;
|
---|
498 |
|
---|
499 | memcpy(&id, clientMsg.buffer, 4);
|
---|
500 | cout << "id: " << id << endl;
|
---|
501 |
|
---|
502 | Player* p = mapPlayers[id];
|
---|
503 | unsigned int objectId = p->currentGame->processFlagPickupRequest(p);
|
---|
504 |
|
---|
505 | if (objectId >= 0) {
|
---|
506 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
507 |
|
---|
508 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
---|
509 | memcpy(serverMsg.buffer, &objectId, 4);
|
---|
510 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
511 |
|
---|
512 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
513 | p->serialize(serverMsg.buffer);
|
---|
514 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
515 | }
|
---|
516 |
|
---|
517 | break;
|
---|
518 | }
|
---|
519 | case MSG_TYPE_DROP_FLAG:
|
---|
520 | {
|
---|
521 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
522 | cout << "DROP_FLAG" << endl;
|
---|
523 |
|
---|
524 | unsigned int id;
|
---|
525 |
|
---|
526 | memcpy(&id, clientMsg.buffer, 4);
|
---|
527 | cout << "id: " << id << endl;
|
---|
528 |
|
---|
529 | Player* p = mapPlayers[id];
|
---|
530 |
|
---|
531 | ObjectType flagType = OBJECT_NONE;
|
---|
532 | if (p->hasBlueFlag)
|
---|
533 | flagType = OBJECT_BLUE_FLAG;
|
---|
534 | else if (p->hasRedFlag)
|
---|
535 | flagType = OBJECT_RED_FLAG;
|
---|
536 |
|
---|
537 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
538 |
|
---|
539 | p->currentGame->addObjectToMap(flagType, p->pos.x, p->pos.y);
|
---|
540 |
|
---|
541 | p->hasBlueFlag = false;
|
---|
542 | p->hasRedFlag = false;
|
---|
543 |
|
---|
544 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
545 | p->serialize(serverMsg.buffer);
|
---|
546 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
547 |
|
---|
548 | break;
|
---|
549 | }
|
---|
550 | case MSG_TYPE_ATTACK:
|
---|
551 | {
|
---|
552 | cout << "Received a START_ATTACK message" << endl;
|
---|
553 |
|
---|
554 | unsigned int id, targetId;
|
---|
555 |
|
---|
556 | memcpy(&id, clientMsg.buffer, 4);
|
---|
557 | memcpy(&targetId, clientMsg.buffer+4, 4);
|
---|
558 |
|
---|
559 | // need to make sure the target is in the sender's game
|
---|
560 |
|
---|
561 | Player* p = mapPlayers[id];
|
---|
562 | p->setTargetPlayer(targetId);
|
---|
563 | p->isChasing = true;
|
---|
564 |
|
---|
565 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
566 |
|
---|
567 | serverMsg.type = MSG_TYPE_ATTACK;
|
---|
568 | memcpy(serverMsg.buffer, &id, 4);
|
---|
569 | memcpy(serverMsg.buffer+4, &targetId, 4);
|
---|
570 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
571 |
|
---|
572 | break;
|
---|
573 | }
|
---|
574 | case MSG_TYPE_PROFILE:
|
---|
575 | {
|
---|
576 | cout << "Received a PROFILE message" << endl;
|
---|
577 |
|
---|
578 | unsigned int id;
|
---|
579 |
|
---|
580 | memcpy(&id, clientMsg.buffer, 4);
|
---|
581 |
|
---|
582 | cout << "Player id: " << id << endl;
|
---|
583 | unsigned int numGames = 0;
|
---|
584 | int** gameHistory = da.getPlayerGameHistory(id, numGames);
|
---|
585 | int* playerRecord = da.getPlayerRecord(id);
|
---|
586 |
|
---|
587 | int honorPoints = playerRecord[0];
|
---|
588 | int wins = playerRecord[1];
|
---|
589 | int losses = playerRecord[2];
|
---|
590 |
|
---|
591 | serverMsg.type = MSG_TYPE_PROFILE;
|
---|
592 |
|
---|
593 | memcpy(serverMsg.buffer, &honorPoints, 4);
|
---|
594 | memcpy(serverMsg.buffer+4, &wins, 4);
|
---|
595 | memcpy(serverMsg.buffer+8, &losses, 4);
|
---|
596 | memcpy(serverMsg.buffer+12, &numGames, 4);
|
---|
597 | for (unsigned int i=0; i<numGames; i++) {
|
---|
598 | memcpy(serverMsg.buffer+16+i*16, &gameHistory[i][0], 4);
|
---|
599 | memcpy(serverMsg.buffer+20+i*16, &gameHistory[i][1], 4);
|
---|
600 | memcpy(serverMsg.buffer+24+i*16, &gameHistory[i][2], 4);
|
---|
601 | memcpy(serverMsg.buffer+28+i*16, &gameHistory[i][3], 4);
|
---|
602 | delete[] gameHistory[i];
|
---|
603 | }
|
---|
604 |
|
---|
605 | //delete[] gameHistory;
|
---|
606 | free(gameHistory);
|
---|
607 | delete[] playerRecord;
|
---|
608 |
|
---|
609 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
610 |
|
---|
611 | break;
|
---|
612 | }
|
---|
613 | case MSG_TYPE_CREATE_GAME:
|
---|
614 | {
|
---|
615 | cout << "Received a CREATE_GAME message" << endl;
|
---|
616 |
|
---|
617 | string gameName(clientMsg.buffer);
|
---|
618 | cout << "Game name: " << gameName << endl;
|
---|
619 |
|
---|
620 | // check if this game already exists
|
---|
621 | if (mapGames.find(gameName) != mapGames.end()) {
|
---|
622 | cout << "Error: Game already exists" << endl;
|
---|
623 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
624 | }else {
|
---|
625 | Game* g = new Game(gameName, "../data/map.txt", &msgProcessor);
|
---|
626 | mapGames[gameName] = g;
|
---|
627 |
|
---|
628 | // add flag objects to the map
|
---|
629 | WorldMap* m = g->getMap();
|
---|
630 | m->createObjectsFromStructures();
|
---|
631 |
|
---|
632 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
633 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
634 | }
|
---|
635 |
|
---|
636 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
637 |
|
---|
638 | break;
|
---|
639 | }
|
---|
640 | case MSG_TYPE_JOIN_GAME:
|
---|
641 | {
|
---|
642 | cout << "Received a JOIN_GAME message" << endl;
|
---|
643 |
|
---|
644 | string gameName(clientMsg.buffer);
|
---|
645 | cout << "Game name: " << gameName << endl;
|
---|
646 |
|
---|
647 | // check if this game already exists
|
---|
648 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
649 | cout << "Error: Game does not exist" << endl;
|
---|
650 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
651 | }else {
|
---|
652 | Game* g = mapGames[gameName];
|
---|
653 | map<unsigned int, Player*>& players = g->getPlayers();
|
---|
654 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
655 |
|
---|
656 | if (players.find(p->getId()) != players.end()) {
|
---|
657 | cout << "Player " << p->name << " trying to join a game he's already in" << endl;
|
---|
658 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
659 | }else {
|
---|
660 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
661 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
662 | }
|
---|
663 | }
|
---|
664 |
|
---|
665 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
666 |
|
---|
667 | break;
|
---|
668 | }
|
---|
669 | case MSG_TYPE_LEAVE_GAME:
|
---|
670 | {
|
---|
671 | cout << "Received a LEAVE_GAME message" << endl;
|
---|
672 |
|
---|
673 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
674 | Game* g = p->currentGame;
|
---|
675 |
|
---|
676 | if (g == NULL) {
|
---|
677 | cout << "Player " << p->name << " is trying to leave a game, but is not currently in a game." << endl;
|
---|
678 |
|
---|
679 | /// should send a response back, maybe a new message type is needed
|
---|
680 | // not sure what to do here
|
---|
681 | }else {
|
---|
682 | cout << "Game name: " << g->getName() << endl;
|
---|
683 |
|
---|
684 | if (!p->isDead) {
|
---|
685 | ObjectType flagType = OBJECT_NONE;
|
---|
686 | if (p->hasBlueFlag)
|
---|
687 | flagType = OBJECT_BLUE_FLAG;
|
---|
688 | else if (p->hasRedFlag)
|
---|
689 | flagType = OBJECT_RED_FLAG;
|
---|
690 |
|
---|
691 | if (flagType != OBJECT_NONE)
|
---|
692 | g->addObjectToMap(flagType, p->pos.x, p->pos.y);
|
---|
693 | }
|
---|
694 |
|
---|
695 | p->currentGame = NULL;
|
---|
696 | g->removePlayer(p->getId());
|
---|
697 |
|
---|
698 | unsigned int playerId = p->getId();
|
---|
699 | serverMsg.type = MSG_TYPE_LEAVE_GAME;
|
---|
700 | memcpy(serverMsg.buffer, &playerId, 4);
|
---|
701 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
702 | msgProcessor.broadcastMessage(serverMsg, g->getPlayers());
|
---|
703 |
|
---|
704 | int numPlayers = g->getNumPlayers();
|
---|
705 |
|
---|
706 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
707 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
708 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
709 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
710 |
|
---|
711 | // if there are no more players in the game, remove it
|
---|
712 | if (numPlayers == 0) {
|
---|
713 | mapGames.erase(g->getName());
|
---|
714 | delete g;
|
---|
715 | }
|
---|
716 | }
|
---|
717 |
|
---|
718 | break;
|
---|
719 | }
|
---|
720 | case MSG_TYPE_JOIN_GAME_ACK:
|
---|
721 | {
|
---|
722 | cout << "Received a JOIN_GAME_ACK message" << endl;
|
---|
723 |
|
---|
724 | string gameName(clientMsg.buffer);
|
---|
725 | cout << "Game name: " << gameName << endl;
|
---|
726 |
|
---|
727 | // check if this game already exists
|
---|
728 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
729 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
730 |
|
---|
731 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
732 | }
|
---|
733 |
|
---|
734 | Game* g = mapGames[gameName];
|
---|
735 |
|
---|
736 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
737 |
|
---|
738 | // tell the new player about all map objects
|
---|
739 | // (currently just the flags)
|
---|
740 |
|
---|
741 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
742 | vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
|
---|
743 | vector<WorldMap::Object>::iterator itObjects;
|
---|
744 | cout << "sending items" << endl;
|
---|
745 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
746 | itObjects->serialize(serverMsg.buffer);
|
---|
747 | cout << "sending item id " << itObjects->id << endl;
|
---|
748 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
749 | }
|
---|
750 |
|
---|
751 |
|
---|
752 | // send the current score
|
---|
753 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
754 |
|
---|
755 | unsigned int blueScore = g->getBlueScore();
|
---|
756 | unsigned int redScore = g->getRedScore();
|
---|
757 | memcpy(serverMsg.buffer, &blueScore, 4);
|
---|
758 | memcpy(serverMsg.buffer+4, &redScore, 4);
|
---|
759 |
|
---|
760 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
761 |
|
---|
762 |
|
---|
763 | map<unsigned int, Player*>& oldPlayers = g->getPlayers();
|
---|
764 | g->addPlayer(p, true);
|
---|
765 | p->team = -1;
|
---|
766 |
|
---|
767 | // send info to other players
|
---|
768 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
769 | p->serialize(serverMsg.buffer);
|
---|
770 | cout << "Should be broadcasting the message" << endl;
|
---|
771 | msgProcessor.broadcastMessage(serverMsg, oldPlayers);
|
---|
772 |
|
---|
773 |
|
---|
774 | // tell the new player about all the players in the game (including himself)
|
---|
775 | cout << "Sending other players to new player" << endl;
|
---|
776 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
777 |
|
---|
778 | map<unsigned int, Player*>& allPlayers = g->getPlayers();
|
---|
779 | map<unsigned int, Player*>::iterator it;
|
---|
780 | for (it = allPlayers.begin(); it != allPlayers.end(); it++)
|
---|
781 | {
|
---|
782 | it->second->serialize(serverMsg.buffer);
|
---|
783 |
|
---|
784 | cout << "sending info about " << it->second->name << endl;
|
---|
785 | cout << "sending id " << it->second->getId() << endl;
|
---|
786 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
787 | }
|
---|
788 |
|
---|
789 |
|
---|
790 | int numPlayers = g->getNumPlayers();
|
---|
791 |
|
---|
792 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
793 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
794 | strcpy(serverMsg.buffer+4, gameName.c_str());
|
---|
795 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
796 |
|
---|
797 | break;
|
---|
798 | }
|
---|
799 | case MSG_TYPE_JOIN_TEAM:
|
---|
800 | {
|
---|
801 | cout << "Received a JOIN_TEAM message" << endl;
|
---|
802 |
|
---|
803 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
804 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
805 |
|
---|
806 | memcpy(&(p->team), clientMsg.buffer, 4);
|
---|
807 |
|
---|
808 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
809 | p->serialize(serverMsg.buffer);
|
---|
810 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
811 |
|
---|
812 | break;
|
---|
813 | }
|
---|
814 | case MSG_TYPE_START_GAME:
|
---|
815 | {
|
---|
816 | cout << "Received a START_GAME message" << endl;
|
---|
817 |
|
---|
818 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
819 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
820 |
|
---|
821 | serverMsg.type = MSG_TYPE_START_GAME;
|
---|
822 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
823 |
|
---|
824 | break;
|
---|
825 | }
|
---|
826 | default:
|
---|
827 | {
|
---|
828 | outputLog << "Received unknown message of type " << clientMsg.type << endl;
|
---|
829 |
|
---|
830 | break;
|
---|
831 | }
|
---|
832 | }
|
---|
833 | }
|
---|
834 |
|
---|
835 | Player *findPlayerByName(map<unsigned int, Player*> &m, string name)
|
---|
836 | {
|
---|
837 | map<unsigned int, Player*>::iterator it;
|
---|
838 |
|
---|
839 | for (it = m.begin(); it != m.end(); it++)
|
---|
840 | {
|
---|
841 | if ( it->second->name.compare(name) == 0 )
|
---|
842 | return it->second;
|
---|
843 | }
|
---|
844 |
|
---|
845 | return NULL;
|
---|
846 | }
|
---|
847 |
|
---|
848 | Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr)
|
---|
849 | {
|
---|
850 | map<unsigned int, Player*>::iterator it;
|
---|
851 |
|
---|
852 | for (it = m.begin(); it != m.end(); it++)
|
---|
853 | {
|
---|
854 | if ( it->second->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
---|
855 | it->second->addr.sin_port == addr.sin_port )
|
---|
856 | return it->second;
|
---|
857 | }
|
---|
858 |
|
---|
859 | return NULL;
|
---|
860 | }
|
---|
861 |
|
---|
862 | void quit(int sig) {
|
---|
863 | done = true;
|
---|
864 | }
|
---|