clients can choose their slots

This commit is contained in:
Titus Tscharntke 2010-04-30 01:08:29 +00:00
parent 140a304541
commit 0687a56e27
8 changed files with 163 additions and 30 deletions

View File

@ -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; i<GameConstants::maxPlayers; ++i)
{// find my current index by looking at editable listBoxes
if(listBoxFactions[i].getEditable()){
myCurrentIndex=i;
}
}
if (myCurrentIndex!=-1)
for(int i=0; i<GameConstants::maxPlayers; ++i)
{
if(listBoxFactions[i].getEditable()){
if(listBoxFactions[i].mouseClick(x, y)){
soundRenderer.playFx(coreData.getClickSoundA());
ClientInterface* clientInterface= NetworkManager::getInstance().getClientInterface();
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(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; i<GameConstants::maxPlayers; ++i){
renderer.renderLabel(&labelPlayers[i]);

View File

@ -412,6 +412,57 @@ void MenuStateCustomGame::update()
bool haveAtLeastOneNetworkClientConnected = false;
Config &config = Config::getInstance();
// handle setting changes from clients
SwitchSetupRequest** switchSetupRequests=serverInterface->getSwitchSetupRequests();
for(int i= 0; i<mapInfo.players; ++i)
{
if(switchSetupRequests[i]!=NULL)
{
if(listBoxControls[i].getSelectedItemIndex() == ctNetwork)
{
//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)
{
//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; i<mapInfo.players; ++i)
{
if(listBoxControls[i].getSelectedItemIndex() == ctNetwork)
@ -523,32 +574,6 @@ void MenuStateCustomGame::update()
needToSetChangedGameSettings = false;
}
SwitchSetupRequest** switchSetupRequests=serverInterface->getSwitchSetupRequests();
for(int i= 0; i<mapInfo.players; ++i)
{
if(switchSetupRequests[i]!=NULL)
{
if(listBoxControls[i].getSelectedItemIndex() == ctNetwork)
{
//printf("switchSetupRequests[i]->getSelectedFactionName()=%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;

View File

@ -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);

View File

@ -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;}

View File

@ -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

View File

@ -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

View File

@ -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<GameConstants::maxPlayers);
assert(toPlayerIndex>=0 && toPlayerIndex<GameConstants::maxPlayers);
if(fromPlayerIndex==toPlayerIndex) return false;// doubleclicked or whatever
//printf(" checking if slot %d is free?\n",toPlayerIndex);
if( !slots[toPlayerIndex]->isConnected()) {
//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__);

View File

@ -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();