Changeset b48ef09 in network-game
- Timestamp:
- Sep 27, 2013, 3:43:51 PM (11 years ago)
- Branches:
- master
- Children:
- 7d8d5d3
- Parents:
- 803566d
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Game.cpp
r803566d rb48ef09 48 48 49 49 bool Game::addPlayer(Player* p) { 50 if (players. count(p->id) == 0) {50 if (players.find(p->id) == players.end()) { 51 51 players[p->id] = p; 52 52 return true; -
common/MessageContainer.h
r803566d rb48ef09 35 35 #define MSG_TYPE_LEAVE_GAME 19 36 36 #define MSG_TYPE_GAME_INFO 20 37 #define MSG_TYPE_JOIN_GAME_INFO 21 38 #define MSG_TYPE_JOIN_GAME_FAILURE 22 39 #define MSG_TYPE_JOIN_GAME_ACK 23 37 40 38 41 typedef struct -
server/server.cpp
r803566d rb48ef09 939 939 cout << "Game name: " << gameName << endl; 940 940 941 Player* p = findPlayerByAddr(mapPlayers, from); 941 // check if this game already exists 942 if (mapGames.find(gameName) != mapGames.end()) { 943 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE; 944 broadcastResponse = false; 945 return broadcastResponse; 946 } 942 947 943 948 Game* g = new Game(gameName); 944 949 mapGames[gameName] = g; 945 g->addPlayer(p); 946 int numPlayers = g->getNumPlayers(); 947 948 p->team = rand() % 2; // choose a random team (either 0 or 1) 950 951 Player* p = findPlayerByAddr(mapPlayers, from); 949 952 p->currentGame = g; 950 953 951 map<unsigned int, Player*>& otherPlayers = g->getPlayers(); 952 953 // tell the new player about all the existing players 954 cout << "Sending other players to new player" << endl; 955 serverMsg.type = MSG_TYPE_LOGIN; 956 957 map<unsigned int, Player*>::iterator it; 958 for (it = otherPlayers.begin(); it != otherPlayers.end(); it++) 959 { 960 it->second->serialize(serverMsg.buffer); 961 962 cout << "sending info about " << it->second->name << endl; 963 cout << "sending id " << it->second->id << endl; 964 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 965 error("sendMessage"); 966 } 967 968 // tell the new player about all map objects 969 // (currently just the flags) 970 971 serverMsg.type = MSG_TYPE_OBJECT; 972 vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects(); 973 vector<WorldMap::Object>::iterator itObjects; 974 cout << "sending items" << endl; 975 976 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) { 977 itObjects->serialize(serverMsg.buffer); 978 cout << "sending item id " << itObjects->id << endl; 979 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 980 error("sendMessage"); 981 } 982 cout << "Done sending items" << endl; 983 984 // send the current score 985 serverMsg.type = MSG_TYPE_SCORE; 986 987 int game_blueScore = g->getBlueScore(); 988 int game_redScore = g->getRedScore(); 989 memcpy(serverMsg.buffer, &game_blueScore, 4); 990 memcpy(serverMsg.buffer+4, &game_redScore, 4); 991 992 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 993 error("sendMessage"); 994 995 serverMsg.type = MSG_TYPE_PLAYER; 996 p->serialize(serverMsg.buffer); 997 cout << "Should be broadcasting the message" << endl; 998 999 for (it = otherPlayers.begin(); it != otherPlayers.end(); it++) 1000 { 1001 cout << "Sent message back to " << it->second->name << endl; 1002 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 1003 error("sendMessage"); 1004 } 1005 1006 serverMsg.type = MSG_TYPE_GAME_INFO; 1007 memcpy(serverMsg.buffer, &numPlayers, 4); 1008 strcpy(serverMsg.buffer+4, gameName.c_str()); 1009 broadcastResponse = true; 954 serverMsg.type = MSG_TYPE_JOIN_GAME_INFO; 955 strcpy(serverMsg.buffer, gameName.c_str()); 956 broadcastResponse = false; 1010 957 1011 958 break; … … 1018 965 cout << "Game name: " << gameName << endl; 1019 966 967 // check if this game already exists 968 if (mapGames.find(gameName) == mapGames.end()) { 969 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE; 970 broadcastResponse = false; 971 return broadcastResponse; 972 } 973 974 Game* g = mapGames[gameName]; 975 map<unsigned int, Player*>& players = g->getPlayers(); 1020 976 Player* p = findPlayerByAddr(mapPlayers, from); 1021 977 978 if (players.find(p->id) != players.end()) { 979 cout << "Player " << p->name << " trying to join a game he's already in" << endl; 980 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE; 981 broadcastResponse = false; 982 return broadcastResponse; 983 } 984 985 p->currentGame = g; 986 987 serverMsg.type = MSG_TYPE_JOIN_GAME_INFO; 988 strcpy(serverMsg.buffer, gameName.c_str()); 989 broadcastResponse = false; 990 991 break; 992 } 993 case MSG_TYPE_JOIN_GAME_ACK: 994 { 995 cout << "Received a JOIN_GAME_ACK message" << endl; 996 997 string gameName(clientMsg.buffer); 998 cout << "Game name: " << gameName << endl; 999 1000 // check if this game already exists 1001 if (mapGames.find(gameName) == mapGames.end()) { 1002 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE; 1003 broadcastResponse = false; 1004 return broadcastResponse; 1005 } 1006 1022 1007 Game* g = mapGames[gameName]; 1023 if (!g->addPlayer(p)) 1024 cout << "Player " << p->name << " trying to join a game he's already in" << endl; 1025 int numPlayers = g->getNumPlayers(); 1026 1008 1009 Player* p = findPlayerByAddr(mapPlayers, from); 1027 1010 p->team = rand() % 2; // choose a random team (either 0 or 1) 1028 1011 p->currentGame = g; … … 1082 1065 } 1083 1066 1067 g->addPlayer(p); 1068 int numPlayers = g->getNumPlayers(); 1069 1084 1070 serverMsg.type = MSG_TYPE_GAME_INFO; 1085 1071 memcpy(serverMsg.buffer, &numPlayers, 4); 1086 1072 strcpy(serverMsg.buffer+4, gameName.c_str()); 1087 1073 broadcastResponse = true; 1088 1089 1074 break; 1090 1075 }
Note:
See TracChangeset
for help on using the changeset viewer.