source: network-game/server/server.cpp@ 35f6097

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

Players are revived correctly in individual games

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