source: network-game/server/server.cpp@ 778d0c9

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

Correction in the server's creation of the FINISH_GAME message

  • Property mode set to 100644
File size: 46.7 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>
[60017fc]9#include <cmath>
[371ce29]10
[01d0d00]11#include <vector>
12#include <map>
13
[d05086b]14#include <csignal>
15
[d211210]16#include <sys/time.h>
17
[73f75c1]18#include <sys/socket.h>
[371ce29]19#include <netdb.h>
[73f75c1]20#include <netinet/in.h>
21#include <arpa/inet.h>
22
[b128109]23#include <crypt.h>
24
[edfd1d0]25/*
[e3535b3]26#include <openssl/bio.h>
27#include <openssl/ssl.h>
28#include <openssl/err.h>
[edfd1d0]29*/
[e3535b3]30
[b53c6b3]31#include "../common/Compiler.h"
[3b1efcc]32#include "../common/Common.h"
[9b5d30b]33#include "../common/MessageProcessor.h"
[60017fc]34#include "../common/WorldMap.h"
[edfd1d0]35#include "../common/Player.h"
[8dad966]36#include "../common/Projectile.h"
[99afbb8]37#include "../common/Game.h"
[c9f6a1c]38#include "../common/GameSummary.h"
[b53c6b3]39
[36082e8]40#include "DataAccess.h"
[d2b411a]41
[e3535b3]42using namespace std;
43
[d05086b]44bool done;
45
[d211210]46// from used to be const. Removed that so I could take a reference
47// and use it to send messages
[95ffe57]48bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed, ofstream& outputLog);
[01d0d00]49
[95ffe57]50void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers);
51Player *findPlayerByName(map<unsigned int, Player*> &m, string name);
52Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr);
[c76134b]53void damagePlayer(Player *p, int damage);
[8e540f4]54
[95ffe57]55void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog);
[411c1ae]56
[73f75c1]57// this should probably go somewhere in the common folder
[e3535b3]58void error(const char *msg)
59{
60 perror(msg);
61 exit(0);
62}
63
[d05086b]64void quit(int sig) {
65 done = true;
66}
67
[e3535b3]68int main(int argc, char *argv[])
69{
70 int sock, length, n;
71 struct sockaddr_in server;
[3b1efcc]72 struct sockaddr_in from; // info of client sending the message
[e084950]73 NETWORK_MSG clientMsg, serverMsg;
[9b5d30b]74 MessageProcessor msgProcessor;
[95ffe57]75 map<unsigned int, Player*> mapPlayers;
[8dad966]76 map<unsigned int, Projectile> mapProjectiles;
[f41a7f9]77 map<string, Game*> mapGames;
[8dad966]78 unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
[b8601ee]79 int scoreBlue, scoreRed;
[d05086b]80 ofstream outputLog;
81
82 done = false;
[b8601ee]83
84 scoreBlue = 0;
85 scoreRed = 0;
[e084950]86
[d05086b]87 signal(SIGINT, quit);
88
[edfd1d0]89 //SSL_load_error_strings();
90 //ERR_load_BIO_strings();
91 //OpenSSL_add_all_algorithms();
[e3535b3]92
[95ffe57]93 if (argc != 2)
94 {
95 cerr << "ERROR, expected server [domain] [port]" << endl;
[73f75c1]96 exit(1);
[e3535b3]97 }
[60017fc]98
[d05086b]99 outputLog.open("server.log", ios::app);
100 outputLog << "Started server on " << getCurrentDateTimeString() << endl;
101
[7b43385]102 WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
[6e66ffd]103
104 // add some items to the map. They will be sent out
105 // to players when they login
[95ffe57]106 for (int y=0; y<gameMap->height; y++)
107 {
108 for (int x=0; x<gameMap->width; x++)
109 {
110 switch (gameMap->getStructure(x, y))
111 {
[5f868c0]112 case WorldMap::STRUCTURE_BLUE_FLAG:
113 gameMap->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
114 break;
115 case WorldMap::STRUCTURE_RED_FLAG:
116 gameMap->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
117 break;
118 }
119 }
120 }
[6e66ffd]121
[371ce29]122 sock = socket(AF_INET, SOCK_DGRAM, 0);
[5b1e31e]123 if (sock < 0)
124 error("Opening socket");
[e3535b3]125 length = sizeof(server);
126 bzero(&server,length);
127 server.sin_family=AF_INET;
128 server.sin_port=htons(atoi(argv[1]));
[2488852]129 server.sin_addr.s_addr=INADDR_ANY;
130 if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
[e084950]131 error("binding");
[73f75c1]132
[371ce29]133 set_nonblock(sock);
134
[da692b9]135 bool broadcastResponse;
[d211210]136 timespec ts;
[430c80e]137 int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0;
[95ffe57]138 while (!done)
139 {
[371ce29]140 usleep(5000);
141
[d211210]142 clock_gettime(CLOCK_REALTIME, &ts);
[430c80e]143 // make the number smaller so millis can fit in an int
[d69eb32]144 ts.tv_sec -= 1368000000;
[430c80e]145 curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
[d211210]146
[95ffe57]147 if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50)
148 {
[d211210]149 timeLastUpdated = curTime;
150
[d05086b]151 msgProcessor.cleanAckedMessages(&outputLog);
152 msgProcessor.resendUnackedMessages(sock, &outputLog);
[9b5d30b]153
[95ffe57]154 map<unsigned int, Player*>::iterator it;
[11d21ee]155
[ce2bb87]156 cout << "Updating player targets and respawning dead players" << endl;
157
[11d21ee]158 // set targets for all chasing players (or make them attack if they're close enough)
[95ffe57]159 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
160 {
[ffadc8e]161 Player* p = it->second;
162
[c76134b]163 // check if it's time to revive dead players
[ffadc8e]164 if (p->isDead)
[95ffe57]165 {
[35f6097]166
[ffadc8e]167 if (getCurrentMillis() - p->timeDied >= 10000)
[95ffe57]168 {
[ffadc8e]169 p->isDead = false;
[c76134b]170
171 POSITION spawnPos;
172
[ffadc8e]173 switch (p->team)
[95ffe57]174 {
[c76134b]175 case 0:// blue team
[35f6097]176 spawnPos = p->currentGame->getMap()->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
[c76134b]177 break;
178 case 1:// red team
[35f6097]179 spawnPos = p->currentGame->getMap()->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
[c76134b]180 break;
181 default:
182 // should never go here
183 cout << "Error: Invalid team" << endl;
184 break;
185 }
186
187 // spawn the player to the right of their flag location
188 spawnPos.x = (spawnPos.x+1) * 25 + 12;
189 spawnPos.y = spawnPos.y * 25 + 12;
190
[ffadc8e]191 p->pos = spawnPos.toFloat();
192 p->target = spawnPos;
193 p->health = p->maxHealth;
[c76134b]194
195 serverMsg.type = MSG_TYPE_PLAYER;
[ffadc8e]196 p->serialize(serverMsg.buffer);
[c76134b]197
[95ffe57]198 map<unsigned int, Player*>::iterator it2;
[35f6097]199 for (it2 = p->currentGame->getPlayers().begin(); it2 != p->currentGame->getPlayers().end(); it2++)
[c76134b]200 {
[35f6097]201 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
[c76134b]202 error("sendMessage");
203 }
204 }
205
206 continue;
207 }
208
[ffadc8e]209 if (p->currentGame != NULL) {
210 map<unsigned int, Player*> playersInGame = p->currentGame->getPlayers();
211 if (p->updateTarget(playersInGame))
[5b1e31e]212 {
[ffadc8e]213 serverMsg.type = MSG_TYPE_PLAYER;
214 p->serialize(serverMsg.buffer);
215
216 map<unsigned int, Player*>::iterator it2;
217 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
218 {
219 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
220 error("sendMessage");
221 }
[11d21ee]222 }
223 }
224 }
225
[ce2bb87]226 cout << "Processing players in a game" << endl;
227
[402cf86]228 // process players currently in a game
[23559e7]229 FLOAT_POSITION oldPos;
[e62b56c]230 map<string, Game*>::iterator itGames;
231 Game* game = NULL;
232 WorldMap* gameMap = NULL;
[e5b96e2]233 bool gameFinished;
[ce2bb87]234
[e5b96e2]235 for (itGames = mapGames.begin(); itGames != mapGames.end();) {
[e62b56c]236 game = itGames->second;
237 gameMap = game->getMap();
238 map<unsigned int, Player*>& playersInGame = game->getPlayers();
[e5b96e2]239 gameFinished = false;
[402cf86]240
[e62b56c]241 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
242 {
243 Player* p = it->second;
[483a2cb]244
[e62b56c]245 cout << "moving player" << endl;
246 bool broadcastMove = false;
[23559e7]247
[e62b56c]248 // xompute playersInGame here
[23559e7]249
[e62b56c]250 // move player and perform associated tasks
251 oldPos = p->pos;
252 if (p->move(gameMap)) {
[b73bc28]253
[e62b56c]254 cout << "player moved" << endl;
255 if (game->processPlayerMovement(p, oldPos))
256 broadcastMove = true;
257 cout << "player move processed" << endl;
[b73bc28]258
[e62b56c]259
260 WorldMap::ObjectType flagType;
261 POSITION pos;
262 bool flagTurnedIn = false;
263 bool flagReturned = false;
264 bool ownFlagAtBase = false;
265
266 // need to figure out how to move this to a different file
267 // while still sending back flag type and position
268 switch(gameMap->getStructure(p->pos.x/25, p->pos.y/25))
[e4c60ba]269 {
[e62b56c]270 case WorldMap::STRUCTURE_BLUE_FLAG:
[7553db9]271 {
[e62b56c]272 if (p->team == 0 && p->hasRedFlag)
[95ffe57]273 {
[e62b56c]274 // check that your flag is at your base
275 pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
276
277 vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
278 vector<WorldMap::Object>::iterator itObjects;
279
280 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
[95ffe57]281 {
[e62b56c]282 if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
[95ffe57]283 {
[e62b56c]284 if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12)
285 {
286 ownFlagAtBase = true;
287 break;
288 }
[446dc65]289 }
290 }
291
[e62b56c]292 if (ownFlagAtBase)
293 {
294 p->hasRedFlag = false;
295 flagType = WorldMap::OBJECT_RED_FLAG;
296 pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
297 flagTurnedIn = true;
298 scoreBlue++;
299 }
[446dc65]300 }
[7553db9]301
[e62b56c]302 break;
303 }
304 case WorldMap::STRUCTURE_RED_FLAG:
[7553db9]305 {
[e62b56c]306 if (p->team == 1 && p->hasBlueFlag)
307 {
308 // check that your flag is at your base
309 pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
[446dc65]310
[e62b56c]311 vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
312 vector<WorldMap::Object>::iterator itObjects;
[446dc65]313
[e62b56c]314 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
[95ffe57]315 {
[e62b56c]316 if (itObjects->type == WorldMap::OBJECT_RED_FLAG)
[95ffe57]317 {
[e62b56c]318 if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12)
319 {
320 ownFlagAtBase = true;
321 break;
322 }
[446dc65]323 }
324 }
325
[e62b56c]326 if (ownFlagAtBase)
327 {
328 p->hasBlueFlag = false;
329 flagType = WorldMap::OBJECT_BLUE_FLAG;
330 pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
331 flagTurnedIn = true;
332 scoreRed++;
333 }
[446dc65]334 }
[e4c60ba]335
[e62b56c]336 break;
337 }
[7553db9]338 }
[e4c60ba]339
[e62b56c]340 if (flagTurnedIn)
341 {
342 // send an OBJECT message to add the flag back to its spawn point
343 pos.x = pos.x*25+12;
344 pos.y = pos.y*25+12;
345 gameMap->addObject(flagType, pos.x, pos.y);
[e4c60ba]346
[e62b56c]347 serverMsg.type = MSG_TYPE_OBJECT;
348 gameMap->getObjects()->back().serialize(serverMsg.buffer);
[e4c60ba]349
[e62b56c]350 map<unsigned int, Player*>::iterator it2;
[b73bc28]351
[e62b56c]352 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
353 {
354 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
355 error("sendMessage");
356 }
[7553db9]357
[e62b56c]358 serverMsg.type = MSG_TYPE_SCORE;
359 memcpy(serverMsg.buffer, &scoreBlue, 4);
360 memcpy(serverMsg.buffer+4, &scoreRed, 4);
[b8601ee]361
[e62b56c]362 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
363 {
364 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
365 error("sendMessage");
366 }
[b8601ee]367
[e5b96e2]368 // check to see if the game should end
369 // move to its own method
[e62b56c]370 if (scoreBlue == 3 || scoreRed == 3) {
[e5b96e2]371 gameFinished = true;
[e4c60ba]372
[e62b56c]373 unsigned int winningTeam;
374 if (scoreBlue == 3)
375 winningTeam = 0;
376 else if (scoreRed == 3)
377 winningTeam = 1;
[446dc65]378
[e62b56c]379 serverMsg.type = MSG_TYPE_FINISH_GAME;
[c9f6a1c]380
381 // I should create an instance of the GameSummary object here and just serialize it into this message
[778d0c9]382 memcpy(serverMsg.buffer, &winningTeam, 4);
[e62b56c]383 memcpy(serverMsg.buffer+4, &scoreBlue, 4);
384 memcpy(serverMsg.buffer+8, &scoreRed, 4);
[778d0c9]385 strcpy(serverMsg.buffer+12, game->getName().c_str());
[e62b56c]386
387 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
388 {
389 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
390 error("sendMessage");
391 }
392 }
393
394 // this means a PLAYER message will be sent
395 broadcastMove = true;
396 }
397
398 // go through all objects and check if the player is close to one and if its their flag
399 vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
400 vector<WorldMap::Object>::iterator itObjects;
401 POSITION structPos;
402
403 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
[95ffe57]404 {
[e62b56c]405 POSITION pos = itObjects->pos;
406
407 if (posDistance(p->pos, pos.toFloat()) < 10)
[95ffe57]408 {
[e62b56c]409 if (p->team == 0 &&
410 itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
411 {
412 structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
413 flagReturned = true;
414 break;
415 }
416 else if (p->team == 1 &&
417 itObjects->type == WorldMap::OBJECT_RED_FLAG)
418 {
419 structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
420 flagReturned = true;
421 break;
422 }
[95ffe57]423 }
[e62b56c]424 }
425
426 if (flagReturned)
427 {
428 itObjects->pos.x = structPos.x*25+12;
429 itObjects->pos.y = structPos.y*25+12;
430
431 serverMsg.type = MSG_TYPE_OBJECT;
432 itObjects->serialize(serverMsg.buffer);
433
434 map<unsigned int, Player*>::iterator it2;
435 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
[95ffe57]436 {
[e62b56c]437 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
438 error("sendMessage");
[446dc65]439 }
440 }
441
[e62b56c]442 if (broadcastMove)
443 {
444 cout << "broadcasting player move" << endl;
445 serverMsg.type = MSG_TYPE_PLAYER;
446 p->serialize(serverMsg.buffer);
[446dc65]447
[e62b56c]448 // only broadcast message to other players in the same game
449 cout << "about to broadcast move" << endl;
[446dc65]450
[e62b56c]451 map<unsigned int, Player*>::iterator it2;
452 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
453 {
454 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
455 error("sendMessage");
456 }
457 cout << "done broadcasting player move" << endl;
[446dc65]458 }
459 }
460
[e62b56c]461 cout << "processing player attack" << endl;
462
463 // check if the player's attack animation is complete
464 if (p->isAttacking && p->timeAttackStarted+p->attackCooldown <= getCurrentMillis())
[95ffe57]465 {
[e62b56c]466 p->isAttacking = false;
467 cout << "Attack animation is complete" << endl;
468
469 //send everyone an ATTACK message
470 cout << "about to broadcast attack" << endl;
[23559e7]471
[e62b56c]472 serverMsg.type = MSG_TYPE_ATTACK;
473 memcpy(serverMsg.buffer, &p->id, 4);
474 memcpy(serverMsg.buffer+4, &p->targetPlayer, 4);
[402cf86]475
[95ffe57]476 map<unsigned int, Player*>::iterator it2;
[ce2bb87]477 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
[23559e7]478 {
[95ffe57]479 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
[23559e7]480 error("sendMessage");
481 }
[8dad966]482
[e62b56c]483 if (p->attackType == Player::ATTACK_MELEE)
484 {
485 cout << "Melee attack" << endl;
[ff2133a]486
[e62b56c]487 Player* target = playersInGame[p->targetPlayer];
488 damagePlayer(target, p->damage);
[8dad966]489
[e62b56c]490 if (target->isDead)
491 {
492 WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
493 if (target->hasBlueFlag)
494 flagType = WorldMap::OBJECT_BLUE_FLAG;
495 else if (target->hasRedFlag)
496 flagType = WorldMap::OBJECT_RED_FLAG;
[411c1ae]497
[e62b56c]498 if (flagType != WorldMap::OBJECT_NONE) {
499 addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, playersInGame, msgProcessor, sock, outputLog);
500 }
[411c1ae]501 }
502
[e62b56c]503 serverMsg.type = MSG_TYPE_PLAYER;
504 target->serialize(serverMsg.buffer);
505 }
506 else if (p->attackType == Player::ATTACK_RANGED)
507 {
508 cout << "Ranged attack" << endl;
[ff2133a]509
[e62b56c]510 Projectile proj(p->pos.x, p->pos.y, p->targetPlayer, p->damage);
511 game->assignProjectileId(&proj);
512 game->addProjectile(proj);
[8dad966]513
[e62b56c]514 int x = p->pos.x;
515 int y = p->pos.y;
[8795a38]516
[e62b56c]517 serverMsg.type = MSG_TYPE_PROJECTILE;
518 memcpy(serverMsg.buffer, &proj.id, 4);
519 memcpy(serverMsg.buffer+4, &x, 4);
520 memcpy(serverMsg.buffer+8, &y, 4);
521 memcpy(serverMsg.buffer+12, &p->targetPlayer, 4);
522 }
523 else
524 cout << "Invalid attack type: " << p->attackType << endl;
[8dad966]525
[e62b56c]526 // broadcast either a PLAYER or PROJECTILE message
527 cout << "Broadcasting player or projectile message" << endl;
528 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
529 {
530 if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
531 error("sendMessage");
532 }
533 cout << "Done broadcasting" << endl;
[8dad966]534 }
535 }
[e5b96e2]536
537 if (gameFinished) {
538 // send a GAME_INFO message with 0 players to force clients to delete the game
539 int numPlayers = 0;
540 serverMsg.type = MSG_TYPE_GAME_INFO;
541 memcpy(serverMsg.buffer, &numPlayers, 4);
542
543 map<unsigned int, Player*>::iterator it2;
[3b6f46b]544 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
[e5b96e2]545 {
546 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
547 error("sendMessage");
548 }
549
550 // erase game from server
[8ce793b]551 mapGames.erase(itGames++);
[3b6f46b]552 delete game;
[e5b96e2]553 }else
554 itGames++;
[8dad966]555 }
556
[ce2bb87]557 cout << "Processing projectiles" << endl;
558
[8dad966]559 // move all projectiles
[483a2cb]560 // see if this can be moved inside the game class
[45734ff]561 // this method can be moved when I add a MessageProcessor to the Game class
[8dad966]562 map<unsigned int, Projectile>::iterator itProj;
[5ae8dca]563 for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) {
564 game = itGames->second;
565 for (itProj = game->getProjectiles().begin(); itProj != game->getProjectiles().end(); itProj++)
[95ffe57]566 {
[5ae8dca]567 cout << "About to call projectile move" << endl;
568 if (itProj->second.move(game->getPlayers()))
[8dad966]569 {
[5ae8dca]570 // send a REMOVE_PROJECTILE message
571 cout << "send a REMOVE_PROJECTILE message" << endl;
572 serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
573 memcpy(serverMsg.buffer, &itProj->second.id, 4);
574 game->removeProjectile(itProj->second.id);
575
576 map<unsigned int, Player*>::iterator it2;
577 cout << "Broadcasting REMOVE_PROJECTILE" << endl;
578 for (it2 = game->getPlayers().begin(); it2 != game->getPlayers().end(); it2++)
579 {
580 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
581 error("sendMessage");
582 }
[8dad966]583
[5ae8dca]584 cout << "send a PLAYER message after dealing damage" << endl;
585 // send a PLAYER message after dealing damage
586 Player* target = game->getPlayers()[itProj->second.target];
[8dad966]587
[5ae8dca]588 damagePlayer(target, itProj->second.damage);
[8dad966]589
[5ae8dca]590 if (target->isDead)
591 {
592 WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
593 if (target->hasBlueFlag)
594 flagType = WorldMap::OBJECT_BLUE_FLAG;
595 else if (target->hasRedFlag)
596 flagType = WorldMap::OBJECT_RED_FLAG;
[411c1ae]597
[5ae8dca]598 if (flagType != WorldMap::OBJECT_NONE)
599 addObjectToMap(flagType, target->pos.x, target->pos.y, game->getMap(), game->getPlayers(), msgProcessor, sock, outputLog);
600 }
[8dad966]601
[5ae8dca]602 serverMsg.type = MSG_TYPE_PLAYER;
603 target->serialize(serverMsg.buffer);
604
605 cout << "Sending a PLAYER message" << endl;
606 for (it2 = game->getPlayers().begin(); it2 != game->getPlayers().end(); it2++)
607 {
608 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
609 error("sendMessage");
610 }
[8dad966]611 }
[5ae8dca]612 cout << "Projectile was not moved" << endl;
[8dad966]613 }
[d211210]614 }
615 }
616
[d05086b]617 n = msgProcessor.receiveMessage(&clientMsg, sock, &from, &outputLog);
[8e540f4]618
[95ffe57]619 if (n >= 0)
620 {
[99afbb8]621 broadcastResponse = processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed, outputLog);
[371ce29]622
[da692b9]623 if (broadcastResponse)
[3b1efcc]624 {
[da692b9]625 cout << "Should be broadcasting the message" << endl;
626
[ce2bb87]627 // needs to be updated to use the players from the game
[95ffe57]628 map<unsigned int, Player*>::iterator it;
[01d0d00]629 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
[3b1efcc]630 {
[95ffe57]631 cout << "Sent message back to " << it->second->name << endl;
632 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
[3b1efcc]633 error("sendMessage");
634 }
635 }
636 else
637 {
[da692b9]638 cout << "Should be sending back the message" << endl;
639
[d05086b]640 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
[3b1efcc]641 error("sendMessage");
642 }
[ce2bb87]643
644 cout << "Finished processing the message" << endl;
[7b43385]645 }
[8e540f4]646 }
[371ce29]647
[d05086b]648 outputLog << "Stopped server on " << getCurrentDateTimeString() << endl;
649 outputLog.close();
650
[f41a7f9]651 // delete all games
652 map<string, Game*>::iterator itGames;
[95ffe57]653 for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
654 {
[f41a7f9]655 delete itGames->second;
656 }
657
[95ffe57]658 map<unsigned int, Player*>::iterator itPlayers;
659 for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++)
660 {
661 delete itPlayers->second;
662 }
663
[8e540f4]664 return 0;
665}
666
[95ffe57]667bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed, ofstream& outputLog)
[8e540f4]668{
[41ad8ed]669 DataAccess da;
670
[9a4fa04]671 cout << "Inside processMessage" << endl;
672
[b8cb03f]673 cout << "Received message" << endl;
[8e540f4]674 cout << "MSG: type: " << clientMsg.type << endl;
675 cout << "MSG contents: " << clientMsg.buffer << endl;
676
[da692b9]677 // maybe we should make a message class and have this be a member
[3b1efcc]678 bool broadcastResponse = false;
679
[8e540f4]680 // Check that if an invalid message is sent, the client will correctly
681 // receive and display the response. Maybe make a special error msg type
682 switch(clientMsg.type)
683 {
684 case MSG_TYPE_REGISTER:
[d2b411a]685 {
[8e540f4]686 string username(clientMsg.buffer);
687 string password(strchr(clientMsg.buffer, '\0')+1);
[521c88b]688 Player::PlayerClass playerClass;
689
[c4c2a3c]690 serverMsg.type = MSG_TYPE_REGISTER;
[4509648]691 memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4);
[c4c2a3c]692
[8e540f4]693 cout << "username: " << username << endl;
694 cout << "password: " << password << endl;
[d2b411a]695
[521c88b]696 if (playerClass == Player::CLASS_WARRIOR)
697 cout << "class: WARRIOR" << endl;
698 else if (playerClass == Player::CLASS_RANGER)
699 cout << "class: RANGER" << endl;
[c4c2a3c]700 else {
[521c88b]701 cout << "Unknown player class detected" << endl;
[c4c2a3c]702 strcpy(serverMsg.buffer, "You didn't select a class");
703 break;
704 }
[521c88b]705
706 int error = da.insertPlayer(username, password, playerClass);
[41ad8ed]707
[c4c2a3c]708 if (error)
[3b1efcc]709 strcpy(serverMsg.buffer, "Registration failed. Please try again.");
[c4c2a3c]710 else
711 strcpy(serverMsg.buffer, "Registration successful.");
[d2b411a]712
[8e540f4]713 break;
714 }
715 case MSG_TYPE_LOGIN:
716 {
[60017fc]717 cout << "Got login message" << endl;
718
[8e540f4]719 string username(clientMsg.buffer);
[41ad8ed]720 string password(strchr(clientMsg.buffer, '\0')+1);
[8e540f4]721
[41ad8ed]722 Player* p = da.getPlayer(username);
[d2b411a]723
[b128109]724 if (p == NULL || !da.verifyPassword(password, p->password))
[41ad8ed]725 {
726 strcpy(serverMsg.buffer, "Incorrect username or password");
[95ffe57]727 if (p != NULL)
728 delete(p);
[41ad8ed]729 }
[01d0d00]730 else if(findPlayerByName(mapPlayers, username) != NULL)
[41ad8ed]731 {
732 strcpy(serverMsg.buffer, "Player has already logged in.");
[95ffe57]733 delete(p);
[41ad8ed]734 }
735 else
[8e540f4]736 {
[8dad966]737 updateUnusedPlayerId(unusedPlayerId, mapPlayers);
738 p->id = unusedPlayerId;
[d211210]739 cout << "new player id: " << p->id << endl;
[df79cfd]740 p->setAddr(from);
[ce2bb87]741 p->currentGame = NULL;
[df79cfd]742
743 // choose a random team (either 0 or 1)
744 p->team = rand() % 2;
[d211210]745
[f203c5c]746 serverMsg.type = MSG_TYPE_PLAYER;
747
[d211210]748 // tell the new player about all the existing players
749 cout << "Sending other players to new player" << endl;
750
[95ffe57]751 map<unsigned int, Player*>::iterator it;
[d211210]752 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
753 {
[95ffe57]754 it->second->serialize(serverMsg.buffer);
[d211210]755
[95ffe57]756 cout << "sending info about " << it->second->name << endl;
757 cout << "sending id " << it->second->id << endl;
[d05086b]758 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
[5f868c0]759 error("sendMessage");
760 }
761
762 // tell the new player about all map objects
763 // (currently just the flags)
764 serverMsg.type = MSG_TYPE_OBJECT;
[e487381]765 vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
[5f868c0]766 vector<WorldMap::Object>::iterator itObjects;
767 cout << "sending items" << endl;
[e487381]768 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
[5f868c0]769 itObjects->serialize(serverMsg.buffer);
770 cout << "sending item id " << itObjects->id << endl;
[d05086b]771 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
[d211210]772 error("sendMessage");
[d3efa1a]773 }
774
775 // send info about existing games to new player
776 map<string, Game*>::iterator itGames;
777 Game* g;
778 int numPlayers;
779 serverMsg.type = MSG_TYPE_GAME_INFO;
780
781 for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
782 {
783 g = itGames->second;
784 numPlayers = g->getNumPlayers();
785 memcpy(serverMsg.buffer, &numPlayers, 4);
786 strcpy(serverMsg.buffer+4, g->getName().c_str());
787 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
788 error("sendMessage");
[d211210]789 }
[59061f6]790
[b8601ee]791 // send the current score
792 serverMsg.type = MSG_TYPE_SCORE;
793 memcpy(serverMsg.buffer, &scoreBlue, 4);
794 memcpy(serverMsg.buffer+4, &scoreRed, 4);
[d05086b]795 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
[b8601ee]796 error("sendMessage");
797
798 serverMsg.type = MSG_TYPE_PLAYER;
[594d2e9]799 p->serialize(serverMsg.buffer);
[d211210]800 cout << "Should be broadcasting the message" << endl;
801
802 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
803 {
[95ffe57]804 cout << "Sent message back to " << it->second->name << endl;
805 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
[d211210]806 error("sendMessage");
807 }
808
[95ffe57]809 mapPlayers[unusedPlayerId] = p;
[07028b9]810 }
811
[f203c5c]812 serverMsg.type = MSG_TYPE_LOGIN;
[07028b9]813
[8e540f4]814 break;
815 }
816 case MSG_TYPE_LOGOUT:
817 {
818 string name(clientMsg.buffer);
819 cout << "Player logging out: " << name << endl;
820
[01d0d00]821 Player *p = findPlayerByName(mapPlayers, name);
[633f42a]822
[8e540f4]823 if (p == NULL)
824 {
[90eaad2]825 strcpy(serverMsg.buffer+4, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
[8a3ef42]826 cout << "Player not logged in" << endl;
[07028b9]827 }
[01d0d00]828 else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
829 p->addr.sin_port != from.sin_port )
[07028b9]830 {
[90eaad2]831 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]832 cout << "Player logged in using a different connection" << endl;
[2488852]833 }
[8e540f4]834 else
[2488852]835 {
[411c1ae]836 if (!p->isDead) {
837 WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
838 if (p->hasBlueFlag)
839 flagType = WorldMap::OBJECT_BLUE_FLAG;
840 else if (p->hasRedFlag)
841 flagType = WorldMap::OBJECT_RED_FLAG;
842
843 if (flagType != WorldMap::OBJECT_NONE) {
[d05086b]844 addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
[411c1ae]845 }
846 }
847
[1a47469]848 // broadcast to all players before deleting p from the map
[4509648]849 serverMsg.type = MSG_TYPE_LOGOUT;
850 memcpy(serverMsg.buffer, &p->id, 4);
851
[1a47469]852 map<unsigned int, Player*>::iterator it;
853 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
854 {
855 cout << "Sent message back to " << it->second->name << endl;
856 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
857 error("sendMessage");
858 }
859
[8dad966]860 if (p->id < unusedPlayerId)
861 unusedPlayerId = p->id;
[01d0d00]862 mapPlayers.erase(p->id);
[95ffe57]863 delete p;
[90eaad2]864 strcpy(serverMsg.buffer+4, "You have successfully logged out.");
[8e540f4]865 }
[07028b9]866
[d211210]867 serverMsg.type = MSG_TYPE_LOGOUT;
[8a3ef42]868
[8e540f4]869 break;
870 }
871 case MSG_TYPE_CHAT:
872 {
[da692b9]873 cout << "Got a chat message" << endl;
874
[01d0d00]875 Player *p = findPlayerByAddr(mapPlayers, from);
[07028b9]876
[8e540f4]877 if (p == NULL)
878 {
879 strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
[2488852]880 }
[8e540f4]881 else
882 {
[3b1efcc]883 broadcastResponse = true;
884
[b128109]885 ostringstream oss;
886 oss << p->name << ": " << clientMsg.buffer;
[3b1efcc]887
[b128109]888 strcpy(serverMsg.buffer, oss.str().c_str());
[8e540f4]889 }
890
891 serverMsg.type = MSG_TYPE_CHAT;
892
893 break;
[e084950]894 }
[b128109]895 case MSG_TYPE_PLAYER_MOVE:
896 {
897 cout << "PLAYER_MOVE" << endl;
898
899 int id, x, y;
900
901 memcpy(&id, clientMsg.buffer, 4);
902 memcpy(&x, clientMsg.buffer+4, 4);
903 memcpy(&y, clientMsg.buffer+8, 4);
[7b43385]904
[b128109]905 cout << "x: " << x << endl;
906 cout << "y: " << y << endl;
907 cout << "id: " << id << endl;
[7b43385]908
[95ffe57]909 Player* p = mapPlayers[id];
910
911 if ( p->addr.sin_addr.s_addr == from.sin_addr.s_addr &&
912 p->addr.sin_port == from.sin_port )
[b128109]913 {
[0129700]914 if (p->currentGame->startPlayerMovement(id, x, y)) {
[60017fc]915 serverMsg.type = MSG_TYPE_PLAYER_MOVE;
916
917 memcpy(serverMsg.buffer, &id, 4);
[95ffe57]918 memcpy(serverMsg.buffer+4, &p->target.x, 4);
919 memcpy(serverMsg.buffer+8, &p->target.y, 4);
[60017fc]920
921 broadcastResponse = true;
922 }
923 else
924 cout << "Bad terrain detected" << endl;
[b128109]925 }
926 else // nned to send back a message indicating failure
927 cout << "Player id (" << id << ") doesn't match sender" << endl;
928
929 break;
930 }
[5299436]931 case MSG_TYPE_PICKUP_FLAG:
932 {
933 // may want to check the id matches the sender, just like for PLAYER_NOVE
934 cout << "PICKUP_FLAG" << endl;
935
936 int id;
937
938 memcpy(&id, clientMsg.buffer, 4);
939 cout << "id: " << id << endl;
940
[95ffe57]941 Player* p = mapPlayers[id];
[ce2bb87]942 int objectId = p->currentGame->processFlagPickupRequest(p);
[95ffe57]943
[ce2bb87]944 if (objectId >= 0) {
945 serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
946 memcpy(serverMsg.buffer, &objectId, 4);
[5c84d54]947
[ce2bb87]948 map<unsigned int, Player*> players = p->currentGame->getPlayers();
949 map<unsigned int, Player*>::iterator it;
950 for (it = players.begin(); it != players.end(); it++)
951 {
952 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
953 error("sendMessage");
[5c84d54]954 }
955
956 }
957
[ce2bb87]958 // if there was no flag to pickup, we really don't need to send a message
[5c84d54]959 serverMsg.type = MSG_TYPE_PLAYER;
[95ffe57]960 p->serialize(serverMsg.buffer);
[5c84d54]961
[ad1e2fc]962 broadcastResponse = true;
963
[5299436]964 break;
965 }
[e487381]966 case MSG_TYPE_DROP_FLAG:
967 {
968 // may want to check the id matches the sender, just like for PLAYER_NOVE
969 cout << "DROP_FLAG" << endl;
970
971 int id;
972
973 memcpy(&id, clientMsg.buffer, 4);
974 cout << "id: " << id << endl;
975
[95ffe57]976 Player* p = mapPlayers[id];
977
[e487381]978 WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
[95ffe57]979 if (p->hasBlueFlag)
[e487381]980 flagType = WorldMap::OBJECT_BLUE_FLAG;
[95ffe57]981 else if (p->hasRedFlag)
[e487381]982 flagType = WorldMap::OBJECT_RED_FLAG;
983
[b73bc28]984 addObjectToMap(flagType, p->pos.x, p->pos.y, p->currentGame->getMap(), p->currentGame->getPlayers(), msgProcessor, sock, outputLog);
[e487381]985
[95ffe57]986 p->hasBlueFlag = false;
987 p->hasRedFlag = false;
[e487381]988
989 serverMsg.type = MSG_TYPE_PLAYER;
[95ffe57]990 p->serialize(serverMsg.buffer);
[e487381]991
992 broadcastResponse = true;
993
994 break;
995 }
[4b4b153]996 case MSG_TYPE_START_ATTACK:
997 {
998 cout << "Received a START_ATTACK message" << endl;
999
[8a4ed74]1000 int id, targetId;
1001
1002 memcpy(&id, clientMsg.buffer, 4);
1003 memcpy(&targetId, clientMsg.buffer+4, 4);
1004
[ffadc8e]1005 // need to make sure the target is in the sender's game
1006
[95ffe57]1007 Player* source = mapPlayers[id];
[8dad966]1008 source->targetPlayer = targetId;
[11d21ee]1009 source->isChasing = true;
[8dad966]1010
[11d21ee]1011 // this is irrelevant since the client doesn't even listen for START_ATTACK messages
1012 // actually, the client should not ignore this and should instead perform the same movement
1013 // algorithm on its end (following the target player until in range) that the server does.
1014 // Once the attacker is in range, the client should stop movement and wait for messages
1015 // from the server
[4b4b153]1016 serverMsg.type = MSG_TYPE_START_ATTACK;
[8dad966]1017 memcpy(serverMsg.buffer, &id, 4);
1018 memcpy(serverMsg.buffer+4, &targetId, 4);
[4b4b153]1019 broadcastResponse = true;
[8a4ed74]1020
1021 break;
[4b4b153]1022 }
1023 case MSG_TYPE_ATTACK:
1024 {
1025 cout << "Received am ATTACK message" << endl;
[8dad966]1026 cout << "ERROR: Clients should not send ATTACK messages" << endl;
[8a4ed74]1027
1028 break;
[4b4b153]1029 }
[b8f789d]1030 case MSG_TYPE_CREATE_GAME:
[8e540f4]1031 {
[b8f789d]1032 cout << "Received a CREATE_GAME message" << endl;
1033
1034 string gameName(clientMsg.buffer);
1035 cout << "Game name: " << gameName << endl;
1036
[b48ef09]1037 // check if this game already exists
1038 if (mapGames.find(gameName) != mapGames.end()) {
[3ef8cf4]1039 cout << "Error: Game already exists" << endl;
[b48ef09]1040 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
1041 broadcastResponse = false;
1042 return broadcastResponse;
1043 }
[b92e6a7]1044
[a6fe73d]1045 Game* g = new Game(gameName, "../data/map.txt");
[f41a7f9]1046 mapGames[gameName] = g;
[b92e6a7]1047
[70fc3e8]1048 // add flag objects to the map
1049 WorldMap* m = g->getMap();
1050 for (int y=0; y<m->height; y++) {
1051 for (int x=0; x<m->width; x++) {
1052 switch (m->getStructure(x, y)) {
1053 case WorldMap::STRUCTURE_BLUE_FLAG:
1054 m->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
1055 break;
1056 case WorldMap::STRUCTURE_RED_FLAG:
1057 m->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
1058 break;
1059 }
1060 }
1061 }
1062
[7d8d5d3]1063 serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
[b48ef09]1064 strcpy(serverMsg.buffer, gameName.c_str());
1065 broadcastResponse = false;
[b92e6a7]1066
[b48ef09]1067 break;
1068 }
1069 case MSG_TYPE_JOIN_GAME:
1070 {
1071 cout << "Received a JOIN_GAME message" << endl;
[b92e6a7]1072
[b48ef09]1073 string gameName(clientMsg.buffer);
1074 cout << "Game name: " << gameName << endl;
[b92e6a7]1075
[b48ef09]1076 // check if this game already exists
1077 if (mapGames.find(gameName) == mapGames.end()) {
[3ef8cf4]1078 cout << "Error: Game does not exist" << endl;
[b48ef09]1079 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
1080 broadcastResponse = false;
1081 return broadcastResponse;
[b92e6a7]1082 }
1083
[b48ef09]1084 Game* g = mapGames[gameName];
1085 map<unsigned int, Player*>& players = g->getPlayers();
1086 Player* p = findPlayerByAddr(mapPlayers, from);
[f41a7f9]1087
[b48ef09]1088 if (players.find(p->id) != players.end()) {
1089 cout << "Player " << p->name << " trying to join a game he's already in" << endl;
1090 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
1091 broadcastResponse = false;
1092 return broadcastResponse;
[b92e6a7]1093 }
1094
[7d8d5d3]1095 serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
[b48ef09]1096 strcpy(serverMsg.buffer, gameName.c_str());
1097 broadcastResponse = false;
[b8f789d]1098
1099 break;
1100 }
[ab8fd40]1101 case MSG_TYPE_LEAVE_GAME:
1102 {
1103 cout << "Received a LEAVE_GAME message" << endl;
1104
1105 Player* p = findPlayerByAddr(mapPlayers, from);
1106 Game* g = p->currentGame;
1107
1108 if (g == NULL) {
1109 cout << "Player " << p->name << " is trying to leave a game, but is not currently in a game." << endl;
1110
1111 /// should send a response back, maybe a new message type is needed
1112
1113 break;
1114 }
1115
1116 cout << "Game name: " << g->getName() << endl;
[3ef8cf4]1117 p->currentGame = NULL;
[95ffe57]1118
1119 serverMsg.type = MSG_TYPE_LEAVE_GAME;
1120 memcpy(serverMsg.buffer, &p->id, 4);
1121 strcpy(serverMsg.buffer+4, g->getName().c_str());
1122
1123 map<unsigned int, Player*>& players = g->getPlayers();
1124
1125 map<unsigned int, Player*>::iterator it;
1126 for (it = players.begin(); it != players.end(); it++)
1127 {
1128 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
1129 error("sendMessage");
1130 }
1131
[3ef8cf4]1132 g->removePlayer(p->id);
[ab8fd40]1133
1134 int numPlayers = g->getNumPlayers();
1135
1136 serverMsg.type = MSG_TYPE_GAME_INFO;
1137 memcpy(serverMsg.buffer, &numPlayers, 4);
1138 strcpy(serverMsg.buffer+4, g->getName().c_str());
1139 broadcastResponse = true;
1140
[1248984]1141 // if there are no more players in the game, remove it
1142 if (numPlayers == 0) {
1143 mapGames.erase(g->getName());
1144 delete g;
1145 }
1146
[ab8fd40]1147 break;
1148 }
[b48ef09]1149 case MSG_TYPE_JOIN_GAME_ACK:
[b8f789d]1150 {
[b48ef09]1151 cout << "Received a JOIN_GAME_ACK message" << endl;
[e084950]1152
[b8f789d]1153 string gameName(clientMsg.buffer);
1154 cout << "Game name: " << gameName << endl;
1155
[b48ef09]1156 // check if this game already exists
1157 if (mapGames.find(gameName) == mapGames.end()) {
1158 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
1159 broadcastResponse = false;
1160 return broadcastResponse;
1161 }
[b92e6a7]1162
[f41a7f9]1163 Game* g = mapGames[gameName];
[b92e6a7]1164
[b48ef09]1165 Player* p = findPlayerByAddr(mapPlayers, from);
[f41a7f9]1166 p->team = rand() % 2; // choose a random team (either 0 or 1)
[e62b56c]1167 p->currentGame = g;
[b92e6a7]1168
1169 // tell the new player about all map objects
1170 // (currently just the flags)
1171
1172 serverMsg.type = MSG_TYPE_OBJECT;
[f41a7f9]1173 vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
[b92e6a7]1174 vector<WorldMap::Object>::iterator itObjects;
1175 cout << "sending items" << endl;
1176 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
1177 itObjects->serialize(serverMsg.buffer);
1178 cout << "sending item id " << itObjects->id << endl;
1179 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
1180 error("sendMessage");
1181 }
1182
1183
1184 // send the current score
1185 serverMsg.type = MSG_TYPE_SCORE;
1186
[f41a7f9]1187 int game_blueScore = g->getBlueScore();
1188 int game_redScore = g->getRedScore();
[b92e6a7]1189 memcpy(serverMsg.buffer, &game_blueScore, 4);
1190 memcpy(serverMsg.buffer+4, &game_redScore, 4);
1191
1192 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
1193 error("sendMessage");
1194
[453087e]1195 serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
[b92e6a7]1196 p->serialize(serverMsg.buffer);
1197 cout << "Should be broadcasting the message" << endl;
1198
[453087e]1199 map<unsigned int, Player*>& otherPlayers = g->getPlayers();
[2d78e03]1200 map<unsigned int, Player*>::iterator it;
[b92e6a7]1201 for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
1202 {
1203 cout << "Sent message back to " << it->second->name << endl;
1204 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
1205 error("sendMessage");
1206 }
[b8f789d]1207
[b48ef09]1208 g->addPlayer(p);
[453087e]1209
1210 map<unsigned int, Player*>& allPlayers = g->getPlayers();
1211
1212 // tell the new player about all the players in the game (including himself)
1213 cout << "Sending other players to new player" << endl;
1214 serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
1215
1216 for (it = allPlayers.begin(); it != allPlayers.end(); it++)
1217 {
1218 it->second->serialize(serverMsg.buffer);
1219
1220 cout << "sending info about " << it->second->name << endl;
1221 cout << "sending id " << it->second->id << endl;
1222 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
1223 error("sendMessage");
1224 }
1225
[b48ef09]1226 int numPlayers = g->getNumPlayers();
1227
[b8f789d]1228 serverMsg.type = MSG_TYPE_GAME_INFO;
1229 memcpy(serverMsg.buffer, &numPlayers, 4);
1230 strcpy(serverMsg.buffer+4, gameName.c_str());
1231 broadcastResponse = true;
[ab8fd40]1232
[b8f789d]1233 break;
1234 }
1235 default:
1236 {
[8e540f4]1237 serverMsg.type = MSG_TYPE_CHAT;
[b8f789d]1238 strcpy(serverMsg.buffer, "Server error occured. Report this please.");
[e084950]1239
[8e540f4]1240 break;
1241 }
[e3535b3]1242 }
[da692b9]1243
1244 return broadcastResponse;
[e3535b3]1245}
[da692b9]1246
[95ffe57]1247void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers)
[01d0d00]1248{
[1106210]1249 while (mapPlayers.find(id) != mapPlayers.end())
1250 id++;
[01d0d00]1251}
[8dad966]1252
[95ffe57]1253Player *findPlayerByName(map<unsigned int, Player*> &m, string name)
1254{
1255 map<unsigned int, Player*>::iterator it;
1256
1257 for (it = m.begin(); it != m.end(); it++)
1258 {
1259 if ( it->second->name.compare(name) == 0 )
1260 return it->second;
1261 }
1262
1263 return NULL;
1264}
1265
1266Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr)
1267{
1268 map<unsigned int, Player*>::iterator it;
1269
1270 for (it = m.begin(); it != m.end(); it++)
1271 {
1272 if ( it->second->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
1273 it->second->addr.sin_port == addr.sin_port )
1274 return it->second;
1275 }
1276
1277 return NULL;
1278}
1279
[c76134b]1280void damagePlayer(Player *p, int damage) {
1281 p->health -= damage;
1282 if (p->health < 0)
1283 p->health = 0;
1284 if (p->health == 0) {
[66c4ec4]1285 cout << "Player died" << endl;
[c76134b]1286 p->isDead = true;
1287 p->timeDied = getCurrentMillis();
1288 }
1289}
[411c1ae]1290
[95ffe57]1291void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog) {
[411c1ae]1292 NETWORK_MSG serverMsg;
1293
1294 gameMap->addObject(objectType, x, y);
1295
1296 // need to send the OBJECT message too
1297 serverMsg.type = MSG_TYPE_OBJECT;
1298 gameMap->getObjects()->back().serialize(serverMsg.buffer);
1299
[95ffe57]1300 map<unsigned int, Player*>::iterator it;
[411c1ae]1301 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
1302 {
[95ffe57]1303 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
[411c1ae]1304 error("sendMessage");
1305 }
1306}
Note: See TracBrowser for help on using the repository browser.