1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-17 11:56:46 +02:00

Extend protocol

This commit is contained in:
nordsoft 2022-11-27 05:35:05 +04:00
parent 63a402a7fa
commit b9ef95be3e
6 changed files with 94 additions and 26 deletions

View File

@ -12,7 +12,7 @@
#include <QTcpSocket> #include <QTcpSocket>
#include <QAbstractSocket> #include <QAbstractSocket>
const unsigned int ProtocolVersion = 2; const unsigned int ProtocolVersion = 3;
const std::string ProtocolEncoding = "utf8"; const std::string ProtocolEncoding = "utf8";
class ProtocolError: public std::runtime_error class ProtocolError: public std::runtime_error
@ -24,10 +24,10 @@ public:
enum ProtocolConsts enum ProtocolConsts
{ {
//client consts //client consts
GREETING, USERNAME, MESSAGE, VERSION, CREATE, JOIN, LEAVE, READY, GREETING, USERNAME, MESSAGE, VERSION, CREATE, JOIN, LEAVE, KICK, READY, FORCESTART,
//server consts //server consts
SESSIONS, CREATED, JOINED, KICKED, SRVERROR, CHAT, START, STATUS, HOST, MODS SESSIONS, CREATED, JOINED, KICKED, SRVERROR, CHAT, START, STATUS, HOST, MODS, CLIENTMODS
}; };
const QMap<ProtocolConsts, QString> ProtocolStrings const QMap<ProtocolConsts, QString> ProtocolStrings
@ -39,7 +39,9 @@ const QMap<ProtocolConsts, QString> ProtocolStrings
{CREATE, "<NEW>%1<PSWD>%2<COUNT>%3<MODS>%4"}, //last placeholder for the mods {CREATE, "<NEW>%1<PSWD>%2<COUNT>%3<MODS>%4"}, //last placeholder for the mods
{JOIN, "<JOIN>%1<PSWD>%2<MODS>%3"}, //last placeholder for the mods {JOIN, "<JOIN>%1<PSWD>%2<MODS>%3"}, //last placeholder for the mods
{LEAVE, "<LEAVE>%1"}, //session {LEAVE, "<LEAVE>%1"}, //session
{KICK, "<KICK>%1"}, //player username
{READY, "<READY>%1"}, //session {READY, "<READY>%1"}, //session
{FORCESTART, "<FORCESTART>%1"}, //session
//server consts //server consts
{CREATED, "CREATED"}, {CREATED, "CREATED"},
@ -51,6 +53,7 @@ const QMap<ProtocolConsts, QString> ProtocolStrings
{STATUS, "STATUS"}, //joined_players:player_name:is_ready {STATUS, "STATUS"}, //joined_players:player_name:is_ready
{SRVERROR, "ERROR"}, {SRVERROR, "ERROR"},
{MODS, "MODS"}, //amount:modname:modversion {MODS, "MODS"}, //amount:modname:modversion
{CLIENTMODS, "MODSOTHER"}, //username:amount:modname:modversion
{CHAT, "MSG"} //username:message {CHAT, "MSG"} //username:message
}; };

View File

