1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +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 <QAbstractSocket>
const unsigned int ProtocolVersion = 2;
const unsigned int ProtocolVersion = 3;
const std::string ProtocolEncoding = "utf8";
class ProtocolError: public std::runtime_error
@ -24,10 +24,10 @@ public:
enum ProtocolConsts
{
//client consts
GREETING, USERNAME, MESSAGE, VERSION, CREATE, JOIN, LEAVE, READY,
GREETING, USERNAME, MESSAGE, VERSION, CREATE, JOIN, LEAVE, KICK, READY, FORCESTART,
//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
@ -39,7 +39,9 @@ const QMap<ProtocolConsts, QString> ProtocolStrings
{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
{LEAVE, "<LEAVE>%1"}, //session
{KICK, "<KICK>%1"}, //player username
{READY, "<READY>%1"}, //session
{FORCESTART, "<FORCESTART>%1"}, //session
//server consts
{CREATED, "CREATED"},
@ -51,6 +53,7 @@ const QMap<ProtocolConsts, QString> ProtocolStrings
{STATUS, "STATUS"}, //joined_players:player_name:is_ready
{SRVERROR, "ERROR"},
{MODS, "MODS"}, //amount:modname:modversion
{CLIENTMODS, "MODSOTHER"}, //username:amount:modname:modversion
{CHAT, "MSG"} //username:message
};

View File

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

View File

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

View File

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

View File

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