Explorar el Código

Now counting the reply from central when broadcasting and replying to BC
requests with the count of servers and players.

Mihawk hace 12 años
padre
commit
aa534d98e0
Se han modificado 6 ficheros con 164 adiciones y 34 borrados
  1. 98 3
      App.cpp
  2. 17 0
      App.h
  3. 11 16
      Client.cpp
  4. 3 13
      Client.h
  5. 34 1
      SshClient.cpp
  6. 1 1
      cimsqwbot.pro

+ 98 - 3
App.cpp

@@ -366,8 +366,8 @@ void App::say(const QString &msg, int *serverCount, int *userCount)
     if(ac->client()->state() == Client::ConnectedState)
     {
       ac->client()->say(msg);
-      *userCount += ac->queryThread()->playerCount();
-      *serverCount++;
+      *userCount += ac->queryThread()->playerCount() - 1;
+      (*serverCount)++;
     }
 	}
 	print("Say command sent to all clients\n");
@@ -408,6 +408,26 @@ void App::listClients()
 	}
 }
 
+void App::activeClientsReplyCounters(int *serverCount, int *playerCount)
+{
+  ActiveClient* ac;
+
+  foreach(ac, myClients)
+  {
+    if(ac->client()->state() == Client::ConnectedState)
+    {
+      int pc = ac->queryThread()->playerCount();
+      if(pc == 255 || pc == 0)
+        pc = 0;
+      else
+        pc--;
+
+      *playerCount += pc;
+      (*serverCount)++;
+    }
+  }
+}
+
 void App::timerEvent(QTimerEvent *e)
 {
 	if(e->timerId() == myClientsFrameTimerID)
@@ -455,12 +475,41 @@ App::~App()
 	delete myServer;
 }
 
+void App::setReplyHash(const QString &serverAddress, const QString &hash)
+{
+  ActiveClient* ac;
+  foreach(ac, myClients)
+  {
+    if(serverAddress == ac->serverAddressString())
+    {
+      ac->setReplyHash(hash);
+      return;
+    }
+  }
+}
+
+void App::incrementReplyCounters(const QString &hash, int userCount, int serverCount)
+{
+  ActiveClient* ac;
+  foreach(ac, myClients)
+  {
+    if(ac->replyHash() == hash)
+    {
+      ac->incrementReplyCounters(userCount, serverCount);
+      return;
+    }
+  }
+}
+
 //========================================================================
 
 App::ActiveClient::ActiveClient(App *app):
   myApp(app),
   myClient(new Client(app)),
-  myQueryThread(new ServerQueryThread())
+  myQueryThread(new ServerQueryThread()),
+  myBroadcastReplyTimer(new QTimer()),
+  myUniqueUserCount(0),
+  myUniqueServerCount(0)
 {
 }
 
@@ -473,6 +522,7 @@ App::ActiveClient::~ActiveClient()
   while(queryThread()->isRunning());
 
   delete myQueryThread;
+  delete myBroadcastReplyTimer;
 }
 
 Client* App::ActiveClient::client()
@@ -492,6 +542,7 @@ void App::ActiveClient::setAddress(const QHostAddress &address, quint16 port)
 
 void App::ActiveClient::run()
 {
+  /* If the client is disconnect check if there is at least one player connected to the server */
   if(client()->state() == Client::DisconnectedState)
   {
     if(!queryThread()->isRunning())
@@ -509,6 +560,7 @@ void App::ActiveClient::run()
     return;
   }
 
+  /* If the client is connected and left alone on the server, disconnect yourself */
   if(client()->state() == Client::ConnectedState)
   {
     int playerCount = queryThread()->playerCount();
@@ -520,5 +572,48 @@ void App::ActiveClient::run()
       return;
     }
   }
