add method queryStatus() to servers and use it on click
This commit is contained in:
parent
43a54cafef
commit
ee35ac5afd
2 changed files with 41 additions and 7 deletions
|
@ -7,14 +7,14 @@
|
||||||
class MCResolver : public QObject {
|
class MCResolver : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
std::string constrDomain;
|
QString constrDomain;
|
||||||
int constrPort;
|
int constrPort;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit MCResolver(QObject *parent, std::string domain, int port): QObject(parent), constrDomain(domain), constrPort(port) {}
|
explicit MCResolver(QObject *parent, QString domain, int port): QObject(parent), constrDomain(domain), constrPort(port) {}
|
||||||
|
|
||||||
void ping() {
|
void ping() {
|
||||||
pingWithDomainSRV(QString::fromStdString(constrDomain), constrPort);
|
pingWithDomainSRV(constrDomain, constrPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -39,6 +39,9 @@
|
||||||
#include "ui/dialogs/CustomMessageBox.h"
|
#include "ui/dialogs/CustomMessageBox.h"
|
||||||
#include "ui_ServersPage.h"
|
#include "ui_ServersPage.h"
|
||||||
|
|
||||||
|
#include "McClient.hpp"
|
||||||
|
#include "McResolver.hpp"
|
||||||
|
|
||||||
#include <FileSystem.h>
|
#include <FileSystem.h>
|
||||||
#include <io/stream_reader.h>
|
#include <io/stream_reader.h>
|
||||||
#include <minecraft/MinecraftInstance.h>
|
#include <minecraft/MinecraftInstance.h>
|
||||||
|
@ -52,7 +55,7 @@
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
static const int COLUMN_COUNT = 2; // 3 , TBD: latency and other nice things.
|
static const int COLUMN_COUNT = 3; // 3 , TBD: latency and other nice things.
|
||||||
|
|
||||||
struct Server {
|
struct Server {
|
||||||
// Types
|
// Types
|
||||||
|
@ -88,6 +91,29 @@ struct Server {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::tuple<QString, int> splitAddress() {
|
||||||
|
auto parts = m_address.split(":");
|
||||||
|
if (parts.size() == 1) {
|
||||||
|
return std::make_tuple(parts[0], 25565);
|
||||||
|
} else {
|
||||||
|
return std::make_tuple(parts[0], parts[1].toInt());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void queryStatus() {
|
||||||
|
auto [domain, port] = splitAddress();
|
||||||
|
MCResolver resolver(nullptr, domain, port);
|
||||||
|
QObject::connect(&resolver, &MCResolver::succeed, [&](QString ip, int port) {
|
||||||
|
qDebug() << "Resolved Addresse for" << domain << ": " << ip << ":" << port;
|
||||||
|
McClient client(nullptr, domain, ip, port);
|
||||||
|
int online = client.getOnlinePlayers();
|
||||||
|
printf("Online players: %d\n", online);
|
||||||
|
|
||||||
|
client.close();
|
||||||
|
});
|
||||||
|
resolver.ping();
|
||||||
|
}
|
||||||
|
|
||||||
void serialize(nbt::tag_compound& server)
|
void serialize(nbt::tag_compound& server)
|
||||||
{
|
{
|
||||||
server.insert("name", m_name.trimmed().toUtf8().toStdString());
|
server.insert("name", m_name.trimmed().toUtf8().toStdString());
|
||||||
|
@ -112,7 +138,6 @@ struct Server {
|
||||||
bool m_checked = false;
|
bool m_checked = false;
|
||||||
bool m_up = false;
|
bool m_up = false;
|
||||||
QString m_motd; // https://mctools.org/motd-creator
|
QString m_motd; // https://mctools.org/motd-creator
|
||||||
int m_ping = 0;
|
|
||||||
int m_currentPlayers = 0;
|
int m_currentPlayers = 0;
|
||||||
int m_maxPlayers = 0;
|
int m_maxPlayers = 0;
|
||||||
};
|
};
|
||||||
|
@ -296,7 +321,7 @@ class ServersModel : public QAbstractListModel {
|
||||||
case 1:
|
case 1:
|
||||||
return tr("Address");
|
return tr("Address");
|
||||||
case 2:
|
case 2:
|
||||||
return tr("Latency");
|
return tr("Online");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -345,7 +370,7 @@ class ServersModel : public QAbstractListModel {
|
||||||
case 2:
|
case 2:
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
return m_servers[row].m_ping;
|
return m_servers[row].m_currentPlayers;
|
||||||
default:
|
default:
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
@ -433,6 +458,14 @@ class ServersModel : public QAbstractListModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void queryServersStatus()
|
||||||
|
{
|
||||||
|
for (auto& server : m_servers) {
|
||||||
|
server.queryStatus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void dirChanged(const QString& path)
|
void dirChanged(const QString& path)
|
||||||
{
|
{
|
||||||
|
@ -737,6 +770,7 @@ void ServersPage::on_actionJoin_triggered()
|
||||||
void ServersPage::on_actionRefresh_triggered()
|
void ServersPage::on_actionRefresh_triggered()
|
||||||
{
|
{
|
||||||
qDebug() << "Action clicked";
|
qDebug() << "Action clicked";
|
||||||
|
m_model->queryServersStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "ServersPage.moc"
|
#include "ServersPage.moc"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue