QTfrontend/netserver.cpp
changeset 420 6cdfc07dceed
parent 404 64a62b679b04
child 443 eec37eb7f5db
equal deleted inserted replaced
419:fdeed9718e6b 420:6cdfc07dceed
    15  * along with this program; if not, write to the Free Software
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  */
    17  */
    18 
    18 
    19 #include "netserver.h"
    19 #include "netserver.h"
       
    20 #include "netconnectedclient.h"
    20 
    21 
    21 #include <QTcpServer>
    22 #include <QTcpServer>
    22 #include <QTcpSocket>
    23 #include <QTcpSocket>
    23 #include <QMessageBox>
    24 #include <QMessageBox>
    24 
    25 
   173   for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
   174   for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
   174     if(*it==this_cl) continue;
   175     if(*it==this_cl) continue;
   175     (*it)->RawSendNet(gameCfg);
   176     (*it)->RawSendNet(gameCfg);
   176   }
   177   }
   177 }
   178 }
   178 
       
   179 HWConnectedClient::HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client) :
       
   180   readyToStart(false),
       
   181   m_hwserver(hwserver),
       
   182   m_client(client)
       
   183 {
       
   184   connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
       
   185   connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
       
   186 }
       
   187 
       
   188 HWConnectedClient::~HWConnectedClient()
       
   189 {
       
   190 }
       
   191 
       
   192 void HWConnectedClient::ClientDisconnect()
       
   193 {
       
   194   emit(HWClientDisconnected(this));
       
   195 }
       
   196 
       
   197 void HWConnectedClient::ClientRead()
       
   198 {
       
   199   try {
       
   200     while (m_client->canReadLine()) {
       
   201       ParseLine(m_client->readLine().trimmed());
       
   202     }
       
   203   } catch(ShouldDisconnectException& e) {
       
   204     m_client->close();
       
   205   }
       
   206 }
       
   207 
       
   208 void HWConnectedClient::ParseLine(const QByteArray & line)
       
   209 {
       
   210   QString msg = QString::fromUtf8 (line.data(), line.size());
       
   211 
       
   212   QStringList lst = msg.split(delimeter);
       
   213   if(!lst.size()) return;
       
   214   if (lst[0] == "NICK") {
       
   215     if(lst.size()<2) return;
       
   216     if(m_hwserver->haveNick(lst[1])) {
       
   217       RawSendNet(QString("ERRONEUSNICKNAME"));
       
   218       throw ShouldDisconnectException();
       
   219     }
       
   220 
       
   221     client_nick=lst[1];
       
   222     qDebug() << "send connected";
       
   223     RawSendNet(QString("CONNECTED"));
       
   224     if(m_hwserver->isChiefClient(this)) RawSendNet(QString("CONFIGASKED"));
       
   225     else {
       
   226       RawSendNet(QString("SLAVE"));
       
   227       // send teams
       
   228       QList<QStringList> team_conf=m_hwserver->getTeamsConfig();
       
   229       for(QList<QStringList>::iterator tmit=team_conf.begin(); tmit!=team_conf.end(); ++tmit) {
       
   230 	RawSendNet(QString("ADDTEAM:")+delimeter+tmit->join(QString(delimeter)));
       
   231       }
       
   232       // send config
       
   233       QMap<QString, QStringList> conf=m_hwserver->getGameCfg();
       
   234       qDebug() << "Config:";
       
   235       for(QMap<QString, QStringList>::iterator it=conf.begin(); it!=conf.end(); ++it) {
       
   236 	RawSendNet(QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value().join(QString(delimeter)));
       
   237 	qDebug() << QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value().join(QString(delimeter));
       
   238       }
       
   239     }
       
   240     return;
       
   241   }
       
   242   if(client_nick=="") return;
       
   243 
       
   244   if (lst[0]=="START:") {
       
   245     readyToStart=true;
       
   246     if(m_hwserver->shouldStart(this)) {
       
   247       // start
       
   248       m_hwserver->sendAll("RUNGAME");
       
   249       m_hwserver->resetStart();
       
   250     }
       
   251     return;
       
   252   }
       
   253 
       
   254   if(lst[0]=="CONFIG_PARAM") {
       
   255     if(!m_hwserver->isChiefClient(this) || lst.size()<3) return; // error or permission denied :)
       
   256     else m_hwserver->m_gameCfg[lst[1]]=lst.mid(2);
       
   257     qDebug() << msg;
       
   258   }
       
   259 
       
   260   if(lst[0]=="ADDTEAM:") {
       
   261     if(lst.size()<11) return;
       
   262     lst.pop_front();
       
   263     
       
   264     // add team ID
       
   265     static unsigned int netTeamID=0;
       
   266     lst.insert(1, QString::number(++netTeamID));
       
   267 
       
   268     // hedgehogs num count
       
   269     int maxAdd=18-m_hwserver->hhnum;
       
   270     if (maxAdd<=0) return; // reject command
       
   271     int toAdd=maxAdd<4 ? maxAdd : 4;
       
   272     m_hwserver->hhnum+=toAdd;
       
   273     // hedgehogs num config
       
   274     QString hhnumCfg=QString("CONFIG_PARAM%1HHNUM+%2+%3%1%4").arg(delimeter).arg(lst[0])\
       
   275       .arg(netTeamID)\
       
   276       .arg(toAdd);
       
   277     
       
   278     // creating color config for new team
       
   279     QString colorCfg=QString("CONFIG_PARAM%1TEAM_COLOR+%2+%3%1%4").arg(delimeter).arg(lst[0])\
       
   280       .arg(netTeamID)\
       
   281       .arg(lst.takeAt(2));
       
   282     qDebug() << "color config:" << colorCfg;
       
   283 
       
   284     m_hwserver->m_gameCfg[colorCfg.split(delimeter)[1]]=colorCfg.split(delimeter).mid(2);
       
   285     m_hwserver->m_gameCfg[hhnumCfg.split(delimeter)[1]]=hhnumCfg.split(delimeter).mid(2);
       
   286     m_teamsCfg.push_back(lst);
       
   287 
       
   288     m_hwserver->sendOthers(this, QString("ADDTEAM:")+delimeter+lst.join(QString(delimeter)));
       
   289     RawSendNet(QString("TEAM_ACCEPTED%1%2%1%3").arg(delimeter).arg(lst[0]).arg(lst[1]));
       
   290     m_hwserver->sendAll(colorCfg);
       
   291     m_hwserver->sendAll(hhnumCfg);
       
   292     return;
       
   293   }
       
   294 
       
   295   if(lst[0]=="REMOVETEAM:") {
       
   296     if(lst.size()<2) return;
       
   297     
       
   298     for(QMap<QString, QStringList>::iterator it=m_hwserver->m_gameCfg.begin(); it!=m_hwserver->m_gameCfg.end(); ++it) {
       
   299       QStringList hhTmpList=it.key().split('+');
       
   300       if(hhTmpList[0] == "HHNUM") {
       
   301 	qDebug() << "hhnum config found";
       
   302 	if(hhTmpList[1]==lst[1]) {
       
   303 	  qDebug() << "hhnum config team found with: " << lst[1] << ":" << it.value()[0].toUInt();
       
   304 	  m_hwserver->hhnum-=it.value()[0].toUInt();
       
   305 	  break;
       
   306 	}
       
   307       }
       
   308     }
       
   309 
       
   310     unsigned int netID=removeTeam(lst[1]);
       
   311     m_hwserver->sendOthers(this, QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID));
       
   312     qDebug() << QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID);
       
   313     return;
       
   314   }
       
   315 
       
   316   m_hwserver->sendOthers(this, msg);
       
   317 }
       
   318 
       
   319 unsigned int HWConnectedClient::removeTeam(const QString& tname)
       
   320 {
       
   321   unsigned int netID=0;
       
   322   for(QList<QStringList>::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) {
       
   323     if((*it)[0]==tname) {
       
   324       netID=(*it)[1].toUInt();
       
   325       m_teamsCfg.erase(it);
       
   326       break;
       
   327     }
       
   328   }
       
   329   return netID;
       
   330 }
       
   331 
       
   332 QList<QStringList> HWConnectedClient::getTeamNames() const
       
   333 {
       
   334   return m_teamsCfg;
       
   335 }
       
   336 
       
   337 void HWConnectedClient::RawSendNet(const QString & str)
       
   338 {
       
   339   RawSendNet(str.toUtf8());
       
   340 }
       
   341 
       
   342 void HWConnectedClient::RawSendNet(const QByteArray & buf)
       
   343 {
       
   344   m_client->write(buf);
       
   345   m_client->write("\n", 1);
       
   346 }
       
   347 
       
   348 QString HWConnectedClient::getClientNick() const
       
   349 {
       
   350   return client_nick;
       
   351 }
       
   352 
       
   353 bool HWConnectedClient::isReady() const
       
   354 {
       
   355   return readyToStart;
       
   356 }
       
   357 
       
   358 QString HWConnectedClient::getHedgehogsDescription() const
       
   359 {
       
   360   return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter);
       
   361 }