Index: server/server.cpp
===================================================================
--- server/server.cpp	(revision cdb0e982399ace2b3ddec199a997d4b76be56ae0)
+++ server/server.cpp	(revision 81c4e8ad973d731da0bbaf6be136da4da3911968)
@@ -43,7 +43,6 @@
 // from used to be const. Removed that so I could take a reference
 // and use it to send messages
-void processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, MessageProcessor& msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, unsigned int& unusedPlayerId, ofstream& outputLog);
-
-void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers);
+void processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, MessageProcessor& msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, ofstream& outputLog);
+
 Player *findPlayerByName(map<unsigned int, Player*> &m, string name);
 Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr);
@@ -63,5 +62,4 @@
    map<unsigned int, Projectile> mapProjectiles;
    map<string, Game*> mapGames;
-   unsigned int unusedPlayerId = 1;
    ofstream outputLog;
 
@@ -117,6 +115,4 @@
 
          map<unsigned int, Player*>::iterator it;
-
-         cout << "Updating player targets and respawning dead players" << endl;
 
          // set targets for all chasing players (or make them attack if they're close enough)
@@ -179,6 +175,4 @@
          }
 
-         cout << "Processing players in a game" << endl;
-
          // process players currently in a game
          map<string, Game*>::iterator itGames;
@@ -188,4 +182,22 @@
             game = itGames->second;
             if (game->handleGameEvents()) {
+               // save game record
+               int winningTeam = -1;
+               if (game->getBlueScore() == 3)
+                  winningTeam = 0;
+               else if (game->getRedScore() == 3)
+                  winningTeam = 1;
+
+               if (winningTeam == -1)
+                  cout << "Error: Game ended, but neither team has a score of 3" << endl;
+               else {
+                  map<unsigned int, Player*>::iterator it;
+                  DataAccess da;
+
+                  for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++) {
+                      da.saveGameHistory(it->second->getId(), winningTeam, game->getBlueScore(), game->getRedScore());
+                  }
+               }
+
                // send a GAME_INFO message with 0 players to force clients to delete the game
                int numPlayers = 0;
@@ -200,6 +212,4 @@
                itGames++;
          }
-
-         cout << "Processing projectiles"  << endl;
 
          // move all projectiles
@@ -230,5 +240,5 @@
       if (msgProcessor.receiveMessage(&clientMsg, &from) >= 0)
       {
-         processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, unusedPlayerId, outputLog);
+         processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, outputLog);
 
          cout << "Finished processing the message" << endl;
@@ -255,5 +265,5 @@
 }
 
-void processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, unsigned int& unusedPlayerId, ofstream& outputLog)
+void processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, ofstream& outputLog)
 {
    NETWORK_MSG serverMsg;
@@ -331,6 +341,4 @@
          else
          {
-            updateUnusedPlayerId(unusedPlayerId, mapPlayers);
-            p->setId(unusedPlayerId);
             cout << "new player id: " << p->getId() << endl;
             p->setAddr(from);
@@ -369,5 +377,5 @@
             msgProcessor.broadcastMessage(serverMsg, mapPlayers);
 
-            mapPlayers[unusedPlayerId] = p;
+            mapPlayers[p->getId()] = p;
          }
 
@@ -403,7 +411,4 @@
 
             msgProcessor.broadcastMessage(serverMsg, mapPlayers);
-
-            if (p->getId() < unusedPlayerId)
-               unusedPlayerId = p->getId();
 
             mapPlayers.erase(p->getId());
@@ -569,33 +574,36 @@
       case MSG_TYPE_PROFILE:
       {
+         cout << "Received a PROFILE message" << endl;
+
+         unsigned int id;
+
+         memcpy(&id, clientMsg.buffer, 4);
+
+         cout << "Player id: " << id << endl;
+         unsigned int numGames = 0;
+         int** gameHistory = da.getPlayerGameHistory(id, numGames);
+         int* playerRecord = da.getPlayerRecord(id);
+
+         int honorPoints = playerRecord[0];
+         int wins = playerRecord[1];
+         int losses = playerRecord[2];
+
          serverMsg.type = MSG_TYPE_PROFILE;
 
-         // each array is the score for one game
-         // the columns are result, team, blue score, and red score
-         // for result 0 is defeat and 1 is victory
-         // for team, 0 is blue and 1 is red
-         int scores[][4] = {
-            {1, 1, 2, 3},
-            {1, 0, 3, 2},
-            {0, 1, 3, 1},
-            {1, 1, 0, 3},
-            {0, 0, 2, 3},
-            {1, 0, 3, 2},
-            {1, 0, 3, 0},
-            {0, 1, 3, 1},
-            {1, 1, 1, 3},
-            {1, 0, 3, 2}
-         };
-
-         int honorPoints = 1000;
-         int numGames = 10;
          memcpy(serverMsg.buffer, &honorPoints, 4);
-         memcpy(serverMsg.buffer+4, &numGames, 4);
-         for (unsigned int i=0; i<sizeof(scores)/sizeof(scores[0]); i++) {
-            memcpy(serverMsg.buffer+8+i*16, &scores[i][0], 4);
-            memcpy(serverMsg.buffer+12+i*16, &scores[i][1], 4);
-            memcpy(serverMsg.buffer+16+i*16, &scores[i][2], 4);
-            memcpy(serverMsg.buffer+20+i*16, &scores[i][3], 4);
-         }
+         memcpy(serverMsg.buffer+4, &wins, 4);
+         memcpy(serverMsg.buffer+8, &losses, 4);
+         memcpy(serverMsg.buffer+12, &numGames, 4);
+         for (unsigned int i=0; i<numGames; i++) {
+            memcpy(serverMsg.buffer+16+i*16, &gameHistory[i][0], 4);
+            memcpy(serverMsg.buffer+20+i*16, &gameHistory[i][1], 4);
+            memcpy(serverMsg.buffer+24+i*16, &gameHistory[i][2], 4);
+            memcpy(serverMsg.buffer+28+i*16, &gameHistory[i][3], 4);
+            delete[] gameHistory[i];
+         }
+
+         //delete[] gameHistory;
+         free(gameHistory);
+         delete[] playerRecord;
 
          msgProcessor.sendMessage(&serverMsg, &from);
@@ -825,10 +833,4 @@
 }
 
-void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers)
-{
-   while (mapPlayers.find(id) != mapPlayers.end())
-      id++;
-}
-
 Player *findPlayerByName(map<unsigned int, Player*> &m, string name)
 {
