123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- #include "Client.h"
- #include <QString>
- #include <stdio.h>
- #include <QTcpSocket>
- #include <QStringList>
- #include "App.h"
- Client::Client(App *app):
- QWClient(),
- myApp(app),
- mySocket(NULL),
- myConnectionRetries(0),
- myOnServerFlag(false)
- {
- }
- Client::~Client()
- {
- }
- void Client::setSocket(QTcpSocket *socket)
- {
- mySocket = socket;
- }
- void Client::say(const QString &msg)
- {
- QString cmd = "say " + msg;
- sendCmd(cmd.toAscii().data());
- }
- void Client::sayTeam(const QString &msg)
- {
- QString cmd = "say_team " + msg;
- sendCmd(cmd.toAscii().data());
- }
- void Client::setTeam(const QString &msg)
- {
- sendCmd(QString("setinfo \"team\" \"" + msg + "\"").toAscii().data());
- }
- void Client::disconnect()
- {
- QWClient::disconnect();
- myOnServerFlag = false;
- }
- void Client::print(const QString &msg)
- {
- QString str;
- str = QString(host()) + ":" + QString::number(port()) + "> " + msg;
- QByteArray b = str.toAscii();
- Client::stripColor(b.data());
- str = QString::fromAscii(b.data());
- printf("%s", str.toAscii().data());
- if(mySocket)
- {
- mySocket->write(str.toAscii());
- mySocket->waitForBytesWritten();
- }
- }
- void Client::yellowText(const QString &text)
- {
- unsigned char *i;
- for(i = (unsigned char*)text.toAscii().data(); *i; i++)
- {
- if(*i >= '0' && *i <= '9')
- *i += (unsigned char) (18 - '0');
- else if(*i > 32 && *i < 128)
- *i |= 128;
- else if(*i == 13)
- *i = ' ';
- }
- return;
- }
- void Client::onDisconnect()
- {
- print("Disconnected..\n");
- myOnServerFlag = false;
- }
- void Client::retryConnection()
- {
- if(myConnectionRetries == ConnectionRetries)
- {
- print("Giving up!\n");
- disconnect();
- myOnServerFlag = false;
- return;
- }
- print("Reconnecting...\n");
- reconnect();
- myConnectionRetries++;
- }
- void Client::parsePrintedLine()
- {
- QRegExp regex("^(.+):\\s+!([A-Za-z]+)\\s*(.*)$");
- if(regex.indexIn(myPrintLine) == -1)
- return;
- /* Flood prot */
- int elapsed = myTimer.elapsed();
- if(elapsed < 20000)
- {
- if(!myFloodMsgPrinted)
- {
- say("Wait " + QString::number((20000-elapsed) / 1000) + " second(s) before issuing a new command.");
- myFloodMsgPrinted = true;
- }
- return;
- }
- QString nick = regex.capturedTexts().at(1);
- QString command = regex.capturedTexts().at(2);
- QString args = regex.capturedTexts().at(3);
- myTimer.restart();
- myFloodMsgPrinted = false;
- if(command == "help")
- {
- say("type:");
- say("> !msg <message> to broadcast a message.");
- say("> !help to show this help message.");
- return;
- }
- if(command == "msg")
- {
- say("Broadcasting...");
- QString hostPort("[" + nick + " - " + QString(host()) + ":" + QString::number(port()) + "]");
- myApp->broadcast(hostPort + args);
- return;
- }
- }
- void Client::onPrint(int, const char *msg)
- {
- if(!strlen(msg))
- return;
- QString text(msg);
- if(text.endsWith('\n'))
- {
- myPrintLine.append(text);
- parsePrintedLine();
- print(myPrintLine);
- myPrintLine.clear();
- }
- else
- {
- myPrintLine.append(text);
- }
- }
- bool Client::isOnServer() const
- {
- return myOnServerFlag;
- }
- void Client::onError(const char *description)
- {
- QString desc(description);
- if(desc == "Client Timed Out.")
- {
- print("Error (" + QString(description) + ")\n");
- }
- else
- {
- print("Error (" + QString(description) + ")\n");
- }
- myOnServerFlag = false;
- }
- void Client::onLevelChanged(int, const char *levelName, float, float, float, float, float, float, float, float, float, float)
- {
- print(QString(levelName) + "\n");
- myDownloadProgressPrintedFlag = false;
- }
- void Client::onChallenge()
- {
- print("challenge\n");
- }
- void Client::onConnection()
- {
- print("connection\n");
- }
- void Client::onConnected()
- {
- print("connected\n");
- myTimer.start();
- }
- void Client::onDownloadStarted(const char *fileName)
- {
- print("Download started " + QString(fileName) + "\n");
- }
- void Client::onOOBPrint(const char *msg)
- {
- print(QString(msg));
- }
- void Client::onStuffedCmd(const char *cmd)
- {
- printf("[%s]\n", cmd);
- QString strCmd(cmd);
- if(strCmd == "skins") //connection sequence complete
- {
- myConnectionRetries = 0;
- myOnServerFlag = true;
- }
- }
- void Client::onDownloadProgress(int percent)
- {
- if(!(percent % 10))
- {
- if(!myDownloadProgressPrintedFlag)
- {
- print("Download " + QString::number(percent) + "%\n");
- myDownloadProgressPrintedFlag = true;
- }
- }
- else
- {
- myDownloadProgressPrintedFlag = false;
- }
- }
- void Client::onDownloadFinished()
- {
- print("Download 100% finished.\n");
- }
|