Client.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "Client.h"
  2. #include <QString>
  3. #include <stdio.h>
  4. #include <QTcpSocket>
  5. #include <QStringList>
  6. #include "App.h"
  7. Client::Client(App *app):
  8. QWClient(),
  9. myApp(app),
  10. mySocket(NULL),
  11. myConnectionRetries(0),
  12. myOnServerFlag(false)
  13. {
  14. }
  15. Client::~Client()
  16. {
  17. }
  18. void Client::setSocket(QTcpSocket *socket)
  19. {
  20. mySocket = socket;
  21. }
  22. void Client::say(const QString &msg)
  23. {
  24. QString cmd = "say " + msg;
  25. sendCmd(cmd.toAscii().data());
  26. }
  27. void Client::sayTeam(const QString &msg)
  28. {
  29. QString cmd = "say_team " + msg;
  30. sendCmd(cmd.toAscii().data());
  31. }
  32. void Client::setTeam(const QString &msg)
  33. {
  34. sendCmd(QString("setinfo \"team\" \"" + msg + "\"").toAscii().data());
  35. }
  36. void Client::disconnect()
  37. {
  38. QWClient::disconnect();
  39. myOnServerFlag = false;
  40. }
  41. void Client::print(const QString &msg)
  42. {
  43. QString str;
  44. str = QString(host()) + ":" + QString::number(port()) + "> " + msg;
  45. QByteArray b = str.toAscii();
  46. Client::stripColor(b.data());
  47. str = QString::fromAscii(b.data());
  48. printf("%s", str.toAscii().data());
  49. if(mySocket)
  50. {
  51. mySocket->write(str.toAscii());
  52. mySocket->waitForBytesWritten();
  53. }
  54. }
  55. void Client::onDisconnect()
  56. {
  57. print("Disconnected..\n");
  58. myOnServerFlag = false;
  59. }
  60. void Client::retryConnection()
  61. {
  62. if(myConnectionRetries == ConnectionRetries)
  63. {
  64. print("Giving up!\n");
  65. disconnect();
  66. myOnServerFlag = false;
  67. return;
  68. }
  69. print("Reconnecting...\n");
  70. reconnect();
  71. myConnectionRetries++;
  72. }
  73. void Client::parsePrintedLine()
  74. {
  75. QRegExp regex("^(.+):\\s+\\.([A-Za-z]+)\\s*(.*)$");
  76. if(regex.indexIn(myPrintLine) == -1)
  77. return;
  78. /* Flood prot */
  79. int elapsed = myTimer.elapsed();
  80. if(elapsed < 20000)
  81. {
  82. if(!myFloodMsgPrinted)
  83. {
  84. say("Wait " + QString::number((20000-elapsed) / 1000) + " second(s) before issuing a new command.");
  85. myFloodMsgPrinted = true;
  86. }
  87. return;
  88. }
  89. QString nick = regex.capturedTexts().at(1);
  90. QString command = regex.capturedTexts().at(2);
  91. QString args = regex.capturedTexts().at(3);
  92. myTimer.restart();
  93. myFloodMsgPrinted = false;
  94. if(command == "help")
  95. {
  96. say("> commands:");
  97. say("> .qw <message>");
  98. say("> .spam <message>");
  99. say("> .help to show this help message.");
  100. return;
  101. }
  102. if(command == "qw" || command == "spam")
  103. {
  104. say("Broadcasting...");
  105. QString server(QString(host()) + ":" + QString::number(port()));
  106. QString message("-" + command + "- " + nick + " - " + server + " : " + args.trimmed());
  107. //-qw- Skillah - #crazy88 : 4on4 MIX qw.foppa.dk:27503 7/8
  108. myApp->broadcast(message);
  109. //myApp->requestBroadcast(command, nick, server, args.trimmed());
  110. nick = parseNameFun(nick); //for the irc message namefun must be removed.
  111. myApp->requestBroadcast("dev", nick, server, args.trimmed());
  112. return;
  113. }
  114. }
  115. void Client::onPrint(int, const char *msg)
  116. {
  117. if(!strlen(msg))
  118. return;
  119. QString text(msg);
  120. if(text.endsWith('\n'))
  121. {
  122. myPrintLine.append(text);
  123. parsePrintedLine();
  124. print(myPrintLine);
  125. myPrintLine.clear();
  126. }
  127. else
  128. {
  129. myPrintLine.append(text);
  130. }
  131. }
  132. bool Client::isOnServer() const
  133. {
  134. return myOnServerFlag;
  135. }
  136. void Client::onError(const char *description)
  137. {
  138. QString desc(description);
  139. if(desc == "Client Timed Out.")
  140. {
  141. print("Error (" + QString(description) + ")\n");
  142. }
  143. else
  144. {
  145. print("Error (" + QString(description) + ")\n");
  146. }
  147. myOnServerFlag = false;
  148. }
  149. void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
  150. {
  151. print(QString(levelName) + "\n");
  152. myDownloadProgressPrintedFlag = false;
  153. }
  154. void Client::onChallenge()
  155. {
  156. print("challenge\n");
  157. }
  158. void Client::onConnection()
  159. {
  160. print("connection\n");
  161. }
  162. void Client::onConnected()
  163. {
  164. print("connected\n");
  165. myTimer.start();
  166. }
  167. void Client::onDownloadStarted(const char *fileName)
  168. {
  169. print("Download started " + QString(fileName) + "\n");
  170. }
  171. void Client::onOOBPrint(const char *msg)
  172. {
  173. print(QString(msg));
  174. }
  175. void Client::onStuffedCmd(const char *cmd)
  176. {
  177. printf("[%s]\n", cmd);
  178. QString strCmd(cmd);
  179. if(strCmd == "skins") //connection sequence complete
  180. {
  181. myConnectionRetries = 0;
  182. myOnServerFlag = true;
  183. }
  184. }
  185. void Client::onDownloadProgress(int percent)
  186. {
  187. if(!(percent % 10))
  188. {
  189. if(!myDownloadProgressPrintedFlag)
  190. {
  191. print("Download " + QString::number(percent) + "%\n");
  192. myDownloadProgressPrintedFlag = true;
  193. }
  194. }
  195. else
  196. {
  197. myDownloadProgressPrintedFlag = false;
  198. }
  199. }
  200. void Client::onDownloadFinished()
  201. {
  202. print("Download 100% finished.\n");
  203. }
  204. QString Client::parseNameFun(const QString &string)
  205. {
  206. QByteArray b(string.toAscii());
  207. QWClient::stripColor(b.data());
  208. return QString(b);
  209. }