QTfrontend/netserver.cpp
changeset 338 d1e75dcd285f
parent 335 751348947fce
child 339 7535ab6c3820
equal deleted inserted replaced
337:a46abb65eb0c 338:d1e75dcd285f
    58 
    58 
    59 void HWNetServer::ClientDisconnect(HWConnectedClient* client)
    59 void HWNetServer::ClientDisconnect(HWConnectedClient* client)
    60 {
    60 {
    61   QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
    61   QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
    62   connclients.erase(it);
    62   connclients.erase(it);
    63   teamChanged();
    63   //teamChanged();
    64 }
    64 }
    65 
    65 
    66 QString HWNetServer::getRunningHostName() const
    66 QString HWNetServer::getRunningHostName() const
    67 {
    67 {
    68   return IPCServer->serverAddress().toString();
    68   return IPCServer->serverAddress().toString();
   106     }
   106     }
   107   }
   107   }
   108   return false;
   108   return false;
   109 }
   109 }
   110 
   110 
   111 QStringList HWNetServer::getTeams() const
   111 QList<QStringList> HWNetServer::getTeamsConfig() const
   112 {
   112 {
   113   QStringList lst;
   113   QList<QStringList> lst;
   114   for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
   114   for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
   115     try {
   115     try {
   116       lst.push_back((*it)->getTeamName());
   116       lst+=(*it)->getTeamNames();
   117     } catch(HWConnectedClient::NoTeamNameException& e) {
   117     } catch(HWConnectedClient::NoTeamNameException& e) {
   118     }
   118     }
   119   }
   119   }
   120   return lst;
   120   return lst;
   121 }
       
   122 
       
   123 void HWNetServer::teamChanged()
       
   124 {
       
   125   for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
       
   126     (*it)->teamChangedNotify();
       
   127   }
       
   128 }
   121 }
   129 
   122 
   130 bool HWNetServer::shouldStart(HWConnectedClient* client)
   123 bool HWNetServer::shouldStart(HWConnectedClient* client)
   131 {
   124 {
   132   QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
   125   QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
   164 }
   157 }
   165 
   158 
   166 HWConnectedClient::HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client) :
   159 HWConnectedClient::HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client) :
   167   readyToStart(false),
   160   readyToStart(false),
   168   m_hwserver(hwserver),
   161   m_hwserver(hwserver),
   169   m_client(client),
   162   m_client(client)
   170   pclent_team(0)
       
   171 {
   163 {
   172   connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
   164   connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
   173   connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
   165   connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
   174 }
   166 }
   175 
   167 
   176 HWConnectedClient::~HWConnectedClient()
   168 HWConnectedClient::~HWConnectedClient()
   177 {
   169 {
   178   if(pclent_team) delete pclent_team;
       
   179 }
   170 }
   180 
   171 
   181 void HWConnectedClient::ClientDisconnect()
   172 void HWConnectedClient::ClientDisconnect()
   182 {
   173 {
   183   emit(HWClientDisconnected(this));
   174   emit(HWClientDisconnected(this));
   210     }
   201     }
   211 
   202 
   212     client_nick=lst[1];
   203     client_nick=lst[1];
   213     qDebug() << "send connected";
   204     qDebug() << "send connected";
   214     RawSendNet(QString("CONNECTED"));
   205     RawSendNet(QString("CONNECTED"));
   215     m_hwserver->teamChanged();
       
   216     if(m_hwserver->isChiefClient(this)) RawSendNet(QString("CONFIGASKED"));
   206     if(m_hwserver->isChiefClient(this)) RawSendNet(QString("CONFIGASKED"));
   217     else {
   207     else {
       
   208       // send config
   218       QMap<QString, QString> conf=m_hwserver->getGameCfg();
   209       QMap<QString, QString> conf=m_hwserver->getGameCfg();
   219       for(QMap<QString, QString>::iterator it=conf.begin(); it!=conf.end(); ++it) {
   210       for(QMap<QString, QString>::iterator it=conf.begin(); it!=conf.end(); ++it) {
   220 	RawSendNet(QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value());
   211 	RawSendNet(QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value());
   221       }
   212       }
       
   213       // send teams
       
   214       QList<QStringList> team_conf=m_hwserver->getTeamsConfig();
       
   215       for(QList<QStringList>::iterator tmit=team_conf.begin(); tmit!=team_conf.end(); ++tmit) {
       
   216 	RawSendNet(QString("ADDTEAM:")+delimeter+tmit->join(QString(delimeter)));
       
   217       }
   222     }
   218     }
   223     return;
   219     return;
   224   }
   220   }
   225   if(client_nick=="") return;
   221   if(client_nick=="") return;
   226 
   222 
   238   }
   234   }
   239 
   235 
   240   if(lst[0]=="ADDTEAM:") {
   236   if(lst[0]=="ADDTEAM:") {
   241     if(lst.size()<10) return;
   237     if(lst.size()<10) return;
   242     lst.pop_front();
   238     lst.pop_front();
   243     if(pclent_team) delete pclent_team;
   239     m_teamsCfg.push_back(lst);
   244     pclent_team=new HWTeam(lst);
   240     m_hwserver->sendOthers(this, msg);
   245     m_hwserver->teamChanged();
       
   246     return;
   241     return;
   247   }
   242   }
   248 
   243 
   249   m_hwserver->sendOthers(this, msg);
   244   m_hwserver->sendOthers(this, msg);
   250 }
   245 }
   251 
   246 
   252 void HWConnectedClient::teamChangedNotify()
   247 QList<QStringList> HWConnectedClient::getTeamNames() const
   253 {
   248 {
   254   QString teams;
   249   return m_teamsCfg;
   255   QStringList lst=m_hwserver->getTeams();
       
   256   for(int i=0; i<lst.size(); i++) {
       
   257     teams+=delimeter+lst[i];
       
   258   }
       
   259   RawSendNet(QString("TEAMCHANGED")+teams);
       
   260 }
   250 }
   261 
   251 
   262 void HWConnectedClient::RawSendNet(const QString & str)
   252 void HWConnectedClient::RawSendNet(const QString & str)
   263 {
   253 {
   264   RawSendNet(str.toUtf8());
   254   RawSendNet(str.toUtf8());
   273 QString HWConnectedClient::getClientNick() const
   263 QString HWConnectedClient::getClientNick() const
   274 {
   264 {
   275   return client_nick;
   265   return client_nick;
   276 }
   266 }
   277 
   267 
   278 QString HWConnectedClient::getTeamName() const
       
   279 {
       
   280   if(!pclent_team) throw NoTeamNameException();
       
   281   return pclent_team->TeamName;
       
   282 }
       
   283 
       
   284 bool HWConnectedClient::isReady() const
   268 bool HWConnectedClient::isReady() const
   285 {
   269 {
   286   return readyToStart;
   270   return readyToStart;
   287 }
   271 }
   288 
   272 
   289 QString HWConnectedClient::getHedgehogsDescription() const
   273 QString HWConnectedClient::getHedgehogsDescription() const
   290 {
   274 {
   291   return pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter);
   275   return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter);
   292 }
   276 }