QTfrontend/newnetclient.cpp
branchhedgeroid
changeset 6023 a28be05b20bc
parent 6015 daffc14a518a
child 6024 d38da7c19e43
--- a/QTfrontend/newnetclient.cpp	Sun Sep 25 18:22:12 2011 +0200
+++ b/QTfrontend/newnetclient.cpp	Sun Sep 25 18:24:01 2011 +0200
@@ -24,10 +24,7 @@
 #include "hwconsts.h"
 #include "newnetclient.h"
 #include "proto.h"
-#include "gameuiconfig.h"
 #include "game.h"
-#include "gamecfgwidget.h"
-#include "teamselect.h"
 #include "misc.h"
 /* only to get the ignoreList from the chat widget */
 #include "hwform.h"
@@ -36,10 +33,7 @@
 
 char delimeter='\n';
 
-HWNewNet::HWNewNet(GameUIConfig * config, GameCFGWidget* pGameCFGWidget, TeamSelWidget* pTeamSelWidget) :
-  config(config),
-  m_pGameCFGWidget(pGameCFGWidget),
-  m_pTeamSelWidget(pTeamSelWidget),
+HWNewNet::HWNewNet() :
   isChief(false),
   m_game_connected(false),
   loginStep(0),
@@ -51,11 +45,6 @@
     connect(&NetSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnect()));
     connect(&NetSocket, SIGNAL(error(QAbstractSocket::SocketError)), this,
             SLOT(displayError(QAbstractSocket::SocketError)));
-
-// config stuff
-    connect(this, SIGNAL(paramChanged(const QString &, const QStringList &)), pGameCFGWidget, SLOT(setParam(const QString &, const QStringList &)));
-    connect(pGameCFGWidget, SIGNAL(paramChanged(const QString &, const QStringList &)), this, SLOT(onParamChanged(const QString &, const QStringList &)));
-    connect(this, SIGNAL(configAsked()), pGameCFGWidget, SLOT(fullNetConfig()));
 }
 
 HWNewNet::~HWNewNet()
@@ -63,7 +52,7 @@
     if (m_game_connected)
     {
         RawSendNet(QString("QUIT%1%2").arg(delimeter).arg("User quit"));
-        emit Disconnected();
+        emit Disconnected(tr("User quit"));
     }
     NetSocket.flush();
 }
@@ -71,15 +60,6 @@
 void HWNewNet::Connect(const QString & hostName, quint16 port, const QString & nick)
 {
     mynick = nick;
-    while (mynick.isEmpty()) {
-        mynick = QInputDialog::getText(m_pGameCFGWidget,
-                 QObject::tr("Nickname"),
-                 QObject::tr("Please enter your nickname"),
-                 QLineEdit::Normal,
-                 QDir::home().dirName());
-        config->setValue("net/nick",mynick);
-        config->updNetNick();
-    }
     myhost = hostName + QString(":%1").arg(port);
     NetSocket.connectToHost(hostName, port);
 }
@@ -124,27 +104,32 @@
 void HWNewNet::AddTeam(const HWTeam & team)
 {
     QString cmd = QString("ADD_TEAM") + delimeter +
-         team.TeamName + delimeter +
-         team.teamColor.name() + delimeter +
-         team.Grave + delimeter +
-         team.Fort + delimeter +
-         team.Voicepack + delimeter +
-         team.Flag + delimeter +
-         QString::number(team.difficulty);
+         team.name() + delimeter +
+         team.color().name() + delimeter +
+         team.grave() + delimeter +
+         team.fort() + delimeter +
+         team.voicepack() + delimeter +
+         team.flag() + delimeter +
+         QString::number(team.difficulty());
 
-    for(int i = 0; i < 8; ++i)
+    for(unsigned int i = 0; i < HEDGEHOGS_PER_TEAM; ++i)
     {
         cmd.append(delimeter);
-        cmd.append(team.Hedgehogs[i].Name);
+        cmd.append(team.hedgehog(i).Name);
         cmd.append(delimeter);
-        cmd.append(team.Hedgehogs[i].Hat);
+        cmd.append(team.hedgehog(i).Hat);
     }
     RawSendNet(cmd);
 }
 
 void HWNewNet::RemoveTeam(const HWTeam & team)
 {
-    RawSendNet(QString("REMOVE_TEAM") + delimeter + team.TeamName);
+    RawSendNet(QString("REMOVE_TEAM") + delimeter + team.name());
+}
+
+void HWNewNet::NewNick(const QString & nick)
+{
+    RawSendNet(QString("NICK%1%2").arg(delimeter).arg(nick));
 }
 
 void HWNewNet::ToggleReady()
