Index: server/server.cpp
===================================================================
--- server/server.cpp	(revision 3ef8cf48cc9aac7dd885ba9eac6507aeb5e902c2)
+++ server/server.cpp	(revision 95ffe57f853925b844eb4eb85ce0128dfc33ed24)
@@ -45,11 +45,13 @@
 // from used to be const. Removed that so I could take a reference
 // and use it to send messages
-bool 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);
-
-void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
+bool 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);
+
+void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers);
 void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles);
+Player *findPlayerByName(map<unsigned int, Player*> &m, string name);
+Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr);
 void damagePlayer(Player *p, int damage);
 
-void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog);
+void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog);
 
 // this should probably go somewhere in the common folder
@@ -58,31 +60,4 @@
     perror(msg);
     exit(0);
-}
-
-Player *findPlayerByName(map<unsigned int, Player> &m, string name)
-{
-   map<unsigned int, Player>::iterator it;
-
-   for (it = m.begin(); it != m.end(); it++)
-   {
-      if ( it->second.name.compare(name) == 0 )
-         return &(it->second);
-   }
-
-   return NULL;
-}
-
-Player *findPlayerByAddr(map<unsigned int, Player> &m, const sockaddr_in &addr)
-{
-   map<unsigned int, Player>::iterator it;
-
-   for (it = m.begin(); it != m.end(); it++)
-   {
-      if ( it->second.addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
-           it->second.addr.sin_port == addr.sin_port )
-         return &(it->second);
-   }
-
-   return NULL;
 }
 
@@ -98,5 +73,5 @@
    NETWORK_MSG clientMsg, serverMsg;
    MessageProcessor msgProcessor;
-   map<unsigned int, Player> mapPlayers;
+   map<unsigned int, Player*> mapPlayers;
    map<unsigned int, Projectile> mapProjectiles;
    map<string, Game*> mapGames;
@@ -116,6 +91,7 @@
    //OpenSSL_add_all_algorithms();
 
