From 0509b5d6435dd84a7fd6a275fd7f8fa4851f200c Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Mon, 23 Aug 2010 04:33:21 +0000 Subject: [PATCH] - bugfixes for playername change in lobby screen --- .../menu/menu_state_connected_game.cpp | 101 ++++----- .../menu/menu_state_connected_game.h | 3 + .../menu/menu_state_custom_game.cpp | 205 +++++++++++------- .../glest_game/menu/menu_state_custom_game.h | 3 + .../glest_game/network/client_interface.cpp | 7 +- source/glest_game/network/client_interface.h | 4 +- source/glest_game/network/connection_slot.cpp | 17 +- source/glest_game/network/network_message.cpp | 17 +- source/glest_game/network/network_message.h | 20 +- 9 files changed, 230 insertions(+), 147 deletions(-) diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index a62724ae..23340946 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -48,6 +48,7 @@ struct FormatString { MenuStateConnectedGame::MenuStateConnectedGame(Program *program, MainMenu *mainMenu,JoinMenu joinMenuInfo, bool openNetworkSlots): MenuState(program, mainMenu, "connected-game") //← set on connected-game { + switchSetupRequestFlagType |= ssrft_NetworkPlayerName; updateDataSynchDetailText = false; activeInputLabel = NULL; lastNetworkSendPing = 0; @@ -65,6 +66,7 @@ MenuStateConnectedGame::MenuStateConnectedGame(Program *program, MainMenu *mainM Lang &lang= Lang::getInstance(); NetworkManager &networkManager= NetworkManager::getInstance(); Config &config = Config::getInstance(); + defaultPlayerName = config.getString("NetPlayerName",Socket::getHostName().c_str()); labelMapInfo.setText("?"); @@ -298,23 +300,25 @@ void MenuStateConnectedGame::mouseClick(int x, int y, MouseButton mouseButton){ if (myCurrentIndex!=-1) for(int i=0; iisConnected()){ + if(clientInterface->isConnected()) { clientInterface->setGameSettingsReceived(false); - clientInterface->sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem(),i,-1,listBoxTeams[i].getSelectedItemIndex(),getHumanPlayerName()); + clientInterface->sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem(),i,-1,listBoxTeams[i].getSelectedItemIndex(),getHumanPlayerName(),switchSetupRequestFlagType); + switchSetupRequestFlagType=ssrft_None; } break; } } - if(listBoxTeams[i].getEditable()){ - if(listBoxTeams[i].mouseClick(x, y)){ + if(listBoxTeams[i].getEditable()) { + if(listBoxTeams[i].mouseClick(x, y)) { soundRenderer.playFx(coreData.getClickSoundA()); - if(clientInterface->isConnected()){ + if(clientInterface->isConnected()) { clientInterface->setGameSettingsReceived(false); - clientInterface->sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem(),i,-1,listBoxTeams[i].getSelectedItemIndex(),getHumanPlayerName()); + clientInterface->sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem(),i,-1,listBoxTeams[i].getSelectedItemIndex(),getHumanPlayerName(),switchSetupRequestFlagType); + switchSetupRequestFlagType=ssrft_None; } break; } @@ -327,7 +331,8 @@ void MenuStateConnectedGame::mouseClick(int x, int y, MouseButton mouseButton){ clientInterface->setGameSettingsReceived(false); settingsReceivedFromServer=false; SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] sending a switchSlot request from %d to %d\n",__FILE__,__FUNCTION__,__LINE__,clientInterface->getGameSettings()->getThisFactionIndex(),i); - clientInterface->sendSwitchSetupRequest(listBoxFactions[myCurrentIndex].getSelectedItem(),myCurrentIndex,i,listBoxTeams[myCurrentIndex].getSelectedItemIndex(),getHumanPlayerName()); + clientInterface->sendSwitchSetupRequest(listBoxFactions[myCurrentIndex].getSelectedItem(),myCurrentIndex,i,listBoxTeams[myCurrentIndex].getSelectedItemIndex(),getHumanPlayerName(),switchSetupRequestFlagType); + switchSetupRequestFlagType=ssrft_None; break; } } @@ -474,8 +479,7 @@ void MenuStateConnectedGame::render(){ } } -void MenuStateConnectedGame::update() -{ +void MenuStateConnectedGame::update() { ClientInterface* clientInterface= NetworkManager::getInstance().getClientInterface(); Lang &lang= Lang::getInstance(); @@ -576,8 +580,7 @@ void MenuStateConnectedGame::update() // label = label + " FogOfWar == false"; //} } - else if(clientInterface->getAllowGameDataSynchCheck() == true) - { + else if(clientInterface->getAllowGameDataSynchCheck() == true) { label += " - data synch is ok"; } @@ -591,12 +594,10 @@ void MenuStateConnectedGame::update() //labelStatus.setText(szBuf); labelStatus.setText(label); } - else - { + else { string label = lang.get("ConnectedToServer"); - if(!clientInterface->getServerName().empty()) - { + if(!clientInterface->getServerName().empty()) { label = label + " " + clientInterface->getServerName(); } @@ -651,7 +652,7 @@ void MenuStateConnectedGame::update() //process network messages if(clientInterface->isConnected()) { bool mustSwitchPlayerName = false; - if(clientInterface->getGameSettingsReceived()){ + if(clientInterface->getGameSettingsReceived()) { updateDataSynchDetailText = true; bool errorOnMissingData = (clientInterface->getAllowGameDataSynchCheck() == false); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -776,29 +777,23 @@ void MenuStateConnectedGame::update() } } - if(mustSwitchPlayerName == true || - (needToSetChangedGameSettings == true && - difftime(time(NULL),lastSetChangedGameSettings) >= 2)) { - needToSetChangedGameSettings = false; - lastSetChangedGameSettings = time(NULL); - - ClientInterface* clientInterface= NetworkManager::getInstance().getClientInterface(); - if(clientInterface->isConnected()){ - for(int i=0; igetGameSettings() != NULL && - i == clientInterface->getGameSettings()->getThisFactionIndex()) { - clientInterface->setGameSettingsReceived(false); - clientInterface->sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem(),i,-1,listBoxTeams[i].getSelectedItemIndex(),getHumanPlayerName()); - } - } - } - } - //update lobby clientInterface->updateLobby(); clientInterface= NetworkManager::getInstance().getClientInterface(); if(clientInterface != NULL && clientInterface->isConnected()) { + //if(mustSwitchPlayerName == true || + // (needToSetChangedGameSettings == true && + // difftime(time(NULL),lastSetChangedGameSettings) >= 2)) { + if(clientInterface->getIntroDone() == true && + (switchSetupRequestFlagType & ssrft_NetworkPlayerName) == ssrft_NetworkPlayerName) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //needToSetChangedGameSettings = false; + //lastSetChangedGameSettings = time(NULL); + clientInterface->sendSwitchSetupRequest("",clientInterface->getPlayerIndex(),-1,-1,getHumanPlayerName(),switchSetupRequestFlagType); + switchSetupRequestFlagType=ssrft_None; + } + //call the chat manager chatManager.updateNetwork(); @@ -926,9 +921,7 @@ bool MenuStateConnectedGame::hasNetworkGameSettings() return hasNetworkSlot; } - - -void MenuStateConnectedGame::reloadFactions(){ +void MenuStateConnectedGame::reloadFactions() { vector results; @@ -980,6 +973,7 @@ void MenuStateConnectedGame::keyDown(char key) { } activeInputLabel->setText(text); + switchSetupRequestFlagType |= ssrft_NetworkPlayerName; needToSetChangedGameSettings = true; lastSetChangedGameSettings = time(NULL); } @@ -1002,11 +996,12 @@ void MenuStateConnectedGame::keyPress(char c) { if(&labelPlayerNames[i] == activeInputLabel){ if((c>='0' && c<='9')||(c>='a' && c<='z')||(c>='A' && c<='Z')|| (c=='-')||(c=='(')||(c==')')){ - if(activeInputLabel->getText().size()getText().size()getText(); text.insert(text.end()-1, c); activeInputLabel->setText(text); + switchSetupRequestFlagType |= ssrft_NetworkPlayerName; needToSetChangedGameSettings = true; lastSetChangedGameSettings = time(NULL); } @@ -1032,24 +1027,21 @@ void MenuStateConnectedGame::keyUp(char key) { } } -void MenuStateConnectedGame::setActiveInputLabel(GraphicLabel *newLable) -{ - if(newLable!=NULL){ +void MenuStateConnectedGame::setActiveInputLabel(GraphicLabel *newLable) { + if(newLable!=NULL) { string text= newLable->getText(); size_t found; found=text.find_last_of("_"); - if (found==string::npos) - { + if (found==string::npos) { text=text+"_"; } newLable->setText(text); } - if(activeInputLabel!=NULL && !activeInputLabel->getText().empty()){ + if(activeInputLabel!=NULL && !activeInputLabel->getText().empty()) { string text= activeInputLabel->getText(); size_t found; found=text.find_last_of("_"); - if (found!=string::npos) - { + if (found!=string::npos) { text=text.substr(0,found); } activeInputLabel->setText(text); @@ -1058,15 +1050,24 @@ void MenuStateConnectedGame::setActiveInputLabel(GraphicLabel *newLable) } string MenuStateConnectedGame::getHumanPlayerName() { - string result = Config::getInstance().getString("NetPlayerName",Socket::getHostName().c_str()); + string result = defaultPlayerName; NetworkManager &networkManager= NetworkManager::getInstance(); ClientInterface* clientInterface= networkManager.getClientInterface(); for(int j=0; jgetGameSettings() != NULL && j == clientInterface->getGameSettings()->getThisFactionIndex() && labelPlayerNames[j].getText() != "") { result = labelPlayerNames[j].getText(); + + if(activeInputLabel != NULL) { + size_t found = result.find_last_of("_"); + if (found != string::npos) { + result = result.substr(0,found); + } + } + break; } } diff --git a/source/glest_game/menu/menu_state_connected_game.h b/source/glest_game/menu/menu_state_connected_game.h index d81ae6a1..c4beaa2e 100644 --- a/source/glest_game/menu/menu_state_connected_game.h +++ b/source/glest_game/menu/menu_state_connected_game.h @@ -97,6 +97,9 @@ private: string lastTileDataSynchError; string lastTechtreeDataSynchError; + int8 switchSetupRequestFlagType; + string defaultPlayerName; + public: MenuStateConnectedGame(Program *program, MainMenu *mainMenu, JoinMenu joinMenuInfo=jmSimple, bool openNetworkSlots= false); diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 4f77ec7a..7b1712c9 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -58,6 +58,7 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b Lang &lang= Lang::getInstance(); NetworkManager &networkManager= NetworkManager::getInstance(); Config &config = Config::getInstance(); + defaultPlayerName = config.getString("NetPlayerName",Socket::getHostName().c_str()); showFullConsole=false; @@ -645,39 +646,54 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ for(int i=0; i(listBoxControls[j].getSelectedItemIndex()); - if(ct==ctHuman){ - if(humanIndex1==-1){ + if(ct == ctHuman) { + if(humanIndex1 == -1) { humanIndex1= j; } - else{ + else { humanIndex2= j; } } } - //no human - if(humanIndex1==-1 && humanIndex2==-1){ - listBoxControls[i].setSelectedItemIndex(ctHuman); - } + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] humanIndex1 = %d, humanIndex2 = %d\n",__FILE__,__FUNCTION__,__LINE__,humanIndex1,humanIndex2); + //no human + if(humanIndex1 == -1 && humanIndex2 == -1) { + listBoxControls[i].setSelectedItemIndex(ctHuman); + //labelPlayerNames[i].setText(""); + //labelPlayerNames[i].setText(getHumanPlayerName()); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] i = %d, labelPlayerNames[i].getText() [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,labelPlayerNames[i].getText().c_str()); + } //2 humans - if(humanIndex1!=-1 && humanIndex2!=-1){ - listBoxControls[humanIndex1==i? humanIndex2: humanIndex1].setSelectedItemIndex(ctClosed); + else if(humanIndex1 != -1 && humanIndex2 != -1) { + int closeSlotIndex = (humanIndex1 == i ? humanIndex2: humanIndex1); + int humanSlotIndex = (closeSlotIndex == humanIndex1 ? humanIndex2 : humanIndex1); + + string origPlayName = labelPlayerNames[closeSlotIndex].getText(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] closeSlotIndex = %d, origPlayName [%s]\n",__FILE__,__FUNCTION__,__LINE__,closeSlotIndex,origPlayName.c_str()); + + listBoxControls[closeSlotIndex].setSelectedItemIndex(ctClosed); + //labelPlayerNames[closeSlotIndex].setText(""); + + labelPlayerNames[humanSlotIndex].setText((origPlayName != "" ? origPlayName : getHumanPlayerName())); } updateNetworkSlots(); needToRepublishToMasterserver = true; - if(hasNetworkGameSettings() == true) - { + if(hasNetworkGameSettings() == true) { needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time(NULL);; + lastSetChangedGameSettings = time(NULL); } } else if(listBoxFactions[i].mouseClick(x, y)){ @@ -686,7 +702,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ if(hasNetworkGameSettings() == true) { needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time(NULL);; + lastSetChangedGameSettings = time(NULL); } } else if(listBoxTeams[i].mouseClick(x, y)) @@ -881,49 +897,66 @@ void MenuStateCustomGame::update() { //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); // handle setting changes from clients - SwitchSetupRequest** switchSetupRequests=serverInterface->getSwitchSetupRequests(); - for(int i= 0; igetSwitchSetupRequests(); + for(int i= 0; i< mapInfo.players; ++i) { if(switchSetupRequests[i] != NULL) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] switchSetupRequests[i]->getSwitchFlags() = %d\n",__FILE__,__FUNCTION__,__LINE__,switchSetupRequests[i]->getSwitchFlags()); if(listBoxControls[i].getSelectedItemIndex() == ctNetwork) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //printf("switchSetupRequests[i]->getSelectedFactionName()=%s\n",switchSetupRequests[i]->getSelectedFactionName().c_str()); //printf("switchSetupRequests[i]->getToTeam()=%d\n",switchSetupRequests[i]->getToTeam()); - if(switchSetupRequests[i]->getToFactionIndex()!=-1) { - int k=switchSetupRequests[i]->getToFactionIndex(); - labelPlayerNames[k].setText(switchSetupRequests[i]->getNetworkPlayerName()); + if(switchSetupRequests[i]->getToFactionIndex() != -1) { + int newFactionIdx = switchSetupRequests[i]->getToFactionIndex(); + /* + if(switchSetupRequests[i]->getNetworkPlayerName() != GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + labelPlayerNames[k].setText(switchSetupRequests[i]->getNetworkPlayerName()); + } + else { + labelPlayerNames[k].setText(""); + } + */ //printf("switchSlot request from %d to %d\n",switchSetupRequests[i]->getCurrentFactionIndex(),switchSetupRequests[i]->getToFactionIndex()); - if(serverInterface->switchSlot(switchSetupRequests[i]->getCurrentFactionIndex(),switchSetupRequests[i]->getToFactionIndex())){ + int switchFactionIdx = switchSetupRequests[i]->getCurrentFactionIndex(); + if(serverInterface->switchSlot(switchFactionIdx,newFactionIdx)) { try { - if(switchSetupRequests[i]->getSelectedFactionName()!=""){ - listBoxFactions[k].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName()); + if(switchSetupRequests[i]->getSelectedFactionName() != ""){ + listBoxFactions[newFactionIdx].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName()); } - if(switchSetupRequests[i]->getToTeam()!=-1) { - listBoxTeams[k].setSelectedItemIndex(switchSetupRequests[i]->getToTeam()); + if(switchSetupRequests[i]->getToTeam() != -1) { + listBoxTeams[newFactionIdx].setSelectedItemIndex(switchSetupRequests[i]->getToTeam()); + } + if(switchSetupRequests[i]->getNetworkPlayerName() != "") { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] i = %d, labelPlayerNames[newFactionIdx].getText() [%s] switchSetupRequests[i]->getNetworkPlayerName() [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,labelPlayerNames[newFactionIdx].getText().c_str(),switchSetupRequests[i]->getNetworkPlayerName().c_str()); + labelPlayerNames[newFactionIdx].setText(switchSetupRequests[i]->getNetworkPlayerName()); } - - labelPlayerNames[k].setText(switchSetupRequests[i]->getNetworkPlayerName()); } catch(const runtime_error &e) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] caught exception error = [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what()); } } } - else - { + else { try { - - labelPlayerNames[i].setText(switchSetupRequests[i]->getNetworkPlayerName()); - - if(switchSetupRequests[i]->getSelectedFactionName()!=""){ + if(switchSetupRequests[i]->getSelectedFactionName() != "") { listBoxFactions[i].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName()); } - if(switchSetupRequests[i]->getToTeam()!=-1) { + if(switchSetupRequests[i]->getToTeam() != -1) { listBoxTeams[i].setSelectedItemIndex(switchSetupRequests[i]->getToTeam()); } + + if((switchSetupRequests[i]->getSwitchFlags() & ssrft_NetworkPlayerName) == ssrft_NetworkPlayerName) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, switchSetupRequests[i]->getSwitchFlags() = %d, switchSetupRequests[i]->getNetworkPlayerName() [%s], labelPlayerNames[i].getText() [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,switchSetupRequests[i]->getSwitchFlags(),switchSetupRequests[i]->getNetworkPlayerName().c_str(),labelPlayerNames[i].getText().c_str()); + if(switchSetupRequests[i]->getNetworkPlayerName() != GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + labelPlayerNames[i].setText(switchSetupRequests[i]->getNetworkPlayerName()); + } + else { + labelPlayerNames[i].setText(""); + } + //switchSetupRequests[i]->clearSwitchFlag(ssrft_NetworkPlayerName); + } } catch(const runtime_error &e) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] caught exception error = [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what()); @@ -938,12 +971,10 @@ void MenuStateCustomGame::update() { //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] mapInfo.players = %d\n",__FILE__,__FUNCTION__,__LINE__,mapInfo.players); - for(int i= 0; igetSlot(i); @@ -1371,8 +1402,11 @@ void MenuStateCustomGame::loadGameSettings(GameSettings *gameSettings) { gameSettings->setFactionControl(slotIndex, ct); if(ct == ctHuman) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, slotIndex = %d, getHumanPlayerName(i) [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,slotIndex,getHumanPlayerName(i).c_str()); + gameSettings->setThisFactionIndex(slotIndex); - gameSettings->setNetworkPlayerName(slotIndex, getHumanPlayerName(slotIndex)); + gameSettings->setNetworkPlayerName(slotIndex, getHumanPlayerName(i)); + labelPlayerNames[i].setText(getHumanPlayerName(i)); } gameSettings->setTeam(slotIndex, listBoxTeams[i].getSelectedItemIndex()); gameSettings->setStartLocationIndex(slotIndex, i); @@ -1381,22 +1415,32 @@ void MenuStateCustomGame::loadGameSettings(GameSettings *gameSettings) { if(listBoxControls[i].getSelectedItemIndex() == ctNetwork) { ConnectionSlot* connectionSlot= serverInterface->getSlot(i); if(connectionSlot != NULL && connectionSlot->isConnected()) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, connectionSlot->getName() [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,connectionSlot->getName().c_str()); + gameSettings->setNetworkPlayerName(slotIndex, connectionSlot->getName()); - labelPlayerNames[slotIndex].setText(connectionSlot->getName()); + labelPlayerNames[i].setText(connectionSlot->getName()); } else { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, playername unconnected\n",__FILE__,__FUNCTION__,__LINE__,i); + gameSettings->setNetworkPlayerName(slotIndex, GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME); - labelPlayerNames[slotIndex].setText(""); + labelPlayerNames[i].setText(""); } } else if (listBoxControls[i].getSelectedItemIndex() != ctHuman) { AIPlayerCount++; + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, playername is AI (blank)\n",__FILE__,__FUNCTION__,__LINE__,i); + gameSettings->setNetworkPlayerName(slotIndex, string("AI") + intToStr(AIPlayerCount)); - labelPlayerNames[slotIndex].setText(string("AI") + intToStr(AIPlayerCount)); + labelPlayerNames[i].setText(""); } factionCount++; } + else { + //gameSettings->setNetworkPlayerName(""); + labelPlayerNames[i].setText(""); + } } // Next save closed slots int closedCount = 0; @@ -1521,7 +1565,7 @@ GameSettings MenuStateCustomGame::loadGameSettingsFromFile(std::string fileName) gameSettings.setTeam(i,properties.getInt(string("FactionTeamForIndex") + intToStr(i),"0") ); gameSettings.setStartLocationIndex(i,properties.getInt(string("FactionStartLocationForIndex") + intToStr(i),intToStr(i).c_str()) ); gameSettings.setFactionTypeName(i,properties.getString(string("FactionTypeNameForIndex") + intToStr(i),"?") ); - gameSettings.setNetworkPlayerName(i,properties.getString(string("FactionPlayerNameForIndex") + intToStr(i),"?") ); + gameSettings.setNetworkPlayerName(i,properties.getString(string("FactionPlayerNameForIndex") + intToStr(i),"") ); } SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); @@ -1583,6 +1627,10 @@ GameSettings MenuStateCustomGame::loadGameSettingsFromFile(std::string fileName) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] factionName = [%s]\n",__FILE__,__FUNCTION__,__LINE__,factionName.c_str()); listBoxFactions[i].setSelectedItem(factionName); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, gameSettings.getNetworkPlayerName(i) [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,gameSettings.getNetworkPlayerName(i).c_str()); + + labelPlayerNames[i].setText(gameSettings.getNetworkPlayerName(i)); } updateControlers(); @@ -1609,8 +1657,7 @@ GameSettings MenuStateCustomGame::loadGameSettingsFromFile(std::string fileName) // ============ PRIVATE =========================== -bool MenuStateCustomGame::hasNetworkGameSettings() -{ +bool MenuStateCustomGame::hasNetworkGameSettings() { bool hasNetworkSlot = false; try { @@ -1752,15 +1799,13 @@ void MenuStateCustomGame::closeUnusedSlots(){ } } -void MenuStateCustomGame::updateNetworkSlots() -{ +void MenuStateCustomGame::updateNetworkSlots() { try { ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface(); - for(int i= 0; igetSlot(i) == NULL && listBoxControls[i].getSelectedItemIndex() == ctNetwork) - { + for(int i= 0; igetSlot(i) == NULL && + listBoxControls[i].getSelectedItemIndex() == ctNetwork) { try { serverInterface->addSlot(i); } @@ -1777,11 +1822,11 @@ void MenuStateCustomGame::updateNetworkSlots() } // Revert network to CPU - listBoxControls[i].setSelectedItemIndex(2); + listBoxControls[i].setSelectedItemIndex(ctCpu); } } - if(serverInterface->getSlot(i) != NULL && listBoxControls[i].getSelectedItemIndex() != ctNetwork) - { + if(serverInterface->getSlot(i) != NULL && + listBoxControls[i].getSelectedItemIndex() != ctNetwork) { serverInterface->removeSlot(i); } } @@ -1799,7 +1844,7 @@ void MenuStateCustomGame::updateNetworkSlots() void MenuStateCustomGame::keyDown(char key) { if(activeInputLabel!=NULL) { - if(key==vkBack){ + if(key==vkBack) { string text= activeInputLabel->getText(); if(text.size()>1){ text.erase(text.end()-2); @@ -1807,8 +1852,7 @@ void MenuStateCustomGame::keyDown(char key) { activeInputLabel->setText(text); MutexSafeWrapper safeMutex(&masterServerThreadAccessor); - if(hasNetworkGameSettings() == true) - { + if(hasNetworkGameSettings() == true) { needToSetChangedGameSettings = true; lastSetChangedGameSettings = time(NULL); } @@ -1817,8 +1861,8 @@ void MenuStateCustomGame::keyDown(char key) { else { //send key to the chat manager chatManager.keyDown(key); - if(!chatManager.getEditEnabled()){ - if(key=='M'){ + if(!chatManager.getEditEnabled()) { + if(key=='M') { showFullConsole= true; } } @@ -1828,18 +1872,17 @@ void MenuStateCustomGame::keyDown(char key) { void MenuStateCustomGame::keyPress(char c) { if(activeInputLabel!=NULL) { int maxTextSize= 16; - for(int i=0; i='0' && c<='9')||(c>='a' && c<='z')||(c>='A' && c<='Z')|| (c=='-')||(c=='(')||(c==')')){ - if(activeInputLabel->getText().size()getText().size()getText(); text.insert(text.end()-1, c); activeInputLabel->setText(text); MutexSafeWrapper safeMutex(&masterServerThreadAccessor); - if(hasNetworkGameSettings() == true) - { + if(hasNetworkGameSettings() == true) { needToSetChangedGameSettings = true; lastSetChangedGameSettings = time(NULL); } @@ -1881,9 +1924,8 @@ void MenuStateCustomGame::showMessageBox(const string &text, const string &heade } } -void MenuStateCustomGame::setActiveInputLabel(GraphicLabel *newLable) -{ - if(newLable!=NULL){ +void MenuStateCustomGame::setActiveInputLabel(GraphicLabel *newLable) { + if(newLable!=NULL) { string text= newLable->getText(); size_t found; found=text.find_last_of("_"); @@ -1907,17 +1949,19 @@ void MenuStateCustomGame::setActiveInputLabel(GraphicLabel *newLable) } string MenuStateCustomGame::getHumanPlayerName(int index) { - string result = Config::getInstance().getString("NetPlayerName",Socket::getHostName().c_str()); + string result = defaultPlayerName; //printf("\nIn [%s::%s Line: %d] index = %d\n",__FILE__,__FUNCTION__,__LINE__,index); //fflush(stdout); if(index < 0) { - for(int j=0; j(listBoxControls[j].getSelectedItemIndex()); - if(ct == ctHuman) { - index = j; - break; + for(int j = 0; j < GameConstants::maxPlayers; ++j) { + if(listBoxControls[j].getSelectedItemIndex() >= 0) { + ControlType ct = static_cast(listBoxControls[j].getSelectedItemIndex()); + if(ct == ctHuman) { + index = j; + break; + } } } } @@ -1925,8 +1969,17 @@ string MenuStateCustomGame::getHumanPlayerName(int index) { //printf("\nIn [%s::%s Line: %d] index = %d, labelPlayerNames[index].getText() = [%s]\n",__FILE__,__FUNCTION__,__LINE__,index,(index >= 0 ? labelPlayerNames[index].getText().c_str() : "?")); //fflush(stdout); - if(index >= 0 && labelPlayerNames[index].getText() != "") { + if(index >= 0 && index < GameConstants::maxPlayers && + labelPlayerNames[index].getText() != "" && + labelPlayerNames[index].getText() != GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { result = labelPlayerNames[index].getText(); + + if(activeInputLabel != NULL) { + size_t found = result.find_last_of("_"); + if (found != string::npos) { + result = result.substr(0,found); + } + } } return result; diff --git a/source/glest_game/menu/menu_state_custom_game.h b/source/glest_game/menu/menu_state_custom_game.h index bfe313a0..75d9041e 100644 --- a/source/glest_game/menu/menu_state_custom_game.h +++ b/source/glest_game/menu/menu_state_custom_game.h @@ -107,6 +107,9 @@ private: string lastTileDataSynchError; string lastTechtreeDataSynchError; + string defaultPlayerName; + int8 switchSetupRequestFlagType; + public: MenuStateCustomGame(Program *program, MainMenu *mainMenu ,bool openNetworkSlots= false, bool parentMenuIsMasterserver=false); ~MenuStateCustomGame(); diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index 245f99ef..329912f5 100755 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -913,11 +913,12 @@ void ClientInterface::stopServerDiscovery() { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } -void ClientInterface::sendSwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, int8 toFactionIndex,int8 toTeam, string networkPlayerName) -{ +void ClientInterface::sendSwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, + int8 toFactionIndex,int8 toTeam, string networkPlayerName, + int8 flags) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //printf("string-cuf-tof-team= %s-%d-%d-%d\n",selectedFactionName.c_str(),currentFactionIndex,toFactionIndex,toTeam); - SwitchSetupRequest message=SwitchSetupRequest(selectedFactionName, currentFactionIndex, toFactionIndex,toTeam,networkPlayerName); + SwitchSetupRequest message=SwitchSetupRequest(selectedFactionName, currentFactionIndex, toFactionIndex,toTeam,networkPlayerName, flags); sendMessage(&message); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } diff --git a/source/glest_game/network/client_interface.h b/source/glest_game/network/client_interface.h index 50bc1533..c7e6242a 100644 --- a/source/glest_game/network/client_interface.h +++ b/source/glest_game/network/client_interface.h @@ -91,7 +91,9 @@ public: void discoverServers(DiscoveredServersInterface *cb); void stopServerDiscovery(); - void sendSwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, int8 toFactionIndex, int8 toTeam,string networkPlayerName); + void sendSwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, + int8 toFactionIndex, int8 toTeam,string networkPlayerName, + int8 flags); virtual bool getConnectHasHandshaked() const { return gotIntro; } std::string getServerIpAddress(); diff --git a/source/glest_game/network/connection_slot.cpp b/source/glest_game/network/connection_slot.cpp index 0132bbe5..ad7c27da 100644 --- a/source/glest_game/network/connection_slot.cpp +++ b/source/glest_game/network/connection_slot.cpp @@ -302,7 +302,7 @@ void ConnectionSlot::update(bool checkForNewClients) { case nmtText: { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtText\n",__FILE__,__FUNCTION__); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtText gotIntro = %d\n",__FILE__,__FUNCTION__,__LINE__,gotIntro); if(gotIntro == true) { NetworkMessageText networkMessageText; @@ -320,7 +320,7 @@ void ConnectionSlot::update(bool checkForNewClients) { //command list case nmtCommandList: { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtCommandList\n",__FILE__,__FUNCTION__); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtCommandList gotIntro = %d\n",__FILE__,__FUNCTION__,__LINE__,gotIntro); //throw runtime_error("test"); @@ -571,16 +571,21 @@ void ConnectionSlot::update(bool checkForNewClients) { case nmtSwitchSetupRequest: { + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtSwitchSetupRequest gotIntro = %d\n",__FILE__,__FUNCTION__,__LINE__,gotIntro); + if(gotIntro == true) { SwitchSetupRequest switchSetupRequest; if(receiveMessage(&switchSetupRequest)) { Mutex *mutex = getServerSynchAccessor(); if(mutex != NULL) mutex->p(); - if(serverInterface->getSwitchSetupRequests()[switchSetupRequest.getCurrentFactionIndex()]==NULL) { - serverInterface->getSwitchSetupRequests()[switchSetupRequest.getCurrentFactionIndex()]= new SwitchSetupRequest(); + int factionIdx = switchSetupRequest.getCurrentFactionIndex(); + if(serverInterface->getSwitchSetupRequests()[factionIdx] == NULL) { + serverInterface->getSwitchSetupRequests()[factionIdx]= new SwitchSetupRequest(); } - *(serverInterface->getSwitchSetupRequests()[switchSetupRequest.getCurrentFactionIndex()])=switchSetupRequest; + *(serverInterface->getSwitchSetupRequests()[factionIdx]) = switchSetupRequest; + + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] factionIdx = %d, switchSetupRequest.getNetworkPlayerName() = %s\n",__FILE__,__FUNCTION__,__LINE__,factionIdx,switchSetupRequest.getNetworkPlayerName().c_str()); if(mutex != NULL) mutex->v(); } @@ -594,7 +599,7 @@ void ConnectionSlot::update(bool checkForNewClients) { } default: { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] networkMessageType = %d\n",__FILE__,__FUNCTION__,__LINE__,networkMessageType); if(gotIntro == true) { //throw runtime_error("Unexpected message in connection slot: " + intToStr(networkMessageType)); diff --git a/source/glest_game/network/network_message.cpp b/source/glest_game/network/network_message.cpp index ad0b6b63..cb93d726 100644 --- a/source/glest_game/network/network_message.cpp +++ b/source/glest_game/network/network_message.cpp @@ -824,28 +824,29 @@ void NetworkMessageSynchNetworkGameDataFileGet::send(Socket* socket) const { // ===================================================== -// class NetworkMessageSynchNetworkGameDataFileGet +// class SwitchSetupRequest // ===================================================== -SwitchSetupRequest::SwitchSetupRequest() -{ +SwitchSetupRequest::SwitchSetupRequest() { data.messageType= nmtSwitchSetupRequest; data.selectedFactionName=""; data.currentFactionIndex=-1; data.toFactionIndex=-1; data.toTeam = -1; data.networkPlayerName=""; + data.switchFlags = ssrft_None; } - -SwitchSetupRequest::SwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, int8 toFactionIndex,int8 toTeam,string networkPlayerName) -{ +SwitchSetupRequest::SwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, + int8 toFactionIndex,int8 toTeam,string networkPlayerName, + int8 flags) { data.messageType= nmtSwitchSetupRequest; data.selectedFactionName=selectedFactionName; data.currentFactionIndex=currentFactionIndex; data.toFactionIndex=toFactionIndex; data.toTeam = toTeam; data.networkPlayerName=networkPlayerName; + data.switchFlags = flags; } bool SwitchSetupRequest::receive(Socket* socket) { @@ -857,13 +858,11 @@ bool SwitchSetupRequest::receive(Socket* socket) { return result; } -void SwitchSetupRequest::send(Socket* socket) const -{ +void SwitchSetupRequest::send(Socket* socket) const { assert(data.messageType==nmtSwitchSetupRequest); NetworkMessage::send(socket, &data, sizeof(data)); } - // ===================================================== // class PlayerIndexMessage // ===================================================== diff --git a/source/glest_game/network/network_message.h b/source/glest_game/network/network_message.h index 11ce45f5..3b15025f 100644 --- a/source/glest_game/network/network_message.h +++ b/source/glest_game/network/network_message.h @@ -546,10 +546,21 @@ public: // to switch its settings // ===================================================== +// Each bit represents which item in the packet has a changed value +enum SwitchSetupRequestFlagType { + ssrft_None = 0x00, + ssrft_SelectedFactionName = 0x01, + ssrft_CurrentFactionIndex = 0x02, + ssrft_ToFactionIndex = 0x04, + ssrft_ToTeam = 0x08, + ssrft_NetworkPlayerName = 0x10 +}; + #pragma pack(push, 1) class SwitchSetupRequest: public NetworkMessage{ private: static const int maxStringSize= 256; + static const int maxPlayernameStringSize= 80; private: struct Data{ @@ -558,7 +569,8 @@ private: int8 currentFactionIndex; int8 toFactionIndex; int8 toTeam; - NetworkString networkPlayerName; + NetworkString networkPlayerName; + int8 switchFlags; }; private: @@ -566,13 +578,17 @@ private: public: SwitchSetupRequest(); - SwitchSetupRequest( string selectedFactionName, int8 currentFactionIndex, int8 toFactionIndex,int8 toTeam,string networkPlayerName); + SwitchSetupRequest( string selectedFactionName, int8 currentFactionIndex, + int8 toFactionIndex,int8 toTeam,string networkPlayerName, int8 flags); string getSelectedFactionName() const {return data.selectedFactionName.getString();} int getCurrentFactionIndex() const {return data.currentFactionIndex;} int getToFactionIndex() const {return data.toFactionIndex;} int getToTeam() const {return data.toTeam;} string getNetworkPlayerName() const {return data.networkPlayerName.getString(); } + int getSwitchFlags() const {return data.switchFlags;} + int addSwitchFlag(SwitchSetupRequestFlagType flag) { data.switchFlags |= flag;} + int clearSwitchFlag(SwitchSetupRequestFlagType flag) { data.switchFlags &= ~flag;} virtual bool receive(Socket* socket); virtual void send(Socket* socket) const;