@@ -166,7 +151,7 @@
 
 void HWNewNet::RawSendNet(const QByteArray & buf)
 {
-  qDebug() << "Client: " << QString(buf).split("\n");
+    qDebug() << "Client: " << QString(buf).split("\n");
     NetSocket.write(buf);
     NetSocket.write("\n\n", 2);
 }
@@ -191,29 +176,33 @@
 
 void HWNewNet::OnDisconnect()
 {
-    if(m_game_connected) emit Disconnected();
+    if(m_game_connected) emit Disconnected("");
     m_game_connected = false;
 }
 
 void HWNewNet::displayError(QAbstractSocket::SocketError socketError)
 {
-    emit Disconnected();
     m_game_connected = false;
 
     switch (socketError) {
         case QAbstractSocket::RemoteHostClosedError:
             break;
         case QAbstractSocket::HostNotFoundError:
-            emit showMessage(tr("The host was not found. Please check the host name and port settings."));
+            emit Disconnected(tr("The host was not found. Please check the host name and port settings."));
             break;
         case QAbstractSocket::ConnectionRefusedError:
-            emit showMessage(tr("Connection refused"));
+            emit Disconnected(tr("Connection refused"));
             break;
         default:
-            emit showMessage(NetSocket.errorString());
+            emit Disconnected(NetSocket.errorString());
         }
 }
 
+void HWNewNet::SendPasswordHash(const QString & hash)
+{
+    RawSendNet(QString("PASSWORD%1%2").arg(delimeter).arg(hash));
+}
+
 void HWNewNet::ParseCmd(const QStringList & lst)
 {
   qDebug() << "Server: " << lst;
@@ -235,17 +224,17 @@
 
     if (lst[0] == "ERROR") {
         if (lst.size() == 2)
-            emit showMessage("Error: " + lst[1]);
+            emit Error(lst[1]);
         else
-            emit showMessage("Unknown error");
+            emit Error("Unknown error");
         return;
     }
 
     if (lst[0] == "WARNING") {
         if (lst.size() == 2)
-            emit showMessage("Warning: " + lst[1]);
+            emit Warning(lst[1]);
         else
-            emit showMessage("Unknown warning");
+            emit Warning("Unknown warning");
         return;
     }
 
@@ -383,21 +372,19 @@
             qWarning("Net: Bad REMOVETEAM message");
             return;
         }
-        m_pTeamSelWidget->removeNetTeam(HWTeam(lst[1]));
+        emit RemoveNetTeam(HWTeam(lst[1]));
         return;
     }
 
     if(lst[0] == "ROOMABANDONED") {
         netClientState = 2;
-        emit showMessage(HWNewNet::tr("Room destroyed"));
-        emit LeftRoom();
+        emit LeftRoom(tr("Room destroyed"));
         return;
     }
 
     if(lst[0] == "KICKED") {
         netClientState = 2;
-        emit showMessage(HWNewNet::tr("You got kicked"));
-        emit LeftRoom();
+        emit LeftRoom(tr("You got kicked"));
         return;
     }
 
@@ -418,11 +405,14 @@
                 if (isChief)
                     emit configAsked();
             }