-   if (argc < 2) {
-      cerr << "ERROR, no port provided" << endl;
+   if (argc != 2)
+   {
+      cerr << "ERROR, expected server [domain] [port]" << endl;
       exit(1);
    }
@@ -128,7 +104,10 @@
    // add some items to the map. They will be sent out
    // to players when they login
-   for (int y=0; y<gameMap->height; y++) {
-      for (int x=0; x<gameMap->width; x++) {
-         switch (gameMap->getStructure(x, y)) {
+   for (int y=0; y<gameMap->height; y++)
+   {
+      for (int x=0; x<gameMap->width; x++)
+      {
+         switch (gameMap->getStructure(x, y))
+         {
             case WorldMap::STRUCTURE_BLUE_FLAG:
                gameMap->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
@@ -157,5 +136,6 @@
    timespec ts;
    int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0;
-   while (!done) {
+   while (!done)
+   {
 
       usleep(5000);
@@ -166,5 +146,6 @@
       curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
 
-      if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50) {
+      if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50)
+      {
          timeLastUpdated = curTime;
 
@@ -172,16 +153,20 @@
          msgProcessor.resendUnackedMessages(sock, &outputLog);
 
-         map<unsigned int, Player>::iterator it;
+         map<unsigned int, Player*>::iterator it;
 
          // set targets for all chasing players (or make them attack if they're close enough)
-         for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
+         for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
+         {
             // check if it's time to revive dead players
-            if (it->second.isDead) {
-               if (getCurrentMillis() - it->second.timeDied >= 10000) {
-                  it->second.isDead = false;
+            if (it->second->isDead)
+            {
+               if (getCurrentMillis() - it->second->timeDied >= 10000)
+               {
+                  it->second->isDead = false;
 
                   POSITION spawnPos;
  
-                  switch (it->second.team) {
+                  switch (it->second->team)
+                  {
                   case 0:// blue team
                      spawnPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
@@ -200,15 +185,15 @@
                   spawnPos.y = spawnPos.y * 25 + 12;
 
-                  it->second.pos = spawnPos.toFloat();
-                  it->second.target = spawnPos;
-                  it->second.health = it->second.maxHealth;
+                  it->second->pos = spawnPos.toFloat();
+                  it->second->target = spawnPos;
+                  it->second->health = it->second->maxHealth;
 
                   serverMsg.type = MSG_TYPE_PLAYER;
-                  it->second.serialize(serverMsg.buffer);
-
-                  map<unsigned int, Player>::iterator it2;
+                  it->second->serialize(serverMsg.buffer);
+
+                  map<unsigned int, Player*>::iterator it2;
                   for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                   {
-                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
+                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
                         error("sendMessage");
                   }
@@ -218,12 +203,13 @@
             }
 
-            if (it->second.updateTarget(mapPlayers)) {
+            if (it->second->updateTarget(mapPlayers))
+            {
                serverMsg.type = MSG_TYPE_PLAYER;
-               it->second.serialize(serverMsg.buffer);
-
-               map<unsigned int, Player>::iterator it2;
+               it->second->serialize(serverMsg.buffer);
+
+               map<unsigned int, Player*>::iterator it2;
                for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                {
-                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
+                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
                      error("sendMessage");
                }
@@ -235,18 +221,20 @@
          FLOAT_POSITION oldPos;
          bool broadcastMove = false;
-         for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
-            oldPos = it->second.pos;
-            if (it->second.move(gameMap)) {
+         for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
+         {
+            oldPos = it->second->pos;
+            if (it->second->move(gameMap)) {
 
                // check if the move needs to be canceled
-               switch(gameMap->getElement(it->second.pos.x/25, it->second.pos.y/25)) {
+               switch(gameMap->getElement(it->second->pos.x/25, it->second->pos.y/25))
+               {
                   case WorldMap::TERRAIN_NONE:
                   case WorldMap::TERRAIN_OCEAN:
                   case WorldMap::TERRAIN_ROCK:
                   {
-                     it->second.pos = oldPos;
-                     it->second.target.x = it->second.pos.x;
-                     it->second.target.y = it->second.pos.y;
-                     it->second.isChasing = false;
+                     it->second->pos = oldPos;
+                     it->second->target.x = it->second->pos.x;
+                     it->second->target.y = it->second->pos.y;
+                     it->second->isChasing = false;
                      broadcastMove = true;
                      break;
@@ -263,8 +251,9 @@
                bool ownFlagAtBase = false;
         
-               switch(gameMap->getStructure(it->second.pos.x/25, it->second.pos.y/25)) {
+               switch(gameMap->getStructure(it->second->pos.x/25, it->second->pos.y/25))
+               {
                   case WorldMap::STRUCTURE_BLUE_FLAG:
                   {
-                     if (it->second.team == 0 && it->second.hasRedFlag)
+                     if (it->second->team == 0 && it->second->hasRedFlag)
                      {
                         // check that your flag is at your base
@@ -274,7 +263,10 @@
                         vector<WorldMap::Object>::iterator itObjects;
 
-                        for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
-                           if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG) {
-                              if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
+                        for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
+                        {
+                           if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
+                           {
+                              if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12)
+                              {
                                  ownFlagAtBase = true;
                                  break;
@@ -283,6 +275,7 @@
                         }
 
-                        if (ownFlagAtBase) {
-                           it->second.hasRedFlag = false;
+                        if (ownFlagAtBase)
+                        {
+                           it->second->hasRedFlag = false;
                            flagType = WorldMap::OBJECT_RED_FLAG;
                            pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
@@ -296,5 +289,5 @@
                   case WorldMap::STRUCTURE_RED_FLAG:
                   {
-                     if (it->second.team == 1 && it->second.hasBlueFlag)
+                     if (it->second->team == 1 && it->second->hasBlueFlag)
                      {
                         // check that your flag is at your base
@@ -304,7 +297,10 @@
                         vector<WorldMap::Object>::iterator itObjects;
 
-                        for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
-                           if (itObjects->type == WorldMap::OBJECT_RED_FLAG) {
-                              if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
+                        for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
+                        {
+                           if (itObjects->type == WorldMap::OBJECT_RED_FLAG)
+                           {
+                              if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12)
+                              {
                                  ownFlagAtBase = true;
                                  break;
@@ -313,6 +309,7 @@
                         }
 
-                        if (ownFlagAtBase) {
-                           it->second.hasBlueFlag = false;
+                        if (ownFlagAtBase)
+                        {
+                           it->second->hasBlueFlag = false;
                            flagType = WorldMap::OBJECT_BLUE_FLAG;
                            pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
@@ -326,5 +323,6 @@
                }
 
-               if (flagTurnedIn) {
+               if (flagTurnedIn)
+               {
                   // send an OBJECT message to add the flag back to its spawn point
                   pos.x = pos.x*25+12;
@@ -335,8 +333,8 @@
                   gameMap->getObjects()->back().serialize(serverMsg.buffer);
 
-                  map<unsigned int, Player>::iterator it2;
+                  map<unsigned int, Player*>::iterator it2;
                   for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                   {
-                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
+                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
                         error("sendMessage");
                   }
@@ -348,5 +346,5 @@
                   for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                   {
-                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
+                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
                         error("sendMessage");
                   }
@@ -361,15 +359,20 @@
                POSITION structPos;
 
-               for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
+               for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
+               {
                   POSITION pos = itObjects->pos;
 
-                  if (posDistance(it->second.pos, pos.toFloat()) < 10) {
-                     if (it->second.team == 0 && 
-                        itObjects->type == WorldMap::OBJECT_BLUE_FLAG) {
+                  if (posDistance(it->second->pos, pos.toFloat()) < 10)
+                  {
+                     if (it->second->team == 0 && 
+                         itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
+                     {
                         structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
                         flagReturned = true;
                         break;
-                     } else if (it->second.team == 1 &&
-                        itObjects->type == WorldMap::OBJECT_RED_FLAG) {
+                     }
+                     else if (it->second->team == 1 &&
+                              itObjects->type == WorldMap::OBJECT_RED_FLAG)
+                     {
                         structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
                         flagReturned = true;
@@ -379,5 +382,6 @@
                }
 
-               if (flagReturned) {
+               if (flagReturned)
+               {
                   itObjects->pos.x = structPos.x*25+12;
                   itObjects->pos.y = structPos.y*25+12;
@@ -386,21 +390,22 @@
                   itObjects->serialize(serverMsg.buffer);
 
-                  map<unsigned int, Player>::iterator it2;
+                  map<unsigned int, Player*>::iterator it2;
                   for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                   {
-                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
+                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
                         error("sendMessage");
                   }
                }
 
-               if (broadcastMove) {
+               if (broadcastMove)
+               {
                   serverMsg.type = MSG_TYPE_PLAYER;
-                  it->second.serialize(serverMsg.buffer);
+                  it->second->serialize(serverMsg.buffer);
 
                   cout << "about to broadcast move" << endl;
-                  map<unsigned int, Player>::iterator it2;
+                  map<unsigned int, Player*>::iterator it2;
                   for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                   {
-                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
+                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
                         error("sendMessage");
                   }
@@ -409,6 +414,7 @@
 
             // check if the player's attack animation is complete
-            if (it->second.isAttacking && it->second.timeAttackStarted+it->second.attackCooldown <= getCurrentMillis()) {
-               it->second.isAttacking = false;
+            if (it->second->isAttacking && it->second->timeAttackStarted+it->second->attackCooldown <= getCurrentMillis())
+            {
+               it->second->isAttacking = false;
                cout << "Attack animation is complete" << endl;
 
@@ -417,21 +423,23 @@
 
                serverMsg.type = MSG_TYPE_ATTACK; 
-               memcpy(serverMsg.buffer, &it->second.id, 4);
-               memcpy(serverMsg.buffer+4, &it->second.targetPlayer, 4);
-
-               map<unsigned int, Player>::iterator it2;
+               memcpy(serverMsg.buffer, &it->second->id, 4);
+               memcpy(serverMsg.buffer+4, &it->second->targetPlayer, 4);
+
+               map<unsigned int, Player*>::iterator it2;
                for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                {
-                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
+                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
                      error("sendMessage");
                }
 
-               if (it->second.attackType == Player::ATTACK_MELEE) {
+               if (it->second->attackType == Player::ATTACK_MELEE)
+               {
                   cout << "Melee attack" << endl;
 
-                  Player* target = &mapPlayers[it->second.targetPlayer];
-                  damagePlayer(target, it->second.damage);
-
-                  if (target->isDead) {
+                  Player* target = mapPlayers[it->second->targetPlayer];
+                  damagePlayer(target, it->second->damage);
+
+                  if (target->isDead)
+                  {
                      WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
                      if (target->hasBlueFlag)
@@ -447,14 +455,16 @@
                   serverMsg.type = MSG_TYPE_PLAYER;
                   target->serialize(serverMsg.buffer);
-               }else if (it->second.attackType == Player::ATTACK_RANGED) {
+               }
+               else if (it->second->attackType == Player::ATTACK_RANGED)
+               {
                   cout << "Ranged attack" << endl;
 
-                  Projectile proj(it->second.pos.x, it->second.pos.y, it->second.targetPlayer, it->second.damage);
+                  Projectile proj(it->second->pos.x, it->second->pos.y, it->second->targetPlayer, it->second->damage);
                   proj.id = unusedProjectileId;
                   updateUnusedProjectileId(unusedProjectileId, mapProjectiles);
                   mapProjectiles[proj.id] = proj;
 
-                  int x = it->second.pos.x;
-                  int y = it->second.pos.y;
+                  int x = it->second->pos.x;
+                  int y = it->second->pos.y;
 
                   serverMsg.type = MSG_TYPE_PROJECTILE;
@@ -462,8 +472,8 @@
                   memcpy(serverMsg.buffer+4, &x, 4);
                   memcpy(serverMsg.buffer+8, &y, 4);
-                  memcpy(serverMsg.buffer+12, &it->second.targetPlayer, 4);
-               }else {
-                  cout << "Invalid attack type: " << it->second.attackType << endl;
-               }
+                  memcpy(serverMsg.buffer+12, &it->second->targetPlayer, 4);
+               }
+               else
+                  cout << "Invalid attack type: " << it->second->attackType << endl;
 
                // broadcast either a PLAYER or PROJECTILE message
@@ -471,5 +481,5 @@
                for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                {
-                  if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
+                  if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
                      error("sendMessage");
                }
@@ -480,7 +490,9 @@
          // move all projectiles
          map<unsigned int, Projectile>::iterator itProj;
-         for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) {
+         for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++)
+         {
             cout << "About to call projectile move" << endl;
-            if (itProj->second.move(mapPlayers)) {
+            if (itProj->second.move(mapPlayers))
+            {
                // send a REMOVE_PROJECTILE message
                cout << "send a REMOVE_PROJECTILE message" << endl;
@@ -489,9 +501,9 @@
                mapProjectiles.erase(itProj->second.id);
 
-               map<unsigned int, Player>::iterator it2;
+               map<unsigned int, Player*>::iterator it2;
                cout << "Broadcasting REMOVE_PROJECTILE" << endl;
                for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                {
-                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
+                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
                      error("sendMessage");
                }
@@ -499,9 +511,10 @@
                cout << "send a PLAYER message after dealing damage" << endl;
                // send a PLAYER message after dealing damage
-               Player* target = &mapPlayers[itProj->second.target];
+               Player* target = mapPlayers[itProj->second.target];
 
                damagePlayer(target, itProj->second.damage);
 
-               if (target->isDead) {
+               if (target->isDead)
+               {
                   WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
                   if (target->hasBlueFlag)
@@ -510,7 +523,6 @@
                      flagType = WorldMap::OBJECT_RED_FLAG;
 
-                  if (flagType != WorldMap::OBJECT_NONE) {
+                  if (flagType != WorldMap::OBJECT_NONE)
                      addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
-                  }
                }
 
@@ -521,5 +533,5 @@
                for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                {
-                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
+                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
                      error("sendMessage");
                }
@@ -531,5 +543,6 @@
       n = msgProcessor.receiveMessage(&clientMsg, sock, &from, &outputLog);
 
-      if (n >= 0) {
+      if (n >= 0)
+      {
          broadcastResponse = processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed, outputLog);
 
@@ -538,9 +551,9 @@
             cout << "Should be broadcasting the message" << endl;
 
-            map<unsigned int, Player>::iterator it;
+            map<unsigned int, Player*>::iterator it;
             for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
             {
-               cout << "Sent message back to " << it->second.name << endl;
-               if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
+               cout << "Sent message back to " << it->second->name << endl;
+               if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
                   error("sendMessage");
             }
@@ -561,12 +574,19 @@
    // delete all games
    map<string, Game*>::iterator itGames;
-   for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) {
+   for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
+   {
       delete itGames->second;
    }
 
+   map<unsigned int, Player*>::iterator itPlayers;
+   for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++)
+   {
+      delete itPlayers->second;
+   }
+
    return 0;
 }
 
-bool 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)
+bool 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)
 {
    DataAccess da;
@@ -593,5 +613,4 @@
          memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4);
          serverMsg.type = MSG_TYPE_REGISTER;
-
 
          cout << "username: " << username << endl;
@@ -629,8 +648,11 @@
          {
             strcpy(serverMsg.buffer, "Incorrect username or password");
+            if (p != NULL)
+               delete(p);
          }
          else if(findPlayerByName(mapPlayers, username) != NULL)
          {
             strcpy(serverMsg.buffer, "Player has already logged in.");
+            delete(p);
          }
          else
@@ -649,11 +671,11 @@
             cout << "Sending other players to new player" << endl;
 
-            map<unsigned int, Player>::iterator it;
+            map<unsigned int, Player*>::iterator it;
             for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
             {
-               it->second.serialize(serverMsg.buffer);
-
-               cout << "sending info about " << it->second.name  << endl;
-               cout << "sending id " << it->second.id  << endl;
+               it->second->serialize(serverMsg.buffer);
+
+               cout << "sending info about " << it->second->name  << endl;
+               cout << "sending id " << it->second->id  << endl;
                if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
                   error("sendMessage");
@@ -686,14 +708,13 @@
             for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
             {
-               cout << "Sent message back to " << it->second.name << endl;
-               if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
+               cout << "Sent message back to " << it->second->name << endl;
+               if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
                   error("sendMessage");
             }
 
-            mapPlayers[unusedPlayerId] = *p;
+            mapPlayers[unusedPlayerId] = p;
          }
 
          serverMsg.type = MSG_TYPE_LOGIN;
-         delete(p);
 
          break;
@@ -734,4 +755,5 @@
                unusedPlayerId = p->id;
             mapPlayers.erase(p->id);
+            delete p;
             strcpy(serverMsg.buffer, "You have successfully logged out.");
          }
@@ -779,6 +801,8 @@
          cout << "id: " << id << endl;
          
-         if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
-              mapPlayers[id].addr.sin_port == from.sin_port )
+         Player* p = mapPlayers[id];
+
+         if ( p->addr.sin_addr.s_addr == from.sin_addr.s_addr &&
+              p->addr.sin_port == from.sin_port )
          {
             // we need to make sure the player can move here
@@ -788,15 +812,15 @@
                cout << "valid terrain" << endl;
 
-               mapPlayers[id].target.x = x;
-               mapPlayers[id].target.y = y;
-
-               mapPlayers[id].isChasing = false;
-               mapPlayers[id].isAttacking = false;
+               p->target.x = x;
+               p->target.y = y;
+
+               p->isChasing = false;
+               p->isAttacking = false;
 
                serverMsg.type = MSG_TYPE_PLAYER_MOVE;
                
                memcpy(serverMsg.buffer, &id, 4);
-               memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
-               memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
+               memcpy(serverMsg.buffer+4, &p->target.x, 4);
+               memcpy(serverMsg.buffer+8, &p->target.y, 4);
 
                broadcastResponse = true;
@@ -820,4 +844,6 @@
          cout << "id: " << id << endl;
 
+         Player* p = mapPlayers[id];
+
          vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
          vector<WorldMap::Object>::iterator itObjects;
@@ -827,17 +853,17 @@
             bool gotFlag = false;
 
-            if (posDistance(mapPlayers[id].pos, pos.toFloat()) < 10) {
+            if (posDistance(p->pos, pos.toFloat()) < 10) {
                switch (itObjects->type) {
                   case WorldMap::OBJECT_BLUE_FLAG:
-                     if (mapPlayers[id].team == 1) {
+                     if (p->team == 1) {
                         gotFlag = true;
-                        mapPlayers[id].hasBlueFlag = true;
+                        p->hasBlueFlag = true;
                         broadcastResponse = true;
                      }
                      break;
                   case WorldMap::OBJECT_RED_FLAG:
-                     if (mapPlayers[id].team == 0) {
+                     if (p->team == 0) {
                         gotFlag = true;
-                        mapPlayers[id].hasRedFlag = true;
+                        p->hasRedFlag = true;
                         broadcastResponse = true;
                      }
@@ -849,8 +875,8 @@
                   memcpy(serverMsg.buffer, &itObjects->id, 4);
 
-                  map<unsigned int, Player>::iterator it;
+                  map<unsigned int, Player*>::iterator it;
                   for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
                   {
-                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
+                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
                         error("sendMessage");
                   }
@@ -868,5 +894,5 @@
 
          serverMsg.type = MSG_TYPE_PLAYER;
-         mapPlayers[id].serialize(serverMsg.buffer);
+         p->serialize(serverMsg.buffer);
 
          break;
@@ -882,17 +908,19 @@
          cout << "id: " << id << endl;
 
+         Player* p = mapPlayers[id];
+
          WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
-         if (mapPlayers[id].hasBlueFlag)
+         if (p->hasBlueFlag)
             flagType = WorldMap::OBJECT_BLUE_FLAG;
-         else if (mapPlayers[id].hasRedFlag)
+         else if (p->hasRedFlag)
             flagType = WorldMap::OBJECT_RED_FLAG;
 
-         addObjectToMap(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
-
-         mapPlayers[id].hasBlueFlag = false;
-         mapPlayers[id].hasRedFlag = false;
+         addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
+
+         p->hasBlueFlag = false;
+         p->hasRedFlag = false;
 
          serverMsg.type = MSG_TYPE_PLAYER;
-         mapPlayers[id].serialize(serverMsg.buffer);
+         p->serialize(serverMsg.buffer);
 
          broadcastResponse = true;
@@ -909,5 +937,5 @@
          memcpy(&targetId, clientMsg.buffer+4, 4);
 
-         Player* source = &mapPlayers[id];
+         Player* source = mapPlayers[id];
          source->targetPlayer = targetId;
          source->isChasing = true;
@@ -1010,4 +1038,18 @@
          cout << "Game name: " << g->getName() << endl;
          p->currentGame = NULL;
+
+         serverMsg.type = MSG_TYPE_LEAVE_GAME;
+         memcpy(serverMsg.buffer, &p->id, 4);
+         strcpy(serverMsg.buffer+4, g->getName().c_str());
+
+         map<unsigned int, Player*>& players = g->getPlayers();
+
+         map<unsigned int, Player*>::iterator it;
+         for (it = players.begin(); it != players.end(); it++)
+         {
+            if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
+               error("sendMessage");
+         }
+
          g->removePlayer(p->id);
 
@@ -1048,5 +1090,5 @@
          // tell the new player about all the existing players
          cout << "Sending other players to new player" << endl;
-         serverMsg.type = MSG_TYPE_LOGIN;
+         serverMsg.type = MSG_TYPE_PLAYER;
 
          map<unsigned int, Player*>::iterator it;
@@ -1120,5 +1162,5 @@
 }
 
-void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
+void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers)
 {
    while (mapPlayers.find(id) != mapPlayers.end())
@@ -1130,4 +1172,31 @@
    while (mapProjectiles.find(id) != mapProjectiles.end())
       id++;
+}
+
+Player *findPlayerByName(map<unsigned int, Player*> &m, string name)
+{
+   map<unsigned int, Player*>::iterator it;
+
+   for (it = m.begin(); it != m.end(); it++)
+   {
+      if ( it->second->name.compare(name) == 0 )
+         return it->second;
+   }
+
+   return NULL;
+}
+
+Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr)
+{
+   map<unsigned int, Player*>::iterator it;
+
+   for (it = m.begin(); it != m.end(); it++)
+   {
+      if ( it->second->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
+           it->second->addr.sin_port == addr.sin_port )
+         return it->second;
+   }
+
+   return NULL;
 }
 
@@ -1143,5 +1212,5 @@
 }
 
-void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog) {
+void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog) {
    NETWORK_MSG serverMsg;
 
@@ -1152,8 +1221,8 @@
    gameMap->getObjects()->back().serialize(serverMsg.buffer);
 
-   map<unsigned int, Player>::iterator it;
+   map<unsigned int, Player*>::iterator it;
    for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    {
-      if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
+      if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
          error("sendMessage");
    }
