source: network-game/server/server.cpp@ e708305

Last change on this file since e708305 was 64a1f4e, checked in by dportnoy <dmp1488@…>, 11 years ago

Game.addPlayer reset all the player's stats as well

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