+
+  /* Say the broadcast count */
+  if(!myBroadcastReplyTimer->isActive() && myReplyTimerWasActive)
+  {
+    myApp->activeClientsReplyCounters(&myUniqueServerCount, &myUniqueUserCount); //add my internal counter to the list
+
+    client()->say("::cims:: Sent to " + QString::number(myUniqueServerCount) + " channels, " + QString::number(myUniqueUserCount) + " unique users.");
+    myUniqueServerCount = myUniqueUserCount = 0;
+  }
+  myReplyTimerWasActive = myBroadcastReplyTimer->isActive();
+
   client()->run();
 }
+
+const QString App::ActiveClient::serverAddressString()
+{
+  return QString(queryThread()->serverAddress().toString() + ':' + QString::number(queryThread()->serverPort()));
+}
+
+void App::ActiveClient::setReplyHash(const QString &hash)
+{
+  myReplyHash = hash;
+
+  /* Wait for 5 seconds */
+  myBroadcastReplyTimer->setSingleShot(true);
+  myBroadcastReplyTimer->start(5000);
+}
+
+const QString& App::ActiveClient::replyHash() const
+{
+  return myReplyHash;
+}
+
+bool App::ActiveClient::isWaitingReply() const
+{
+  return myBroadcastReplyTimer->isActive();
+}
+
+void App::ActiveClient::incrementReplyCounters(int userCount, int serverCount)
+{
+  if(!myBroadcastReplyTimer->isActive())
+    return;
+  myUniqueUserCount += userCount;
+  myUniqueServerCount += serverCount;
+}

+ 17 - 0
App.h

@@ -6,6 +6,7 @@
 #include <QSettings>
 #include <QHostAddress>
 #include <QTime>
+#include <QTimer>
 
 class SshClient;
 class QTcpSocket;
@@ -24,6 +25,9 @@ public:
   void									broadcast(const QString& msg, int *serverCount, int *userCount);
   void                  broadcast(const QString& msg);
   void                  requestBroadcast(const QString& type, const QString& user, const QString& server, const QString& message);
+  void                  setReplyHash(const QString& serverAddress, const QString& hash);
+  void                  incrementReplyCounters(const QString& hash, int userCount, int serverCount);
+  void                  activeClientsReplyCounters(int *serverCount, int *playerCount);
 
 protected:
 	void									timerEvent(QTimerEvent *e);
@@ -40,12 +44,25 @@ private:
     void              run();
     void              setAddress(const QHostAddress &address, quint16 port);
 
+    const QString     serverAddressString();
+    void              setReplyHash(const QString& hash); //this activates the 5 seconds reply timer too
+    const QString&    replyHash() const;
+    void              incrementReplyCounters(int userCount, int serverCount);
+    bool              isWaitingReply() const;
+
   private:
     App*              myApp;
     Client*           myClient;
     ServerQueryThread*myQueryThread;
     ActiveClient(const ActiveClient&) {}
     QTime             myDisconnectTime;
+
+    /* Count server reply while timer is active */
+    QString           myReplyHash;
+    QTimer*           myBroadcastReplyTimer;
+    int               myUniqueUserCount;
+    int               myUniqueServerCount;
+    bool              myReplyTimerWasActive;
   };
 
   QTcpSocket*           mySocket;

+ 11 - 16
Client.cpp

@@ -63,22 +63,6 @@ void Client::print(const QString &msg)
 	}
 }
 
