2022-10-21 03:53:30 +02:00
|
|
|
#ifndef LOBBY_MOC_H
|
|
|
|
#define LOBBY_MOC_H
|
|
|
|
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QTcpSocket>
|
|
|
|
#include <QAbstractSocket>
|
|
|
|
|
2022-10-30 20:50:16 +02:00
|
|
|
const unsigned int ProtocolVersion = 1;
|
2022-10-30 20:53:05 +02:00
|
|
|
#ifdef VCMI_WINDOWS
|
|
|
|
const std::string ProtocolEncoding = "utf16";
|
|
|
|
#else
|
2022-10-30 20:50:16 +02:00
|
|
|
const std::string ProtocolEncoding = "utf8";
|
2022-10-30 20:53:05 +02:00
|
|
|
#endif
|
2022-10-30 20:50:16 +02:00
|
|
|
|
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-29 19:10:21 +02:00
|
|
|
SESSIONS, CREATED, JOINED, KICKED, SRVERROR, 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
|
2022-10-30 20:50:16 +02:00
|
|
|
{GREETING, "%1<GREETINGS>%2<VER>%3"}, //protocol_version byte, encoding bytes, encoding, name, version
|
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-29 19:10:21 +02:00
|
|
|
{SRVERROR, "ERROR"},
|
2022-10-23 00:55:22 +02:00
|
|
|
{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;
|
2022-11-08 02:44:34 +02:00
|
|
|
QStringList gameArgs;
|
2022-10-23 00:55:22 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void protocolAssert(bool);
|
2022-10-21 03:53:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // LOBBY_MOC_H
|