QTfrontend/newnetclient.cpp
changeset 329 4c3aad46baa5
parent 322 e7175ae57945
child 332 10080f681118
equal deleted inserted replaced
328:95625db21bf8 329:4c3aad46baa5
    14  * You should have received a copy of the GNU General Public License
    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
    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 <QUuid>
       
    20 #include <QMessageBox>
    19 #include <QMessageBox>
    21 #include <QDebug>
    20 #include <QDebug>
    22 
    21 
    23 #include "newnetclient.h"
    22 #include "newnetclient.h"
    24 #include "proto.h"
    23 #include "proto.h"
    62 	     team.HHName[5] + delimeter + team.HHName[6] + delimeter + team.HHName[7]);
    61 	     team.HHName[5] + delimeter + team.HHName[6] + delimeter + team.HHName[7]);
    63 }
    62 }
    64 
    63 
    65 void HWNewNet::StartGame()
    64 void HWNewNet::StartGame()
    66 {
    65 {
    67   seed = QUuid::createUuid().toString();
    66   RawSendNet(QString("START:"));
    68   RawSendNet(QString("START:") + delimeter + seed);
       
    69 }
    67 }
    70 
    68 
    71 void HWNewNet::SendNet(const QByteArray & buf)
    69 void HWNewNet::SendNet(const QByteArray & buf)
    72 {
    70 {
    73   QString msg = QString(buf.toBase64());
    71   QString msg = QString(buf.toBase64());
    74   qDebug() << "to net:" << buf << ":" << msg;
    72   qDebug() << "to net:" << buf << ":" << msg;
    75 
    73 
    76   //NetBuffer += buf;
    74   //NetBuffer += buf;
    77   RawSendNet(QString(msg));
    75   RawSendNet(QString("GAMEMSG:%1%2").arg(delimeter).arg(msg));
    78 }
    76 }
    79 
    77 
    80 void HWNewNet::RawSendNet(const QString & str)
    78 void HWNewNet::RawSendNet(const QString & str)
    81 {
    79 {
    82   RawSendNet(str.toUtf8());
    80   RawSendNet(str.toUtf8());
    83 }
    81 }
    84 
    82 
    85 void HWNewNet::RawSendNet(const QByteArray & buf)
    83 void HWNewNet::RawSendNet(const QByteArray & buf)
    86 {
    84 {
    87   if (buf.size() > 510) return;
       
    88   NetSocket.write(buf);
    85   NetSocket.write(buf);
    89   NetSocket.write("\n", 1);
    86   NetSocket.write("\n", 1);
    90 }
    87 }
    91 
    88 
    92 void HWNewNet::ClientRead()
    89 void HWNewNet::ClientRead()
   167     QByteArray cache;
   164     QByteArray cache;
   168     emit FromNet(HWProto::addStringListToBuffer(cache, lst));
   165     emit FromNet(HWProto::addStringListToBuffer(cache, lst));
   169     return;
   166     return;
   170   }
   167   }
   171 
   168 
   172   QByteArray em = QByteArray::fromBase64(msg.toAscii());
   169   if (lst[0] == "CONFIG_PARAM") {
   173   qDebug() << "to engine:" << em;
   170   	if (lst[1] == "SEED") {
   174   emit FromNet(em);
   171       emit seedChanged(lst[2]);
       
   172 	  return;
       
   173   	}
       
   174   	if (lst[1] == "MAP") {
       
   175       emit mapChanged(lst[2]);
       
   176 	  return;
       
   177   	}
       
   178   	if (lst[1] == "THEME") {
       
   179       emit themeChanged(lst[2]);
       
   180 	  return;
       
   181   	}
       
   182   	if (lst[1] == "HEALTH") {
       
   183       emit initHealthChanged(lst[2].toUInt());
       
   184 	  return;
       
   185   	}
       
   186   	if (lst[1] == "TURNTIME") {
       
   187       emit turnTimeChanged(lst[2].toUInt());
       
   188 	  return;
       
   189   	}
       
   190   	if (lst[1] == "FORTSMODE") {
       
   191       emit fortsModeChanged(lst[2].toInt() != 0);
       
   192 	  return;
       
   193   	}
       
   194   	qDebug() << "unknow config param: " << lst[1];
       
   195     return;
       
   196   }
       
   197 
       
   198 
       
   199   // should be kinda game states, which don't allow "GAMEMSG:" at configure step,
       
   200   // "CONNECTED" at round phase, etc.
       
   201   if (lst[0] == "GAMEMSG:") {
       
   202     QByteArray em = QByteArray::fromBase64(lst[1].toAscii());
       
   203     qDebug() << "to engine:" << em;
       
   204     emit FromNet(em);
       
   205     return;
       
   206   }
       
   207 
       
   208   qDebug() << "unknown net command: " << msg;
       
   209 
   175 }
   210 }
   176 
   211 
   177 
   212 
   178 void HWNewNet::ConfigAsked()
   213 void HWNewNet::ConfigAsked()
   179 {
   214 {
   192   connect(game, SIGNAL(SendNet(const QByteArray &)), this, SLOT(SendNet(const QByteArray &)));
   227   connect(game, SIGNAL(SendNet(const QByteArray &)), this, SLOT(SendNet(const QByteArray &)));
   193   connect(this, SIGNAL(FromNet(const QByteArray &)), game, SLOT(FromNet(const QByteArray &)));
   228   connect(this, SIGNAL(FromNet(const QByteArray &)), game, SLOT(FromNet(const QByteArray &)));
   194   connect(this, SIGNAL(LocalCFG(const QString &)), game, SLOT(LocalCFG(const QString &)));
   229   connect(this, SIGNAL(LocalCFG(const QString &)), game, SLOT(LocalCFG(const QString &)));
   195   game->StartNet();
   230   game->StartNet();
   196 }
   231 }
       
   232 
       
   233 void HWNewNet::onSeedChanged(const QString & seed)
       
   234 {
       
   235   RawSendNet(QString("CONFIG_PARAM%1SEED%1%2").arg(delimeter).arg(seed));
       
   236 }
       
   237 
       
   238 void HWNewNet::onMapChanged(const QString & map)
       
   239 {
       
   240   RawSendNet(QString("CONFIG_PARAM%1MAP%1%2").arg(delimeter).arg(map));
       
   241 }
       
   242 
       
   243 void HWNewNet::onThemeChanged(const QString & theme)
       
   244 {
       
   245   RawSendNet(QString("CONFIG_PARAM%1THEME%1%2").arg(delimeter).arg(theme));
       
   246 }
       
   247 
       
   248 void HWNewNet::onInitHealthChanged(quint32 health)
       
   249 {
       
   250   RawSendNet(QString("CONFIG_PARAM%1HEALTH%1%2").arg(delimeter).arg(health));
       
   251 }
       
   252 
       
   253 void HWNewNet::onTurnTimeChanged(quint32 time)
       
   254 {
       
   255   RawSendNet(QString("CONFIG_PARAM%1TURNTIME%1%2").arg(delimeter).arg(time));
       
   256 }
       
   257 
       
   258 void HWNewNet::onFortsModeChanged(bool value)
       
   259 {
       
   260   RawSendNet(QString("CONFIG_PARAM%1FORTSMODE%1%2").arg(delimeter).arg(value));
       
   261 }