-void Client::yellowText(const QString &text)
-{
-	unsigned char *i;
-
-	for(i = (unsigned char*)text.toAscii().data(); *i; i++)
-	{
-		if(*i >= '0' && *i <= '9')
-			*i += (unsigned char) (18 - '0');
-		else if(*i > 32 && *i < 128)
-			*i |= 128;
-		else if(*i == 13)
-			*i = ' ';
-	}
-	return;
-}
-
 void Client::onDisconnect()
 {
 	print("Disconnected..\n");
@@ -144,7 +128,10 @@ void Client::parsePrintedLine()
 
     //-qw- Skillah - #crazy88 : 4on4 MIX qw.foppa.dk:27503 7/8
     myApp->broadcast(message);
+
     //myApp->requestBroadcast(command, nick, server, args.trimmed());
+
+    nick = parseNameFun(nick); //for the irc message namefun must be removed.
     myApp->requestBroadcast("dev", nick, server, args.trimmed());
 
 		return;
@@ -252,3 +239,11 @@ void Client::onDownloadFinished()
 {
 	print("Download 100% finished.\n");
 }
+
+QString Client::parseNameFun(const QString &string)
+{
+  QByteArray b(string.toAscii());
+  QWClient::stripColor(b.data());
+
+  return QString(b);
+}

+ 3 - 13
Client.h

@@ -21,7 +21,6 @@ public:
 	void setSocket(class QTcpSocket* socket);
   void retryConnection(); //unused
   bool isOnServer() const;
-//  quint8 playerCount() const;
 
 protected:
 	void onLevelChanged(int playerNum, const char *levelName, float gravity, float stopSpeed, float maxSpeed, float spectatorMaxSpeed, float accelerate, float airAccelerate, float waterAccelerate, float friction, float waterFriction, float entGravity);
@@ -47,20 +46,11 @@ private:
 	QString						myPrintLine;
 	bool							myDownloadProgressPrintedFlag;
   bool              myOnServerFlag;
-//	struct Player {
-//		int slot;
-//		int userID;
-//		QString team;
-//		QString name;
-//	};
-//  Player*           myPlayers[32];
-//  quint8            myPlayerCount;
-//  void cleanupPlayers();
 
-	void print(const QString& msg);
+  void print(const QString& msg);
 	void parsePrintedLine();
-//	static const QString& getInfoKeyValue(const QStringList& infoString, const QString& key);
-	static void yellowText(const QString& text);
+
+  static QString parseNameFun(const QString& string);
 };
 
 #endif // CLIENT_H

+ 34 - 1
SshClient.cpp

@@ -158,7 +158,40 @@ void SshClient::parse(const QDateTime &time, const QString &command, const QStri
     int serverCount, userCount;
 
     myApp->broadcast(a.cap(3) + " " + a.cap(5) + " - " + a.cap(4) + " : " + a.cap(6), &serverCount, &userCount);
-    write("BC_RE " + a.cap(1) + " " + QString::number(userCount) + "," + QString::number(serverCount) + "\n");
+    write("BC_RE " + a.cap(1) + " Players=" + QString::number(userCount) + ",Servers=" + QString::number(serverCount) + "\n");
+
+    return;
+  }
+
+  /* BC_ID - Broadcast reply with the ID of the broadcast you just generated */
+  if(command == "BC_ID")
+  {
+    //BC_ID 4e5fca581569e168c7a86a0a9a91949f for: QDEV,-dev-,qw://123.123
+    QRegExp a("^([A-Za-z0-9]+) for: .+,-(.+)-,qw://(.+)$");
+    if(a.indexIn(commandData) == -1)
+      return;
+
+    QString hash = a.cap(1);
+    //QString frequency = a.cap(2);
+    QString server = a.cap(3);
+
+    myApp->setReplyHash(server, hash);
+
+    return;
+  }
+
+  /* BC_RE - Broadcast reply to increment the counters */
+  if(command == "BC_RE")
+  {
+    QRegExp a("^([A-Za-z0-9]+) (Users|Players)=([0-9]+),(Channels|Servers)=([0-9]+)$");
+    if(a.indexIn(commandData) == -1)
+      return;
+
+    QString hash = a.cap(1);
+    int     userCount = a.cap(3).toInt();
+    int     serverCount = a.cap(5).toInt();
+
+    myApp->incrementReplyCounters(hash, userCount, serverCount);
 
     return;
   }

+ 1 - 1
cimsqwbot.pro

@@ -1,6 +1,6 @@
 TEMPLATE = app
 CONFIG += console
-QT     += network sql xml
+QT     += network sql
 QT     -= gui
 
 SOURCES += main.cpp \