mirror of
https://github.com/QuasarApp/Hanoi-Towers.git
synced 2025-05-10 00:19: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 <userdatarequest.h>
|
||||
#include "config.h"
|
||||
#include "basedefines.h"
|
||||
#include "badrequest.h"
|
||||
#include "hanoierrorcodes.h"
|
||||
|
||||
HanoiServer::HanoiServer() {
|
||||
QString address = DEFAULT_ADDRESS;
|
||||
@ -43,7 +46,7 @@ HanoiServer::HanoiServer() {
|
||||
QH::ParserResult HanoiServer::parsePackage(const QH::Package &pkg,
|
||||
const QH::AbstractNodeInfo *sender) {
|
||||
|
||||
auto parentResult = DataBaseNode::parsePackage(pkg, sender);
|
||||
auto parentResult = SingleServer::parsePackage(pkg, sender);
|
||||
if (parentResult != QH::ParserResult::NotProcessed) {
|
||||
return parentResult;
|
||||
}
|
||||
@ -61,7 +64,22 @@ QH::ParserResult HanoiServer::parsePackage(const QH::Package &pkg,
|
||||
|
||||
} else if (H_16<UserDataRequest>() == pkg.hdr.command) {
|
||||
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;
|
||||
|
||||
|
@ -35,8 +35,8 @@ BackEnd::BackEnd(QQmlApplicationEngine *engine):
|
||||
init();
|
||||
setProfile(_settings->getStrValue(CURRENT_PROFILE_KEY, DEFAULT_USER));
|
||||
|
||||
// connect(&_client, &HanoiClient::statusChanged,
|
||||
// this, &BackEnd::handleLogined);
|
||||
connect(&_client, &HanoiClient::statusChanged,
|
||||
this, &BackEnd::handleLogined);
|
||||
|
||||
_loginModel = new LoginView::LVMainModel("userLogin");
|
||||
_loginModel->setComponents(LoginView::Nickname);
|
||||
@ -48,8 +48,8 @@ BackEnd::BackEnd(QQmlApplicationEngine *engine):
|
||||
connect(_loginModel , &LoginView::LVMainModel::sigRegisterRequest,
|
||||
this, &BackEnd::handleOnlineRequest);
|
||||
|
||||
// connect(&_client , &HanoiClient::requestError,
|
||||
// this, &BackEnd::handleOnlineRequestError);
|
||||
connect(&_client , &HanoiClient::requestError,
|
||||
this, &BackEnd::handleOnlineRequestError);
|
||||
|
||||
}
|
||||
|
||||
@ -218,10 +218,10 @@ void BackEnd::handleOnlineRequestError(const QString &) {
|
||||
|
||||
void BackEnd::handleLogined(int state) {
|
||||
|
||||
// if (state == 2) {
|
||||
// auto logineduser = _client.user();
|
||||
// _profileList[logineduser->mail()]->update(logineduser.data());
|
||||
// }
|
||||
if (state == 2) {
|
||||
auto logineduser = _client.currentProfile();
|
||||
_profileList[logineduser.name()]->setUserData(logineduser.userData());
|
||||
}
|
||||
}
|
||||
|
||||
bool BackEnd::randomColor() const {
|
||||
@ -292,8 +292,8 @@ QObject* BackEnd::gameState() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QObject *BackEnd::onlineStatus() {
|
||||
return nullptr;// &_client;
|
||||
QObject *BackEnd::client() {
|
||||
return &_client;
|
||||
}
|
||||
|
||||
bool BackEnd::isOnline(const QString &name) {
|
||||
@ -340,16 +340,16 @@ void BackEnd::setProfile(QString profile) {
|
||||
|
||||
auto profileData = dynamic_cast<ProfileData*>(profileObject());
|
||||
|
||||
// if (profileData->isOnline()) {
|
||||
// _client.setUser(profileData->userData());
|
||||
// if (!_client.login()) {
|
||||
// QmlNotificationService::NotificationService::getService()->setNotify(
|
||||
// tr("Login failed"),
|
||||
// tr("Failed to login %0, if this account was created by you, try to restore it.").arg(_profile),
|
||||
// "",
|
||||
// QmlNotificationService::NotificationData::Warning);
|
||||
// }
|
||||
// }
|
||||
if (profileData->isOnline()) {
|
||||
_client.setUser(profileData->userData());
|
||||
if (!_client.login()) {
|
||||
QmlNotificationService::NotificationService::getService()->setNotify(
|
||||
tr("Login failed"),
|
||||
tr("Failed to login %0, if this account was created by you, try to restore it.").arg(_profile),
|
||||
"",
|
||||
QmlNotificationService::NotificationData::Warning);
|
||||
}
|
||||
}
|
||||
|
||||
emit profileChanged(_profile);
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ class BackEnd: public QObject
|
||||
Q_PROPERTY(bool randomColor READ randomColor WRITE setRandomColor NOTIFY randomColorChanged)
|
||||
Q_PROPERTY(bool animation READ animation WRITE setAnimation NOTIFY animationChanged)
|
||||
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* usersListModel READ usersListModel NOTIFY usersListModelChanged)
|
||||
|
||||
@ -129,10 +129,10 @@ public slots:
|
||||
QObject *gameState();
|
||||
|
||||
/**
|
||||
* @brief onlineStatus
|
||||
* @brief client
|
||||
* @return
|
||||
*/
|
||||
QObject *onlineStatus();
|
||||
QObject *client();
|
||||
|
||||
/**
|
||||
* @brief profileObject
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include <localuser.h>
|
||||
#include <authrequest.h>
|
||||
#include <userdatarequest.h>
|
||||
#include "hanoierrorcodes.h"
|
||||
|
||||
HanoiClient::HanoiClient() {
|
||||
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) {
|
||||
QH::PKG::UserMember obj(pkg);
|
||||
|
||||
auto localuser = db()->getObject(obj)->cloneRaw();
|
||||
localuser->copyFrom(&obj);
|
||||
LocalUser user(&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;
|
||||
}
|
||||
|
||||
setStatus(Status::Logined);
|
||||
}
|
||||
|
||||
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(
|
||||
tr("Jnline error"), error, "",
|
||||
QmlNotificationService::NotificationData::Error);
|
||||
}
|
||||
|
||||
bool HanoiClient::login(const QString& userId) {
|
||||
bool HanoiClient::login(const QString& userId, const QByteArray &hashPassword) {
|
||||
QH::PKG::AuthRequest request;
|
||||
request.setId(userId);
|
||||
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);
|
||||
}
|
||||
|
||||
bool HanoiClient::signIn(const QString &userId) {
|
||||
bool HanoiClient::signIn(const QString &userId, const QByteArray& hashPassword) {
|
||||
QH::PKG::AuthRequest request;
|
||||
request.setId(userId);
|
||||
request.setAuthenticationData(hashPassword);
|
||||
request.setRequest(QH::PKG::UserRequestType::SignIn);
|
||||
|
||||
return sendData(&request, _serverAddress);
|
||||
@ -102,6 +120,17 @@ ProfileData HanoiClient::defaultProfile() const {
|
||||
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 {
|
||||
return _currentUserName;
|
||||
}
|
||||
@ -110,14 +139,14 @@ void HanoiClient::setCurrentUserName(const QString ¤tUserName) {
|
||||
_currentUserName = currentUserName;
|
||||
}
|
||||
|
||||
ProfileData HanoiClient::currentProfile() {
|
||||
ProfileData *HanoiClient::currentProfile() {
|
||||
|
||||
auto userData = getLocalUser(_currentUserName);
|
||||
|
||||
if (userData)
|
||||
return userData->userData();
|
||||
|
||||
return defaultProfile();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void HanoiClient::connectToServer(const QH::HostAddress &host) {
|
||||
@ -125,4 +154,19 @@ void HanoiClient::connectToServer(const QH::HostAddress &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 {
|
||||
OfflineUser,
|
||||
Dissconnected,
|
||||
Connected
|
||||
Connected,
|
||||
Logined
|
||||
};
|
||||
|
||||
class HanoiClient: public QH::DataBaseNode
|
||||
@ -45,20 +46,29 @@ public:
|
||||
|
||||
QString currentUserName() const;
|
||||
void setCurrentUserName(const QString ¤tUserName);
|
||||
ProfileData currentProfile();
|
||||
ProfileData* currentProfile();
|
||||
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:
|
||||
void requestError(const QString & err);
|
||||
void statusChanged(int state);
|
||||
|
||||
private slots:
|
||||
void handleError(const QString& error);
|
||||
void handleError(unsigned char status, const QString& error);
|
||||
|
||||
private:
|
||||
|
||||
bool login(const QString &userId);
|
||||
bool signIn(const QString &userId);
|
||||
bool login(const QString &userId, const QByteArray &hashPassword = {});
|
||||
bool signIn(const QString &userId, const QByteArray &hashPassword);
|
||||
|
||||
bool userDatarequest(const QString &userId);
|
||||
const LocalUser *getLocalUser(const QString &userId) const;
|
||||
@ -72,6 +82,8 @@ private:
|
||||
QList<LocalUser*> _usersList;
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#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) {
|
||||
if (!QH::PKG::DBObject::copyFrom(other))
|
||||
return false;
|
||||
@ -45,11 +49,11 @@ bool LocalUser::isValid() const {
|
||||
}
|
||||
|
||||
QH::PKG::DBVariantMap LocalUser::variantMap() const {
|
||||
return {{"passwordHash", {_hashPassword, MT::InsertUpdate}},
|
||||
{"token", {_token.toBytes(), MT::InsertUpdate}},
|
||||
{"userdata", {_userData.toBytes(), MT::InsertUpdate}},
|
||||
{"onlineUser", {_online, MT::InsertUpdate}},
|
||||
{"updateTime", {_updateTime, MT::InsertUpdate}}};
|
||||
return {{"passwordHash", {_hashPassword, MT::InsertUpdate}},
|
||||
{"token", {_token.toBytes(), MT::InsertUpdate}},
|
||||
{"userdata", {_userData.toBytes(), MT::InsertUpdate}},
|
||||
{"onlineUser", {_online, MT::InsertUpdate}},
|
||||
{"updateTime", {static_cast<int>(time(nullptr)), MT::InsertUpdate}}};
|
||||
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ class LocalUser: public QH::PKG::DBObject
|
||||
{
|
||||
public:
|
||||
LocalUser();
|
||||
LocalUser(const QH::PKG::DBObject*);
|
||||
|
||||
bool copyFrom(const QH::PKG::AbstractData *other) override;
|
||||
bool fromSqlRecord(const QSqlRecord &q) override;
|
||||
@ -19,9 +20,6 @@ public:
|
||||
bool online() const;
|
||||
void setOnline(bool online);
|
||||
|
||||
QString userName() const;
|
||||
void setUserName(const QString &userName);
|
||||
|
||||
QByteArray hashPassword() const;
|
||||
void setHashPassword(const QByteArray &hashPassword);
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import QtQuick 2.9
|
||||
import QtQuick.Controls 2.2
|
||||
import QtQuick 2.15
|
||||
import QtQuick.Controls 2.15
|
||||
import QtQuick.Layouts 1.3
|
||||
|
||||
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