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

Last change on this file since cdb0e98 was cdb0e98, checked in by Dmitry Portnoy <dmp1488@…>, 11 years ago

Server replies to the client's PROFILE request with hard-coded data for ten games

  • Property mode set to 100644
File size: 27.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 }
[cdb0e98]569 case MSG_TYPE_PROFILE:
570 {
571 serverMsg.type = MSG_TYPE_PROFILE;
572
573 // each array is the score for one game
574 // the columns are result, team, blue score, and red score
575 // for result 0 is defeat and 1 is victory
576 // for team, 0 is blue and 1 is red
577 int scores[][4] = {
578 {1, 1, 2, 3},
579 {1, 0, 3, 2},
580 {0, 1, 3, 1},
581 {1, 1, 0, 3},
582 {0, 0, 2, 3},
583 {1, 0, 3, 2},
584 {1, 0, 3, 0},
585 {0, 1, 3, 1},
586 {1, 1, 1, 3},
587 {1, 0, 3, 2}
588 };
589
590 int honorPoints = 1000;
591 int numGames = 10;
592 memcpy(serverMsg.buffer, &honorPoints, 4);
593 memcpy(serverMsg.buffer+4, &numGames, 4);
594 for (unsigned int i=0; i<sizeof(scores)/sizeof(scores[0]); i++) {
595 memcpy(serverMsg.buffer+8+i*16, &scores[i][0], 4);
596 memcpy(serverMsg.buffer+12+i*16, &scores[i][1], 4);
597 memcpy(serverMsg.buffer+16+i*16, &scores[i][2], 4);
598 memcpy(serverMsg.buffer+20+i*16, &scores[i][3], 4);
599 }
600
601 msgProcessor.sendMessage(&serverMsg, &from);
602
603 break;
604 }
[b8f789d]605 case MSG_TYPE_CREATE_GAME:
[8e540f4]606 {
[b8f789d]607 cout << "Received a CREATE_GAME message" << endl;
608
609 string gameName(clientMsg.buffer);
610 cout << "Game name: " << gameName << endl;
611
[b48ef09]612 // check if this game already exists
613 if (mapGames.find(gameName) != mapGames.end()) {
[3ef8cf4]614 cout << "Error: Game already exists" << endl;
[b48ef09]615 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
[8554263]616 }else {
[d05c484]617 Game* g = new Game(gameName, "../data/map.txt", &msgProcessor);
[8554263]618 mapGames[gameName] = g;
619
620 // add flag objects to the map
621 WorldMap* m = g->getMap();
[0678d60]622 m->createObjectsFromStructures();
[8554263]623
624 serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
625 strcpy(serverMsg.buffer, gameName.c_str());
[70fc3e8]626 }
627
[8554263]628 msgProcessor.sendMessage(&serverMsg, &from);
[b92e6a7]629
[b48ef09]630 break;
631 }
632 case MSG_TYPE_JOIN_GAME:
633 {
634 cout << "Received a JOIN_GAME message" << endl;
[b92e6a7]635
[b48ef09]636 string gameName(clientMsg.buffer);
637 cout << "Game name: " << gameName << endl;
[b92e6a7]638
[b48ef09]639 // check if this game already exists
640 if (mapGames.find(gameName) == mapGames.end()) {
[3ef8cf4]641 cout << "Error: Game does not exist" << endl;
[b48ef09]642 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
[8554263]643 }else {
644 Game* g = mapGames[gameName];
645 map<unsigned int, Player*>& players = g->getPlayers();
646 Player* p = findPlayerByAddr(mapPlayers, from);
647
[204edcf]648 if (players.find(p->getId()) != players.end()) {
[8554263]649 cout << "Player " << p->name << " trying to join a game he's already in" << endl;
650 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
651 }else {
652 serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
653 strcpy(serverMsg.buffer, gameName.c_str());
654 }
[b92e6a7]655 }
656
[8554263]657 msgProcessor.sendMessage(&serverMsg, &from);
[b8f789d]658
659 break;
660 }
[ab8fd40]661 case MSG_TYPE_LEAVE_GAME:
662 {
663 cout << "Received a LEAVE_GAME message" << endl;
664
665 Player* p = findPlayerByAddr(mapPlayers, from);
666 Game* g = p->currentGame;
667
668 if (g == NULL) {
669 cout << "Player " << p->name << " is trying to leave a game, but is not currently in a game." << endl;
670
671 /// should send a response back, maybe a new message type is needed
[8554263]672 // not sure what to do here
673 }else {
674 cout << "Game name: " << g->getName() << endl;
[360c0f1]675
676 if (!p->isDead) {
[7f884ea]677 ObjectType flagType = OBJECT_NONE;
[360c0f1]678 if (p->hasBlueFlag)
[7f884ea]679 flagType = OBJECT_BLUE_FLAG;
[360c0f1]680 else if (p->hasRedFlag)
[7f884ea]681 flagType = OBJECT_RED_FLAG;
[360c0f1]682
[7f884ea]683 if (flagType != OBJECT_NONE)
[d05c484]684 g->addObjectToMap(flagType, p->pos.x, p->pos.y);
[360c0f1]685 }
[bcfd99a]686
687 p->currentGame = NULL;
[204edcf]688 g->removePlayer(p->getId());
[360c0f1]689
[204edcf]690 unsigned int playerId = p->getId();
[8554263]691 serverMsg.type = MSG_TYPE_LEAVE_GAME;
[204edcf]692 memcpy(serverMsg.buffer, &playerId, 4);
[8554263]693 strcpy(serverMsg.buffer+4, g->getName().c_str());
[d05c484]694 msgProcessor.broadcastMessage(serverMsg, g->getPlayers());
[95ffe57]695
[8554263]696 int numPlayers = g->getNumPlayers();
[ab8fd40]697
[8554263]698 serverMsg.type = MSG_TYPE_GAME_INFO;
699 memcpy(serverMsg.buffer, &numPlayers, 4);
700 strcpy(serverMsg.buffer+4, g->getName().c_str());
[d05c484]701 msgProcessor.broadcastMessage(serverMsg, mapPlayers);
[8554263]702
703 // if there are no more players in the game, remove it
704 if (numPlayers == 0) {
705 mapGames.erase(g->getName());
706 delete g;
707 }
[1248984]708 }
709
[ab8fd40]710 break;
711 }
[b48ef09]712 case MSG_TYPE_JOIN_GAME_ACK:
[b8f789d]713 {
[b48ef09]714 cout << "Received a JOIN_GAME_ACK message" << endl;
[e084950]715
[b8f789d]716 string gameName(clientMsg.buffer);
717 cout << "Game name: " << gameName << endl;
718
[b48ef09]719 // check if this game already exists
720 if (mapGames.find(gameName) == mapGames.end()) {
721 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
[8554263]722
723 msgProcessor.sendMessage(&serverMsg, &from);
[b48ef09]724 }
[b92e6a7]725
[f41a7f9]726 Game* g = mapGames[gameName];
[b92e6a7]727
[b48ef09]728 Player* p = findPlayerByAddr(mapPlayers, from);
[b92e6a7]729
730 // tell the new player about all map objects
731 // (currently just the flags)
732
733 serverMsg.type = MSG_TYPE_OBJECT;
[f41a7f9]734 vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
[b92e6a7]735 vector<WorldMap::Object>::iterator itObjects;
736 cout << "sending items" << endl;
737 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
738 itObjects->serialize(serverMsg.buffer);
739 cout << "sending item id " << itObjects->id << endl;
[8554263]740 msgProcessor.sendMessage(&serverMsg, &from);
[b92e6a7]741 }
742
743
744 // send the current score
745 serverMsg.type = MSG_TYPE_SCORE;
746
[9ba9b96]747 unsigned int blueScore = g->getBlueScore();
748 unsigned int redScore = g->getRedScore();
[f3fb980]749 memcpy(serverMsg.buffer, &blueScore, 4);
750 memcpy(serverMsg.buffer+4, &redScore, 4);
[b92e6a7]751
[8554263]752 msgProcessor.sendMessage(&serverMsg, &from);
[b92e6a7]753
[64a1f4e]754
755 map<unsigned int, Player*>& oldPlayers = g->getPlayers();
[f15d6a9]756 g->addPlayer(p, true);
757 p->team = -1;
[64a1f4e]758
[8554263]759 // send info to other players
[453087e]760 serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
[b92e6a7]761 p->serialize(serverMsg.buffer);
762 cout << "Should be broadcasting the message" << endl;
[64a1f4e]763 msgProcessor.broadcastMessage(serverMsg, oldPlayers);
[453087e]764
765
766 // tell the new player about all the players in the game (including himself)
767 cout << "Sending other players to new player" << endl;
768 serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
[8554263]769
770 map<unsigned int, Player*>& allPlayers = g->getPlayers();
771 map<unsigned int, Player*>::iterator it;
[453087e]772 for (it = allPlayers.begin(); it != allPlayers.end(); it++)
773 {
774 it->second->serialize(serverMsg.buffer);
775
776 cout << "sending info about " << it->second->name << endl;
[204edcf]777 cout << "sending id " << it->second->getId() << endl;
[8554263]778 msgProcessor.sendMessage(&serverMsg, &from);
[453087e]779 }
780
[64a1f4e]781
[b48ef09]782 int numPlayers = g->getNumPlayers();
783
[b8f789d]784 serverMsg.type = MSG_TYPE_GAME_INFO;
785 memcpy(serverMsg.buffer, &numPlayers, 4);
786 strcpy(serverMsg.buffer+4, gameName.c_str());
[d05c484]787 msgProcessor.broadcastMessage(serverMsg, mapPlayers);
[ab8fd40]788
[b8f789d]789 break;
790 }
[f15d6a9]791 case MSG_TYPE_JOIN_TEAM:
792 {
793 cout << "Received a JOIN_TEAM message" << endl;
794
795 Player* p = findPlayerByAddr(mapPlayers, from);
796 map<unsigned int, Player*> players = p->currentGame->getPlayers();
797
798 memcpy(&(p->team), clientMsg.buffer, 4);
799
800 serverMsg.type = MSG_TYPE_PLAYER;
801 p->serialize(serverMsg.buffer);
802 msgProcessor.broadcastMessage(serverMsg, players);
[abc4d56]803
804 break;
805 }
806 case MSG_TYPE_START_GAME:
807 {
808 cout << "Received a START_GAME message" << endl;
809
810 Player* p = findPlayerByAddr(mapPlayers, from);
811 map<unsigned int, Player*> players = p->currentGame->getPlayers();
812
813 serverMsg.type = MSG_TYPE_START_GAME;
814 msgProcessor.broadcastMessage(serverMsg, players);
815
816 break;
[f15d6a9]817 }
[b8f789d]818 default:
819 {
[9ee50ce]820 outputLog << "Received unknown message of type " << clientMsg.type << endl;
[e084950]821
[8e540f4]822 break;
823 }
[e3535b3]824 }
[8554263]825}
[da692b9]826
[95ffe57]827void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers)
[01d0d00]828{
[1106210]829 while (mapPlayers.find(id) != mapPlayers.end())
830 id++;
[01d0d00]831}
[8dad966]832
[95ffe57]833Player *findPlayerByName(map<unsigned int, Player*> &m, string name)
834{
835 map<unsigned int, Player*>::iterator it;
836
837 for (it = m.begin(); it != m.end(); it++)
838 {
839 if ( it->second->name.compare(name) == 0 )
840 return it->second;
841 }
842
843 return NULL;
844}
845
846Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr)
847{
848 map<unsigned int, Player*>::iterator it;
849
850 for (it = m.begin(); it != m.end(); it++)
851 {
852 if ( it->second->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
853 it->second->addr.sin_port == addr.sin_port )
854 return it->second;
855 }
856
857 return NULL;
858}
859
[f3fb980]860void quit(int sig) {
861 done = true;
862}
Note: See TracBrowser for help on using the repository browser.