From 68ef7471d5346a1cdd6f6c630d52e4afafde14da Mon Sep 17 00:00:00 2001 From: titiger Date: Wed, 24 Jun 2015 12:38:05 +0200 Subject: [PATCH] reload defaults in headless mode if last player disconnects If last player disconnects form a headless server the headless loads his defaults for network games. --- .../menu/menu_state_custom_game.cpp | 34 ++++++++++++++++++- .../glest_game/menu/menu_state_custom_game.h | 3 ++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 9e3caaa1..97755589 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -110,6 +110,7 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, hasCheckedForUPNP = false; needToPublishDelayed=false; mapPublishingDelayTimer=time(NULL); + headlessHasConnectedPlayer=false; lastCheckedCRCTilesetName = ""; lastCheckedCRCTechtreeName = ""; @@ -1079,7 +1080,6 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton) { string advanceToItemStartingWith = ""; if(::Shared::Platform::Window::isKeyStateModPressed(KMOD_SHIFT) == true) { const wchar_t lastKey = ::Shared::Platform::Window::extractLastKeyPressed(); -// xxx: // string hehe=lastKey; // printf("lastKey = %d [%c] '%s'\n",lastKey,lastKey,hehe); advanceToItemStartingWith = lastKey; @@ -2885,6 +2885,10 @@ void MenuStateCustomGame::update() { //} } + if(this->headlessServerMode == true) { + lastPlayerDisconnected(); + } + //call the chat manager chatManager.updateNetwork(); @@ -4171,6 +4175,34 @@ void MenuStateCustomGame::setupUIFromGameSettings(const GameSettings &gameSettin } // ============ PRIVATE =========================== +void MenuStateCustomGame::lastPlayerDisconnected() { + // this is for headless mode only! + // if last player disconnects we load the network defaults. + if(this->headlessServerMode == false) { + return; + } + + ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface(); + bool foundConnectedPlayer=false; + for(int i = 0; i < GameConstants::maxPlayers; ++i) { + if(serverInterface->getSlot(i,true) != NULL && + (listBoxControls[i].getSelectedItemIndex() == ctNetwork || + listBoxControls[i].getSelectedItemIndex() == ctNetworkUnassigned)) { + if(serverInterface->getSlot(i,true)->isConnected() == true) { + foundConnectedPlayer=true; + } + } + } + + if(!foundConnectedPlayer && headlessHasConnectedPlayer==true ){ + // load defaults + string data_path = getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + if(fileExists(data_path + DEFAULT_NETWORKGAME_FILENAME) == true) + loadGameSettings(data_path + DEFAULT_NETWORKGAME_FILENAME); + } + headlessHasConnectedPlayer=foundConnectedPlayer; +} + bool MenuStateCustomGame::hasNetworkGameSettings() { bool hasNetworkSlot = false; diff --git a/source/glest_game/menu/menu_state_custom_game.h b/source/glest_game/menu/menu_state_custom_game.h index 54b6f4db..76a3ad6d 100644 --- a/source/glest_game/menu/menu_state_custom_game.h +++ b/source/glest_game/menu/menu_state_custom_game.h @@ -145,6 +145,8 @@ private: time_t mapPublishingDelayTimer; bool needToPublishDelayed; + bool headlessHasConnectedPlayer; + bool needToRepublishToMasterserver; bool needToBroadcastServerSettings; std::map publishToServerInfo; @@ -251,6 +253,7 @@ public: virtual bool isVideoPlaying(); private: + void lastPlayerDisconnected(); bool hasNetworkGameSettings(); void loadGameSettings(GameSettings *gameSettings, bool forceCloseUnusedSlots=false); void loadMapInfo(string file, MapInfo *mapInfo,bool loadMapPreview);