Index: server/server.cpp
===================================================================
--- server/server.cpp	(revision b92e6a797b3a06a7df69743ddb9f239cf9c163de)
+++ server/server.cpp	(revision f41a7f944c0904ddffe09163e64afcf46e0a00ee)
@@ -45,5 +45,5 @@
 // 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);
+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);
@@ -100,5 +100,5 @@
    map<unsigned int, Player> mapPlayers;
    map<unsigned int, Projectile> mapProjectiles;
-   map<string, Game> mapGames;
+   map<string, Game*> mapGames;
    unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
    int scoreBlue, scoreRed;
@@ -559,8 +559,14 @@
    outputLog.close();
 
+   // delete all games
+   map<string, Game*>::iterator itGames;
+   for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) {
+      delete itGames->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;
@@ -937,13 +943,13 @@
          Player* p = findPlayerByAddr(mapPlayers, from);
 
-         mapGames[gameName] = Game(gameName);
-         Game& g = mapGames[gameName];
-         g.addPlayer(p);
-         int numPlayers = g.getNumPlayers();
-
-         map<unsigned int, Player*>& otherPlayers = g.getPlayers();
-
-         // choose a random team (either 0 or 1)
-         p->team = rand() % 2;
+         Game* g = new Game(gameName);
+         mapGames[gameName] = g;
+         g->addPlayer(p);
+         int numPlayers = g->getNumPlayers();
+
+         p->team = rand() % 2; // choose a random team (either 0 or 1)
+         p->currentGame = g;
+
+         map<unsigned int, Player*>& otherPlayers = g->getPlayers();
 
          // tell the new player about all the existing players
@@ -965,5 +971,83 @@
 
          serverMsg.type = MSG_TYPE_OBJECT;
-         vector<WorldMap::Object>* vctObjects = g.getMap()->getObjects();
+         vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
+         vector<WorldMap::Object>::iterator itObjects;
+         cout << "sending items" << endl;
+
+         for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
+            itObjects->serialize(serverMsg.buffer);
+            cout << "sending item id " << itObjects->id  << endl;
+            if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
+               error("sendMessage");
+         }
+         cout << "Done sending items" << endl;
+
+         // send the current score
+         serverMsg.type = MSG_TYPE_SCORE;
+
+         int game_blueScore = g->getBlueScore();
+         int game_redScore = g->getRedScore();
+         memcpy(serverMsg.buffer, &game_blueScore, 4);
+         memcpy(serverMsg.buffer+4, &game_redScore, 4);
+
+         if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
+            error("sendMessage");
+
+         serverMsg.type = MSG_TYPE_PLAYER;
+         p->serialize(serverMsg.buffer);
+         cout << "Should be broadcasting the message" << endl;
+
+         for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
+         {
+            cout << "Sent message back to " << it->second->name << endl;
+            if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
+               error("sendMessage");
+         }
+
+         serverMsg.type = MSG_TYPE_GAME_INFO;
+         memcpy(serverMsg.buffer, &numPlayers, 4);
+         strcpy(serverMsg.buffer+4, gameName.c_str());
+         broadcastResponse = true;
+
+         break;
+      }
+      case MSG_TYPE_JOIN_GAME:
+      {
+         cout << "Received a JOIN_GAME message" << endl;
+
+         string gameName(clientMsg.buffer);
+         cout << "Game name: " << gameName << endl;
+
+         Player* p = findPlayerByAddr(mapPlayers, from);
+
+         Game* g = mapGames[gameName];
+         if (!g->addPlayer(p))
+            cout << "Player " << p->name << " trying to join a game he's already in" << endl;
+         int numPlayers = g->getNumPlayers();
+
+         p->team = rand() % 2; // choose a random team (either 0 or 1)
+         p->currentGame = g;
+
+         map<unsigned int, Player*>& otherPlayers = g->getPlayers();
+
+         // tell the new player about all the existing players
+         cout << "Sending other players to new player" << endl;
+
+         map<unsigned int, Player*>::iterator it;
+         for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
+         {
+            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");
+         }
+
+         // tell the new player about all map objects
+         // (currently just the flags)
+
+         serverMsg.type = MSG_TYPE_OBJECT;
+         vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
          vector<WorldMap::Object>::iterator itObjects;
          cout << "sending items" << endl;
@@ -975,86 +1059,10 @@
          }
 
+
          // send the current score
          serverMsg.type = MSG_TYPE_SCORE;
 
-         int game_blueScore = g.getBlueScore();
-         int game_redScore = g.getRedScore();
-         memcpy(serverMsg.buffer, &game_blueScore, 4);
-         memcpy(serverMsg.buffer+4, &game_redScore, 4);
-
-         if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
-            error("sendMessage");
-
-         serverMsg.type = MSG_TYPE_PLAYER;
-         p->serialize(serverMsg.buffer);
-         cout << "Should be broadcasting the message" << endl;
-
-         for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
-         {
-            cout << "Sent message back to " << it->second->name << endl;
-            if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
-               error("sendMessage");
-         }
-
-         serverMsg.type = MSG_TYPE_GAME_INFO;
-         memcpy(serverMsg.buffer, &numPlayers, 4);
-         strcpy(serverMsg.buffer+4, gameName.c_str());
-         broadcastResponse = true;
-
-         break;
-      }
-      case MSG_TYPE_JOIN_GAME:
-      {
-         cout << "Received a JOIN_GAME message" << endl;
-
-         string gameName(clientMsg.buffer);
-         cout << "Game name: " << gameName << endl;
-
-         Player* p = findPlayerByAddr(mapPlayers, from);
-
-         Game& g = mapGames[gameName];
-         if (!g.addPlayer(p))
-            cout << "Player " << p->name << " trying to join a game he's already in" << endl;
-         int numPlayers = g.getNumPlayers();
-
-         map<unsigned int, Player*>& otherPlayers = g.getPlayers();
-
-         // choose a random team (either 0 or 1)
-         p->team = rand() % 2;
-
-         // tell the new player about all the existing players
-         cout << "Sending other players to new player" << endl;
-
-         map<unsigned int, Player*>::iterator it;
-         for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
-         {
-            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");
-         }
-
-         // tell the new player about all map objects
-         // (currently just the flags)
-
-         serverMsg.type = MSG_TYPE_OBJECT;
-         vector<WorldMap::Object>* vctObjects = g.getMap()->getObjects();
-         vector<WorldMap::Object>::iterator itObjects;
-         cout << "sending items" << endl;
-         for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
-            itObjects->serialize(serverMsg.buffer);
-            cout << "sending item id " << itObjects->id  << endl;
-            if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
-               error("sendMessage");
-         }
-
-
-         // send the current score
-         serverMsg.type = MSG_TYPE_SCORE;
-
-         int game_blueScore = g.getBlueScore();
-         int game_redScore = g.getRedScore();
+         int game_blueScore = g->getBlueScore();
+         int game_redScore = g->getRedScore();
          memcpy(serverMsg.buffer, &game_blueScore, 4);
          memcpy(serverMsg.buffer+4, &game_redScore, 4);
