Launcher MP fixes:
- connecting works - no instant exit with an invalid/missing server
This commit is contained in:
parent
054c4bec0f
commit
16b8335a5e
3 changed files with 53 additions and 12 deletions
|
@ -30,17 +30,29 @@ MPServersModel::~MPServersModel()
|
|||
|
||||
int MPServersModel::rowCount(const QModelIndex&) const
|
||||
{
|
||||
// if query failed, we have two item:
|
||||
// 1) - 'no servers found'
|
||||
// 2) - 'custom server'
|
||||
if (m_servers.empty())
|
||||
return 2;
|
||||
|
||||
return m_servers.size() + 1;
|
||||
}
|
||||
|
||||
QVariant MPServersModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
int row = index.row();
|
||||
if ((row < 0) || (row > m_servers.size())) {
|
||||
const int row = index.row();
|
||||
const int customServerRow = (m_servers.empty() ? 1 : m_servers.size());
|
||||
|
||||
if ((row == 0) && m_servers.empty()) {
|
||||
if (role == Qt::DisplayRole) {
|
||||
return tr("No servers available");
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
if (row == m_servers.size()) {
|
||||
if (row == customServerRow) {
|
||||
if (role == Qt::DisplayRole) {
|
||||
return tr("Custom server");
|
||||
} else if (role == IsCustomIndexRole) {
|
||||
|
@ -114,6 +126,7 @@ void MPServersModel::onRefreshMPServersDone(simgear::HTTP::Request*)
|
|||
m_servers.push_back(ServerInfo(name, loc, host, port));
|
||||
}
|
||||
endResetModel();
|
||||
emit validChanged();
|
||||
|
||||
restoreMPServerSelection();
|
||||
m_mpServerRequest.clear();
|
||||
|
@ -126,6 +139,7 @@ void MPServersModel::onRefreshMPServersFailed(simgear::HTTP::Request*)
|
|||
beginResetModel();
|
||||
m_servers.clear();
|
||||
endResetModel();
|
||||
emit validChanged();
|
||||
restoreMPServerSelection();
|
||||
}
|
||||
|
||||
|
@ -160,6 +174,10 @@ void MPServersModel::requestRestore()
|
|||
|
||||
QString MPServersModel::serverForIndex(int index) const
|
||||
{
|
||||
if (!valid()) {
|
||||
return (index == 1) ? "__custom__" : "__noservers__";
|
||||
}
|
||||
|
||||
if ((index < 0) || (index > m_servers.size())) {
|
||||
return QString();
|
||||
}
|
||||
|
@ -173,6 +191,9 @@ QString MPServersModel::serverForIndex(int index) const
|
|||
|
||||
int MPServersModel::portForIndex(int index) const
|
||||
{
|
||||
if (!valid())
|
||||
return 0;
|
||||
|
||||
if ((index < 0) || (index >= m_servers.size())) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -180,6 +201,11 @@ int MPServersModel::portForIndex(int index) const
|
|||
return m_servers.at(index).port;
|
||||
}
|
||||
|
||||
bool MPServersModel::valid() const
|
||||
{
|
||||
return !m_servers.empty();
|
||||
}
|
||||
|
||||
MPServersModel::ServerInfo::ServerInfo(QString n, QString l, QString h, int p)
|
||||
{
|
||||
name = n;
|
||||
|
|
|
@ -10,6 +10,7 @@ class MPServersModel : public QAbstractListModel
|
|||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(bool valid READ valid NOTIFY validChanged)
|
||||
public:
|
||||
MPServersModel(QObject* parent = nullptr);
|
||||
~MPServersModel();
|
||||
|
@ -31,9 +32,14 @@ public:
|
|||
|
||||
Q_INVOKABLE QString serverForIndex(int index) const;
|
||||
Q_INVOKABLE int portForIndex(int index) const;
|
||||
|
||||
bool valid() const;
|
||||
|
||||
signals:
|
||||
void restoreIndex(int index);
|
||||
void restoreDefault();
|
||||
void validChanged();
|
||||
|
||||
private:
|
||||
|
||||
SGSharedPtr<RemoteXMLRequest> m_mpServerRequest;
|
||||
|
|
|
@ -160,7 +160,6 @@ Item {
|
|||
|
||||
onValueChanged: {
|
||||
if (value) {
|
||||
console.info("MP enabled, doing refresh")
|
||||
_launcher.queryMPServers();
|
||||
}
|
||||
}
|
||||
|
@ -190,7 +189,9 @@ Item {
|
|||
description: qsTr("Select a server close to you for better responsiveness and reduced lag when flying online.")
|
||||
choices: _launcher.mpServersModel
|
||||
|
||||
readonly property bool currentIsCustom: (_launcher.mpServersModel.serverForIndex(selectedIndex) == "__custom__")
|
||||
readonly property bool currentIsCustom: (_launcher.mpServersModel.serverForIndex(selectedIndex) === "__custom__")
|
||||
readonly property bool currentIsNoServers: (_launcher.mpServersModel.serverForIndex(selectedIndex) === "__noservers__")
|
||||
|
||||
property string __savedServer;
|
||||
|
||||
keywords: ["server", "hostname"]
|
||||
|
@ -216,6 +217,7 @@ Item {
|
|||
{
|
||||
target: _launcher.mpServersModel
|
||||
onRestoreIndex: { mpServer.selectedIndex = index }
|
||||
onRestoreDefault: { mpServer.selectedIndex = 0; }
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -233,25 +235,32 @@ Item {
|
|||
|
||||
onApply: {
|
||||
if (enableMP.checked) {
|
||||
if (mpServer.currentIsCustom) {
|
||||
if (mpServer.currentIsNoServers) {
|
||||
console.warn("MP enabled but no valid server selected, skipping");
|
||||
} else if (mpServer.currentIsCustom) {
|
||||
var pieces = mpCustomServer.value.split(':')
|
||||
var port = defaultMPPort;
|
||||
if (pieces.length > 1) {
|
||||
port = pieces[1];
|
||||
}
|
||||
|
||||
_config.setProperty("/sim/multiplay/txhost", pieces[0]);
|
||||
_config.setProperty("/sim/multiplay/txport", port);
|
||||
if (pieces[0].length > 0) {
|
||||
_config.setProperty("/sim/multiplay/txhost", pieces[0]);
|
||||
_config.setProperty("/sim/multiplay/txport", port);
|
||||
} else {
|
||||
console.warn("Custom MP server selected but no hostname defined");
|
||||
}
|
||||
} else {
|
||||
var sel = mpServer.selectedIndex
|
||||
var host = _mpServers.serverForIndex(sel);
|
||||
console.log("MP host is " + host)
|
||||
var host = _launcher.mpServersModel.serverForIndex(sel);
|
||||
console.info("MP host is " + host)
|
||||
if (host.length > 0) {
|
||||
_config.setProperty("/sim/multiplay/txhost", host);
|
||||
}
|
||||
|
||||
var port = _mpServers.portForIndex(sel);
|
||||
if (port == 0) {
|
||||
var port = _launcher.mpServersModel.portForIndex(sel);
|
||||
if (port === 0) {
|
||||
console.info("Using default MP port");
|
||||
port = defaultMPPort;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue