Changeset b92e6a7 in network-game
- Timestamp:
- Sep 26, 2013, 8:29:14 PM (11 years ago)
- Branches:
- master
- Children:
- f41a7f9
- Parents:
- 321fbbc
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Game.cpp
r321fbbc rb92e6a7 6 6 this->id = 0; 7 7 this->name = ""; 8 this->blueScore = 0; 9 this->redScore = 0; 10 this->worldMap = NULL; 8 11 } 9 12 … … 11 14 this->id = 0; 12 15 this->name = name; 16 this->blueScore = 0; 17 this->redScore = 0; 18 this->worldMap = WorldMap::loadMapFromFile("../data/map.txt"); 13 19 } 14 20 15 21 Game::~Game() { 16 } 17 18 void Game::setId(int id) { 19 this->id = id; 22 delete this->worldMap; 20 23 } 21 24 22 25 int Game::getNumPlayers() { 23 return players.size(); 26 return this->players.size(); 27 } 28 29 map<unsigned int, Player*>& Game::getPlayers() { 30 return this->players; 31 } 32 33 int Game::getRedScore() { 34 return this->redScore; 35 } 36 37 int Game::getBlueScore() { 38 return this->blueScore; 39 } 40 41 WorldMap* Game::getMap() { 42 return this->worldMap; 43 } 44 45 void Game::setId(unsigned int id) { 46 this->id = id; 24 47 } 25 48 … … 33 56 } 34 57 35 bool Game::removePlayer( int id) {58 bool Game::removePlayer(unsigned int id) { 36 59 if (players.erase(id) == 1) 37 60 return true; … … 39 62 return false; 40 63 } 64 65 void Game::setRedScore(int score) { 66 this->redScore = score; 67 } 68 69 void Game::setBlueScore(int score) { 70 this->blueScore = score; 71 } -
common/Game.h
r321fbbc rb92e6a7 15 15 16 16 #include "Player.h" 17 #include "WorldMap.h" 17 18 18 19 using namespace std; … … 20 21 class Game { 21 22 private: 22 int id;23 unsigned int id; 23 24 string name; 24 map<int, Player*> players; 25 map<unsigned int, Player*> players; 26 WorldMap* worldMap; 27 int blueScore; 28 int redScore; 25 29 26 30 public: … … 31 35 32 36 int getNumPlayers(); 37 map<unsigned int, Player*>& getPlayers(); 38 int getBlueScore(); 39 int getRedScore(); 40 WorldMap* getMap(); 33 41 34 void setId( int id);42 void setId(unsigned int id); 35 43 bool addPlayer(Player* p); 36 bool removePlayer(int id); 44 bool removePlayer(unsigned int id); 45 void setBlueScore(int score); 46 void setRedScore(int score); 37 47 }; 38 48 -
common/WorldMap.cpp
r321fbbc rb92e6a7 194 194 WorldMap* WorldMap::loadMapFromFile(string filename) 195 195 { 196 196 WorldMap* m = NULL; 197 197 198 198 ifstream file(filename.c_str()); -
server/server.cpp
r321fbbc rb92e6a7 935 935 cout << "Game name: " << gameName << endl; 936 936 937 Player* p = findPlayerByAddr(mapPlayers, from); 938 937 939 mapGames[gameName] = Game(gameName); 938 mapGames[gameName].addPlayer(findPlayerByAddr(mapPlayers, from)); 939 int numPlayers = mapGames[gameName].getNumPlayers(); 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; 948 949 // tell the new player about all the existing players 950 cout << "Sending other players to new player" << endl; 951 952 map<unsigned int, Player*>::iterator it; 953 for (it = otherPlayers.begin(); it != otherPlayers.end(); it++) 954 { 955 it->second->serialize(serverMsg.buffer); 956 957 cout << "sending info about " << it->second->name << endl; 958 cout << "sending id " << it->second->id << endl; 959 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 960 error("sendMessage"); 961 } 962 963 // tell the new player about all map objects 964 // (currently just the flags) 965 966 serverMsg.type = MSG_TYPE_OBJECT; 967 vector<WorldMap::Object>* vctObjects = g.getMap()->getObjects(); 968 vector<WorldMap::Object>::iterator itObjects; 969 cout << "sending items" << endl; 970 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) { 971 itObjects->serialize(serverMsg.buffer); 972 cout << "sending item id " << itObjects->id << endl; 973 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 974 error("sendMessage"); 975 } 976 977 // send the current score 978 serverMsg.type = MSG_TYPE_SCORE; 979 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 } 940 998 941 999 serverMsg.type = MSG_TYPE_GAME_INFO; … … 953 1011 cout << "Game name: " << gameName << endl; 954 1012 955 mapGames[gameName].addPlayer(findPlayerByAddr(mapPlayers, from)); 956 int numPlayers = mapGames[gameName].getNumPlayers(); 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(); 1059 memcpy(serverMsg.buffer, &game_blueScore, 4); 1060 memcpy(serverMsg.buffer+4, &game_redScore, 4); 1061 1062 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 1063 error("sendMessage"); 1064 1065 serverMsg.type = MSG_TYPE_PLAYER; 1066 p->serialize(serverMsg.buffer); 1067 cout << "Should be broadcasting the message" << endl; 1068 1069 for (it = otherPlayers.begin(); it != otherPlayers.end(); it++) 1070 { 1071 cout << "Sent message back to " << it->second->name << endl; 1072 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 1073 error("sendMessage"); 1074 } 957 1075 958 1076 serverMsg.type = MSG_TYPE_GAME_INFO;
Note:
See TracChangeset
for help on using the changeset viewer.