220 lines
4.3 KiB
C++
Raw Normal View History

2019-02-13 13:03:40 +03:00
#include "client.h"
2019-02-15 23:36:59 +03:00
#include <QRegularExpression>
2019-02-13 13:03:40 +03:00
#include <QTcpSocket>
2019-02-15 23:36:59 +03:00
#include <QVariantMap>
2019-03-01 07:56:15 +03:00
#include <QDateTime>
2019-03-01 15:26:45 +03:00
#include <quasarapp.h>
2019-02-13 13:03:40 +03:00
namespace ClientProtocol {
2019-03-01 15:26:45 +03:00
bool Client::receiveData(QVariantMap map) {
auto command = static_cast<Command>(map.value("command", Undefined).toInt());
auto type = static_cast<Type>(map.value("type", 2).toInt());
2019-03-02 13:29:21 +03:00
int index = map.value("sig", -1).toInt();
2019-03-01 07:56:15 +03:00
if (index < 0 || index > 255)
2019-03-01 15:26:45 +03:00
return false;
2019-03-01 07:56:15 +03:00
2019-03-02 13:29:21 +03:00
#define idx static_cast<quint8>(index)
2019-03-01 15:26:45 +03:00
2019-03-02 13:29:21 +03:00
if (!(_requestsMap.contains(idx) && _requestsMap[idx].isEmpty())) {
2019-03-01 15:26:45 +03:00
QuasarAppUtils::Params::verboseLog("wrong sig of package");
return false;
}
2019-03-02 13:29:21 +03:00
map["time"] = QDateTime::currentMSecsSinceEpoch();
_requestsMap[idx] = map;
2019-03-01 07:56:15 +03:00
if ((command == Login || command == PlayerData) && type == Responke) {
_online = map.value("token", "").toByteArray().size() ==
NetworkClasses::getSizeType(NetworkClasses::SHA256);
emit onlineChanged(_online);
}
2019-03-01 15:26:45 +03:00
emit sigIncommingData(map);
return true;
2019-03-01 07:56:15 +03:00
}
2019-03-01 07:56:15 +03:00
void Client::setOnline(bool newStatus)
{
if (newStatus != _online) {
_online = newStatus;
emit onlineChanged(_online);
}
}
2019-02-13 13:03:40 +03:00
void Client::incommingData() {
auto array = _destination->readAll();
if (_downloadPackage.hdr.isValid()) {
_downloadPackage.data.append(array);
} else {
memcpy(&_downloadPackage.hdr,
2019-02-14 14:09:17 +03:00
array.data(), sizeof(Header));
_downloadPackage.data.append(array.mid(sizeof(Header)));
2019-02-13 13:03:40 +03:00
}
if (_downloadPackage.isValid()) {
2019-02-15 23:36:59 +03:00
QVariantMap res;
2019-03-01 15:26:45 +03:00
if (_downloadPackage.parse(res) && !receiveData(res)) {
Q_UNUSED(res);
// ban
2019-02-15 23:36:59 +03:00
}
2019-02-13 13:03:40 +03:00
_downloadPackage.reset();
return;
}
}
Client::Client(QObject *ptr):
QObject (ptr) {
_destination = new QTcpSocket(this);
_destination->connectToHost(DEFAULT_SNAKE_SERVER, DEFAULT_SNAKE_PORT);
connect(_destination, &QTcpSocket::readyRead,
this, &Client::incommingData);
}
2019-03-02 13:29:21 +03:00
bool Client::sendPackage(Package &pkg) {
2019-02-13 13:03:40 +03:00
if (!pkg.isValid()) {
return false;
}
if (!_destination->isValid()) {
qCritical() << "destination server not valid!";
return false;
}
if (!_destination->waitForConnected()) {
qCritical() << "no connected to server! " << _destination->errorString();
return false;
}
2019-03-02 13:29:21 +03:00
auto index = nextIndex();
_requestsMap[index] = {};
pkg.hdr.sig = index;
2019-02-13 13:03:40 +03:00
2019-03-02 13:29:21 +03:00
auto bytes = pkg.toBytes();
2019-03-01 15:26:45 +03:00
bool sendet = bytes.size() == _destination->write(bytes);
return sendet;
2019-02-13 13:03:40 +03:00
}
2019-03-02 13:29:21 +03:00
unsigned char Client::nextIndex() {
return static_cast<unsigned char>((currentIndex++) % 256);
}
2019-02-15 23:36:59 +03:00
bool Client::login(const QString &gmail, const QByteArray &pass) {
if (!pass.size()) {
return false;
}
2019-02-13 14:28:42 +03:00
2019-02-15 23:36:59 +03:00
if (!gmail.size()) {
return false;
}
Package pcg;
QVariantMap map;
map["gmail"] = gmail;
map["type"] = Request;
map["hash"] = pass;
map["command"] = Command::Login;
if (!pcg.create(map)) {
return false;
};
if (!sendPackage(pcg)) {
return false;
}
return true;
}
bool Client::updateData() {
if (!isOnline()) {
return false;
}
Package pcg;
QVariantMap map;
map["type"] = Request;
map["token"] = _token;
map["command"] = Command::PlayerData;
2019-02-15 23:36:59 +03:00
if (!pcg.create(map)) {
return false;
};
if (!sendPackage(pcg)) {
return false;
}
2019-02-13 14:28:42 +03:00
2019-02-15 23:36:59 +03:00
return true;
}
2019-02-13 14:28:42 +03:00
bool Client::savaData(QVariantMap gameData) {
if (!isOnline()) {
return false;
}
Package pcg;
gameData["type"] = Request;
gameData["token"] = _token;
gameData["command"] = Command::ApplyData;
if (!pcg.create(gameData)) {
return false;
};
if (!sendPackage(pcg)) {
return false;
}
return true;
2019-02-13 14:28:42 +03:00
}
bool Client::getItem(int id) {
if (!isOnline()) {
2019-02-15 23:36:59 +03:00
return false;
}
if (!id) {
return false;
}
Package pcg;
QVariantMap map;
map["id"] = id;
map["type"] = Request;
map["token"] = _token;
map["command"] = Command::Item;
2019-02-15 23:36:59 +03:00
if (!pcg.create(map)) {
return false;
};
if (!sendPackage(pcg)) {
return false;
}
2019-02-13 14:28:42 +03:00
2019-02-15 23:36:59 +03:00
return true;
2019-02-13 14:28:42 +03:00
}
bool Client::isOnline() const
{
return _online;
}
2019-02-13 13:03:40 +03:00
}