Changeset f41a7f9 in network-game


Ignore:
Timestamp:
Sep 26, 2013, 10:07:24 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
f203c5c
Parents:
b92e6a7
Message:

Each player now holds a reference to their current game

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • common/Player.cpp

    rb92e6a7 rf41a7f9  
    3232   this->hasBlueFlag = false;
    3333   this->hasRedFlag = false;
     34
     35   this->currentGame = NULL;
    3436}
    3537
     
    6163   this->hasBlueFlag = p.hasBlueFlag;
    6264   this->hasRedFlag = p.hasRedFlag;
     65
     66   this->currentGame = p.currentGame;
    6367}
    6468
     
    8892   this->hasBlueFlag = false;
    8993   this->hasRedFlag = false;
     94
     95   this->currentGame = NULL;
    9096}
    9197
  • common/Player.h

    rb92e6a7 rf41a7f9  
    1818
    1919using namespace std;
     20
     21//forward declaration
     22class Game;
    2023
    2124class Player {
     
    7780   bool hasBlueFlag;
    7881   bool hasRedFlag;
     82
     83   Game* currentGame;
    7984};
    8085
  • server/server.cpp

    rb92e6a7 rf41a7f9  
    4545// from used to be const. Removed that so I could take a reference
    4646// and use it to send messages
    47 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);
     47bool 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);
    4848
    4949void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
     
    100100   map<unsigned int, Player> mapPlayers;
    101101   map<unsigned int, Projectile> mapProjectiles;
    102    map<string, Game> mapGames;
     102   map<string, Game*> mapGames;
    103103   unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
    104104   int scoreBlue, scoreRed;
     
    559559   outputLog.close();
    560560
     561   // delete all games
     562   map<string, Game*>::iterator itGames;
     563   for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) {
     564      delete itGames->second;
     565   }
     566
    561567   return 0;
    562568}
    563569
    564 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)
     570bool 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)
    565571{
    566572   DataAccess da;
     
    937943         Player* p = findPlayerByAddr(mapPlayers, from);
    938944
    939          mapGames[gameName] = Game(gameName);
    940          Game& g = mapGames[gameName];
    941          g.addPlayer(p);
    942          int numPlayers = g.getNumPlayers();
    943 
    944          map<unsigned int, Player*>& otherPlayers = g.getPlayers();
    945 
    946          // choose a random team (either 0 or 1)
    947          p->team = rand() % 2;
     945         Game* g = new Game(gameName);
     946         mapGames[gameName] = g;
     947         g->addPlayer(p);
     948         int numPlayers = g->getNumPlayers();
     949
     950         p->team = rand() % 2; // choose a random team (either 0 or 1)
     951         p->currentGame = g;
     952
     953         map<unsigned int, Player*>& otherPlayers = g->getPlayers();
    948954
    949955         // tell the new player about all the existing players
     
    965971
    966972         serverMsg.type = MSG_TYPE_OBJECT;
    967          vector<WorldMap::Object>* vctObjects = g.getMap()->getObjects();
     973         vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
     974         vector<WorldMap::Object>::iterator itObjects;
     975         cout << "sending items" << endl;
     976
     977         for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
     978            itObjects->serialize(serverMsg.buffer);
     979            cout << "sending item id " << itObjects->id  << endl;
     980            if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
     981               error("sendMessage");
     982         }
     983         cout << "Done sending items" << endl;
     984
     985         // send the current score
     986         serverMsg.type = MSG_TYPE_SCORE;
     987
     988         int game_blueScore = g->getBlueScore();
     989         int game_redScore = g->getRedScore();
     990         memcpy(serverMsg.buffer, &game_blueScore, 4);
     991         memcpy(serverMsg.buffer+4, &game_redScore, 4);
     992
     993         if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
     994            error("sendMessage");
     995
     996         serverMsg.type = MSG_TYPE_PLAYER;
     997         p->serialize(serverMsg.buffer);
     998         cout << "Should be broadcasting the message" << endl;
     999
     1000         for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
     1001         {
     1002            cout << "Sent message back to " << it->second->name << endl;
     1003            if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
     1004               error("sendMessage");
     1005         }
     1006
     1007         serverMsg.type = MSG_TYPE_GAME_INFO;
     1008         memcpy(serverMsg.buffer, &numPlayers, 4);
     1009         strcpy(serverMsg.buffer+4, gameName.c_str());
     1010         broadcastResponse = true;
     1011
     1012         break;
     1013      }
     1014      case MSG_TYPE_JOIN_GAME:
     1015      {
     1016         cout << "Received a JOIN_GAME message" << endl;
     1017
     1018         string gameName(clientMsg.buffer);
     1019         cout << "Game name: " << gameName << endl;
     1020
     1021         Player* p = findPlayerByAddr(mapPlayers, from);
     1022
     1023         Game* g = mapGames[gameName];
     1024         if (!g->addPlayer(p))
     1025            cout << "Player " << p->name << " trying to join a game he's already in" << endl;
     1026         int numPlayers = g->getNumPlayers();
     1027
     1028         p->team = rand() % 2; // choose a random team (either 0 or 1)
     1029         p->currentGame = g;
     1030
     1031         map<unsigned int, Player*>& otherPlayers = g->getPlayers();
     1032
     1033         // tell the new player about all the existing players
     1034         cout << "Sending other players to new player" << endl;
     1035
     1036         map<unsigned int, Player*>::iterator it;
     1037         for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
     1038         {
     1039            it->second->serialize(serverMsg.buffer);
     1040
     1041            cout << "sending info about " << it->second->name  << endl;
     1042            cout << "sending id " << it->second->id  << endl;
     1043            if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
     1044               error("sendMessage");
     1045         }
     1046
     1047         // tell the new player about all map objects
     1048         // (currently just the flags)
     1049
     1050         serverMsg.type = MSG_TYPE_OBJECT;
     1051         vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
    9681052         vector<WorldMap::Object>::iterator itObjects;
    9691053         cout << "sending items" << endl;
     
    9751059         }
    9761060
     1061
    9771062         // send the current score
    9781063         serverMsg.type = MSG_TYPE_SCORE;
    9791064
    980          int game_blueScore = g.getBlueScore();
    981          int game_redScore = g.getRedScore();
    982          memcpy(serverMsg.buffer, &game_blueScore, 4);
    983          memcpy(serverMsg.buffer+4, &game_redScore, 4);
    984 
    985          if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
    986             error("sendMessage");
    987 
    988          serverMsg.type = MSG_TYPE_PLAYER;
    989          p->serialize(serverMsg.buffer);
    990          cout << "Should be broadcasting the message" << endl;
    991 
    992          for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
    993          {
    994             cout << "Sent message back to " << it->second->name << endl;
    995             if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
    996                error("sendMessage");
    997          }
    998 
    999          serverMsg.type = MSG_TYPE_GAME_INFO;
    1000          memcpy(serverMsg.buffer, &numPlayers, 4);
    1001          strcpy(serverMsg.buffer+4, gameName.c_str());
    1002          broadcastResponse = true;
    1003 
    1004          break;
    1005       }
    1006       case MSG_TYPE_JOIN_GAME:
    1007       {
    1008          cout << "Received a JOIN_GAME message" << endl;
    1009 
    1010          string gameName(clientMsg.buffer);
    1011          cout << "Game name: " << gameName << endl;
    1012 
    1013          Player* p = findPlayerByAddr(mapPlayers, from);
    1014 
    1015          Game& g = mapGames[gameName];
    1016          if (!g.addPlayer(p))
    1017             cout << "Player " << p->name << " trying to join a game he's already in" << endl;
    1018          int numPlayers = g.getNumPlayers();
    1019 
    1020          map<unsigned int, Player*>& otherPlayers = g.getPlayers();
    1021 
    1022          // choose a random team (either 0 or 1)
    1023          p->team = rand() % 2;
    1024 
    1025          // tell the new player about all the existing players
    1026          cout << "Sending other players to new player" << endl;
    1027 
    1028          map<unsigned int, Player*>::iterator it;
    1029          for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
    1030          {
    1031             it->second->serialize(serverMsg.buffer);
    1032 
    1033             cout << "sending info about " << it->second->name  << endl;
    1034             cout << "sending id " << it->second->id  << endl;
    1035             if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
    1036                error("sendMessage");
    1037          }
    1038 
    1039          // tell the new player about all map objects
    1040          // (currently just the flags)
    1041 
    1042          serverMsg.type = MSG_TYPE_OBJECT;
    1043          vector<WorldMap::Object>* vctObjects = g.getMap()->getObjects();
    1044          vector<WorldMap::Object>::iterator itObjects;
    1045          cout << "sending items" << endl;
    1046          for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
    1047             itObjects->serialize(serverMsg.buffer);
    1048             cout << "sending item id " << itObjects->id  << endl;
    1049             if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
    1050                error("sendMessage");
    1051          }
    1052 
    1053 
    1054          // send the current score
    1055          serverMsg.type = MSG_TYPE_SCORE;
    1056 
    1057          int game_blueScore = g.getBlueScore();
    1058          int game_redScore = g.getRedScore();
     1065         int game_blueScore = g->getBlueScore();
     1066         int game_redScore = g->getRedScore();
    10591067         memcpy(serverMsg.buffer, &game_blueScore, 4);
    10601068         memcpy(serverMsg.buffer+4, &game_redScore, 4);
Note: See TracChangeset for help on using the changeset viewer.