Changeset f41a7f9 in network-game
- Timestamp:
- Sep 26, 2013, 10:07:24 PM (11 years ago)
- Branches:
- master
- Children:
- f203c5c
- Parents:
- b92e6a7
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Player.cpp
rb92e6a7 rf41a7f9 32 32 this->hasBlueFlag = false; 33 33 this->hasRedFlag = false; 34 35 this->currentGame = NULL; 34 36 } 35 37 … … 61 63 this->hasBlueFlag = p.hasBlueFlag; 62 64 this->hasRedFlag = p.hasRedFlag; 65 66 this->currentGame = p.currentGame; 63 67 } 64 68 … … 88 92 this->hasBlueFlag = false; 89 93 this->hasRedFlag = false; 94 95 this->currentGame = NULL; 90 96 } 91 97 -
common/Player.h
rb92e6a7 rf41a7f9 18 18 19 19 using namespace std; 20 21 //forward declaration 22 class Game; 20 23 21 24 class Player { … … 77 80 bool hasBlueFlag; 78 81 bool hasRedFlag; 82 83 Game* currentGame; 79 84 }; 80 85 -
server/server.cpp
rb92e6a7 rf41a7f9 45 45 // from used to be const. Removed that so I could take a reference 46 46 // 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);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); 48 48 49 49 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers); … … 100 100 map<unsigned int, Player> mapPlayers; 101 101 map<unsigned int, Projectile> mapProjectiles; 102 map<string, Game > mapGames;102 map<string, Game*> mapGames; 103 103 unsigned int unusedPlayerId = 1, unusedProjectileId = 1; 104 104 int scoreBlue, scoreRed; … … 559 559 outputLog.close(); 560 560 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 561 567 return 0; 562 568 } 563 569 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)570 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) 565 571 { 566 572 DataAccess da; … … 937 943 Player* p = findPlayerByAddr(mapPlayers, from); 938 944 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(); 948 954 949 955 // tell the new player about all the existing players … … 965 971 966 972 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(); 968 1052 vector<WorldMap::Object>::iterator itObjects; 969 1053 cout << "sending items" << endl; … … 975 1059 } 976 1060 1061 977 1062 // send the current score 978 1063 serverMsg.type = MSG_TYPE_SCORE; 979 1064 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(); 1059 1067 memcpy(serverMsg.buffer, &game_blueScore, 4); 1060 1068 memcpy(serverMsg.buffer+4, &game_redScore, 4);
Note:
See TracChangeset
for help on using the changeset viewer.