Client.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #ifndef CLIENT_H
  2. #define CLIENT_H
  3. #include "QWClient.h"
  4. #include <QString>
  5. #include <QStringList>
  6. #include <QList>
  7. #include <QTime>
  8. #include "ServerQuery.h"
  9. class App;
  10. class QTime;
  11. class QTimer;
  12. class ActiveClient;
  13. /**
  14. The QW BOT itself.
  15. @author Mihawk <luiz@netdome.biz>
  16. @file Client.h
  17. */
  18. class Client : public QWClient
  19. {
  20. public:
  21. /**
  22. Constructor.
  23. @param app Pointer to the application
  24. @param ac Pointer to the monitoring class
  25. @param supportsSendPrivate Indicates if this server supports s-p command (All messages are sent privately to the user on the server)
  26. */
  27. Client(App* app, ActiveClient* ac, bool supportsSendPrivate = false);
  28. /**
  29. Destructor.
  30. */
  31. ~Client();
  32. /**
  33. Connects the bot to the QW server specified.
  34. @param host Server host
  35. @param port Server port
  36. */
  37. void connect(const char *host, quint16 port);
  38. /**
  39. Runs the QW client, must be called everyframe or so
  40. otherwise the bot won't run.
  41. */
  42. void run();
  43. /**
  44. Disconnects the QW client from the server.
  45. */
  46. void disconnect();
  47. /**
  48. Says something on the server we are in.
  49. If the nickname is specified s-p will be used if possible.
  50. */
  51. void say(const QString& msg, const QString& nickName = "");
  52. /**
  53. Sets the bot team.
  54. */
  55. void setTeam(const QString& msg);
  56. /**
  57. Returns whether we are fully connected to a server.
  58. @return True if fully connected, otherwise false
  59. */
  60. bool isOnServer() const;
  61. /**
  62. Returns whether the frequencies are muted.
  63. @return True if the frequency is muted, otherwise returns false
  64. */
  65. bool isQWMuted() const;
  66. bool isSpamMuted() const;
  67. /**
  68. Sets the player list that are on this server
  69. including the bot in it. This information comes
  70. from the Query object outside this class.
  71. FIXME: Make the bot parse the player list inside itself.
  72. @param playerList The player list
  73. */
  74. void setPlayerList(PlayerList &playerList);
  75. /**
  76. Sets the maxclients that can connect to this server
  77. currenctly this information comes from the Query object.
  78. FIXME: Make the bot gather this info himself, since he is
  79. connected to the server.
  80. @param maxClients The maximum number of clients that can connect to this
  81. particular server
  82. */
  83. void setMaxClients(int maxClients);
  84. /**
  85. Returns whether this server supports s-p feature or not.
  86. @return True if supports, otherwise false
  87. */
  88. bool supportsSendPrivate() const;
  89. protected:
  90. /**
  91. Called everytime the level changes.
  92. @param playerNum
  93. @param levelName
  94. @param gravity
  95. @param stopSpeed
  96. @param maxSpeed
  97. @param spectatorMaxSpeed
  98. @param accelerate
  99. @param airAccelerate
  100. @param waterAccelerate
  101. @param friction
  102. @param waterFriction
  103. @param entGravity
  104. */
  105. 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);
  106. /**
  107. Called when the client is disconnected from the server.
  108. */
  109. void onDisconnect();
  110. /**
  111. Called when the client receives information to be printed on the screen.
  112. @param level Message level
  113. @param msg The message
  114. */
  115. void onPrint(int level, const char *msg);
  116. /**
  117. Called on (respectively):
  118. Challenge response.
  119. Connection acknowledged.
  120. Connected. (Not fully connected yet, signon start)
  121. */
  122. void onChallenge();
  123. void onConnection();
  124. void onConnected();
  125. /**
  126. Called when download of a file started.
  127. @param fileName The file to be downloaded
  128. */
  129. void onDownloadStarted(const char *fileName);
  130. /**
  131. Called when the last download finished.
  132. */
  133. void onDownloadFinished();
  134. /**
  135. Called when the progress for a download changed.
  136. @param percent Actual download percentage
  137. */
  138. void onDownloadProgress(int percent);
  139. /**
  140. Called when the server stuffs cmd into the client.
  141. @param cmd The command being stuffed into the client
  142. */
  143. void onStuffedCmd(const char *cmd);
  144. /**
  145. Called when an error occurs.
  146. @param description The error description
  147. */
  148. void onError(const char *description);
  149. /**
  150. Called when the client receives an out-of-band print.
  151. @param msg The message to be printed
  152. */
  153. void onOOBPrint(const char *msg);
  154. private:
  155. // The monitor that monitors the server that this client connects to
  156. ActiveClient* myActiveClient;
  157. // Pointer to the application
  158. App* myApp;
  159. // The last line printed into our client
  160. QString myPrintLine;
  161. // Flag to avoid information to repeat on the client
  162. bool myDownloadProgressPrintedFlag;
  163. // Flag to indicate if the client is fully connected
  164. bool myOnServerFlag;
  165. // Flags to indicate whether frequencies are muted or not
  166. bool mySpamMutedFlag;
  167. bool myQWMutedFlag;
  168. QTimer* myJoinMessageTimer; // After that time the bot will say hi.
  169. QTimer* myKeepNickTimer; // Interval in which the bot tries to retake his nickname
  170. // Flood timers
  171. QTimer* myFloodTimer; // Floodtimer for all commands
  172. QTimer* myQWBroadcastFloodTimer; // Floodtimer for .qw broadcasts
  173. QTimer* mySpamBroadcastFloodTimer; // Floodtimer for .spam broadcasts
  174. // Used to calculate how much time is there left on the flood timers
  175. QTime myFloodTimerStart;
  176. QTime myQWBroadcastFloodTimerStart;
  177. QTime mySpamBroadcastFloodTimerStart;
  178. // Used to avoid information printed countless times
  179. bool myJoinMessagePrinted;
  180. bool myFloodMsgPrinted;
  181. // Inicates whether bot has to say Hi
  182. bool myJoinMessageScheduled;
  183. // Indicates whether this server supports s-p command
  184. // This is set manually the client doesn't auto detect it
  185. bool mySupportsSendPrivate;
  186. // List of players on this server we are connected to
  187. PlayerList myPlayerList;
  188. // Max clients allowed on this server
  189. int myMaxClients;
  190. /**
  191. Prints a message directly to the console or telnet user.
  192. @param msg The message
  193. */
  194. void print(const QString& msg);
  195. /**
  196. Parses the last line that was printed.
  197. */
  198. void parsePrintedLine();
  199. /**
  200. Returns the player count on this server.
  201. @return The player count
  202. */
  203. int playerCount() const;
  204. /**
  205. Returns the maximum number of clients allowed on this server.
  206. @return The maximum number of clients allowed
  207. */
  208. int maxClients() const;
  209. /**
  210. Parses any namefun string and converts it to readable text.
  211. @param string The text to be parsed
  212. */
  213. static QString parseNameFun(const QString& string);
  214. };
  215. #endif // CLIENT_H