1 #ifndef ABSTRACTCLIENT_H
2 #define ABSTRACTCLIENT_H
3 
4 #include "pb/response.pb.h"
5 #include "pb/serverinfo_user.pb.h"
6 
7 #include <QMutex>
8 #include <QObject>
9 #include <QVariant>
10 
11 class PendingCommand;
12 class CommandContainer;
13 class RoomEvent;
14 class GameEventContainer;
15 class ServerMessage;
16 class Event_ServerIdentification;
17 class Event_AddToList;
18 class Event_RemoveFromList;
19 class Event_UserJoined;
20 class Event_UserLeft;
21 class Event_ServerMessage;
22 class Event_ListRooms;
23 class Event_GameJoined;
24 class Event_UserMessage;
25 class Event_NotifyUser;
26 class Event_ConnectionClosed;
27 class Event_ServerShutdown;
28 class Event_ReplayAdded;
29 class FeatureSet;
30 
31 enum ClientStatus
32 {
33     StatusDisconnected,
34     StatusDisconnecting,
35     StatusConnecting,
36     StatusRegistering,
37     StatusActivating,
38     StatusLoggingIn,
39     StatusLoggedIn,
40     StatusRequestingForgotPassword,
41     StatusSubmitForgotPasswordReset,
42     StatusSubmitForgotPasswordChallenge,
43 };
44 
45 class AbstractClient : public QObject
46 {
47     Q_OBJECT
48 signals:
49     void statusChanged(ClientStatus _status);
50 
51     // Room events
52     void roomEventReceived(const RoomEvent &event);
53     // Game events
54     void gameEventContainerReceived(const GameEventContainer &event);
55     // Session events
56     void serverIdentificationEventReceived(const Event_ServerIdentification &event);
57     void connectionClosedEventReceived(const Event_ConnectionClosed &event);
58     void serverShutdownEventReceived(const Event_ServerShutdown &event);
59     void addToListEventReceived(const Event_AddToList &event);
60     void removeFromListEventReceived(const Event_RemoveFromList &event);
61     void userJoinedEventReceived(const Event_UserJoined &event);
62     void userLeftEventReceived(const Event_UserLeft &event);
63     void serverMessageEventReceived(const Event_ServerMessage &event);
64     void listRoomsEventReceived(const Event_ListRooms &event);
65     void gameJoinedEventReceived(const Event_GameJoined &event);
66     void userMessageEventReceived(const Event_UserMessage &event);
67     void notifyUserEventReceived(const Event_NotifyUser &event);
68     void userInfoChanged(const ServerInfo_User &userInfo);
69     void buddyListReceived(const QList<ServerInfo_User> &buddyList);
70     void ignoreListReceived(const QList<ServerInfo_User> &ignoreList);
71     void replayAddedEventReceived(const Event_ReplayAdded &event);
72     void registerAccepted();
73     void registerAcceptedNeedsActivate();
74     void activateAccepted();
75 
76     void sigQueuePendingCommand(PendingCommand *pend);
77 
78 private:
79     int nextCmdId;
80     mutable QMutex clientMutex;
81     ClientStatus status;
82 private slots:
83     void queuePendingCommand(PendingCommand *pend);
84 protected slots:
85     void processProtocolItem(const ServerMessage &item);
86 
87 protected:
88     QMap<int, PendingCommand *> pendingCommands;
89     QString userName, password, email, country, realName, token;
90     int gender;
91     void setStatus(ClientStatus _status);
getNewCmdId()92     int getNewCmdId()
93     {
94         return nextCmdId++;
95     }
96     virtual void sendCommandContainer(const CommandContainer &cont) = 0;
97 
98 public:
99     AbstractClient(QObject *parent = nullptr);
100     ~AbstractClient();
101 
getStatus()102     ClientStatus getStatus() const
103     {
104         QMutexLocker locker(&clientMutex);
105         return status;
106     }
107     void sendCommand(const CommandContainer &cont);
108     void sendCommand(PendingCommand *pend);
109 
getUserName()110     const QString getUserName()
111     {
112         return userName;
113     }
114 
115     static PendingCommand *prepareSessionCommand(const ::google::protobuf::Message &cmd);
116     static PendingCommand *prepareRoomCommand(const ::google::protobuf::Message &cmd, int roomId);
117     static PendingCommand *prepareModeratorCommand(const ::google::protobuf::Message &cmd);
118     static PendingCommand *prepareAdminCommand(const ::google::protobuf::Message &cmd);
119 
120     QMap<QString, bool> clientFeatures;
121 };
122 
123 #endif
124