@ -12,7 +12,6 @@ Lobby::Lobby(QWidget *parent) :
ui(new Ui::Lobby) ui(new Ui::Lobby)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->buttonReady->setEnabled(false);
connect(&socketLobby, SIGNAL(text(QString)), this, SLOT(sysMessage(QString))); connect(&socketLobby, SIGNAL(text(QString)), this, SLOT(sysMessage(QString)));
connect(&socketLobby, SIGNAL(receive(QString)), this, SLOT(dispatchMessage(QString))); connect(&socketLobby, SIGNAL(receive(QString)), this, SLOT(dispatchMessage(QString)));
@ -24,6 +23,7 @@ Lobby::Lobby(QWidget *parent) :
ui->serverEdit->setText(hostString); ui->serverEdit->setText(hostString);
ui->userEdit->setText(QString::fromStdString(settings["launcher"]["lobbyUsername"].String())); ui->userEdit->setText(QString::fromStdString(settings["launcher"]["lobbyUsername"].String()));
ui->kickButton->setVisible(false);
} }
Lobby::~Lobby() Lobby::~Lobby()
@ -35,6 +35,11 @@ QMap<QString, QString> Lobby::buildModsMap() const
{ {
QMap<QString, QString> result; QMap<QString, QString> result;
QObject * mainWindow = qApp->activeWindow(); QObject * mainWindow = qApp->activeWindow();
if(!mainWindow)
mainWindow = parent();
if(!mainWindow)
return result; //probably something is really wrong here
while(mainWindow->parent()) while(mainWindow->parent())
mainWindow = mainWindow->parent(); mainWindow = mainWindow->parent();
const auto & modlist = qobject_cast<MainWindow*>(mainWindow)->getModList(); const auto & modlist = qobject_cast<MainWindow*>(mainWindow)->getModList();
@ -85,7 +90,6 @@ void Lobby::serverCommand(const ServerCommand & command) try
hostSession = args[0]; hostSession = args[0];
session = args[0]; session = args[0];
sysMessage("new session started"); sysMessage("new session started");
ui->buttonReady->setEnabled(true);
break; break;
case SESSIONS: case SESSIONS:
@ -121,12 +125,11 @@ void Lobby::serverCommand(const ServerCommand & command) try
if(args[1] == username) if(args[1] == username)
{ {
ui->buttonReady->setText("Ready");
ui->chat->clear(); //cleanup the chat ui->chat->clear(); //cleanup the chat
sysMessage(joinStr.arg("you", args[0])); sysMessage(joinStr.arg("you", args[0]));
session = args[0]; session = args[0];
ui->stackedWidget->setCurrentWidget(command.command == JOINED ? ui->roomPage : ui->sessionsPage); ui->stackedWidget->setCurrentWidget(command.command == JOINED ? ui->roomPage : ui->sessionsPage);
if(command.command == KICKED)
ui->buttonReady->setEnabled(false);
} }
else else
{ {
@ -165,6 +168,15 @@ void Lobby::serverCommand(const ServerCommand & command) try
break; break;
} }
case CLIENTMODS: {
protocolAssert(args.size() > 1);
amount = args[1].toInt();
protocolAssert(amount * 2 == (args.size() - 2));
tagPoint = 2;
break;
}
case STATUS: case STATUS:
protocolAssert(args.size() > 0); protocolAssert(args.size() > 0);
@ -175,7 +187,16 @@ void Lobby::serverCommand(const ServerCommand & command) try
ui->playersList->clear(); ui->playersList->clear();
for(int i = 0; i < amount; ++i, tagPoint += 2) for(int i = 0; i < amount; ++i, tagPoint += 2)
{ {
if(args[tagPoint + 1] == "True")
ui->playersList->addItem(new QListWidgetItem(QIcon("icons:mod-enabled.png"), args[tagPoint])); ui->playersList->addItem(new QListWidgetItem(QIcon("icons:mod-enabled.png"), args[tagPoint]));
else
ui->playersList->addItem(new QListWidgetItem(QIcon("icons:mod-disabled.png"), args[tagPoint]));
if(args[tagPoint] == username)
if(args[tagPoint + 1] == "True")
ui->buttonReady->setText("Not ready");
else
ui->buttonReady->setText("Ready");
} }
break; break;
@ -256,6 +277,7 @@ void Lobby::onDisconnected()
ui->stackedWidget->setCurrentWidget(ui->sessionsPage); ui->stackedWidget->setCurrentWidget(ui->sessionsPage);
ui->connectButton->setChecked(false); ui->connectButton->setChecked(false);
ui->serverEdit->setEnabled(true); ui->serverEdit->setEnabled(true);
ui->userEdit->setEnabled(true);
ui->newButton->setEnabled(false); ui->newButton->setEnabled(false);
ui->joinButton->setEnabled(false); ui->joinButton->setEnabled(false);
ui->sessionsTable->clear(); ui->sessionsTable->clear();
@ -316,6 +338,7 @@ void Lobby::on_connectButton_toggled(bool checked)
node["lobbyUsername"].String() = username.toStdString(); node["lobbyUsername"].String() = username.toStdString();
ui->serverEdit->setEnabled(false); ui->serverEdit->setEnabled(false);
ui->userEdit->setEnabled(false);
sysMessage("Connecting to " + serverUrl + ":" + QString::number(serverPort)); sysMessage("Connecting to " + serverUrl + ":" + QString::number(serverPort));
//show text immediately //show text immediately
@ -327,6 +350,7 @@ void Lobby::on_connectButton_toggled(bool checked)
else else
{ {
ui->serverEdit->setEnabled(true); ui->serverEdit->setEnabled(true);
ui->userEdit->setEnabled(true);
socketLobby.disconnectServer(); socketLobby.disconnectServer();
} }
} }
@ -349,22 +373,36 @@ void Lobby::on_joinButton_clicked()
} }
} }
void Lobby::on_buttonLeave_clicked() void Lobby::on_buttonLeave_clicked()
{ {
socketLobby.requestLeaveSession(session); socketLobby.requestLeaveSession(session);
} }
void Lobby::on_buttonReady_clicked() void Lobby::on_buttonReady_clicked()
{ {
if(ui->buttonReady->text() == "Ready")
ui->buttonReady->setText("Not ready");
else
ui->buttonReady->setText("Ready");
socketLobby.requestReadySession(session); socketLobby.requestReadySession(session);
} }
void Lobby::on_sessionsTable_itemSelectionChanged() void Lobby::on_sessionsTable_itemSelectionChanged()
{ {
auto selection = ui->sessionsTable->selectedItems(); auto selection = ui->sessionsTable->selectedItems();
ui->joinButton->setEnabled(!selection.empty()); ui->joinButton->setEnabled(!selection.empty());
} }
void Lobby::on_playersList_currentRowChanged(int currentRow)
{
ui->kickButton->setVisible(ui->playersList->currentItem()
&& currentRow > 0
&& ui->playersList->currentItem()->text() != username);
}
void Lobby::on_kickButton_clicked()
{
if(ui->playersList->currentItem() && ui->playersList->currentItem()->text() != username)
socketLobby.send(ProtocolStrings[KICK].arg(ui->playersList->currentItem()->text()));
}

