1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 20:49:56 +00:00
openmw-tes3mp/apps/browser/ServerInfoDialog.cpp

104 lines
3.1 KiB
C++
Raw Normal View History

2017-04-27 03:40:41 +00:00
#include <apps/browser/netutils/QueryClient.hpp>
2017-01-11 14:04:53 +00:00
#include "qdebug.h"
#include "ServerInfoDialog.hpp"
2017-05-06 18:17:08 +00:00
#include <algorithm>
#include <utility>
#include <QThread>
2017-04-27 03:40:41 +00:00
using namespace std;
using namespace RakNet;
2017-01-11 14:04:53 +00:00
ThrWorker::ThrWorker(ServerInfoDialog *dialog, QString addr, unsigned short port): addr(std::move(addr)), port(port), stopped(false)
{
this->dialog = dialog;
}
void ThrWorker::process()
{
stopped = false;
auto newSD = QueryClient::Get().Update(SystemAddress(addr.toUtf8(), port));
if (dialog != nullptr)
dialog->setData(newSD);
stopped = true;
emit finished();
}
ServerInfoDialog::ServerInfoDialog(const QString &addr, QWidget *parent): QDialog(parent)
2017-01-11 14:04:53 +00:00
{
setupUi(this);
refreshThread = new QThread;
QStringList list = addr.split(':');
worker = new ThrWorker(this, list[0].toLatin1(), list[1].toUShort());
worker->moveToThread(refreshThread);
connect(refreshThread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), refreshThread, SLOT(quit()));
connect(refreshThread, SIGNAL(finished()), this, SLOT(refresh()));
connect(btnRefresh, &QPushButton::clicked, [this]{
if (!refreshThread->isRunning())
refreshThread->start();
});
2017-01-11 14:04:53 +00:00
}
ServerInfoDialog::~ServerInfoDialog()
{
worker->dialog = nullptr;
if (!refreshThread->isRunning())
refreshThread->terminate();
}
2017-01-11 14:04:53 +00:00
bool ServerInfoDialog::isUpdated()
{
return sd.first != UNASSIGNED_SYSTEM_ADDRESS;
2017-01-11 14:04:53 +00:00
}
void ServerInfoDialog::setData(std::pair<RakNet::SystemAddress, QueryData> &newSD)
2017-04-27 03:40:41 +00:00
{
sd = newSD;
2017-04-27 03:40:41 +00:00
}
void ServerInfoDialog::refresh()
2017-01-11 14:04:53 +00:00
{
2017-04-27 03:40:41 +00:00
if (sd.first != UNASSIGNED_SYSTEM_ADDRESS)
{
2017-04-27 03:40:41 +00:00
leAddr->setText(sd.first.ToString(true, ':'));
lblName->setText(sd.second.GetName());
int ping = PingRakNetServer(sd.first.ToString(false), sd.first.GetPort());
lblPing->setNum(ping);
btnConnect->setDisabled(ping == PING_UNREACHABLE);
listPlayers->clear();
for (const auto &player : sd.second.players)
2017-04-27 03:40:41 +00:00
listPlayers->addItem(QString::fromStdString(player));
listPlugins->clear();
for (const auto &plugin : sd.second.plugins)
2017-04-27 03:40:41 +00:00
listPlugins->addItem(QString::fromStdString(plugin.name));
2017-05-06 18:17:08 +00:00
listRules->clear();
const static vector<std::string> defaultRules {"gamemode", "maxPlayers", "name", "passw", "players", "version"};
for (auto &rule : sd.second.rules)
2017-04-27 03:41:35 +00:00
{
if (::find(defaultRules.begin(), defaultRules.end(), rule.first) != defaultRules.end())
2017-04-27 03:41:35 +00:00
continue;
QString ruleStr = QString::fromStdString(rule.first) + " : ";
if (rule.second.type == 's')
ruleStr += QString::fromStdString(rule.second.str);
2017-04-27 03:41:35 +00:00
else
ruleStr += QString::number(rule.second.val);
listRules->addItem(ruleStr);
2017-04-27 03:41:35 +00:00
}
2017-04-27 03:40:41 +00:00
lblPlayers->setText(QString::number(sd.second.players.size()) + " / " + QString::number(sd.second.GetMaxPlayers()));
}
}
int ServerInfoDialog::exec()
{
if (!refreshThread->isRunning())
refreshThread->start();
return QDialog::exec();
2017-01-11 14:04:53 +00:00
}