2022-10-21 03:53:30 +02:00
|
|
|
#ifndef LOBBY_MOC_H
|
|
|
|
#define LOBBY_MOC_H
|
|
|
|
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QTcpSocket>
|
|
|
|
#include <QAbstractSocket>
|
|
|
|
|
2022-10-23 00:55:22 +02:00
|
|
|
class ProtocolError: public std::runtime_error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ProtocolError(const char * w): std::runtime_error(w) {}
|
|
|
|
};
|
|
|
|
|
2022-10-21 18:29:27 +02:00
|
|
|
enum ProtocolConsts
|
|
|
|
{
|
2022-10-23 00:55:22 +02:00
|
|
|
//client consts
|
2022-10-23 21:10:46 +02:00
|
|
|
GREETING, USERNAME, MESSAGE, VERSION, CREATE, JOIN, LEAVE, READY,
|
2022-10-23 00:55:22 +02:00
|
|
|
|
|
|
|
//server consts
|
2022-10-25 03:27:53 +02:00
|
|
|
SESSIONS, CREATED, JOINED, KICKED, ERROR, CHAT, START, STATUS, HOST
|
2022-10-21 18:29:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const QMap<ProtocolConsts, QString> ProtocolStrings
|
|
|
|
{
|
2022-10-23 00:55:22 +02:00
|
|
|
//client consts
|
|
|
|
{GREETING, "<GREETINGS>%1<VER>%2"},
|
2022-10-21 18:29:27 +02:00
|
|
|
{USERNAME, "<USER>%1"},
|
|
|
|
{MESSAGE, "<MSG>%1"},
|
2022-10-23 00:55:22 +02:00
|
|
|
{CREATE, "<NEW>%1<PSWD>%2<COUNT>%3"},
|
|
|
|
{JOIN, "<JOIN>%1<PSWD>%2"},
|
2022-10-23 21:10:46 +02:00
|
|
|
{LEAVE, "<LEAVE>%1"}, //session
|
|
|
|
{READY, "<READY>%1"}, //session
|
2022-10-23 00:55:22 +02:00
|
|
|
|
|
|
|
//server consts
|
|
|
|
{CREATED, "CREATED"},
|
|
|
|
{SESSIONS, "SESSIONS"}, //amount:session_name:joined_players:total_players:is_protected
|
|
|
|
{JOINED, "JOIN"}, //session_name:username
|
|
|
|
{KICKED, "KICK"}, //session_name:username
|
2022-10-23 21:24:33 +02:00
|
|
|
{START, "START"}, //session_name:uuid
|
2022-10-25 03:27:53 +02:00
|
|
|
{HOST, "HOST"}, //host_uuid:players_count
|
2022-10-23 21:10:46 +02:00
|
|
|
{STATUS, "STATUS"}, //joined_players:player_name:is_ready
|
2022-10-23 00:55:22 +02:00
|
|
|
{ERROR, "ERROR"},
|
|
|
|
{CHAT, "MSG"} //username:message
|
|
|
|
};
|
|
|
|
|
|
|
|
class ServerCommand
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ServerCommand(ProtocolConsts, const QStringList & arguments);
|
|
|
|
|
|
|
|
const ProtocolConsts command;
|
|
|
|
const QStringList arguments;
|
2022-10-21 18:29:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class SocketLobby : public QObject
|
2022-10-21 03:53:30 +02:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2022-10-21 18:29:27 +02:00
|
|
|
explicit SocketLobby(QObject *parent = 0);
|
|
|
|
void connectServer(const QString & host, int port, const QString & username);
|
|
|
|
void disconnectServer();
|
2022-10-23 00:55:22 +02:00
|
|
|
void requestNewSession(const QString & session, int totalPlayers, const QString & pswd);
|
|
|
|
void requestJoinSession(const QString & session, const QString & pswd);
|
2022-10-23 21:10:46 +02:00
|
|
|
void requestLeaveSession(const QString & session);
|
|
|
|
void requestReadySession(const QString & session);
|
2022-10-21 03:53:30 +02:00
|
|
|
|
|
|
|
void send(const QString &);
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
|
|
|
void text(QString);
|
2022-10-23 00:55:22 +02:00
|
|
|
void receive(QString);
|
2022-10-25 03:27:53 +02:00
|
|
|
void disconnect();
|
2022-10-21 03:53:30 +02:00
|
|
|
|
|
|
|
public slots:
|
|
|
|
|
|
|
|
void connected();
|
|
|
|
void disconnected();
|
|
|
|
void bytesWritten(qint64 bytes);
|
|
|
|
void readyRead();
|
|
|
|
|
|
|
|
private:
|
|
|
|
QTcpSocket *socket;
|
|
|
|
bool isConnected = false;
|
2022-10-21 18:29:27 +02:00
|
|
|
QString username;
|
2022-10-21 03:53:30 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
class Lobby;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Lobby : public QWidget
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Lobby(QWidget *parent = nullptr);
|
|
|
|
~Lobby();
|
|
|
|
|
|
|
|
private slots:
|
2022-10-21 18:29:27 +02:00
|
|
|
void on_messageEdit_returnPressed();
|
2022-10-21 03:53:30 +02:00
|
|
|
|
2022-10-23 00:55:22 +02:00
|
|
|
void chatMessage(QString);
|
|
|
|
void dispatchMessage(QString);
|
|
|
|
void serverCommand(const ServerCommand &);
|
2022-10-21 03:53:30 +02:00
|
|
|
|
2022-10-21 18:29:27 +02:00
|
|
|
void on_connectButton_toggled(bool checked);
|
|
|
|
|
2022-10-23 00:55:22 +02:00
|
|
|
void on_newButton_clicked();
|
|
|
|
|
|
|
|
void on_joinButton_clicked();
|
|
|
|
|
2022-10-23 21:10:46 +02:00
|
|
|
void on_buttonLeave_clicked();
|
|
|
|
|
|
|
|
void on_buttonReady_clicked();
|
|
|
|
|
2022-10-25 03:27:53 +02:00
|
|
|
void onDisconnected();
|
|
|
|
|
2022-10-21 03:53:30 +02:00
|
|
|
private:
|
|
|
|
Ui::Lobby *ui;
|
2022-10-21 18:29:27 +02:00
|
|
|
SocketLobby socketLobby;
|
2022-10-23 00:55:22 +02:00
|
|
|
QString hostSession;
|
|
|
|
QString session;
|
|
|
|
QString username;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void protocolAssert(bool);
|
2022-10-21 03:53:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // LOBBY_MOC_H
|