source: network-game/server/server.cpp@ 5ae8dca

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

Server moves projectiles in all individual games and damages players and sends correct messages when a projectile hits in individual games

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