82 lines
2.4 KiB
C++
82 lines
2.4 KiB
C++
#ifndef NETWORKMANAGER_H
|
|
#define NETWORKMANAGER_H
|
|
|
|
#include <QObject>
|
|
#include <QWebSocket>
|
|
#include <QJsonObject>
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QTimer>
|
|
|
|
class NetworkManager : public QObject {
|
|
Q_OBJECT
|
|
public:
|
|
static NetworkManager& instance() {
|
|
static NetworkManager mgr;
|
|
return mgr;
|
|
}
|
|
|
|
void connectToServer(const QString& url);
|
|
void disconnect();
|
|
bool isConnected() const;
|
|
|
|
void sendMessage(const QString& type, const QJsonObject& payload = {});
|
|
|
|
void joinRoom(const QString& roomId, const QString& nickname);
|
|
void createRoom(const QString& nickname, int maxPlayers);
|
|
void leaveRoom();
|
|
void setReady(bool ready);
|
|
void startGame();
|
|
void selectCard(const QString& cardUid);
|
|
void confirmAction(const QString& action, const QJsonObject& extra = {});
|
|
void effectResponse(const QJsonObject& response);
|
|
void listRooms();
|
|
void sendChat(const QString& text);
|
|
void sendBinaryFrame(const QByteArray& data);
|
|
|
|
signals:
|
|
void connected();
|
|
void disconnected();
|
|
void connectionError(const QString& error);
|
|
|
|
void roomStateUpdated(const QJsonObject& roomState);
|
|
void gameStarted(const QJsonObject& initConfig);
|
|
void gameStateSnapshot(const QJsonObject& snapshot);
|
|
void cardMoveEvent(const QJsonObject& event);
|
|
void cardFlipEvent(const QJsonObject& event);
|
|
void effectTriggered(const QJsonObject& event);
|
|
void requestChoice(const QJsonObject& request);
|
|
void playerExited(const QJsonObject& event);
|
|
void settlementNotify(const QJsonObject& event);
|
|
void gameEnded(const QJsonObject& result);
|
|
void errorNotify(const QJsonObject& error);
|
|
void chatMessage(const QJsonObject& msg);
|
|
|
|
void roomCreated(const QString& roomId);
|
|
void roomJoined(const QJsonObject& roomInfo);
|
|
void roomLeft();
|
|
void roomListReceived(const QJsonArray& rooms);
|
|
void settlementResult(const QJsonObject& data);
|
|
void binaryFrameReceived(const QByteArray& data);
|
|
|
|
private slots:
|
|
void onConnected();
|
|
void onDisconnected();
|
|
void onTextMessageReceived(const QString& message);
|
|
void onError(QAbstractSocket::SocketError error);
|
|
void onHeartbeat();
|
|
|
|
private:
|
|
explicit NetworkManager(QObject* parent = nullptr);
|
|
~NetworkManager() override = default;
|
|
|
|
void handleServerMessage(const QJsonObject& msg);
|
|
|
|
QWebSocket _socket;
|
|
QTimer _heartbeatTimer;
|
|
int _seqNum = 0;
|
|
QString _serverUrl;
|
|
};
|
|
|
|
#endif // NETWORKMANAGER_H
|