Client.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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);
  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. Sets the client to try to autodetect s-p support.
  100. */
  101. void setAutoDetectSP(bool autoDetect);
  102. protected:
  103. /**
  104. Called everytime the level changes.
  105. @param playerNum
  106. @param levelName
  107. @param gravity
  108. @param stopSpeed
  109. @param maxSpeed
  110. @param spectatorMaxSpeed
  111. @param accelerate
  112. @param airAccelerate
  113. @param waterAccelerate
  114. @param friction
  115. @param waterFriction
  116. @param entGravity
  117. */
  118. 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);
  119. /**
  120. Called when the client is disconnected from the server.
  121. */
  122. void onDisconnect();
  123. /**
  124. Called when the client receives information to be printed on the screen.
  125. @param level Message level
  126. @param msg The message
  127. */
  128. void onPrint(int level, const char *msg);
  129. /**
  130. Called on (respectively):
  131. Challenge response.
  132. Connection acknowledged.
  133. Connected. (Not fully connected yet, signon start)
  134. */
  135. void onChallenge();
  136. void onConnection();
  137. void onConnected();
  138. /**
  139. Called when download of a file started.
  140. @param fileName The file to be downloaded
  141. */
  142. void onDownloadStarted(const char *fileName);
  143. /**
  144. Called when the last download finished.
  145. */
  146. void onDownloadFinished();
  147. /**
  148. Called when the progress for a download changed.
  149. @param percent Actual download percentage
  150. */
  151. void onDownloadProgress(int percent);
  152. /**
  153. Called when the server stuffs cmd into the client.
  154. @param cmd The command being stuffed into the client
  155. */
  156. void onStuffedCmd(const char *cmd);
  157. /**
  158. Called when an error occurs.
  159. @param description The error description
  160. */
  161. void onError(const char *description);
  162. /**
  163. Called when the client receives an out-of-band print.
  164. @param msg The message to be printed
  165. */
  166. void onOOBPrint(const char *msg);
  167. private:
  168. // The monitor that monitors the server that this client connects to
  169. ActiveClient* myActiveClient;
  170. // Pointer to the application
  171. App* myApp;
  172. // The last line printed into our client
  173. QString myPrintLine;
  174. // Flag to avoid information to repeat on the client
  175. bool myDownloadProgressPrintedFlag;
  176. // Flag to indicate if the client is fully connected
  177. bool myOnServerFlag;
  178. // Flags to indicate whether frequencies are muted or not
  179. bool mySpamMutedFlag;
  180. bool myQWMutedFlag;
  181. QTimer* myKeepNickTimer; // Interval in which the bot tries to retake his nickname
  182. // Flood timers
  183. QTimer* myFloodTimer; // Floodtimer for all commands
  184. QTimer* myQWBroadcastFloodTimer; // Floodtimer for .qw broadcasts
  185. QTimer* mySpamBroadcastFloodTimer; // Floodtimer for .spam broadcasts
  186. // Used to calculate how much time is there left on the flood timers
  187. QTime myFloodTimerStart;
  188. QTime myQWBroadcastFloodTimerStart;
  189. QTime mySpamBroadcastFloodTimerStart;
  190. // Used to avoid information printed countless times
  191. bool myFloodMsgPrinted;
  192. // Indicates whether this server supports s-p command
  193. QTimer* mySPDetectionTimer; // Timer to wait for a say s-p command reply (if it replies there is support for it)
  194. bool mySPSupport; // Do we have SP support?
  195. bool mySPAutoDetect; // If this is true the client will try to auto-detect the SP support
  196. // List of players on this server we are connected to
  197. PlayerList myPlayerList;
  198. // Max clients allowed on this server
  199. int myMaxClients;
  200. QTimer* myCmdScheduledTimer; // Timer for one scheduled command
  201. QString myCmdScheduled; // The scheduled command
  202. /**
  203. Schedules a command for execution after the time specified expires
  204. @param cmd The command
  205. @param time The time to wait for the command to be sent
  206. */
  207. void scheduleCmd(const QString& cmd, int time);
  208. /**
  209. Prints a message directly to the console or telnet user.
  210. @param msg The message
  211. */
  212. void print(const QString& msg);
  213. /**
  214. Parses the last line that was printed.
  215. */
  216. void parsePrintedLine();
  217. /**
  218. Returns the player count on this server.
  219. @return The player count
  220. */
  221. int playerCount() const;
  222. /**
  223. Returns the maximum number of clients allowed on this server.
  224. @return The maximum number of clients allowed
  225. */
  226. int maxClients() const;
  227. /**
  228. Detects whether this server supports s-p command or not.
  229. */
  230. void spDetection();
  231. /**
  232. Parses any namefun string and converts it to readable text.
  233. @param string The text to be parsed
  234. */
  235. static QString parseNameFun(const QString& string);
  236. };
  237. #endif // CLIENT_H