From b6d94cd43ea3715f4f81b552d01fc58a584ff1c3 Mon Sep 17 00:00:00 2001 From: EndrII <endriimail@gmail.com> Date: Wed, 7 Aug 2019 22:15:26 +0300 Subject: [PATCH] added listner for client --- SnakeClient/SnakeApp/SnakeApp.pro | 2 + .../back-end/ProfileViewItems/mainclient.cpp | 47 +++++++++++++++++++ .../back-end/ProfileViewItems/mainclient.h | 23 +++++++++ SnakeServer/ClientProtocol/client.cpp | 4 +- SnakeServer/ClientProtocol/client.h | 9 +++- .../tst_testsnakeserver.cpp | 4 +- 6 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 SnakeClient/SnakeApp/back-end/ProfileViewItems/mainclient.cpp create mode 100644 SnakeClient/SnakeApp/back-end/ProfileViewItems/mainclient.h diff --git a/SnakeClient/SnakeApp/SnakeApp.pro b/SnakeClient/SnakeApp/SnakeApp.pro index 8b19a16..4dc6b5a 100644 --- a/SnakeClient/SnakeApp/SnakeApp.pro +++ b/SnakeClient/SnakeApp/SnakeApp.pro @@ -13,6 +13,7 @@ DEFINES += QT_DEPRECATED_WARNINGS #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ + back-end/ProfileViewItems/mainclient.cpp \ back-end/ProfileViewItems/mainmenumodel.cpp \ back-end/ProfileViewItems/networkclient.cpp \ back-end/ProfileViewItems/playerclientdata.cpp \ @@ -86,6 +87,7 @@ RESOURCES += \ qml.qrc HEADERS += \ + back-end/ProfileViewItems/mainclient.h \ back-end/ProfileViewItems/mainmenumodel.h \ back-end/ProfileViewItems/networkclient.h \ back-end/ProfileViewItems/playerclientdata.h \ diff --git a/SnakeClient/SnakeApp/back-end/ProfileViewItems/mainclient.cpp b/SnakeClient/SnakeApp/back-end/ProfileViewItems/mainclient.cpp new file mode 100644 index 0000000..08a4c9c --- /dev/null +++ b/SnakeClient/SnakeApp/back-end/ProfileViewItems/mainclient.cpp @@ -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(); +} diff --git a/SnakeClient/SnakeApp/back-end/ProfileViewItems/mainclient.h b/SnakeClient/SnakeApp/back-end/ProfileViewItems/mainclient.h new file mode 100644 index 0000000..fa07223 --- /dev/null +++ b/SnakeClient/SnakeApp/back-end/ProfileViewItems/mainclient.h @@ -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 diff --git a/SnakeServer/ClientProtocol/client.cpp b/SnakeServer/ClientProtocol/client.cpp index 111665c..79e1938 100644 --- a/SnakeServer/ClientProtocol/client.cpp +++ b/SnakeServer/ClientProtocol/client.cpp @@ -52,6 +52,8 @@ void Client::updateStatuses(Command extCmd, Command cmd, Type type, const QByteA } + _currentUserId = data.id(); + setLoginStatus(cmd == Command::UpdatePlayerData && validData); } } @@ -375,7 +377,7 @@ const bool& Client::isLogin() const { return _logined; } -bool Client::changeSubscribe(Command cmd, bool subscribe, int id) { +bool Client::setSubscribe(Command cmd, bool subscribe, int id) { if (!isLogin()) { return false; } diff --git a/SnakeServer/ClientProtocol/client.h b/SnakeServer/ClientProtocol/client.h index 6e913a7..9f42606 100644 --- a/SnakeServer/ClientProtocol/client.h +++ b/SnakeServer/ClientProtocol/client.h @@ -28,6 +28,7 @@ private: QHash<unsigned char, QVariantMap> _requestsMap; QHash<quint8, bool> _subscribe; // command and data confirmation + /** * @brief checkCommand - return old sendet command if commnad not valid return undefined command * @param sig - sig package @@ -52,6 +53,10 @@ private slots: void incommingData(); void handleDisconnected(); +protected: + int _currentUserId = -1; + + public: explicit Client(const QString& addrress = LOCAL_SNAKE_SERVER, unsigned short port = DEFAULT_SNAKE_PORT, @@ -111,14 +116,14 @@ public: const bool &isLogin() const; /** - * @brief changeSubscribe change subscribe of command "cmd" + * @brief setSubscribe change subscribe of command "cmd" * @param cmd - command of subscribe * @param subscribe - boolean barametr. true is subscribe, false is unsubscribe * @param id - id of object for commands (Player and Item). * If this parameter is -1 then subscribe on all changes of objects. * @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 diff --git a/SnakeServer/serverProtocolTests/tst_testsnakeserver.cpp b/SnakeServer/serverProtocolTests/tst_testsnakeserver.cpp index 6f98098..d4bd526 100644 --- a/SnakeServer/serverProtocolTests/tst_testsnakeserver.cpp +++ b/SnakeServer/serverProtocolTests/tst_testsnakeserver.cpp @@ -357,8 +357,8 @@ void testSankeServer::testApplyData(ClientProtocol::Client &cle) { void testSankeServer::testWebSockets(ClientProtocol::Client &cle) { - QVERIFY(cle.changeSubscribe(ClientProtocol::Command::Player, true)); - QVERIFY(cle.changeSubscribe(ClientProtocol::Command::Player, false)); + QVERIFY(cle.setSubscribe(ClientProtocol::Command::Player, true)); + QVERIFY(cle.setSubscribe(ClientProtocol::Command::Player, false)); }