From 0687a56e272f39df3103e8fbdd1af75cd30e376e Mon Sep 17 00:00:00 2001 From: Titus Tscharntke Date: Fri, 30 Apr 2010 01:08:29 +0000 Subject: [PATCH] clients can choose their slots --- .../menu/menu_state_connected_game.cpp | 32 +++++++- .../menu/menu_state_custom_game.cpp | 77 ++++++++++++------- .../glest_game/network/client_interface.cpp | 10 ++- source/glest_game/network/connection_slot.h | 2 + source/glest_game/network/network_message.cpp | 21 +++++ source/glest_game/network/network_message.h | 27 +++++++ .../glest_game/network/server_interface.cpp | 23 ++++++ source/glest_game/network/server_interface.h | 1 + 8 files changed, 163 insertions(+), 30 deletions(-) diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index 5a6d602e3..91d98e685 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -174,6 +174,8 @@ void MenuStateConnectedGame::mouseClick(int x, int y, MouseButton mouseButton){ NetworkManager &networkManager= NetworkManager::getInstance(); ClientInterface* clientInterface= networkManager.getClientInterface(); + if (!settingsReceivedFromServer) return; + if(buttonDisconnect.mouseClick(x,y)){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -216,23 +218,46 @@ void MenuStateConnectedGame::mouseClick(int x, int y, MouseButton mouseButton){ } else { + int myCurrentIndex=-1; + for(int i=0; iisConnected()){ clientInterface->setGameSettingsReceived(false); - clientInterface->sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem(),clientInterface->getGameSettings()->getThisFactionIndex(),-1,listBoxTeams[i].getSelectedItemIndex()); + clientInterface->sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem(),i,-1,listBoxTeams[i].getSelectedItemIndex()); } + break; } } if(listBoxTeams[i].getEditable()){ if(listBoxTeams[i].mouseClick(x, y)){ + soundRenderer.playFx(coreData.getClickSoundA()); if(clientInterface->isConnected()){ clientInterface->setGameSettingsReceived(false); - clientInterface->sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem(),clientInterface->getGameSettings()->getThisFactionIndex(),-1,listBoxTeams[i].getSelectedItemIndex()); + clientInterface->sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem(),i,-1,listBoxTeams[i].getSelectedItemIndex()); } + break; + } + } + if((listBoxControls[i].getSelectedItemIndex()==ctNetwork) && (labelNetStatus[i].getText()=="???")){ + if(grabSlotButton[i].mouseClick(x, y) ) + { + soundRenderer.playFx(coreData.getClickSoundA()); + 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()); + break; } } } @@ -304,6 +329,7 @@ void MenuStateConnectedGame::mouseMove(int x, int y, const MouseState *ms){ listBoxControls[i].mouseMove(x, y); listBoxFactions[i].mouseMove(x, y); listBoxTeams[i].mouseMove(x, y); + grabSlotButton[i].mouseMove(x, y); } listBoxMap.mouseMove(x, y); listBoxFogOfWar.mouseMove(x, y); @@ -320,7 +346,7 @@ void MenuStateConnectedGame::render(){ int i; renderer.renderButton(&buttonDisconnect); - renderer.renderButton(&buttonPlayNow); + //renderer.renderButton(&buttonPlayNow); for(i=0; igetSwitchSetupRequests(); + for(int i= 0; igetSelectedFactionName()=%s\n",switchSetupRequests[i]->getSelectedFactionName().c_str()); + //printf("switchSetupRequests[i]->getToTeam()=%d\n",switchSetupRequests[i]->getToTeam()); + + if(switchSetupRequests[i]->getToFactionIndex()!=-1) + { + //printf("switchSlot request from %d to %d\n",switchSetupRequests[i]->getCurrentFactionIndex(),switchSetupRequests[i]->getToFactionIndex()); + if(serverInterface->switchSlot(switchSetupRequests[i]->getCurrentFactionIndex(),switchSetupRequests[i]->getToFactionIndex())){ + int k=switchSetupRequests[i]->getToFactionIndex(); + try { + if(switchSetupRequests[i]->getSelectedFactionName()!=""){ + listBoxFactions[k].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName()); + } + if(switchSetupRequests[i]->getToTeam()!=-1) + listBoxTeams[k].setSelectedItemIndex(switchSetupRequests[i]->getToTeam()); + } + 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 + { + try { + if(switchSetupRequests[i]->getSelectedFactionName()!=""){ + listBoxFactions[i].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName()); + } + if(switchSetupRequests[i]->getToTeam()!=-1) + listBoxTeams[i].setSelectedItemIndex(switchSetupRequests[i]->getToTeam()); + } + catch(const runtime_error &e) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] caught exception error = [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what()); + } + } + } + + delete switchSetupRequests[i]; + switchSetupRequests[i]=NULL; + } + } + for(int i= 0; igetSwitchSetupRequests(); - for(int i= 0; igetSelectedFactionName()=%s\n",switchSetupRequests[i]->getSelectedFactionName().c_str()); - //printf("switchSetupRequests[i]->getToTeam()=%d\n",switchSetupRequests[i]->getToTeam()); - - try { - if(switchSetupRequests[i]->getSelectedFactionName()!=""){ - listBoxFactions[i].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName()); - } - if(switchSetupRequests[i]->getToTeam()!=-1) - listBoxTeams[i].setSelectedItemIndex(switchSetupRequests[i]->getToTeam()); - } - catch(const runtime_error &e) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] caught exception error = [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what()); - } - } - - delete switchSetupRequests[i]; - switchSetupRequests[i]=NULL; - } - } if(difftime(time(NULL),lastSetChangedGameSettings) >= 2) { GameSettings gameSettings; diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index 729b03d41..373b3a96a 100755 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -366,7 +366,15 @@ void ClientInterface::updateLobby() } } break; - + case nmtPlayerIndexMessage: + { + PlayerIndexMessage playerIndexMessage(-1); + if(receiveMessage(&playerIndexMessage)) + { + playerIndex= playerIndexMessage.getPlayerIndex(); + } + } + break; default: { string sErr = string(__FILE__) + "::" + string(__FUNCTION__) + " Unexpected network message: " + intToStr(networkMessageType); diff --git a/source/glest_game/network/connection_slot.h b/source/glest_game/network/connection_slot.h index 25542037b..cbd1e193c 100644 --- a/source/glest_game/network/connection_slot.h +++ b/source/glest_game/network/connection_slot.h @@ -46,6 +46,8 @@ public: void update(bool checkForNewClients); virtual void update(); + void setPlayerIndex(int value) { playerIndex = value; } + int getPlayerIndex() {return playerIndex;} void setReady() {ready= true;} const string &getName() const {return name;} diff --git a/source/glest_game/network/network_message.cpp b/source/glest_game/network/network_message.cpp index d8f00ab3d..cc9d08eee 100644 --- a/source/glest_game/network/network_message.cpp +++ b/source/glest_game/network/network_message.cpp @@ -491,6 +491,27 @@ void SwitchSetupRequest::send(Socket* socket) const } +// ===================================================== +// class PlayerIndexMessage +// ===================================================== + + +PlayerIndexMessage::PlayerIndexMessage(int16 playerIndex) +{ + data.messageType= nmtPlayerIndexMessage; + data.playerIndex=playerIndex; +} + +bool PlayerIndexMessage::receive(Socket* socket) +{ + return NetworkMessage::receive(socket, &data, sizeof(data)); +} + +void PlayerIndexMessage::send(Socket* socket) const +{ + assert(data.messageType==nmtPlayerIndexMessage); + NetworkMessage::send(socket, &data, sizeof(data)); +} }}//end namespace diff --git a/source/glest_game/network/network_message.h b/source/glest_game/network/network_message.h index 90121beb4..46773e22f 100644 --- a/source/glest_game/network/network_message.h +++ b/source/glest_game/network/network_message.h @@ -39,6 +39,7 @@ enum NetworkMessageType{ nmtSynchNetworkGameDataFileGet, nmtBroadCastSetup, nmtSwitchSetupRequest, + nmtPlayerIndexMessage, nmtCount }; @@ -448,6 +449,32 @@ public: virtual void send(Socket* socket) const; }; +// ===================================================== +// class SwitchSetupRequest +// +// Message sent from the server to the client +// when the client connects and vice versa +// ===================================================== + +class PlayerIndexMessage: public NetworkMessage{ + +private: + struct Data{ + int8 messageType; + int16 playerIndex; + }; + +private: + Data data; + +public: + PlayerIndexMessage( int16 playerIndex); + + int16 getPlayerIndex() const {return data.playerIndex;} + + virtual bool receive(Socket* socket); + virtual void send(Socket* socket) const; +}; }}//end namespace diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index daac8592e..80701dca3 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -74,6 +74,29 @@ void ServerInterface::addSlot(int playerIndex){ SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END\n",__FILE__,__FUNCTION__); } +bool ServerInterface::switchSlot(int fromPlayerIndex,int toPlayerIndex){ + + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] START\n",__FILE__,__FUNCTION__); + bool result=false; + assert(fromPlayerIndex>=0 && fromPlayerIndex=0 && toPlayerIndexisConnected()) { + //printf(" yes, its free :)\n"); + slots[fromPlayerIndex]->setPlayerIndex(toPlayerIndex); + ConnectionSlot *tmp=slots[toPlayerIndex]; + slots[toPlayerIndex]= slots[fromPlayerIndex]; + slots[fromPlayerIndex]=tmp; + PlayerIndexMessage playerIndexMessage(toPlayerIndex); + slots[toPlayerIndex]->sendMessage(&playerIndexMessage); + result=true; + } + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END\n",__FILE__,__FUNCTION__); + return result; +} + void ServerInterface::removeSlot(int playerIndex){ SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] START\n",__FILE__,__FUNCTION__); diff --git a/source/glest_game/network/server_interface.h b/source/glest_game/network/server_interface.h index 4d66ff036..468705bb1 100644 --- a/source/glest_game/network/server_interface.h +++ b/source/glest_game/network/server_interface.h @@ -69,6 +69,7 @@ public: ServerSocket* getServerSocket() {return &serverSocket;} SwitchSetupRequest** getSwitchSetupRequests() {return &switchSetupRequests[0];} void addSlot(int playerIndex); + bool switchSlot(int fromPlayerIndex,int toPlayerIndex); void removeSlot(int playerIndex); ConnectionSlot* getSlot(int playerIndex); int getConnectedSlotCount();