View File

@ -36,6 +36,10 @@ private slots:
void on_sessionsTable_itemSelectionChanged(); void on_sessionsTable_itemSelectionChanged();
void on_playersList_currentRowChanged(int currentRow);
void on_kickButton_clicked();
private: private:
QString serverUrl; QString serverUrl;
int serverPort; int serverPort;

View File

@ -147,14 +147,28 @@
</widget> </widget>
<widget class="QWidget" name="roomPage"> <widget class="QWidget" name="roomPage">
<layout class="QGridLayout" name="gridLayout_3"> <layout class="QGridLayout" name="gridLayout_3">
<item row="4" column="0"> <item row="5" column="1">
<widget class="QPushButton" name="buttonReady">
<property name="text">
<string>Ready</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Mods mismatch</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QPushButton" name="buttonLeave"> <widget class="QPushButton" name="buttonLeave">
<property name="text"> <property name="text">
<string>Leave</string> <string>Leave</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="0" colspan="2"> <item row="4" column="0" colspan="2">
<widget class="QListWidget" name="modsList"> <widget class="QListWidget" name="modsList">
<property name="editTriggers"> <property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set> <set>QAbstractItemView::NoEditTriggers</set>
@ -164,37 +178,33 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0" colspan="2"> <item row="2" column="0" colspan="2">
<widget class="QListWidget" name="playersList"> <widget class="QListWidget" name="playersList">
<property name="editTriggers"> <property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set> <set>QAbstractItemView::NoEditTriggers</set>
</property> </property>
<property name="selectionMode"> <property name="selectionMode">
<enum>QAbstractItemView::NoSelection</enum> <enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="1"> <item row="1" column="1">
<widget class="QPushButton" name="buttonReady"> <widget class="QPushButton" name="kickButton">
<property name="text"> <property name="text">
<string>Ready</string> <string>Kick player</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="0" colspan="2"> <item row="1" column="0">
<widget class="QLabel" name="label_3"> <widget class="QLabel" name="label_3">
<property name="text"> <property name="text">
<string>Players in the room</string> <string>Players in the room</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Mods mismatch</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</widget> </widget>

View File

@ -122,6 +122,11 @@ void MainWindow::on_startGameButton_clicked()
startGame({}); startGame({});
} }
void MainWindow::on_tabSelectList_currentRowChanged(int currentRow)
{
ui->startGameButton->setEnabled(currentRow != TabRows::LOBBY);
}
const CModList & MainWindow::getModList() const const CModList & MainWindow::getModList() const
{ {
return ui->modlistView->getModList(); return ui->modlistView->getModList();

View File

@ -30,6 +30,11 @@ private:
Ui::MainWindow * ui; Ui::MainWindow * ui;
void load(); void load();
enum TabRows
{
MODS = 0, SETTINGS = 1, LOBBY = 2
};
public: public:
explicit MainWindow(QWidget * parent = 0); explicit MainWindow(QWidget * parent = 0);
~MainWindow(); ~MainWindow();
@ -39,4 +44,7 @@ public:
public slots: public slots:
void on_startGameButton_clicked(); void on_startGameButton_clicked();
private slots:
void on_tabSelectList_currentRowChanged(int currentRow);
}; };