diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index 182690ffd..4e642cef0 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -56,6 +56,10 @@ MenuStateConnectedGame::MenuStateConnectedGame(Program *program, MainMenu *mainM switchSetupRequestFlagType |= ssrft_NetworkPlayerName; updateDataSynchDetailText = false; + needToBroadcastServerSettings=false; + broadcastServerSettingsDelayTimer=0; + lastGameSettingsReceivedCount=0; + currentTechName_factionPreview=""; currentFactionName_factionPreview=""; @@ -936,31 +940,42 @@ bool MenuStateConnectedGame::isMasterserverAdmin() { return result; } -void MenuStateConnectedGame::broadCastGameSettingsToMasterserver() { - NetworkManager &networkManager= NetworkManager::getInstance(); - ClientInterface *clientInterface = networkManager.getClientInterface(); - - for(int i = 0; i < mapInfo.players; ++i) { - if(listBoxControls[i].getSelectedItemIndex() == ctNetworkUnassigned) { - listBoxControls[i].setSelectedItemIndex(ctNetwork); - } - } - for(int i = mapInfo.players; i < GameConstants::maxPlayers; ++i) { - if(listBoxControls[i].getSelectedItemIndex() == ctNetwork) { - listBoxControls[i].setSelectedItemIndex(ctNetworkUnassigned); - } +void MenuStateConnectedGame::broadCastGameSettingsToMasterserver(bool forceNow) { + if(isMasterserverAdmin() == false) { + return; } - GameSettings gameSettings = *clientInterface->getGameSettings(); - loadGameSettings(&gameSettings); + if(forceNow == true || + (needToBroadcastServerSettings == true && difftime(time(NULL),broadcastServerSettingsDelayTimer) >= 2)) { + //printf("In [%s:%s] Line: %d forceNow = %d broadcastServerSettingsDelayTimer = %lu, now =%lu\n",__FILE__,__FUNCTION__,__LINE__,forceNow,broadcastServerSettingsDelayTimer,time(NULL)); - //printf("broadcast settings:\n%s\n",gameSettings.toString().c_str()); + needToBroadcastServerSettings = false; + broadcastServerSettingsDelayTimer = time(NULL); - //printf("Client sending map [%s] admin key [%d]\n",gameSettings.getMap().c_str(),gameSettings.getMasterserver_admin()); + NetworkManager &networkManager= NetworkManager::getInstance(); + ClientInterface *clientInterface = networkManager.getClientInterface(); - //clientInterface->setGameSettings(&gameSettings); - clientInterface->broadcastGameSetup(&gameSettings); + for(int i = 0; i < mapInfo.players; ++i) { + if(listBoxControls[i].getSelectedItemIndex() == ctNetworkUnassigned) { + listBoxControls[i].setSelectedItemIndex(ctNetwork); + } + } + for(int i = mapInfo.players; i < GameConstants::maxPlayers; ++i) { + if(listBoxControls[i].getSelectedItemIndex() == ctNetwork) { + listBoxControls[i].setSelectedItemIndex(ctNetworkUnassigned); + } + } + GameSettings gameSettings = *clientInterface->getGameSettings(); + loadGameSettings(&gameSettings); + + //printf("broadcast settings:\n%s\n",gameSettings.toString().c_str()); + + //printf("Client sending map [%s] admin key [%d]\n",gameSettings.getMap().c_str(),gameSettings.getMasterserver_admin()); + + //clientInterface->setGameSettings(&gameSettings); + clientInterface->broadcastGameSetup(&gameSettings); + } } void MenuStateConnectedGame::updateResourceMultiplier(const int index) { @@ -1030,7 +1045,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt // mapPublishingDelayTimer=time(NULL); //} - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } else if(listBoxFogOfWar.mouseClick(x, y)) { //MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL),string(__FILE__) + "_" + intToStr(__LINE__)); @@ -1045,7 +1062,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt // lastSetChangedGameSettings = time(NULL); // } - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } else if(listBoxAllowObservers.mouseClick(x, y)) { // MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL),string(__FILE__) + "_" + intToStr(__LINE__)); @@ -1061,7 +1080,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt // lastSetChangedGameSettings = time(NULL); // } - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } else if(listBoxEnableObserverMode.mouseClick(x, y)) { //MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL),string(__FILE__) + "_" + intToStr(__LINE__)); @@ -1076,7 +1097,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt // lastSetChangedGameSettings = time(NULL); // } - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } else if (listBoxEnableSwitchTeamMode.mouseClick(x, y)) { //MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL),string(__FILE__) + "_" + intToStr(__LINE__)); @@ -1091,7 +1114,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt // lastSetChangedGameSettings = time(NULL); // } - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } else if(listBoxAISwitchTeamAcceptPercent.getEnabled() && listBoxAISwitchTeamAcceptPercent.mouseClick(x, y)) { //MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL),string(__FILE__) + "_" + intToStr(__LINE__)); @@ -1106,7 +1131,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt // lastSetChangedGameSettings = time(NULL); // } - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } // else if (listBoxAdvanced.getSelectedItemIndex() == 1 && listBoxPathFinderType.mouseClick(x, y)) { // MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL),string(__FILE__) + "_" + intToStr(__LINE__)); @@ -1138,7 +1165,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt // mapPublishingDelayTimer=time(NULL); // } - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } // else if(listBoxMapFilter.mouseClick(x, y)){ // MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL),string(__FILE__) + "_" + intToStr(__LINE__)); @@ -1176,7 +1205,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt // lastSetChangedGameSettings = time(NULL); //} - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } // else if(listBoxPublishServer.mouseClick(x, y) && listBoxPublishServer.getEditable()) { // MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL),string(__FILE__) + "_" + intToStr(__LINE__)); @@ -1200,7 +1231,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt soundRenderer.playFx(coreData.getClickSoundC()); - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } else { NetworkManager &networkManager= NetworkManager::getInstance(); @@ -1212,7 +1245,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt //if (listBoxAdvanced.getSelectedItemIndex() == 1) { // set multiplier if(listBoxRMultiplier[i].mouseClick(x, y)) { - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } //} @@ -1273,7 +1308,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt // } updateResourceMultiplier(i); - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } // else if(buttonClearBlockedPlayers.mouseClick(x, y)) { // ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface(); @@ -1333,7 +1370,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt // lastSetChangedGameSettings = time(NULL); // } - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } else if(clientInterface->getGameSettings()->getStartLocationIndex(clientInterface->getGameSettings()->getThisFactionIndex()) != i && listBoxTeams[i].mouseClick(x, y)) { @@ -1356,7 +1395,9 @@ void MenuStateConnectedGame::mouseClickAdmin(int x, int y, MouseButton mouseButt // lastSetChangedGameSettings = time(NULL);; // } - broadCastGameSettingsToMasterserver(); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings=true; + broadcastServerSettingsDelayTimer=time(NULL); } // else if(labelPlayerNames[i].mouseClick(x, y)) { // SetActivePlayerNameEditor(); @@ -2039,6 +2080,7 @@ void MenuStateConnectedGame::update() { if(clientInterface != NULL && clientInterface->isConnected()) { //printf("#2 admin key [%d] client key [%d]\n",settings->getMasterserver_admin(),clientInterface->getSessionKey()); + broadCastGameSettingsToMasterserver(false); listBoxMap.setEditable(isMasterserverAdmin()); buttonPlayNow.setVisible(isMasterserverAdmin()); @@ -2553,7 +2595,11 @@ void MenuStateConnectedGame::update() { try { //bool mustSwitchPlayerName = false; - if(clientInterface->getGameSettingsReceived()) { + if(clientInterface->getGameSettingsReceived() && + lastGameSettingsReceivedCount != clientInterface->getGameSettingsReceivedCount()) { + broadCastGameSettingsToMasterserver(needToBroadcastServerSettings); + + lastGameSettingsReceivedCount = clientInterface->getGameSettingsReceivedCount(); updateDataSynchDetailText = true; bool errorOnMissingData = (clientInterface->getAllowGameDataSynchCheck() == false); vector maps,tilesets,techtree; diff --git a/source/glest_game/menu/menu_state_connected_game.h b/source/glest_game/menu/menu_state_connected_game.h index 9d663f3e9..70ec336cd 100644 --- a/source/glest_game/menu/menu_state_connected_game.h +++ b/source/glest_game/menu/menu_state_connected_game.h @@ -180,6 +180,10 @@ private: GraphicButton buttonPlayNow; + bool needToBroadcastServerSettings; + time_t broadcastServerSettingsDelayTimer; + int lastGameSettingsReceivedCount; + public: MenuStateConnectedGame(Program *program, MainMenu *mainMenu, JoinMenu joinMenuInfo=jmSimple, bool openNetworkSlots= false); @@ -221,7 +225,7 @@ private: void reloadFactions(bool keepExistingSelectedItem); void PlayNow(bool saveGame); bool isMasterserverAdmin(); - void broadCastGameSettingsToMasterserver(); + void broadCastGameSettingsToMasterserver(bool forceNow); void updateResourceMultiplier(const int index); }; diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index 512b5c0e0..bc1d61bc3 100644 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -53,7 +53,7 @@ ClientInterface::ClientInterface() : GameNetworkInterface() { launchGame= false; introDone= false; playerIndex= -1; - gameSettingsReceived=false; + setGameSettingsReceived(false); gotIntro = false; lastNetworkCommandListSendTime = 0; currentFrameCount = 0; @@ -485,7 +485,7 @@ void ClientInterface::updateLobby() { launchGame= true; } else if(networkMessageLaunch.getMessageType() == nmtBroadCastSetup) { - gameSettingsReceived=true; + setGameSettingsReceived(true); } } } @@ -1272,5 +1272,10 @@ void ClientInterface::broadcastGameStart(const GameSettings *gameSettings) { sendMessage(&networkMessageLaunch); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); } +void ClientInterface::setGameSettingsReceived(bool value) { + //printf("In [%s:%s] Line: %d gameSettingsReceived = %d value = %d, gameSettingsReceivedCount = %d\n",__FILE__,__FUNCTION__,__LINE__,gameSettingsReceived,value,gameSettingsReceivedCount); + gameSettingsReceived = value; + gameSettingsReceivedCount++; +} }}//end namespace diff --git a/source/glest_game/network/client_interface.h b/source/glest_game/network/client_interface.h index 3eddbf350..c39ee9fd3 100644 --- a/source/glest_game/network/client_interface.h +++ b/source/glest_game/network/client_interface.h @@ -42,6 +42,7 @@ private: bool launchGame; int playerIndex; bool gameSettingsReceived; + int gameSettingsReceivedCount; time_t connectedTime; bool gotIntro; @@ -83,7 +84,10 @@ public: bool getLaunchGame() const {return launchGame;} bool getIntroDone() const {return introDone;} bool getGameSettingsReceived() const {return gameSettingsReceived;} - void setGameSettingsReceived(bool value) {gameSettingsReceived=value;} + void setGameSettingsReceived(bool value); + + int getGameSettingsReceivedCount() const { return gameSettingsReceivedCount; } + int getPlayerIndex() const {return playerIndex;} //const GameSettings *getGameSettings() {return &gameSettings;}