200 lines
3.8 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-02-13 13:03:40 +03:00
namespace ClientProtocol {
2019-03-01 07:56:15 +03:00
void 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-01 07:56:15 +03:00
int index = map.value("sig", -1).toInt();
2019-03-01 07:56:15 +03:00
if (index < 0 || index > 255)
return;
map["time"] = QDateTime::currentMSecsSinceEpoch();
_requestsMap[index] = map;
if ((command == Login || command == PlayerData) && type == Responke) {
_online = map.value("token", "").toByteArray().size() ==
NetworkClasses::getSizeType(NetworkClasses::SHA256);
emit onlineChanged(_online);
}
}
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;
if (_downloadPackage.parse(res)) {
2019-03-01 07:56:15 +03:00
receiveData(res);
2019-02-15 23:36:59 +03:00
emit sigIncommingData(res);
}
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);
}
bool Client::sendPackage(const Package &pkg) {
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;
}
auto bytes = pkg.toBytes();
return bytes.size() == _destination->write(bytes);
}
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
}