feat(network): Ip input field for multiplayer room creation

This commit is contained in:
vampiric_x 2025-01-17 19:03:31 +01:00
parent 3698478977
commit 913cc27e3a
3 changed files with 80 additions and 27 deletions

View file

@ -10,6 +10,10 @@
#include <QMetaType> #include <QMetaType>
#include <QTime> #include <QTime>
#include <QtConcurrent/QtConcurrentRun> #include <QtConcurrent/QtConcurrentRun>
#include <QHostInfo>
#include <QNetworkInterface>
#include <QClipboard>
#include <QApplication>
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/settings.h" #include "common/settings.h"
#include "core/core.h" #include "core/core.h"
@ -53,6 +57,7 @@ HostRoomWindow::HostRoomWindow(QWidget* parent, QStandardItemModel* list,
// Connect all the widgets to the appropriate events // Connect all the widgets to the appropriate events
connect(ui->host, &QPushButton::clicked, this, &HostRoomWindow::Host); connect(ui->host, &QPushButton::clicked, this, &HostRoomWindow::Host);
connect(ui->copy_ip_button, &QPushButton::clicked, this, &HostRoomWindow::CopyIPToClipboard);
// Restore the settings: // Restore the settings:
ui->username->setText( ui->username->setText(
@ -76,6 +81,8 @@ HostRoomWindow::HostRoomWindow(QWidget* parent, QStandardItemModel* list,
} }
ui->room_description->setText( ui->room_description->setText(
QString::fromStdString(UISettings::values.multiplayer_room_description.GetValue())); QString::fromStdString(UISettings::values.multiplayer_room_description.GetValue()));
SetLocalIPAddress();
} }
HostRoomWindow::~HostRoomWindow() = default; HostRoomWindow::~HostRoomWindow() = default;
@ -148,6 +155,7 @@ void HostRoomWindow::Host() {
} }
} }
ui->host->setDisabled(true); ui->host->setDisabled(true);
std::string ip_address = ui->server_address_box->text().toStdString();
const AnnounceMultiplayerRoom::GameInfo game{ const AnnounceMultiplayerRoom::GameInfo game{
.name = ui->game_list->currentData(Qt::DisplayRole).toString().toStdString(), .name = ui->game_list->currentData(Qt::DisplayRole).toString().toStdString(),
@ -164,7 +172,7 @@ void HostRoomWindow::Host() {
if (auto room = room_network.GetRoom().lock()) { if (auto room = room_network.GetRoom().lock()) {
const bool created = const bool created =
room->Create(ui->room_name->text().toStdString(), room->Create(ui->room_name->text().toStdString(),
ui->room_description->toPlainText().toStdString(), "", port, password, ui->room_description->toPlainText().toStdString(), ip_address, port, password,
ui->max_player->value(), Settings::values.citron_username.GetValue(), ui->max_player->value(), Settings::values.citron_username.GetValue(),
game, CreateVerifyBackend(is_public), ban_list); game, CreateVerifyBackend(is_public), ban_list);
if (!created) { if (!created) {
@ -216,7 +224,7 @@ void HostRoomWindow::Host() {
} }
#endif #endif
// TODO: Check what to do with this // TODO: Check what to do with this
member->Join(ui->username->text().toStdString(), "127.0.0.1", port, 0, member->Join(ui->username->text().toStdString(), ip_address.c_str(), port, 0,
Network::NoPreferredIP, password, token); Network::NoPreferredIP, password, token);
// Store settings // Store settings
@ -261,3 +269,25 @@ bool ComboBoxProxyModel::lessThan(const QModelIndex& left, const QModelIndex& ri
auto rightData = right.data(GameListItemPath::TitleRole).toString(); auto rightData = right.data(GameListItemPath::TitleRole).toString();
return leftData.compare(rightData) < 0; return leftData.compare(rightData) < 0;
} }
void HostRoomWindow::SetLocalIPAddress() {
QString local_ip;
foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost)) {
local_ip = address.toString();
break;
}
}
if (!local_ip.isEmpty()) {
ui->server_address_box->setText(local_ip);
} else {
ui->server_address_box->setPlaceholderText(tr("Enter Server Address"));
}
}
void HostRoomWindow::CopyIPToClipboard() {
QClipboard* clipboard = QApplication::clipboard();
clipboard->setText(ui->server_address_box->text());
}

View file

@ -51,6 +51,8 @@ signals:
private: private:
void Host(); void Host();
void SetLocalIPAddress();
void CopyIPToClipboard();
std::unique_ptr<Network::VerifyUser::Backend> CreateVerifyBackend(bool use_validation) const; std::unique_ptr<Network::VerifyUser::Backend> CreateVerifyBackend(bool use_validation) const;
std::unique_ptr<Ui::HostRoom> ui; std::unique_ptr<Ui::HostRoom> ui;

View file

@ -7,13 +7,34 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>607</width> <width>607</width>
<height>211</height> <height>236</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Create Room</string> <string>Create Room</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="server_address_label">
<property name="text">
<string>Server Address:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="server_address_box"/>
</item>
<item>
<widget class="QPushButton" name="copy_ip_button">
<property name="text">
<string>Copy</string>
</property>
</widget>
</item>
</layout>
</item>
<item> <item>
<widget class="QWidget" name="settings" native="true"> <widget class="QWidget" name="settings" native="true">
<layout class="QHBoxLayout"> <layout class="QHBoxLayout">
@ -31,38 +52,38 @@
<property name="labelAlignment"> <property name="labelAlignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property> </property>
<item row="0" column="0"> <item row="1" column="0">
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
<string>Room Name</string> <string>Room Name</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="1" column="1">
<widget class="QLineEdit" name="room_name"> <widget class="QLineEdit" name="room_name">
<property name="maxLength"> <property name="maxLength">
<number>50</number> <number>50</number>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0"> <item row="2" column="0">
<widget class="QLabel" name="label_3"> <widget class="QLabel" name="label_3">
<property name="text"> <property name="text">
<string>Preferred Game</string> <string>Preferred Game</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="2" column="1">
<widget class="QComboBox" name="game_list"/> <widget class="QComboBox" name="game_list"/>
</item> </item>
<item row="2" column="0"> <item row="3" column="0">
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="label_2">
<property name="text"> <property name="text">
<string>Max Players</string> <string>Max Players</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="3" column="1">
<widget class="QSpinBox" name="max_player"> <widget class="QSpinBox" name="max_player">
<property name="minimum"> <property name="minimum">
<number>2</number> <number>2</number>
@ -82,9 +103,6 @@
<property name="labelAlignment"> <property name="labelAlignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property> </property>
<item row="0" column="1">
<widget class="QLineEdit" name="username"/>
</item>
<item row="0" column="0"> <item row="0" column="0">
<widget class="QLabel" name="label_6"> <widget class="QLabel" name="label_6">
<property name="text"> <property name="text">
@ -92,6 +110,16 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QLineEdit" name="username"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QLineEdit" name="password"> <widget class="QLineEdit" name="password">
<property name="echoMode"> <property name="echoMode">
@ -102,6 +130,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Port</string>
</property>
</widget>
</item>
<item row="2" column="1"> <item row="2" column="1">
<widget class="QLineEdit" name="port"> <widget class="QLineEdit" name="port">
<property name="inputMethodHints"> <property name="inputMethodHints">
@ -112,20 +147,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Port</string>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
</layout> </layout>