- Timestamp:
- Jan 28, 2014, 12:42:17 AM (11 years ago)
- Branches:
- master
- Children:
- d6b5f74
- Parents:
- dfb9363
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/main.cpp
rdfb9363 r6f64166 58 58 void shutdownWinSock(); 59 59 void createGui(ALLEGRO_FONT* font); 60 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, unsigned int& curPlayerId); 60 61 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames, unsigned int& curPlayerId); 62 void handleMsgPlayer(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames); 63 void handleMsgGameInfo(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames); 64 61 65 int getRefreshRate(int width, int height); 62 66 void drawMessageStatus(ALLEGRO_FONT* font); … … 123 127 chat chatConsole, debugConsole; 124 128 bool debugging; 125 map<string, int> mapGames;126 129 Game* game; 127 130 GameSummary* gameSummary; … … 135 138 ALLEGRO_TIMER *timer = NULL; 136 139 map<unsigned int, Player*> mapPlayers; 140 map<string, int> mapGames; 137 141 unsigned int curPlayerId = -1; 138 142 ofstream outputLog; … … 369 373 370 374 if (msgProcessor.receiveMessage(&msgFrom, &from) >= 0) 371 processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId);375 processMessage(msgFrom, state, chatConsole, mapPlayers, mapGames, curPlayerId); 372 376 373 377 if (redraw) … … 655 659 } 656 660 657 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, unsigned int& curPlayerId)661 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames, unsigned int& curPlayerId) 658 662 { 659 663 cout << "Total players in map: " << mapPlayers.size() << endl; … … 758 762 break; 759 763 } 760 case MSG_TYPE_PLAYER:761 {762 cout << "Received MSG_TYPE_PLAYER" << endl;763 764 Player p("", "");765 p.deserialize(msg.buffer);766 p.timeLastUpdated = getCurrentMillis();767 p.isChasing = false;768 if (p.health <= 0)769 p.isDead = true;770 else771 p.isDead = false;772 773 if (mapPlayers.find(p.getId()) != mapPlayers.end())774 *(mapPlayers[p.getId()]) = p;775 else776 mapPlayers[p.getId()] = new Player(p);777 778 break;779 }780 case MSG_TYPE_PLAYER_MOVE:781 {782 unsigned int id;783 int x, y;784 785 memcpy(&id, msg.buffer, 4);786 memcpy(&x, msg.buffer+4, 4);787 memcpy(&y, msg.buffer+8, 4);788 789 mapPlayers[id]->target.x = x;790 mapPlayers[id]->target.y = y;791 792 mapPlayers[id]->isChasing = false;793 mapPlayers[id]->setTargetPlayer(0);794 795 break;796 }797 764 case MSG_TYPE_CHAT: 798 765 { 799 766 chatConsole.addLine(response); 800 801 break;802 }803 case MSG_TYPE_ATTACK:804 {805 cout << "Received START_ATTACK message" << endl;806 807 unsigned int id, targetID;808 memcpy(&id, msg.buffer, 4);809 memcpy(&targetID, msg.buffer+4, 4);810 811 cout << "source id: " << id << endl;812 cout << "target id: " << targetID << endl;813 814 Player* source = mapPlayers[id];815 source->setTargetPlayer(targetID);816 source->isChasing = true;817 818 break;819 }820 case MSG_TYPE_GAME_INFO:821 {822 cout << "Received a GAME_INFO message" << endl;823 824 string gameName(msg.buffer+4);825 int numPlayers;826 827 memcpy(&numPlayers, msg.buffer, 4);828 829 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;830 831 if (numPlayers > 0)832 mapGames[gameName] = numPlayers;833 else834 mapGames.erase(gameName);835 767 836 768 break; … … 863 795 { 864 796 cout << "Received a JOIN_GAME_FAILURE message" << endl; 797 798 break; 799 } 800 case MSG_TYPE_PLAYER: 801 { 802 handleMsgPlayer(msg, mapPlayers, mapGames); 803 804 break; 805 } 806 case MSG_TYPE_GAME_INFO: 807 { 808 handleMsgGameInfo(msg, mapPlayers, mapGames); 865 809 866 810 break; … … 881 825 switch(msg.type) 882 826 { 883 case MSG_TYPE_GAME_INFO:884 {885 cout << "Received a GAME_INFO message" << endl;886 887 string gameName(msg.buffer+4);888 int numPlayers;889 890 memcpy(&numPlayers, msg.buffer, 4);891 892 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;893 894 if (numPlayers > 0)895 mapGames[gameName] = numPlayers;896 else897 mapGames.erase(gameName);898 899 break;900 }901 case MSG_TYPE_SCORE:902 {903 cout << "Received SCORE message!" << endl;904 905 int blueScore;906 memcpy(&blueScore, msg.buffer, 4);907 cout << "blue score: " << blueScore << endl;908 game->setBlueScore(blueScore);909 910 int redScore;911 memcpy(&redScore, msg.buffer+4, 4);912 cout << "red score: " << redScore << endl;913 game->setRedScore(redScore);914 915 cout << "Processed SCORE message!" << endl;916 917 break;918 }919 827 case MSG_TYPE_FINISH_GAME: 920 828 { … … 929 837 string gameName(msg.buffer+12); 930 838 931 cout << "winner: " << winner << endl;932 cout << "blueScore: " << blueScore << endl;933 cout << "redScore: " << redScore << endl;934 cout << "gameName: " << gameName << endl;935 936 839 gameSummary = new GameSummary(gameName, winner, blueScore, redScore); 937 840 … … 940 843 state = STATE_LOBBY; 941 844 wndCurrent = wndGameSummary; 942 943 944 cout << "winner from obj: " << gameSummary->getWinner() << endl;945 cout << "blueScore from obj: " << gameSummary->getBlueScore() << endl;946 cout << "redScore from obj: " << gameSummary->getRedScore() << endl;947 cout << "gameName from obj: " << gameSummary->getName() << endl;948 break;949 }950 case MSG_TYPE_PLAYER:951 {952 cout << "Received MSG_TYPE_PLAYER" << endl;953 954 Player p("", "");955 p.deserialize(msg.buffer);956 p.timeLastUpdated = getCurrentMillis();957 p.isChasing = false;958 if (p.health <= 0)959 p.isDead = true;960 else961 p.isDead = false;962 963 if (mapPlayers.find(p.getId()) != mapPlayers.end())964 *(mapPlayers[p.getId()]) = p;965 else966 mapPlayers[p.getId()] = new Player(p);967 845 968 846 break; … … 1121 999 break; 1122 1000 } 1001 case MSG_TYPE_PLAYER: 1002 { 1003 handleMsgPlayer(msg, mapPlayers, mapGames); 1004 1005 break; 1006 } 1007 case MSG_TYPE_GAME_INFO: 1008 { 1009 handleMsgGameInfo(msg, mapPlayers, mapGames); 1010 1011 break; 1012 } 1123 1013 default: 1124 1014 { … … 1212 1102 } 1213 1103 } 1104 } 1105 1106 // message handling functions 1107 1108 void handleMsgPlayer(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames) { 1109 cout << "Received MSG_TYPE_PLAYER" << endl; 1110 1111 Player p("", ""); 1112 p.deserialize(msg.buffer); 1113 p.timeLastUpdated = getCurrentMillis(); 1114 p.isChasing = false; 1115 if (p.health <= 0) 1116 p.isDead = true; 1117 else 1118 p.isDead = false; 1119 1120 if (mapPlayers.find(p.getId()) != mapPlayers.end()) 1121 *(mapPlayers[p.getId()]) = p; 1122 else 1123 mapPlayers[p.getId()] = new Player(p); 1124 } 1125 1126 void handleMsgGameInfo(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames) { 1127 cout << "Received a GAME_INFO message" << endl; 1128 1129 string gameName(msg.buffer+4); 1130 int numPlayers; 1131 1132 memcpy(&numPlayers, msg.buffer, 4); 1133 1134 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl; 1135 1136 if (numPlayers > 0) 1137 mapGames[gameName] = numPlayers; 1138 else 1139 mapGames.erase(gameName); 1214 1140 } 1215 1141
Note:
See TracChangeset
for help on using the changeset viewer.