QTfrontend/netconnectedclient.cpp
changeset 420 6cdfc07dceed
child 443 eec37eb7f5db
equal deleted inserted replaced
419:fdeed9718e6b 420:6cdfc07dceed
       
     1 /*
       
     2  * Hedgewars, a worms-like game
       
     3  * Copyright (c) 2006 Igor Ulyanov <iulyanov@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    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
       
    17  */
       
    18 
       
    19 #include "netconnectedclient.h"
       
    20 #include "netserver.h"
       
    21 
       
    22 #include <QTcpServer>
       
    23 #include <QTcpSocket>
       
    24 #include <QStringList>
       
    25 
       
    26 #include <QDebug>
       
    27 
       
    28 extern char delimeter;
       
    29 
       
    30 HWConnectedClient::HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client) :
       
    31   readyToStart(false),
       
    32   m_hwserver(hwserver),
       
    33   m_client(client)
       
    34 {
       
    35   connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
       
    36   connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
       
    37 }
       
    38 
       
    39 HWConnectedClient::~HWConnectedClient()
       
    40 {
       
    41 }
       
    42 
       
    43 void HWConnectedClient::ClientDisconnect()
       
    44 {
       
    45   emit(HWClientDisconnected(this));
       
    46 }
       
    47 
       
    48 void HWConnectedClient::ClientRead()
       
    49 {
       
    50   try {
       
    51     while (m_client->canReadLine()) {
       
    52       ParseLine(m_client->readLine().trimmed());
       
    53     }
       
    54   } catch(ShouldDisconnectException& e) {
       
    55     m_client->close();
       
    56   }
       
    57 }
       
    58 
       
    59 void HWConnectedClient::ParseLine(const QByteArray & line)
       
    60 {
       
    61   QString msg = QString::fromUtf8 (line.data(), line.size());
       
    62 
       
    63   QStringList lst = msg.split(delimeter);
       
    64   if(!lst.size()) return;
       
    65   if (lst[0] == "NICK") {
       
    66     if(lst.size()<2) return;
       
    67     if(m_hwserver->haveNick(lst[1])) {
       
    68       RawSendNet(QString("ERRONEUSNICKNAME"));
       
    69       throw ShouldDisconnectException();
       
    70     }
       
    71 
       
    72     client_nick=lst[1];
       
    73     qDebug() << "send connected";
       
    74     RawSendNet(QString("CONNECTED"));
       
    75     if(m_hwserver->isChiefClient(this)) RawSendNet(QString("CONFIGASKED"));
       
    76     else {
       
    77       RawSendNet(QString("SLAVE"));
       
    78       // send teams
       
    79       QList<QStringList> team_conf=m_hwserver->getTeamsConfig();
       
    80       for(QList<QStringList>::iterator tmit=team_conf.begin(); tmit!=team_conf.end(); ++tmit) {
       
    81 	RawSendNet(QString("ADDTEAM:")+delimeter+tmit->join(QString(delimeter)));
       
    82       }
       
    83       // send config
       
    84       QMap<QString, QStringList> conf=m_hwserver->getGameCfg();
       
    85       qDebug() << "Config:";
       
    86       for(QMap<QString, QStringList>::iterator it=conf.begin(); it!=conf.end(); ++it) {
       
    87 	RawSendNet(QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value().join(QString(delimeter)));
       
    88 	qDebug() << QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value().join(QString(delimeter));
       
    89       }
       
    90     }
       
    91     return;
       
    92   }
       
    93   if(client_nick=="") return;
       
    94 
       
    95   if (lst[0]=="START:") {
       
    96     readyToStart=true;
       
    97     if(m_hwserver->shouldStart(this)) {
       
    98       // start
       
    99       m_hwserver->sendAll("RUNGAME");
       
   100       m_hwserver->resetStart();
       
   101     }
       
   102     return;
       
   103   }
       
   104 
       
   105   if(lst[0]=="CONFIG_PARAM") {
       
   106     if(!m_hwserver->isChiefClient(this) || lst.size()<3) return; // error or permission denied :)
       
   107     else m_hwserver->m_gameCfg[lst[1]]=lst.mid(2);
       
   108     qDebug() << msg;
       
   109   }
       
   110 
       
   111   if(lst[0]=="ADDTEAM:") {
       
   112     if(lst.size()<11) return;
       
   113     lst.pop_front();
       
   114     
       
   115     // add team ID
       
   116     static unsigned int netTeamID=0;
       
   117     lst.insert(1, QString::number(++netTeamID));
       
   118 
       
   119     // hedgehogs num count
       
   120     int maxAdd=18-m_hwserver->hhnum;
       
   121     if (maxAdd<=0) return; // reject command
       
   122     int toAdd=maxAdd<4 ? maxAdd : 4;
       
   123     m_hwserver->hhnum+=toAdd;
       
   124     // hedgehogs num config
       
   125     QString hhnumCfg=QString("CONFIG_PARAM%1HHNUM+%2+%3%1%4").arg(delimeter).arg(lst[0])\
       
   126       .arg(netTeamID)\
       
   127       .arg(toAdd);
       
   128     
       
   129     // creating color config for new team
       
   130     QString colorCfg=QString("CONFIG_PARAM%1TEAM_COLOR+%2+%3%1%4").arg(delimeter).arg(lst[0])\
       
   131       .arg(netTeamID)\
       
   132       .arg(lst.takeAt(2));
       
   133     qDebug() << "color config:" << colorCfg;
       
   134 
       
   135     m_hwserver->m_gameCfg[colorCfg.split(delimeter)[1]]=colorCfg.split(delimeter).mid(2);
       
   136     m_hwserver->m_gameCfg[hhnumCfg.split(delimeter)[1]]=hhnumCfg.split(delimeter).mid(2);
       
   137     m_teamsCfg.push_back(lst);
       
   138 
       
   139     m_hwserver->sendOthers(this, QString("ADDTEAM:")+delimeter+lst.join(QString(delimeter)));
       
   140     RawSendNet(QString("TEAM_ACCEPTED%1%2%1%3").arg(delimeter).arg(lst[0]).arg(lst[1]));
       
   141     m_hwserver->sendAll(colorCfg);
       
   142     m_hwserver->sendAll(hhnumCfg);
       
   143     return;
       
   144   }
       
   145 
       
   146   if(lst[0]=="REMOVETEAM:") {
       
   147     if(lst.size()<2) return;
       
   148     
       
   149     for(QMap<QString, QStringList>::iterator it=m_hwserver->m_gameCfg.begin(); it!=m_hwserver->m_gameCfg.end(); ++it) {
       
   150       QStringList hhTmpList=it.key().split('+');
       
   151       if(hhTmpList[0] == "HHNUM") {
       
   152 	qDebug() << "hhnum config found";
       
   153 	if(hhTmpList[1]==lst[1]) {
       
   154 	  qDebug() << "hhnum config team found with: " << lst[1] << ":" << it.value()[0].toUInt();
       
   155 	  m_hwserver->hhnum-=it.value()[0].toUInt();
       
   156 	  break;
       
   157 	}
       
   158       }
       
   159     }
       
   160 
       
   161     unsigned int netID=removeTeam(lst[1]);
       
   162     m_hwserver->sendOthers(this, QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID));
       
   163     qDebug() << QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID);
       
   164     return;
       
   165   }
       
   166 
       
   167   m_hwserver->sendOthers(this, msg);
       
   168 }
       
   169 
       
   170 unsigned int HWConnectedClient::removeTeam(const QString& tname)
       
   171 {
       
   172   unsigned int netID=0;
       
   173   for(QList<QStringList>::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) {
       
   174     if((*it)[0]==tname) {
       
   175       netID=(*it)[1].toUInt();
       
   176       m_teamsCfg.erase(it);
       
   177       break;
       
   178     }
       
   179   }
       
   180   return netID;
       
   181 }
       
   182 
       
   183 QList<QStringList> HWConnectedClient::getTeamNames() const
       
   184 {
       
   185   return m_teamsCfg;
       
   186 }
       
   187 
       
   188 void HWConnectedClient::RawSendNet(const QString & str)
       
   189 {
       
   190   RawSendNet(str.toUtf8());
       
   191 }
       
   192 
       
   193 void HWConnectedClient::RawSendNet(const QByteArray & buf)
       
   194 {
       
   195   m_client->write(buf);
       
   196   m_client->write("\n", 1);
       
   197 }
       
   198 
       
   199 QString HWConnectedClient::getClientNick() const
       
   200 {
       
   201   return client_nick;
       
   202 }
       
   203 
       
   204 bool HWConnectedClient::isReady() const
       
   205 {
       
   206   return readyToStart;
       
   207 }
       
   208 
       
   209 QString HWConnectedClient::getHedgehogsDescription() const
       
   210 {
       
   211   return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter);
       
   212 }