source: network-game/server/server.cpp@ 3b6f46b

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

Minor code changes

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