Changeset b8601ee in network-game for server/server.cpp


Ignore:
Timestamp:
May 25, 2013, 11:51:29 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
15efb4e
Parents:
5c84d54
Message:

The server keeps track of each team's score and sends SCORE meesages to the clients upon login and when the score changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/server.cpp

    r5c84d54 rb8601ee  
    3838// from used to be const. Removed that so I could take a reference
    3939// and use it to send messages
    40 bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg, int sock);
     40bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed);
    4141
    4242void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
     
    8484   map<unsigned int, Player> mapPlayers;
    8585   unsigned int unusedId = 1;
     86   int scoreBlue, scoreRed;
     87
     88   scoreBlue = 0;
     89   scoreRed = 0;
    8690
    8791   //SSL_load_error_strings();
     
    176180                        pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
    177181                        flagTurnedIn = true;
     182                        scoreBlue++;
    178183                     }
    179184
     
    188193                        pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
    189194                        flagTurnedIn = true;
     195                        scoreRed++;
    190196                     }
    191197
     
    204210
    205211                  map<unsigned int, Player>::iterator it2;
     212                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
     213                  {
     214                     if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     215                        error("sendMessage");
     216                  }
     217
     218                  serverMsg.type = MSG_TYPE_SCORE;
     219                  memcpy(serverMsg.buffer, &scoreBlue, 4);
     220                  memcpy(serverMsg.buffer+4, &scoreRed, 4);
     221
    206222                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    207223                  {
     
    233249
    234250      if (n >= 0) {
    235          broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg, sock);
     251         broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg, sock, scoreBlue, scoreRed);
    236252
    237253         // probably replace this with a function that prints based on the
     
    263279}
    264280
    265 bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg, int sock)
     281bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg, int sock, int &scoreBlue, int &scoreRed)
    266282{
    267283   DataAccess da;
     
    355371            }
    356372
     373            // send the current score
     374            serverMsg.type = MSG_TYPE_SCORE;
     375            memcpy(serverMsg.buffer, &scoreBlue, 4);
     376            memcpy(serverMsg.buffer+4, &scoreRed, 4);
     377            if ( sendMessage(&serverMsg, sock, &from) < 0 )
     378               error("sendMessage");
     379
     380            serverMsg.type = MSG_TYPE_PLAYER;
    357381            p->serialize(serverMsg.buffer);
    358382            cout << "Should be broadcasting the message" << endl;
Note: See TracChangeset for help on using the changeset viewer.