Client.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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("type:");
  111. say("> !msg <message> to broadcast a message.");
  112. say("> !help to show this help message.");
  113. return;
  114. }
  115. if(command == "msg")
  116. {
  117. say("Broadcasting...");
  118. QString hostPort("[" + nick + " - " + QString(host()) + ":" + QString::number(port()) + "]");
  119. myApp->broadcast(hostPort + args);
  120. return;
  121. }
  122. }
  123. void Client::onPrint(int, const char *msg)
  124. {
  125. if(!strlen(msg))
  126. return;
  127. QString text(msg);
  128. if(text.endsWith('\n'))
  129. {
  130. myPrintLine.append(text);
  131. parsePrintedLine();
  132. print(myPrintLine);
  133. myPrintLine.clear();
  134. }
  135. else
  136. {
  137. myPrintLine.append(text);
  138. }
  139. }
  140. bool Client::isOnServer() const
  141. {
  142. return myOnServerFlag;
  143. }
  144. void Client::onError(const char *description)
  145. {
  146. QString desc(description);
  147. if(desc == "Client Timed Out.")
  148. {
  149. print("Error (" + QString(description) + ")\n");
  150. }
  151. else
  152. {
  153. print("Error (" + QString(description) + ")\n");
  154. }
  155. myOnServerFlag = false;
  156. }
  157. void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
  158. {
  159. print(QString(levelName) + "\n");
  160. myDownloadProgressPrintedFlag = false;
  161. }
  162. void Client::onChallenge()
  163. {
  164. print("challenge\n");
  165. }
  166. void Client::onConnection()
  167. {
  168. print("connection\n");
  169. }
  170. void Client::onConnected()
  171. {
  172. print("connected\n");
  173. myTimer.start();
  174. }
  175. void Client::onDownloadStarted(const char *fileName)
  176. {
  177. print("Download started " + QString(fileName) + "\n");
  178. }
  179. void Client::onOOBPrint(const char *msg)
  180. {
  181. print(QString(msg));
  182. }
  183. void Client::onStuffedCmd(const char *cmd)
  184. {
  185. printf("[%s]\n", cmd);
  186. QString strCmd(cmd);
  187. if(strCmd == "skins") //connection sequence complete
  188. {
  189. myConnectionRetries = 0;
  190. myOnServerFlag = true;
  191. }
  192. }
  193. void Client::onDownloadProgress(int percent)
  194. {
  195. if(!(percent % 10))
  196. {
  197. if(!myDownloadProgressPrintedFlag)
  198. {
  199. print("Download " + QString::number(percent) + "%\n");
  200. myDownloadProgressPrintedFlag = true;
  201. }
  202. }
  203. else
  204. {
  205. myDownloadProgressPrintedFlag = false;
  206. }
  207. }
  208. void Client::onDownloadFinished()
  209. {
  210. print("Download 100% finished.\n");
  211. }