small cleanup: enum instead of magic numbers
authorsheepluva
Mon, 26 Sep 2011 00:15:10 +0200
changeset 6036 727d13409891
parent 6034 8cdfac630261
child 6038 58d9badf3e7f
small cleanup: enum instead of magic numbers
QTfrontend/hwform.cpp
QTfrontend/newnetclient.cpp
QTfrontend/newnetclient.h
--- a/QTfrontend/hwform.cpp	Sun Sep 25 23:29:04 2011 +0200
+++ b/QTfrontend/hwform.cpp	Mon Sep 26 00:15:10 2011 +0200
@@ -275,26 +275,27 @@
 #ifdef USE_XFIRE
 void HWForm::updateXfire(void)
 {
-    if(hwnet)
+    if(hwnet && (hwnet->clientState() != HWNewNet::Disconnected))
     {
         xfire_setvalue(XFIRE_SERVER, !hwnet->getHost().compare("netserver.hedgewars.org:46631") ? "Official server" : hwnet->getHost().toAscii());
-        switch(hwnet->getClientState())
+        switch(hwnet->clientState())
         {
-            case 1: // Connecting
+            case HWNewNet::Connecting: // Connecting
+            case HWNewNet::Connected:
             xfire_setvalue(XFIRE_STATUS, "Connecting");
             xfire_setvalue(XFIRE_NICKNAME, "-");
             xfire_setvalue(XFIRE_ROOM, "-");
-            case 2: // In lobby
+            case HWNewNet::InLobby: // In lobby
             xfire_setvalue(XFIRE_STATUS, "Online");
             xfire_setvalue(XFIRE_NICKNAME, hwnet->getNick().toAscii());
             xfire_setvalue(XFIRE_ROOM, "In game lobby");
             break;
-            case 3: // In room
+            case HWNewNet::InRoom: // In room
             xfire_setvalue(XFIRE_STATUS, "Online");
             xfire_setvalue(XFIRE_NICKNAME, hwnet->getNick().toAscii());
             xfire_setvalue(XFIRE_ROOM, (hwnet->getRoom() + " (waiting for players)").toAscii());
             break;
-            case 5: // In game
+            case HWNewNet::InGame: // In game
             xfire_setvalue(XFIRE_STATUS, "Online");
             xfire_setvalue(XFIRE_NICKNAME, hwnet->getNick().toAscii());
             xfire_setvalue(XFIRE_ROOM, (hwnet->getRoom() + " (playing or spectating)").toAscii());
@@ -783,7 +784,7 @@
     GoToPage(ID_PAGE_CONNECTING);
 
     connect(hwnet, SIGNAL(AskForRunGame()), this, SLOT(CreateNetGame()));
-    connect(hwnet, SIGNAL(Connected()), this, SLOT(NetConnected()));
+    connect(hwnet, SIGNAL(connected()), this, SLOT(NetConnected()));
     connect(hwnet, SIGNAL(Error(const QString&)), this, SLOT(NetError(const QString&)));
     connect(hwnet, SIGNAL(Warning(const QString&)), this, SLOT(NetWarning(const QString&)));
     connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter()));
@@ -899,7 +900,7 @@
     connect(ui.pageAdmin, SIGNAL(clearAccountsCache()), hwnet, SLOT(clearAccountsCache()));
 
 // disconnect
-    connect(hwnet, SIGNAL(Disconnected(const QString&)), this, SLOT(ForcedDisconnect(const QString&)), Qt::QueuedConnection);
+    connect(hwnet, SIGNAL(disconnected(const QString&)), this, SLOT(ForcedDisconnect(const QString&)), Qt::QueuedConnection);
 
 // config stuff
     connect(hwnet, SIGNAL(paramChanged(const QString &, const QStringList &)), ui.pageNetGame->pGameCFG, SLOT(setParam(const QString &, const QStringList &)));
--- a/QTfrontend/newnetclient.cpp	Sun Sep 25 23:29:04 2011 +0200
+++ b/QTfrontend/newnetclient.cpp	Mon Sep 26 00:15:10 2011 +0200
@@ -37,7 +37,7 @@
   isChief(false),
   m_game_connected(false),
   loginStep(0),
-  netClientState(0)
+  netClientState(Disconnected)
 {
 // socket stuff
     connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
@@ -52,13 +52,14 @@
     if (m_game_connected)
     {
         RawSendNet(QString("QUIT%1%2").arg(delimeter).arg("User quit"));
-        emit Disconnected(tr("User quit"));
+        emit disconnected(tr("User quit"));
     }
     NetSocket.flush();
 }
 
 void HWNewNet::Connect(const QString & hostName, quint16 port, const QString & nick)
 {
+    netClientState = Connecting;
     mynick = nick;
     myhost = hostName + QString(":%1").arg(port);
     NetSocket.connectToHost(hostName, port);
@@ -75,7 +76,7 @@
 
 void HWNewNet::CreateRoom(const QString & room)
 {
-    if(netClientState != 2)
+    if(netClientState != InLobby)
     {
         qWarning("Illegal try to create room!");
         return;
@@ -89,7 +90,7 @@
 
 void HWNewNet::JoinRoom(const QString & room)
 {
-    if(netClientState != 2)
+    if(netClientState != InLobby)
     {
         qWarning("Illegal try to join room!");
         return;
@@ -172,11 +173,13 @@
 
 void HWNewNet::OnConnect()
 {
+    netClientState = Connected;
 }
 
 void HWNewNet::OnDisconnect()
 {
-    if(m_game_connected) emit Disconnected("");
+    netClientState = Disconnected;
+    if(m_game_connected) emit disconnected("");
     m_game_connected = false;
 }
 
@@ -188,13 +191,13 @@
         case QAbstractSocket::RemoteHostClosedError:
             break;
         case QAbstractSocket::HostNotFoundError:
-            emit Disconnected(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 Disconnected(tr("Connection refused"));
+            emit disconnected(tr("Connection refused"));
             break;
         default:
-            emit Disconnected(NetSocket.errorString());
+            emit disconnected(NetSocket.errorString());
         }
 }
 
@@ -247,7 +250,7 @@
 
         RawSendNet(QString("NICK%1%2").arg(delimeter).arg(mynick));
         RawSendNet(QString("PROTO%1%2").arg(delimeter).arg(*cProtoVer));
-        netClientState = 1;
+        netClientState = Connected;
         m_game_connected = true;
         emit adminAccess(false);
         return;
@@ -284,7 +287,7 @@
             qWarning("Net: Empty CHAT message");
             return;
         }
-        if (netClientState == 2)
+        if (netClientState == InLobby)
             emit chatStringLobby(lst[1], HWProto::formatChatMsgForFrontend(lst[2]));
         else
             emit chatStringFromNet(HWProto::formatChatMsg(lst[1], lst[2]));
@@ -299,7 +302,7 @@
         }
         QStringList tmp = lst;
         tmp.removeFirst();
-        if (netClientState == 2)
+        if (netClientState == InLobby)
             emit chatStringLobby(tmp.join("\n").prepend('\x01'));
         else
             emit chatStringFromNet(tmp.join("\n").prepend('\x01'));
@@ -377,13 +380,13 @@
     }
 
     if(lst[0] == "ROOMABANDONED") {
-        netClientState = 2;
+        netClientState = InLobby;
         emit LeftRoom(tr("Room destroyed"));
         return;
     }
 
     if(lst[0] == "KICKED") {
-        netClientState = 2;
+        netClientState = InLobby;
         emit LeftRoom(tr("You got kicked"));
         return;
     }
@@ -399,24 +402,15 @@
         {
             if (lst[i] == mynick)
             {
-                netClientState = 3;
+                netClientState = InRoom;
                 emit EnteredGame();
                 emit roomMaster(isChief);
                 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]));
-            }
+
+            emit nickAdded(lst[i], isChief && (lst[i] != mynick));
+            emit chatStringFromNet(tr("%1 *** %2 has joined the room").arg('\x03').arg(lst[i]));
         }
         return;
     }
