Changeset 53643ca in network-game for server/server.cpp


Ignore:
Timestamp:
Jul 19, 2014, 12:32:33 AM (10 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
master
Children:
4c00935
Parents:
cdb0e98
Message:

Server loads user profile and game history info from the database, saves game history to the db after every game, and uses a lua settings file to load db settings

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/server.cpp

    rcdb0e98 r53643ca  
    4343// from used to be const. Removed that so I could take a reference
    4444// and use it to send messages
    45 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);
    46 
    47 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers);
     45void processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, MessageProcessor& msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, ofstream& outputLog);
     46
    4847Player *findPlayerByName(map<unsigned int, Player*> &m, string name);
    4948Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr);
     
    6362   map<unsigned int, Projectile> mapProjectiles;
    6463   map<string, Game*> mapGames;
    65    unsigned int unusedPlayerId = 1;
    6664   ofstream outputLog;
    6765
     
    117115
    118116         map<unsigned int, Player*>::iterator it;
    119 
    120          cout << "Updating player targets and respawning dead players" << endl;
    121117
    122118         // set targets for all chasing players (or make them attack if they're close enough)
     
    179175         }
    180176
    181          cout << "Processing players in a game" << endl;
    182 
    183177         // process players currently in a game
    184178         map<string, Game*>::iterator itGames;
     
    188182            game = itGames->second;
    189183            if (game->handleGameEvents()) {
     184               // save game record
     185               int winningTeam = -1;
     186               if (game->getBlueScore() == 3)
     187                  winningTeam = 0;
     188               else if (game->getRedScore() == 3)
     189                  winningTeam = 1;
     190
     191               if (winningTeam == -1)
     192                  cout << "Error: Game ended, but neither team has a score of 3" << endl;
     193               else {
     194                  map<unsigned int, Player*>::iterator it;
     195                  DataAccess da;
     196
     197                  for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++) {
     198                      da.saveGameHistory(it->second->getId(), winningTeam, game->getBlueScore(), game->getRedScore());
     199                  }
     200               }
     201
    190202               // send a GAME_INFO message with 0 players to force clients to delete the game
    191203               int numPlayers = 0;
     
    200212               itGames++;
    201213         }
    202 
    203          cout << "Processing projectiles"  << endl;
    204214
    205215         // move all projectiles
     
    230240      if (msgProcessor.receiveMessage(&clientMsg, &from) >= 0)
    231241      {
    232          processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, unusedPlayerId, outputLog);
     242         processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, outputLog);
    233243
    234244         cout << "Finished processing the message" << endl;
     
    255265}
    256266
    257 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)
     267void processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, ofstream& outputLog)
    258268{
    259269   NETWORK_MSG serverMsg;
     
    331341         else
    332342         {
    333             updateUnusedPlayerId(unusedPlayerId, mapPlayers);
    334             p->setId(unusedPlayerId);
    335343            cout << "new player id: " << p->getId() << endl;
    336344            p->setAddr(from);
     
    369377            msgProcessor.broadcastMessage(serverMsg, mapPlayers);
    370378
    371             mapPlayers[unusedPlayerId] = p;
     379            mapPlayers[p->getId()] = p;
    372380         }
    373381
     
    403411
    404412            msgProcessor.broadcastMessage(serverMsg, mapPlayers);
    405 
    406             if (p->getId() < unusedPlayerId)
    407                unusedPlayerId = p->getId();
    408413
    409414            mapPlayers.erase(p->getId());
     
    569574      case MSG_TYPE_PROFILE:
    570575      {
     576         cout << "Received a PROFILE message" << endl;
     577
     578         unsigned int id;
     579
     580         memcpy(&id, clientMsg.buffer, 4);
     581
     582         cout << "Player id: " << id << endl;
     583         unsigned int numGames = 0;
     584         int** gameHistory = da.getPlayerGameHistory(id, numGames);
     585         int* playerRecord = da.getPlayerRecord(id);
     586
     587         int honorPoints = playerRecord[0];
     588         int wins = playerRecord[1];
     589         int losses = playerRecord[2];
     590
    571591         serverMsg.type = MSG_TYPE_PROFILE;
    572592
    573          // each array is the score for one game
    574          // the columns are result, team, blue score, and red score
    575          // for result 0 is defeat and 1 is victory
    576          // for team, 0 is blue and 1 is red
    577          int scores[][4] = {
    578             {1, 1, 2, 3},
    579             {1, 0, 3, 2},
    580             {0, 1, 3, 1},
    581             {1, 1, 0, 3},
    582             {0, 0, 2, 3},
    583             {1, 0, 3, 2},
    584             {1, 0, 3, 0},
    585             {0, 1, 3, 1},
    586             {1, 1, 1, 3},
    587             {1, 0, 3, 2}
    588          };
    589 
    590          int honorPoints = 1000;
    591          int numGames = 10;
    592593         memcpy(serverMsg.buffer, &honorPoints, 4);
    593          memcpy(serverMsg.buffer+4, &numGames, 4);
    594          for (unsigned int i=0; i<sizeof(scores)/sizeof(scores[0]); i++) {
    595             memcpy(serverMsg.buffer+8+i*16, &scores[i][0], 4);
    596             memcpy(serverMsg.buffer+12+i*16, &scores[i][1], 4);
    597             memcpy(serverMsg.buffer+16+i*16, &scores[i][2], 4);
    598             memcpy(serverMsg.buffer+20+i*16, &scores[i][3], 4);
    599          }
     594         memcpy(serverMsg.buffer+4, &wins, 4);
     595         memcpy(serverMsg.buffer+8, &losses, 4);
     596         memcpy(serverMsg.buffer+12, &numGames, 4);
     597         for (unsigned int i=0; i<numGames; i++) {
     598            memcpy(serverMsg.buffer+16+i*16, &gameHistory[i][0], 4);
     599            memcpy(serverMsg.buffer+20+i*16, &gameHistory[i][1], 4);
     600            memcpy(serverMsg.buffer+24+i*16, &gameHistory[i][2], 4);
     601            memcpy(serverMsg.buffer+28+i*16, &gameHistory[i][3], 4);
     602            delete[] gameHistory[i];
     603         }
     604
     605         //delete[] gameHistory;
     606         free(gameHistory);
     607         delete[] playerRecord;
    600608
    601609         msgProcessor.sendMessage(&serverMsg, &from);
     
    825833}
    826834
    827 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers)
    828 {
    829    while (mapPlayers.find(id) != mapPlayers.end())
    830       id++;
    831 }
    832 
    833835Player *findPlayerByName(map<unsigned int, Player*> &m, string name)
    834836{
Note: See TracChangeset for help on using the changeset viewer.