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

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

New GameSummary class for storing game results

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