Client.h 7.5 KB

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