Client.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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::yellowText(const QString &text)
  56. {
  57. unsigned char *i;
  58. for(i = (unsigned char*)text.toAscii().data(); *i; i++)
  59. {
  60. if(*i >= '0' && *i <= '9')
  61. *i += (unsigned char) (18 - '0');
  62. else if(*i > 32 && *i < 128)
  63. *i |= 128;
  64. else if(*i == 13)
  65. *i = ' ';
  66. }
  67. return;
  68. }
  69. void Client::onDisconnect()
  70. {
  71. print("Disconnected..\n");
  72. myOnServerFlag = false;
  73. }
  74. void Client::retryConnection()
  75. {
  76. if(myConnectionRetries == ConnectionRetries)
  77. {
  78. print("Giving up!\n");
  79. disconnect();
  80. myOnServerFlag = false;
  81. return;
  82. }
  83. print("Reconnecting...\n");
  84. reconnect();
  85. myConnectionRetries++;
  86. }
  87. void Client::parsePrintedLine()
  88. {
  89. QRegExp regex("^(.+):\\s+\\.([A-Za-z]+)\\s*(.*)$");
  90. if(regex.indexIn(myPrintLine) == -1)
  91. return;
  92. /* Flood prot */
  93. int elapsed = myTimer.elapsed();
  94. if(elapsed < 20000)
  95. {
  96. if(!myFloodMsgPrinted)
  97. {
  98. say("Wait " + QString::number((20000-elapsed) / 1000) + " second(s) before issuing a new command.");
  99. myFloodMsgPrinted = true;
  100. }
  101. return;
  102. }
  103. QString nick = regex.capturedTexts().at(1);
  104. QString command = regex.capturedTexts().at(2);
  105. QString args = regex.capturedTexts().at(3);
  106. myTimer.restart();
  107. myFloodMsgPrinted = false;
  108. if(command == "help")
  109. {
  110. say("> commands:");
  111. say("> .qw <message>");
  112. say("> .spam <message>");
  113. say("> .help to show this help message.");
  114. return;
  115. }
  116. if(command == "qw" || command == "spam")
  117. {
  118. say("Broadcasting...");
  119. QString server(QString(host()) + ":" + QString::number(port()));
  120. QString message("-" + command + "- " + nick + " - " + server + " : " + args.trimmed());
  121. //-qw- Skillah - #crazy88 : 4on4 MIX qw.foppa.dk:27503 7/8
  122. myApp->broadcast(message);
  123. //myApp->requestBroadcast(command, nick, server, args.trimmed());
  124. myApp->requestBroadcast("dev", nick, server, args.trimmed());
  125. return;
  126. }
  127. }
  128. void Client::onPrint(int, const char *msg)
  129. {
  130. if(!strlen(msg))
  131. return;
  132. QString text(msg);
  133. if(text.endsWith('\n'))
  134. {
  135. myPrintLine.append(text);
  136. parsePrintedLine();
  137. print(myPrintLine);
  138. myPrintLine.clear();
  139. }
  140. else
  141. {
  142. myPrintLine.append(text);
  143. }
  144. }
  145. bool Client::isOnServer() const
  146. {
  147. return myOnServerFlag;
  148. }
  149. void Client::onError(const char *description)
  150. {
  151. QString desc(description);
  152. if(desc == "Client Timed Out.")
  153. {
  154. print("Error (" + QString(description) + ")\n");
  155. }
  156. else
  157. {
  158. print("Error (" + QString(description) + ")\n");
  159. }
  160. myOnServerFlag = false;
  161. }
  162. void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
  163. {
  164. print(QString(levelName) + "\n");
  165. myDownloadProgressPrintedFlag = false;
  166. }
  167. void Client::onChallenge()
  168. {
  169. print("challenge\n");
  170. }
  171. void Client::onConnection()
  172. {
  173. print("connection\n");
  174. }
  175. void Client::onConnected()
  176. {
  177. print("connected\n");
  178. myTimer.start();
  179. }
  180. void Client::onDownloadStarted(const char *fileName)
  181. {
  182. print("Download started " + QString(fileName) + "\n");
  183. }
  184. void Client::onOOBPrint(const char *msg)
  185. {
  186. print(QString(msg));
  187. }
  188. void Client::onStuffedCmd(const char *cmd)
  189. {
  190. printf("[%s]\n", cmd);
  191. QString strCmd(cmd);
  192. if(strCmd == "skins") //connection sequence complete
  193. {
  194. myConnectionRetries = 0;
  195. myOnServerFlag = true;
  196. }
  197. }
  198. void Client::onDownloadProgress(int percent)
  199. {
  200. if(!(percent % 10))
  201. {
  202. if(!myDownloadProgressPrintedFlag)
  203. {
  204. print("Download " + QString::number(percent) + "%\n");
  205. myDownloadProgressPrintedFlag = true;
  206. }
  207. }
  208. else
  209. {
  210. myDownloadProgressPrintedFlag = false;
  211. }
  212. }
  213. void Client::onDownloadFinished()
  214. {
  215. print("Download 100% finished.\n");
  216. }