Explorar o código

Added command scheduler and s-p detection is delayed a bit.

Mihawk %!s(int64=12) %!d(string=hai) anos
pai
achega
370d130802
Modificáronse 2 ficheiros con 45 adicións e 11 borrados
  1. 31 8
      Client.cpp
  2. 14 3
      Client.h

+ 31 - 8
Client.cpp

@@ -41,13 +41,16 @@ Client::Client(App *app, ActiveClient* ac):
   myQWBroadcastFloodTimer(new QTimer()),
   mySpamBroadcastFloodTimer(new QTimer()),
   mySPSupport(false),
-  myAutoDetectSP(true),
-  myMaxClients(0)
+  mySPAutoDetect(true),
+  myMaxClients(0),
+  myCmdScheduledTimer(new QTimer())
 {
   myKeepNickTimer->setSingleShot(true);
   myFloodTimer->setSingleShot(true);
   myQWBroadcastFloodTimer->setSingleShot(true);
   mySpamBroadcastFloodTimer->setSingleShot(true);
+  mySPDetectionTimer->setSingleShot(true);
+  myCmdScheduledTimer->setSingleShot(true);
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -58,6 +61,7 @@ Client::~Client()
   delete myQWBroadcastFloodTimer;
   delete mySpamBroadcastFloodTimer;
   delete mySPDetectionTimer;
+  delete myCmdScheduledTimer;
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -446,13 +450,27 @@ void Client::run()
   //    myJoinMessagePrinted = true;
   //  }
 
-  /* Keep nick... Simply set name again after 30 secs */
+  // Keep nick... Simply set name again after 30 secs
   if(!myKeepNickTimer->isActive())
   {
     setName(Settings::globalInstance()->botName().toAscii().data());
     myKeepNickTimer->start(30000);
   }
 
+  // Scheduled commands
+  if(!myCmdScheduled.isEmpty() && !myCmdScheduledTimer->isActive())
+  {
+    if(myCmdScheduled == "say s-p")
+    {
+      say("s-p");
+      mySPDetectionTimer->start(2000);
+    }
+    else
+      sendCmd(myCmdScheduled.toAscii().data());
+
+    myCmdScheduled.clear();
+  }
+
   QWClient::run();
 }
 
@@ -471,7 +489,7 @@ void Client::onStuffedCmd(const char *cmd)
     myOnServerFlag = true;
 
     setPing(Settings::globalInstance()->botPing());
-    if(myAutoDetectSP)
+    if(mySPAutoDetect)
       spDetection();
   }
 }
@@ -502,15 +520,13 @@ void Client::onDownloadFinished()
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::setAutoDetectSP(bool autoDetect)
 {
-  myAutoDetectSP = autoDetect;
+  mySPAutoDetect = autoDetect;
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void Client::spDetection()
 {
-  say("s-p");
-  mySPDetectionTimer->setSingleShot(true);
-  mySPDetectionTimer->start(2000);
+  scheduleCmd("say s-p", 2000);
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -521,3 +537,10 @@ QString Client::parseNameFun(const QString &string)
 
   return QString(b);
 }
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+void Client::scheduleCmd(const QString &cmd, int time)
+{
+  myCmdScheduled = cmd;
+  myCmdScheduledTimer->start(time);
+}

+ 14 - 3
Client.h

@@ -229,7 +229,6 @@ private:
   bool              myQWMutedFlag;
 
   QTimer*           myKeepNickTimer;    // Interval in which the bot tries to retake his nickname
-  QTimer*           mySPDetectionTimer; // Timer to wait for a say s-p command reply (if it replies there is support for it)
 
   // Flood timers
   QTimer*           myFloodTimer;              // Floodtimer for all commands
@@ -245,8 +244,9 @@ private:
   bool							myFloodMsgPrinted;
 
   // Indicates whether this server supports s-p command
-  bool              mySPSupport;
-  bool              myAutoDetectSP; // If this is true the client will try to auto-detect the SP support
+  QTimer*           mySPDetectionTimer; // Timer to wait for a say s-p command reply (if it replies there is support for it)
+  bool              mySPSupport;        // Do we have SP support?
+  bool              mySPAutoDetect;     // If this is true the client will try to auto-detect the SP support
 
   // List of players on this server we are connected to
   PlayerList        myPlayerList;
@@ -254,6 +254,17 @@ private:
   // Max clients allowed on this server
   int               myMaxClients;
 
+  QTimer*           myCmdScheduledTimer; // Timer for one scheduled command
+  QString           myCmdScheduled; // The scheduled command
+
+  /**
+    Schedules a command for execution after the time specified expires
+
+    @param cmd The command
+    @param time The time to wait for the command to be sent
+  */
+  void scheduleCmd(const QString& cmd, int time);
+
   /**
     Prints a message directly to the console or telnet user.