+            // TODO reactivate
+/*
             if (lst[i] != mynick && isChief && config->Form->ui.pageRoomsList->chatWidget->ignoreList.contains(lst[i], Qt::CaseInsensitive) && !config->Form->ui.pageRoomsList->chatWidget->friendsList.contains(lst[i], Qt::CaseInsensitive))
             {
                 kickPlayer(lst[i]);
             }
             else
+*/
             {
                 emit nickAdded(lst[i], isChief && (lst[i] != mynick));
                 emit chatStringFromNet(tr("%1 *** %2 has joined the room").arg('\x03').arg(lst[i]));
@@ -498,28 +488,7 @@
     }
 
     if (lst[0] == "ASKPASSWORD") {
-        bool ok = false;
-        int passLength = config->value("net/passwordlength", 0).toInt();
-        QString hash = config->value("net/passwordhash", "").toString();
-        
-        // If the password is blank, ask the user to enter one in
-        if (passLength == 0)
-        {
-            QString password = QInputDialog::getText(m_pGameCFGWidget, tr("Password"), tr("Your nickname %1 is\nregistered on Hedgewars.org\nPlease provide your password below\nor pick another nickname in game config:").arg(mynick), QLineEdit::Password, passLength==0?NULL:QString(passLength,'\0'), &ok);
-
-            if (!ok) {
-                Disconnect();
-                emit Disconnected();
-                return;
-            }
-            
-            hash = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Md5).toHex();
-            config->setValue("net/passwordhash", hash);
-            config->setValue("net/passwordlength", password.size());
-            config->setNetPasswordLength(password.size());
-        }
-
-        RawSendNet(QString("PASSWORD%1%2").arg(delimeter).arg(hash));
+        emit AskForPassword(mynick);
         return;
     }
 
@@ -549,7 +518,7 @@
             qWarning("Net: Bad TEAM_ACCEPTED message");
             return;
         }
-        m_pTeamSelWidget->changeTeamStatus(lst[1]);
+        emit TeamAccepted(lst[1]);
         return;
     }
 
@@ -577,7 +546,7 @@
             return;
         }
         HWTeam tmptm(lst[1]);
-        tmptm.numHedgehogs = lst[2].toUInt();
+        tmptm.setNumHedgehogs(lst[2].toUInt());
         emit hhnumChanged(tmptm);
         return;
     }
@@ -589,7 +558,7 @@
             return;
         }
         HWTeam tmptm(lst[1]);
-        tmptm.teamColor = QColor(lst[2]);
+        tmptm.setColor(QColor(lst[2]));
         emit teamColorChanged(tmptm);
         return;
     }
@@ -616,11 +585,11 @@
         }
         if (lst[1] == "Authentication failed")
         {
-            // Set the password blank if case the user tries to join and enter his password again
-            config->setValue("net/passwordlength", 0);
-            config->setNetPasswordLength(0);
+            emit AuthFailed();
         }
-        emit showMessage(HWNewNet::tr("Quit reason: ") + lst[1]);
+        m_game_connected = false;
+        Disconnect();
+        emit Disconnected(lst[1]);
         return;
     }
 
@@ -633,15 +602,11 @@
     if (lst[0] == "ROOM_CONTROL_ACCESS") {
         if (lst.size() < 2)
         {
-            qWarning("Net: Bad BYE message");
+            qWarning("Net: Bad ROOM_CONTROL_ACCESS message");
             return;
         }
-        bool b = lst[1] != "0";
-        m_pGameCFGWidget->setEnabled(b);
-        m_pTeamSelWidget->setInteractivity(b);
-        isChief = b;
+        isChief = (lst[1] != "0");
         emit roomMaster(isChief);
-
         return;
     }
 
@@ -653,8 +618,8 @@
     if (isChief)
     RawSendNet(QString("HH_NUM%1%2%1%3")
             .arg(delimeter)
-            .arg(team.TeamName)
-            .arg(team.numHedgehogs));
+            .arg(team.name())
+            .arg(team.numHedgehogs()));
 }
 
 void HWNewNet::onTeamColorChanged(const HWTeam& team)
@@ -662,8 +627,8 @@
     if (isChief)
     RawSendNet(QString("TEAM_COLOR%1%2%1%3")
             .arg(delimeter)
-            .arg(team.TeamName)
-            .arg(team.teamColor.name()));
+            .arg(team.name())
+            .arg(team.color().name()));
 }
 
 void HWNewNet::onParamChanged(const QString & param, const QStringList & value)
@@ -825,21 +790,7 @@
     {
         case 0:
         {
-            bool ok = false;
-            QString newNick = QInputDialog::getText(0, tr("Nickname"), tr("Some one already uses\n your nickname %1\non the server.\nPlease pick another nickname:").arg(mynick), QLineEdit::Normal, mynick, &ok);
-
-            if (!ok || newNick.isEmpty()) {
-                Disconnect();
-                emit Disconnected();
-                return;
-            }
-
-            config->setValue("net/nick", newNick);
-            config->updNetNick();
-            mynick = newNick;
-
-            RawSendNet(QString("NICK%1%2").arg(delimeter).arg(newNick));
-
+            emit NickTaken(mynick);
             break;
         }
     }