@@ -432,9 +426,9 @@
         {
             if (lst[i] == mynick)
             {
-                netClientState = 2;
+                netClientState = InLobby;
                 RawSendNet(QString("LIST"));
-                emit Connected();
+                emit connected();
             }
 
             emit nickAddedLobby(lst[i], false);
@@ -482,7 +476,7 @@
     }
 
     if (lst[0] == "RUN_GAME") {
-        netClientState = 5;
+        netClientState = InGame;
         emit AskForRunGame();
         return;
     }
@@ -589,7 +583,7 @@
         }
         m_game_connected = false;
         Disconnect();
-        emit Disconnected(lst[1]);
+        emit disconnected(lst[1]);
         return;
     }
 
@@ -665,7 +659,7 @@
 
 void HWNewNet::askRoomsList()
 {
-    if(netClientState != 2)
+    if(netClientState != InLobby)
     {
         qWarning("Illegal try to get rooms list!");
         return;
@@ -673,7 +667,7 @@
     RawSendNet(QString("LIST"));
 }
 
-int HWNewNet::getClientState()
+HWNewNet::ClientState HWNewNet::clientState()
 {
     return netClientState;
 }
@@ -700,7 +694,7 @@
 
 void HWNewNet::gameFinished(bool correctly)
 {
-    if (netClientState == 5) netClientState = 3;
+    if (netClientState == InGame) netClientState = InRoom;
     RawSendNet(QString("ROUNDFINISHED%1%2").arg(delimeter).arg(correctly ? "1" : "0"));
 }
 
@@ -755,13 +749,13 @@
 
 void HWNewNet::partRoom()
 {
-    netClientState = 2;
+    netClientState = InLobby;
     RawSendNet(QString("PART"));
 }
 
 bool HWNewNet::isInRoom()
 {
-    return netClientState > 2;
+    return netClientState >= InRoom;
 }
 
 void HWNewNet::setServerMessageNew(const QString & msg)
--- a/QTfrontend/newnetclient.h	Sun Sep 25 23:29:04 2011 +0200
+++ b/QTfrontend/newnetclient.h	Mon Sep 26 00:15:10 2011 +0200
@@ -39,6 +39,8 @@
   Q_OBJECT
 
  public:
+  enum ClientState { Disconnected, Connecting, Connected, InLobby, InRoom, InGame };
+
   HWNewNet();
   ~HWNewNet();
   void Connect(const QString & hostName, quint16 port, const QString & nick);
@@ -47,7 +49,7 @@
   void NewNick(const QString & nick);
   bool isRoomChief();
   bool isInRoom();
-  int getClientState();
+  ClientState clientState();
   QString getNick();
   QString getRoom();
   QString getHost();
@@ -88,12 +90,12 @@
   void handleNotice(int n);
 
   int loginStep;
-  int netClientState;
+  ClientState netClientState;
 
  signals:
   void AskForRunGame();
-  void Connected();
-  void Disconnected(const QString & reason);
+  void connected();
+  void disconnected(const QString & reason);
   void Error(const QString & errmsg);
   void Warning(const QString & wrnmsg);
   void AskForPassword(const QString & nick);