mirror of
https://github.com/QuasarApp/Snake.git
synced 2025-04-29 11:14:40 +00:00
added listner for client
This commit is contained in:
parent
933fd14f14
commit
b6d94cd43e
@ -13,6 +13,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
|||||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
|
back-end/ProfileViewItems/mainclient.cpp \
|
||||||
back-end/ProfileViewItems/mainmenumodel.cpp \
|
back-end/ProfileViewItems/mainmenumodel.cpp \
|
||||||
back-end/ProfileViewItems/networkclient.cpp \
|
back-end/ProfileViewItems/networkclient.cpp \
|
||||||
back-end/ProfileViewItems/playerclientdata.cpp \
|
back-end/ProfileViewItems/playerclientdata.cpp \
|
||||||
@ -86,6 +87,7 @@ RESOURCES += \
|
|||||||
qml.qrc
|
qml.qrc
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
|
back-end/ProfileViewItems/mainclient.h \
|
||||||
back-end/ProfileViewItems/mainmenumodel.h \
|
back-end/ProfileViewItems/mainmenumodel.h \
|
||||||
back-end/ProfileViewItems/networkclient.h \
|
back-end/ProfileViewItems/networkclient.h \
|
||||||
back-end/ProfileViewItems/playerclientdata.h \
|
back-end/ProfileViewItems/playerclientdata.h \
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
#include "mainclient.h"
|
||||||
|
#include "playerclientdata.h"
|
||||||
|
|
||||||
|
#include <QSharedPointer>
|
||||||
|
|
||||||
|
void MainClient::handleReceivePackage(ClientProtocol::Command cmd, const QByteArray &obj) {
|
||||||
|
switch (cmd) {
|
||||||
|
|
||||||
|
case ClientProtocol::Command::Player: {
|
||||||
|
auto playerData = QSharedPointer<PlayerClientData>::create();
|
||||||
|
playerData->fromBytes(obj);
|
||||||
|
|
||||||
|
_users [playerData->id()] = playerData;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainClient::handleLoginChanged(bool logined) {
|
||||||
|
this->setSubscribe(ClientProtocol::Command::Player, logined, _currentUserId);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainClient::handleOnlineChanged(bool) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
MainClient::MainClient() {
|
||||||
|
|
||||||
|
connect(this, &MainClient::sigIncommingData,
|
||||||
|
this, &MainClient::handleReceivePackage);
|
||||||
|
|
||||||
|
connect(this, &MainClient::onlineChanged,
|
||||||
|
this, &MainClient::handleOnlineChanged);
|
||||||
|
|
||||||
|
connect(this, &MainClient::loginChanged,
|
||||||
|
this, &MainClient::handleLoginChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
MainClient::~MainClient() {
|
||||||
|
for (auto &i: _users) {
|
||||||
|
i.clear();
|
||||||
|
}
|
||||||
|
_users.clear();
|
||||||
|
}
|
23
SnakeClient/SnakeApp/back-end/ProfileViewItems/mainclient.h
Normal file
23
SnakeClient/SnakeApp/back-end/ProfileViewItems/mainclient.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef MAINCLIENT_H
|
||||||
|
#define MAINCLIENT_H
|
||||||
|
#include <client.h>
|
||||||
|
|
||||||
|
class PlayerClientData;
|
||||||
|
|
||||||
|
class MainClient: public ClientProtocol::Client
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
private:
|
||||||
|
QHash<int, QSharedPointer<PlayerClientData>> _users;
|
||||||
|
private slots:
|
||||||
|
void handleReceivePackage(ClientProtocol::Command cmd, const QByteArray& obj);
|
||||||
|
void handleLoginChanged(bool);
|
||||||
|
void handleOnlineChanged(bool);
|
||||||
|
|
||||||
|
public:
|
||||||
|
MainClient();
|
||||||
|
~MainClient();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // MAINCLIENT_H
|
@ -52,6 +52,8 @@ void Client::updateStatuses(Command extCmd, Command cmd, Type type, const QByteA
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_currentUserId = data.id();
|
||||||
|
|
||||||
setLoginStatus(cmd == Command::UpdatePlayerData && validData);
|
setLoginStatus(cmd == Command::UpdatePlayerData && validData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -375,7 +377,7 @@ const bool& Client::isLogin() const {
|
|||||||
return _logined;
|
return _logined;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Client::changeSubscribe(Command cmd, bool subscribe, int id) {
|
bool Client::setSubscribe(Command cmd, bool subscribe, int id) {
|
||||||
if (!isLogin()) {
|
if (!isLogin()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ private:
|
|||||||
QHash<unsigned char, QVariantMap> _requestsMap;
|
QHash<unsigned char, QVariantMap> _requestsMap;
|
||||||
QHash<quint8, bool> _subscribe; // command and data confirmation
|
QHash<quint8, bool> _subscribe; // command and data confirmation
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief checkCommand - return old sendet command if commnad not valid return undefined command
|
* @brief checkCommand - return old sendet command if commnad not valid return undefined command
|
||||||
* @param sig - sig package
|
* @param sig - sig package
|
||||||
@ -52,6 +53,10 @@ private slots:
|
|||||||
void incommingData();
|
void incommingData();
|
||||||
void handleDisconnected();
|
void handleDisconnected();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
int _currentUserId = -1;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Client(const QString& addrress = LOCAL_SNAKE_SERVER,
|
explicit Client(const QString& addrress = LOCAL_SNAKE_SERVER,
|
||||||
unsigned short port = DEFAULT_SNAKE_PORT,
|
unsigned short port = DEFAULT_SNAKE_PORT,
|
||||||
@ -111,14 +116,14 @@ public:
|
|||||||
const bool &isLogin() const;
|
const bool &isLogin() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief changeSubscribe change subscribe of command "cmd"
|
* @brief setSubscribe change subscribe of command "cmd"
|
||||||
* @param cmd - command of subscribe
|
* @param cmd - command of subscribe
|
||||||
* @param subscribe - boolean barametr. true is subscribe, false is unsubscribe
|
* @param subscribe - boolean barametr. true is subscribe, false is unsubscribe
|
||||||
* @param id - id of object for commands (Player and Item).
|
* @param id - id of object for commands (Player and Item).
|
||||||
* If this parameter is -1 then subscribe on all changes of objects.
|
* If this parameter is -1 then subscribe on all changes of objects.
|
||||||
* @return true if all good
|
* @return true if all good
|
||||||
*/
|
*/
|
||||||
bool changeSubscribe(Command cmd, bool subscribe, int id = -1);
|
bool setSubscribe(Command cmd, bool subscribe, int id = -1);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief getSubscribe
|
* @brief getSubscribe
|
||||||
|
@ -357,8 +357,8 @@ void testSankeServer::testApplyData(ClientProtocol::Client &cle) {
|
|||||||
|
|
||||||
void testSankeServer::testWebSockets(ClientProtocol::Client &cle) {
|
void testSankeServer::testWebSockets(ClientProtocol::Client &cle) {
|
||||||
|
|
||||||
QVERIFY(cle.changeSubscribe(ClientProtocol::Command::Player, true));
|
QVERIFY(cle.setSubscribe(ClientProtocol::Command::Player, true));
|
||||||
QVERIFY(cle.changeSubscribe(ClientProtocol::Command::Player, false));
|
QVERIFY(cle.setSubscribe(ClientProtocol::Command::Player, false));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user