mirror of
https://github.com/QuasarApp/Hanoi-Towers.git
synced 2025-05-21 05:49:34 +00:00
repair client
This commit is contained in:
parent
e6e75d0477
commit
c725bb18cb
21
HanoiTowers/Protockol/src/hanoierrorcodes.h
Normal file
21
HanoiTowers/Protockol/src/hanoierrorcodes.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#ifndef HANOIERRORCODES_H
|
||||||
|
#define HANOIERRORCODES_H
|
||||||
|
#include "dberrorcodes.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief ErrorCodes This namesapce contains all error codes of the Heart Library.
|
||||||
|
*/
|
||||||
|
namespace ErrorCodes {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The HanoiErrorCodes enum This enum with dafault error codes.
|
||||||
|
*/
|
||||||
|
enum HanoiErrorCodes: unsigned char {
|
||||||
|
/// User with sendet token do not have a permision to requested object.
|
||||||
|
PermissionDenied = QH::ErrorCodes::DBErrorCodes::DBErrorCodes,
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif // HANOIERRORCODES_H
|
@ -13,6 +13,9 @@
|
|||||||
#include <usermember.h>
|
#include <usermember.h>
|
||||||
#include <userdatarequest.h>
|
#include <userdatarequest.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "basedefines.h"
|
||||||
|
#include "badrequest.h"
|
||||||
|
#include "hanoierrorcodes.h"
|
||||||
|
|
||||||
HanoiServer::HanoiServer() {
|
HanoiServer::HanoiServer() {
|
||||||
QString address = DEFAULT_ADDRESS;
|
QString address = DEFAULT_ADDRESS;
|
||||||
@ -43,7 +46,7 @@ HanoiServer::HanoiServer() {
|
|||||||
QH::ParserResult HanoiServer::parsePackage(const QH::Package &pkg,
|
QH::ParserResult HanoiServer::parsePackage(const QH::Package &pkg,
|
||||||
const QH::AbstractNodeInfo *sender) {
|
const QH::AbstractNodeInfo *sender) {
|
||||||
|
|
||||||
auto parentResult = DataBaseNode::parsePackage(pkg, sender);
|
auto parentResult = SingleServer::parsePackage(pkg, sender);
|
||||||
if (parentResult != QH::ParserResult::NotProcessed) {
|
if (parentResult != QH::ParserResult::NotProcessed) {
|
||||||
return parentResult;
|
return parentResult;
|
||||||
}
|
}
|
||||||
@ -61,7 +64,22 @@ QH::ParserResult HanoiServer::parsePackage(const QH::Package &pkg,
|
|||||||
|
|
||||||
} else if (H_16<UserDataRequest>() == pkg.hdr.command) {
|
} else if (H_16<UserDataRequest>() == pkg.hdr.command) {
|
||||||
UserDataRequest obj(pkg);
|
UserDataRequest obj(pkg);
|
||||||
|
QH::BaseId requesterId = getSender(sender, &obj);
|
||||||
|
const QH::PKG::DBObject* userData;
|
||||||
|
|
||||||
|
if (getObject(requesterId, obj, &userData) != QH::DBOperationResult::Allowed) {
|
||||||
|
badRequest(sender->networkAddress(), pkg.hdr,
|
||||||
|
{
|
||||||
|
ErrorCodes::PermissionDenied,
|
||||||
|
"The user don not have a premsion of the requested object."
|
||||||
|
});
|
||||||
|
|
||||||
|
return QH::ParserResult::Error;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!sendData(userData, sender->networkAddress(), &pkg.hdr)) {
|
||||||
|
return QH::ParserResult::Error;
|
||||||
|
}
|
||||||
|
|
||||||
return QH::ParserResult::Processed;
|
return QH::ParserResult::Processed;
|
||||||
|
|
||||||
|
@ -35,8 +35,8 @@ BackEnd::BackEnd(QQmlApplicationEngine *engine):
|
|||||||
init();
|
init();
|
||||||
setProfile(_settings->getStrValue(CURRENT_PROFILE_KEY, DEFAULT_USER));
|
setProfile(_settings->getStrValue(CURRENT_PROFILE_KEY, DEFAULT_USER));
|
||||||
|
|
||||||
// connect(&_client, &HanoiClient::statusChanged,
|
connect(&_client, &HanoiClient::statusChanged,
|
||||||
// this, &BackEnd::handleLogined);
|
this, &BackEnd::handleLogined);
|
||||||
|
|
||||||
_loginModel = new LoginView::LVMainModel("userLogin");
|
_loginModel = new LoginView::LVMainModel("userLogin");
|
||||||
_loginModel->setComponents(LoginView::Nickname);
|
_loginModel->setComponents(LoginView::Nickname);
|
||||||
@ -48,8 +48,8 @@ BackEnd::BackEnd(QQmlApplicationEngine *engine):
|
|||||||
connect(_loginModel , &LoginView::LVMainModel::sigRegisterRequest,
|
connect(_loginModel , &LoginView::LVMainModel::sigRegisterRequest,
|
||||||
this, &BackEnd::handleOnlineRequest);
|
this, &BackEnd::handleOnlineRequest);
|
||||||
|
|
||||||
// connect(&_client , &HanoiClient::requestError,
|
connect(&_client , &HanoiClient::requestError,
|
||||||
// this, &BackEnd::handleOnlineRequestError);
|
this, &BackEnd::handleOnlineRequestError);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,10 +218,10 @@ void BackEnd::handleOnlineRequestError(const QString &) {
|
|||||||
|
|
||||||
void BackEnd::handleLogined(int state) {
|
void BackEnd::handleLogined(int state) {
|
||||||
|
|
||||||
// if (state == 2) {
|
if (state == 2) {
|
||||||
// auto logineduser = _client.user();
|
auto logineduser = _client.currentProfile();
|
||||||
// _profileList[logineduser->mail()]->update(logineduser.data());
|
_profileList[logineduser.name()]->setUserData(logineduser.userData());
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BackEnd::randomColor() const {
|
bool BackEnd::randomColor() const {
|
||||||
@ -292,8 +292,8 @@ QObject* BackEnd::gameState() {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
QObject *BackEnd::onlineStatus() {
|
QObject *BackEnd::client() {
|
||||||
return nullptr;// &_client;
|
return &_client;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BackEnd::isOnline(const QString &name) {
|
bool BackEnd::isOnline(const QString &name) {
|
||||||
@ -340,16 +340,16 @@ void BackEnd::setProfile(QString profile) {
|
|||||||
|
|
||||||
auto profileData = dynamic_cast<ProfileData*>(profileObject());
|
auto profileData = dynamic_cast<ProfileData*>(profileObject());
|
||||||
|
|
||||||
// if (profileData->isOnline()) {
|
if (profileData->isOnline()) {
|
||||||
// _client.setUser(profileData->userData());
|
_client.setUser(profileData->userData());
|
||||||
// if (!_client.login()) {
|
if (!_client.login()) {
|
||||||
// QmlNotificationService::NotificationService::getService()->setNotify(
|
QmlNotificationService::NotificationService::getService()->setNotify(
|
||||||
// tr("Login failed"),
|
tr("Login failed"),
|
||||||
// tr("Failed to login %0, if this account was created by you, try to restore it.").arg(_profile),
|
tr("Failed to login %0, if this account was created by you, try to restore it.").arg(_profile),
|
||||||
// "",
|
"",
|
||||||
// QmlNotificationService::NotificationData::Warning);
|
QmlNotificationService::NotificationData::Warning);
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
emit profileChanged(_profile);
|
emit profileChanged(_profile);
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ class BackEnd: public QObject
|
|||||||
Q_PROPERTY(bool randomColor READ randomColor WRITE setRandomColor NOTIFY randomColorChanged)
|
Q_PROPERTY(bool randomColor READ randomColor WRITE setRandomColor NOTIFY randomColorChanged)
|
||||||
Q_PROPERTY(bool animation READ animation WRITE setAnimation NOTIFY animationChanged)
|
Q_PROPERTY(bool animation READ animation WRITE setAnimation NOTIFY animationChanged)
|
||||||
Q_PROPERTY(QObject* gameState READ gameState)
|
Q_PROPERTY(QObject* gameState READ gameState)
|
||||||
Q_PROPERTY(QObject* onlineStatus READ onlineStatus)
|
Q_PROPERTY(QObject* client READ client)
|
||||||
Q_PROPERTY(QObject* profileObject READ profileObject NOTIFY profileChanged)
|
Q_PROPERTY(QObject* profileObject READ profileObject NOTIFY profileChanged)
|
||||||
// Q_PROPERTY(QObject* usersListModel READ usersListModel NOTIFY usersListModelChanged)
|
// Q_PROPERTY(QObject* usersListModel READ usersListModel NOTIFY usersListModelChanged)
|
||||||
|
|
||||||
@ -129,10 +129,10 @@ public slots:
|
|||||||
QObject *gameState();
|
QObject *gameState();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief onlineStatus
|
* @brief client
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
QObject *onlineStatus();
|
QObject *client();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief profileObject
|
* @brief profileObject
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include <localuser.h>
|
#include <localuser.h>
|
||||||
#include <authrequest.h>
|
#include <authrequest.h>
|
||||||
#include <userdatarequest.h>
|
#include <userdatarequest.h>
|
||||||
|
#include "hanoierrorcodes.h"
|
||||||
|
|
||||||
HanoiClient::HanoiClient() {
|
HanoiClient::HanoiClient() {
|
||||||
initSqlDb("", new QH::SqlDBCache(DEFAULT_UPDATE_INTERVAL, QH::SqlDBCasheWriteMode::Force));
|
initSqlDb("", new QH::SqlDBCache(DEFAULT_UPDATE_INTERVAL, QH::SqlDBCasheWriteMode::Force));
|
||||||
@ -40,13 +41,16 @@ QH::ParserResult HanoiClient::parsePackage(const QH::Package &pkg,
|
|||||||
} else if (H_16<QH::PKG::UserMember>() == pkg.hdr.command) {
|
} else if (H_16<QH::PKG::UserMember>() == pkg.hdr.command) {
|
||||||
QH::PKG::UserMember obj(pkg);
|
QH::PKG::UserMember obj(pkg);
|
||||||
|
|
||||||
auto localuser = db()->getObject(obj)->cloneRaw();
|
LocalUser user(&obj);
|
||||||
localuser->copyFrom(&obj);
|
auto localuser = db()->getObject(user)->clone().dynamicCast<LocalUser>();
|
||||||
|
localuser->setToken(obj.token());
|
||||||
|
localuser->setOnline(true);
|
||||||
|
|
||||||
if (db()->saveObject(localuser)) {
|
if (db()->saveObject(localuser.data())) {
|
||||||
return QH::ParserResult::Error;
|
return QH::ParserResult::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setStatus(Status::Logined);
|
||||||
}
|
}
|
||||||
|
|
||||||
return QH::ParserResult::NotProcessed;
|
return QH::ParserResult::NotProcessed;
|
||||||
@ -58,23 +62,37 @@ QStringList HanoiClient::SQLSources() const {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void HanoiClient::handleError(const QString &error) {
|
void HanoiClient::handleError(unsigned char status, const QString &error) {
|
||||||
|
if (status == ErrorCodes::PermissionDenied) {
|
||||||
|
setStatus(Status::Connected);
|
||||||
|
}
|
||||||
|
|
||||||
QmlNotificationService::NotificationService::getService()->setNotify(
|
QmlNotificationService::NotificationService::getService()->setNotify(
|
||||||
tr("Jnline error"), error, "",
|
tr("Jnline error"), error, "",
|
||||||
QmlNotificationService::NotificationData::Error);
|
QmlNotificationService::NotificationData::Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HanoiClient::login(const QString& userId) {
|
bool HanoiClient::login(const QString& userId, const QByteArray &hashPassword) {
|
||||||
QH::PKG::AuthRequest request;
|
QH::PKG::AuthRequest request;
|
||||||
request.setId(userId);
|
request.setId(userId);
|
||||||
request.setRequest(QH::PKG::UserRequestType::Login);
|
request.setRequest(QH::PKG::UserRequestType::Login);
|
||||||
|
|
||||||
|
if (hashPassword.isEmpty()) {
|
||||||
|
const LocalUser* localUser = getLocalUser(userId);
|
||||||
|
if (!(localUser && localUser->token().isValid())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
request.setToken(localUser->token());
|
||||||
|
}
|
||||||
|
|
||||||
return sendData(&request, _serverAddress);
|
return sendData(&request, _serverAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HanoiClient::signIn(const QString &userId) {
|
bool HanoiClient::signIn(const QString &userId, const QByteArray& hashPassword) {
|
||||||
QH::PKG::AuthRequest request;
|
QH::PKG::AuthRequest request;
|
||||||
request.setId(userId);
|
request.setId(userId);
|
||||||
|
request.setAuthenticationData(hashPassword);
|
||||||
request.setRequest(QH::PKG::UserRequestType::SignIn);
|
request.setRequest(QH::PKG::UserRequestType::SignIn);
|
||||||
|
|
||||||
return sendData(&request, _serverAddress);
|
return sendData(&request, _serverAddress);
|
||||||
@ -102,6 +120,17 @@ ProfileData HanoiClient::defaultProfile() const {
|
|||||||
return ProfileData("User");
|
return ProfileData("User");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Status HanoiClient::getStatus() const {
|
||||||
|
return _status;
|
||||||
|
}
|
||||||
|
|
||||||
|
void HanoiClient::setStatus(const Status &status) {
|
||||||
|
if (_status != status) {
|
||||||
|
_status = status;
|
||||||
|
emit statusChanged(static_cast<int>(_status));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QString HanoiClient::currentUserName() const {
|
QString HanoiClient::currentUserName() const {
|
||||||
return _currentUserName;
|
return _currentUserName;
|
||||||
}
|
}
|
||||||
@ -110,14 +139,14 @@ void HanoiClient::setCurrentUserName(const QString ¤tUserName) {
|
|||||||
_currentUserName = currentUserName;
|
_currentUserName = currentUserName;
|
||||||
}
|
}
|
||||||
|
|
||||||
ProfileData HanoiClient::currentProfile() {
|
ProfileData *HanoiClient::currentProfile() {
|
||||||
|
|
||||||
auto userData = getLocalUser(_currentUserName);
|
auto userData = getLocalUser(_currentUserName);
|
||||||
|
|
||||||
if (userData)
|
if (userData)
|
||||||
return userData->userData();
|
return userData->userData();
|
||||||
|
|
||||||
return defaultProfile();
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HanoiClient::connectToServer(const QH::HostAddress &host) {
|
void HanoiClient::connectToServer(const QH::HostAddress &host) {
|
||||||
@ -125,4 +154,19 @@ void HanoiClient::connectToServer(const QH::HostAddress &host) {
|
|||||||
_serverAddress = host;
|
_serverAddress = host;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HanoiClient::nodeConfirmend(const QH::HostAddress &node) {
|
||||||
|
setStatus(Status::Connected);
|
||||||
|
DataBaseNode::nodeConfirmend(node);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void HanoiClient::nodeConnected(const QH::HostAddress &node) {
|
||||||
|
DataBaseNode::nodeConnected(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HanoiClient::nodeDisconnected(const QH::HostAddress &node) {
|
||||||
|
setStatus(Status::Dissconnected);
|
||||||
|
DataBaseNode::nodeDisconnected(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,7 +29,8 @@ class UserMember;
|
|||||||
enum class Status {
|
enum class Status {
|
||||||
OfflineUser,
|
OfflineUser,
|
||||||
Dissconnected,
|
Dissconnected,
|
||||||
Connected
|
Connected,
|
||||||
|
Logined
|
||||||
};
|
};
|
||||||
|
|
||||||
class HanoiClient: public QH::DataBaseNode
|
class HanoiClient: public QH::DataBaseNode
|
||||||
@ -45,20 +46,29 @@ public:
|
|||||||
|
|
||||||
QString currentUserName() const;
|
QString currentUserName() const;
|
||||||
void setCurrentUserName(const QString ¤tUserName);
|
void setCurrentUserName(const QString ¤tUserName);
|
||||||
ProfileData currentProfile();
|
ProfileData* currentProfile();
|
||||||
void connectToServer(const QH::HostAddress& host);
|
void connectToServer(const QH::HostAddress& host);
|
||||||
|
|
||||||
|
// AbstractNode interface
|
||||||
|
Status getStatus() const;
|
||||||
|
void setStatus(const Status &status);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void nodeConfirmend(const QH::HostAddress &node) override;
|
||||||
|
void nodeConnected(const QH::HostAddress &node) override;
|
||||||
|
void nodeDisconnected(const QH::HostAddress &node) override;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void requestError(const QString & err);
|
void requestError(const QString & err);
|
||||||
void statusChanged(int state);
|
void statusChanged(int state);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void handleError(const QString& error);
|
void handleError(unsigned char status, const QString& error);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
bool login(const QString &userId);
|
bool login(const QString &userId, const QByteArray &hashPassword = {});
|
||||||
bool signIn(const QString &userId);
|
bool signIn(const QString &userId, const QByteArray &hashPassword);
|
||||||
|
|
||||||
bool userDatarequest(const QString &userId);
|
bool userDatarequest(const QString &userId);
|
||||||
const LocalUser *getLocalUser(const QString &userId) const;
|
const LocalUser *getLocalUser(const QString &userId) const;
|
||||||
@ -72,6 +82,8 @@ private:
|
|||||||
QList<LocalUser*> _usersList;
|
QList<LocalUser*> _usersList;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // HANOICLIENT_H
|
#endif // HANOICLIENT_H
|
||||||
|
@ -9,6 +9,10 @@ LocalUser::LocalUser():
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LocalUser::LocalUser(const QH::PKG::DBObject *other ):LocalUser() {
|
||||||
|
copyFrom(other);
|
||||||
|
}
|
||||||
|
|
||||||
bool LocalUser::copyFrom(const QH::PKG::AbstractData *other) {
|
bool LocalUser::copyFrom(const QH::PKG::AbstractData *other) {
|
||||||
if (!QH::PKG::DBObject::copyFrom(other))
|
if (!QH::PKG::DBObject::copyFrom(other))
|
||||||
return false;
|
return false;
|
||||||
@ -49,7 +53,7 @@ QH::PKG::DBVariantMap LocalUser::variantMap() const {
|
|||||||
{"token", {_token.toBytes(), MT::InsertUpdate}},
|
{"token", {_token.toBytes(), MT::InsertUpdate}},
|
||||||
{"userdata", {_userData.toBytes(), MT::InsertUpdate}},
|
{"userdata", {_userData.toBytes(), MT::InsertUpdate}},
|
||||||
{"onlineUser", {_online, MT::InsertUpdate}},
|
{"onlineUser", {_online, MT::InsertUpdate}},
|
||||||
{"updateTime", {_updateTime, MT::InsertUpdate}}};
|
{"updateTime", {static_cast<int>(time(nullptr)), MT::InsertUpdate}}};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ class LocalUser: public QH::PKG::DBObject
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LocalUser();
|
LocalUser();
|
||||||
|
LocalUser(const QH::PKG::DBObject*);
|
||||||
|
|
||||||
bool copyFrom(const QH::PKG::AbstractData *other) override;
|
bool copyFrom(const QH::PKG::AbstractData *other) override;
|
||||||
bool fromSqlRecord(const QSqlRecord &q) override;
|
bool fromSqlRecord(const QSqlRecord &q) override;
|
||||||
@ -19,9 +20,6 @@ public:
|
|||||||
bool online() const;
|
bool online() const;
|
||||||
void setOnline(bool online);
|
void setOnline(bool online);
|
||||||
|
|
||||||
QString userName() const;
|
|
||||||
void setUserName(const QString &userName);
|
|
||||||
|
|
||||||
QByteArray hashPassword() const;
|
QByteArray hashPassword() const;
|
||||||
void setHashPassword(const QByteArray &hashPassword);
|
void setHashPassword(const QByteArray &hashPassword);
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import QtQuick 2.9
|
import QtQuick 2.15
|
||||||
import QtQuick.Controls 2.2
|
import QtQuick.Controls 2.15
|
||||||
import QtQuick.Layouts 1.3
|
import QtQuick.Layouts 1.3
|
||||||
|
|
||||||
import "./../base" as Base
|
import "./../base" as Base
|
||||||
|
2
Heart
2
Heart
@ -1 +1 @@
|
|||||||
Subproject commit 5c794703ad676f5fc64ffb609b7da3b603a7aa8f
|
Subproject commit aa4b51ca8ded1c53540d68d63621bd6ea7ca251a
|
Loading…
x
Reference in New Issue
Block a user