diff --git a/apps/browser/CMakeLists.txt b/apps/browser/CMakeLists.txt index a8389077e..a605652c0 100644 --- a/apps/browser/CMakeLists.txt +++ b/apps/browser/CMakeLists.txt @@ -9,7 +9,6 @@ set(BROWSER main.cpp MainWindow.cpp ServerModel.cpp - NetController.cpp ServerInfoDialog.cpp MySortFilterProxyModel.cpp netutils/HTTPNetwork.cpp @@ -27,7 +26,6 @@ set(BROWSER_HEADER_MOC set(BROWSER_HEADER ${BROWSER_HEADER_MOC} - NetController.hpp netutils/HTTPNetwork.hpp netutils/Utils.hpp netutils/QueryClient.hpp diff --git a/apps/browser/MainWindow.cpp b/apps/browser/MainWindow.cpp index 0d896986b..e924ea5ca 100644 --- a/apps/browser/MainWindow.cpp +++ b/apps/browser/MainWindow.cpp @@ -3,7 +3,6 @@ // #include "MainWindow.hpp" -#include "NetController.hpp" #include "ServerInfoDialog.hpp" #include "components/files/configurationmanager.hpp" #include @@ -12,8 +11,11 @@ #include #include #include +#include +#include using namespace Process; +using namespace std; MainWindow::MainWindow(QWidget *parent) { @@ -58,7 +60,12 @@ void MainWindow::addServerAndUpdate(QString addr) favorites->insertRow(0); QModelIndex mi = favorites->index(0, ServerData::ADDR); favorites->setData(mi, addr, Qt::EditRole); - NetController::get()->updateInfo(favorites, mi); + //NetController::get()->updateInfo(favorites, mi); + //QueryClient::Update(RakNet::SystemAddress()) + /*auto data = QueryClient::Get().Query(); + if(data.empty()) + return; + transform(data.begin(), data.end(), back_inserter());*/ } void MainWindow::addServer() @@ -99,11 +106,42 @@ void MainWindow::deleteServer() bool MainWindow::refresh() { - return NetController::get()->updateInfo(proxyModel->sourceModel()); - /*tblServerBrowser->resizeColumnToContents(ServerData::HOSTNAME); - tblServerBrowser->resizeColumnToContents(ServerData::MODNAME); - tblFavorites->resizeColumnToContents(ServerData::HOSTNAME); - tblFavorites->resizeColumnToContents(ServerData::MODNAME);*/ + auto data = QueryClient::Get().Query(); + if(QueryClient::Get().Status() != ID_MASTER_QUERY) + return false; + + ServerModel *model = ((ServerModel*)proxyModel->sourceModel()); + model->removeRows(0, model->rowCount()); + for(auto server : data) + { + model->insertRow(0); + + QModelIndex mi = model->index(0, ServerData::ADDR); + model->setData(mi, server.first.ToString(true, ':')); + + mi = model->index(0, ServerData::PLAYERS); + model->setData(mi, server.second.GetPlayers()); + + mi = model->index(0, ServerData::MAX_PLAYERS); + model->setData(mi, server.second.GetMaxPlayers()); + + mi = model->index(0, ServerData::HOSTNAME); + model->setData(mi, server.second.GetName()); + + mi = model->index(0, ServerData::MODNAME); + model->setData(mi, server.second.GetGameMode()); + + mi = model->index(0, ServerData::VERSION); + model->setData(mi, server.second.GetVersion()); + + mi = model->index(0, ServerData::PASSW); + model->setData(mi, server.second.GetPassword() == 1); + + mi = model->index(0, ServerData::PING); + model->setData(mi, PingRakNetServer(server.first.ToString(false), server.first.GetPort())); + } + + return true; } void MainWindow::play() @@ -117,7 +155,7 @@ void MainWindow::play() ServerModel *sm = ((ServerModel*)proxyModel->sourceModel()); int sourceId = proxyModel->mapToSource(proxyModel->index(id, ServerData::ADDR)).row(); - NetController::get()->selectServer(&sm->myData[sourceId]); + infoDialog.Server(sm->myData[sourceId].addr); infoDialog.refresh(); if(!infoDialog.exec()) return; @@ -125,7 +163,7 @@ void MainWindow::play() QStringList arguments; arguments.append(QLatin1String("--connect=") + sm->myData[sourceId].addr.toLatin1()); - if(sm->myData[sourceId].needPassw) + if(sm->myData[sourceId].GetPassword() == 1) { bool ok; QString passw = QInputDialog::getText(this, "Connecting to: " + sm->myData[sourceId].addr, "Password: ", QLineEdit::Password, "", &ok); diff --git a/apps/browser/NetController.cpp b/apps/browser/NetController.cpp deleted file mode 100644 index e539f0983..000000000 --- a/apps/browser/NetController.cpp +++ /dev/null @@ -1,266 +0,0 @@ -// -// Created by koncord on 07.01.17. -// - -#include -#include -#include "NetController.hpp" -#include "qdebug.h" - -#include -#include - -#include - -#include -#include -#include -#include -#include - -using namespace std; - -NetController *NetController::mThis = nullptr; - -NetController *NetController::get() -{ - assert(mThis); - return mThis; -} - -void NetController::Create(std::string addr, unsigned short port) -{ - assert(!mThis); - mThis = new NetController(addr, port); -} - -void NetController::Destroy() -{ - assert(mThis); - delete mThis; - mThis = nullptr; -} - -NetController::NetController(std::string addr, unsigned short port) : httpNetwork(addr, port) -{ - -} - -NetController::~NetController() -{ - -} - -struct pattern -{ - pattern(QString value): value(value) {} - bool operator()(const ServerData &data) - { - return value == data.addr; - } - QString value; -}; - -void NetController::setData(QString address, QJsonObject server, ServerModel *model) -{ - QModelIndex mi = model->index(0, ServerData::ADDR); - model->setData(mi, address); - - mi = model->index(0, ServerData::PLAYERS); - model->setData(mi, server["players"].toInt()); - - mi = model->index(0, ServerData::MAX_PLAYERS); - model->setData(mi, server["max_players"].toInt()); - - mi = model->index(0, ServerData::HOSTNAME); - model->setData(mi, server["hostname"].toString()); - - mi = model->index(0, ServerData::MODNAME); - model->setData(mi, server["modname"].toString()); - - mi = model->index(0, ServerData::VERSION); - model->setData(mi, server["version"].toString()); - - mi = model->index(0, ServerData::PASSW); - model->setData(mi, server["passw"].toBool()); - - mi = model->index(0, ServerData::PING); - - // This *should* fix a crash when a port isn't returned by data. - if(!address.contains(":")) - address.append(":25565"); - QStringList addr = address.split(":"); - model->setData(mi, PingRakNetServer(addr[0].toLatin1().data(), addr[1].toUShort())); -} - -bool NetController::downloadInfo(QAbstractItemModel *pModel, QModelIndex index) -{ - ServerModel *model = ((ServerModel *) pModel); - - /* - * download stuff - */ - - QString data; - QJsonParseError err; - - if(index.isValid() && index.row() >= 0) - { - const ServerData &sd = model->myData[index.row()]; - while(true) - { - data = QString::fromStdString(httpNetwork.getData((QString("/api/servers/") + sd.addr).toLatin1())); - if (!data.isEmpty() && data != "NO_CONTENT" && data != "LOST_CONNECTION") - break; - RakSleep(30); - } - qDebug() << "Content for \"" << sd.addr << "\": " << data; - - if(data == "bad request" || data == "not found") // TODO: if server is not registered we should download info directly from the server - { - qDebug() << "Server is not registered"; - return false; - } - - QJsonDocument jsonDocument = QJsonDocument::fromJson(data.toLatin1(), &err); - QJsonObject server = jsonDocument.object()["server"].toObject(); - - setData(sd.addr, server, model); - - return true; - } - - while (true) - { - data = QString::fromStdString(httpNetwork.getData("/api/servers")); - if (!data.isEmpty() && data != "NO_CONTENT" && data != "LOST_CONNECTION") - break; - RakSleep(30); - } - - if(data == "UNKNOWN_ADDRESS") - { - QMessageBox::critical(0, "Error", "Cannot connect to the master server!"); - return false; - } - - qDebug() << "Content: " << data; - - QJsonDocument jsonDocument = QJsonDocument::fromJson(data.toLatin1(), &err); - - QJsonObject listServers = jsonDocument.object()["list servers"].toObject(); - - for(auto iter = listServers.begin(); iter != listServers.end(); iter++) - { - QJsonObject server = iter->toObject(); - qDebug() << iter.key(); - qDebug() << server["hostname"].toString(); - qDebug() << server["modname"].toString(); - qDebug() << server["players"].toInt(); - qDebug() << server["max_players"].toInt(); - qDebug() << server["version"].toString(); - qDebug() << server["passw"].toBool(); - - QVector::Iterator value = std::find_if(model->myData.begin(), model->myData.end(), pattern(iter.key())); - if(value == model->myData.end()) - model->insertRow(0); - - setData(iter.key(), server, model); - } - - return true; -} - -bool NetController::updateInfo(QAbstractItemModel *pModel, QModelIndex index) -{ - ServerModel *model = ((ServerModel*)pModel); - - bool result; - if (index.isValid() && index.row() >= 0) - result = downloadInfo(pModel, index); - else - { - for (auto iter = model->myData.begin(); iter != model->myData.end(); iter++) - { - qDebug() << iter->addr; - } - model->removeRows(0, model->rowCount(index)); - result = downloadInfo(pModel, index); - } - return result; -} - -void NetController::updateInfo() -{ - QString data; - QString uri = "/api/servers/" + sd->addr; - while (true) - { - data = QString::fromStdString(httpNetwork.getData(uri.toLatin1())); - if (!data.isEmpty() && data != "NO_CONTENT" && data != "LOST_CONNECTION") - break; - RakSleep(30); - } - - if(data == "UNKNOWN_ADDRESS") - { - QMessageBox::critical(0, "Error", "Cannot connect to the master server!"); - return; - } - - qDebug() << "Content: " << data; - - QJsonParseError err; - QJsonDocument jsonDocument = QJsonDocument::fromJson(data.toLatin1(), &err); - - QMap map = jsonDocument.toVariant().toMap()["server"].toMap(); - - qDebug() << sd->addr; - qDebug() << map["hostname"].toString(); - qDebug() << map["modname"].toString(); - qDebug() << map["players"].toInt(); - qDebug() << map["max_players"].toInt(); - qDebug() << map["version"].toString(); - qDebug() << map["passw"].toBool(); - - sd->hostName = map["hostname"].toString(); - sd->modName = map["modname"].toString(); - sd->players = map["players"].toInt(); - sd->maxPlayers = map["max_players"].toInt(); - - if(!sd->addr.contains(":")) - sd->addr.append(":25565"); - QStringList addr = sd->addr.split(":"); - sd->ping = PingRakNetServer(addr[0].toLatin1(), addr[1].toUShort()); - if(sd->ping != PING_UNREACHABLE) - sed = getExtendedData(addr[0].toLatin1(), addr[1].toUShort()); - else - qDebug() << "Server is unreachable"; -} - -QStringList NetController::players() -{ - QStringList listPlayers; - for(auto player = sed.players.begin(); player != sed.players.end(); player++) - listPlayers.push_back(player->c_str()); - return listPlayers; -} - -QStringList NetController::plugins() -{ - QStringList listPlugins; - for(auto plugin = sed.plugins.begin(); plugin != sed.plugins.end(); plugin++) - listPlugins.push_back(plugin->c_str()); - return listPlugins; -} - -void NetController::selectServer(ServerData *pServerData) -{ - sd = pServerData; -} - -ServerData *NetController::selectedServer() -{ - return sd; -} - diff --git a/apps/browser/NetController.hpp b/apps/browser/NetController.hpp deleted file mode 100644 index 2dd508068..000000000 --- a/apps/browser/NetController.hpp +++ /dev/null @@ -1,42 +0,0 @@ -// -// Created by koncord on 07.01.17. -// - -#ifndef NEWLAUNCHER_NETCONTROLLER_HPP -#define NEWLAUNCHER_NETCONTROLLER_HPP - - -#include "ServerModel.hpp" -#include "netutils/HTTPNetwork.hpp" -#include "netutils/Utils.hpp" - -struct ServerModel; - -class NetController -{ -public: - static NetController *get(); - static void Create(std::string addr, unsigned short port); - static void Destroy(); - bool updateInfo(QAbstractItemModel *pModel, QModelIndex index= QModelIndex()); - void updateInfo(); - QStringList players(); - QStringList plugins(); - void selectServer(ServerData *pServerData); - ServerData *selectedServer(); -protected: - NetController(std::string addr, unsigned short port); - ~NetController(); -private: - NetController(const NetController &controller); - bool downloadInfo(QAbstractItemModel *pModel, QModelIndex index); - void setData(QString addr, QJsonObject server, ServerModel *model); - - static NetController *mThis; - ServerData *sd; - HTTPNetwork httpNetwork; - ServerExtendedData sed; -}; - - -#endif //NEWLAUNCHER_NETCONTROLLER_HPP diff --git a/apps/browser/ServerInfoDialog.cpp b/apps/browser/ServerInfoDialog.cpp index 5ed4b2a31..eb0170c41 100644 --- a/apps/browser/ServerInfoDialog.cpp +++ b/apps/browser/ServerInfoDialog.cpp @@ -2,10 +2,14 @@ // Created by koncord on 07.01.17. // +#include #include "qdebug.h" -#include "NetController.hpp" #include "ServerInfoDialog.hpp" +#include + +using namespace std; +using namespace RakNet; ServerInfoDialog::ServerInfoDialog(QWidget *parent): QDialog(parent) { @@ -18,22 +22,35 @@ ServerInfoDialog::~ServerInfoDialog() } +void ServerInfoDialog::Server(QString addr) +{ + this->addr = addr; +} + void ServerInfoDialog::refresh() { - NetController::get()->updateInfo(); - ServerData *sd = NetController::get()->selectedServer(); - if (sd) + QStringList list = addr.split(':'); + auto sd = QueryClient::Get().Update(SystemAddress(list[0].toLatin1(), list[1].toUShort())); + if (sd.first != UNASSIGNED_SYSTEM_ADDRESS) { - leAddr->setText(sd->addr); - lblName->setText(sd->hostName); - lblPing->setNum(sd->ping); + leAddr->setText(sd.first.ToString(true, ':')); + lblName->setText(sd.second.GetName()); + lblPing->setNum((int) PingRakNetServer(sd.first.ToString(false), sd.first.GetPort())); listPlayers->clear(); - QStringList players = NetController::get()->players(); - listPlayers->addItems(players); + + for(auto player : sd.second.players) + { + listPlayers->addItem(QString::fromStdString(player)); + }; + listPlugins->clear(); - listPlugins->addItems(NetController::get()->plugins()); + for(auto plugin : sd.second.plugins) + { + listPlugins->addItem(QString::fromStdString(plugin.name)); + } + - lblPlayers->setText(QString::number(players.size()) + " / " + QString::number(sd->maxPlayers)); + lblPlayers->setText(QString::number(sd.second.players.size()) + " / " + QString::number(sd.second.GetMaxPlayers())); } } diff --git a/apps/browser/ServerInfoDialog.hpp b/apps/browser/ServerInfoDialog.hpp index b26b34df5..d67c369fb 100644 --- a/apps/browser/ServerInfoDialog.hpp +++ b/apps/browser/ServerInfoDialog.hpp @@ -13,8 +13,11 @@ class ServerInfoDialog : public QDialog, public Ui::Dialog public: explicit ServerInfoDialog(QWidget *parent = 0); virtual ~ServerInfoDialog(); + void Server(QString addr); public slots: void refresh(); +private: + QString addr; }; diff --git a/apps/browser/ServerModel.cpp b/apps/browser/ServerModel.cpp index 1aaa9e88e..a0c2ad61b 100644 --- a/apps/browser/ServerModel.cpp +++ b/apps/browser/ServerModel.cpp @@ -34,28 +34,28 @@ QVariant ServerModel::data(const QModelIndex &index, int role) const var = sd.addr; break; case ServerData::PASSW: - var = sd.needPassw ? "Yes" : "No"; + var = (int)(sd.rules.at("passw").val) == 1 ? "Yes" : "No"; break; case ServerData::VERSION: - var = sd.version; + var = QString(sd.rules.at("version").str.c_str()); break; case ServerData::PLAYERS: - var = sd.players; + var = (int) sd.rules.at("players").val; break; case ServerData::MAX_PLAYERS: - var = sd.maxPlayers; + var = (int) sd.rules.at("maxPlayers").val; break; case ServerData::HOSTNAME: - var = sd.hostName; + var = QString(sd.rules.at("name").str.c_str()); break; case ServerData::PING: var = sd.ping; break; case ServerData::MODNAME: - if(sd.modName.isEmpty()) + if(sd.rules.at("gamemode").str == "") var = "default"; else - var = sd.modName; + var = QString(sd.rules.at("gamemode").str.c_str()); break; } return var; @@ -133,27 +133,27 @@ bool ServerModel::setData(const QModelIndex &index, const QVariant &value, int r ok = !sd.addr.isEmpty(); break; case ServerData::PASSW: - sd.needPassw = value.toBool(); + sd.SetPassword(value.toBool()); break; case ServerData::VERSION: - sd.version = value.toString(); + sd.SetVersion(value.toString().toLatin1()); ok = !sd.addr.isEmpty(); break; case ServerData::PLAYERS: - sd.players = value.toInt(&ok); + sd.SetPlayers(value.toInt(&ok)); break; case ServerData::MAX_PLAYERS: - sd.maxPlayers = value.toInt(&ok); + sd.SetMaxPlayers(value.toInt(&ok)); break; case ServerData::HOSTNAME: - sd.hostName = value.toString(); + sd.SetName(value.toString().toLatin1()); ok = !sd.addr.isEmpty(); break; case ServerData::PING: sd.ping = value.toInt(&ok); break; case ServerData::MODNAME: - sd.modName = value.toString(); + sd.SetGameMode(value.toString().toLatin1()); break; default: return false; @@ -171,8 +171,7 @@ bool ServerModel::insertRows(int position, int count, const QModelIndex &index) beginInsertRows(QModelIndex(), position, position + count - 1); for (int row = 0; row < count; ++row) { - ServerData sd {"", -1, -1, -1, "", "", false, 0}; - myData.insert(position, sd); + myData.insert(position, {}); } endInsertRows(); @@ -195,6 +194,5 @@ QModelIndex ServerModel::index(int row, int column, const QModelIndex &parent) c { QModelIndex index = QAbstractTableModel::index(row, column, parent); - //qDebug() << "Valid index? " << index.isValid() << " " << row << " " << column; return index; } diff --git a/apps/browser/ServerModel.hpp b/apps/browser/ServerModel.hpp index 78f16501b..0e660633f 100644 --- a/apps/browser/ServerModel.hpp +++ b/apps/browser/ServerModel.hpp @@ -5,16 +5,12 @@ #include #include #include +#include -struct ServerData +struct ServerData : public QueryData { QString addr; - int players, maxPlayers; int ping; - QString hostName; - QString modName; - bool needPassw; - QString version; enum IDS { ADDR, diff --git a/apps/browser/main.cpp b/apps/browser/main.cpp index ad54962c1..def1764b5 100644 --- a/apps/browser/main.cpp +++ b/apps/browser/main.cpp @@ -1,8 +1,8 @@ #include #include #include +#include #include "MainWindow.hpp" -#include "NetController.hpp" std::string loadSettings (Settings::Manager & settings) { @@ -39,8 +39,7 @@ int main(int argc, char *argv[]) // initialize resources, if needed // Q_INIT_RESOURCE(resfile); - NetController::Create(addr, port); - atexit(NetController::Destroy); + QueryClient::Get().SetServer(addr, port); QApplication app(argc, argv); MainWindow d;