From 1bd4db5e8bfe40e58a2fbacfebfb676a81be6ade Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 22 Jan 2017 17:11:13 +0800 Subject: [PATCH] [Browser] Implement update info by index --- apps/browser/NetController.cpp | 91 ++++++++++++++++++++++------------ apps/browser/NetController.hpp | 1 + 2 files changed, 61 insertions(+), 31 deletions(-) diff --git a/apps/browser/NetController.cpp b/apps/browser/NetController.cpp index 61d19458a..a0f6ee60e 100644 --- a/apps/browser/NetController.cpp +++ b/apps/browser/NetController.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -60,6 +61,29 @@ struct pattern 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::PING); + + 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); @@ -69,6 +93,34 @@ bool NetController::downloadInfo(QAbstractItemModel *pModel, QModelIndex index) */ QString data; + QJsonParseError err; + + if(index.isValid() && index.row() >= 0) + { + 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" or 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")); @@ -85,42 +137,24 @@ bool NetController::downloadInfo(QAbstractItemModel *pModel, QModelIndex index) qDebug() << "Content: " << data; - QJsonParseError err; QJsonDocument jsonDocument = QJsonDocument::fromJson(data.toLatin1(), &err); - QMap map = jsonDocument.toVariant().toMap()["list servers"].toMap(); + QJsonObject listServers = jsonDocument.object()["list servers"].toObject(); - for(QMap::Iterator iter = map.begin(); iter != map.end(); iter++) + for(auto iter = listServers.begin(); iter != listServers.end(); iter++) { + QJsonObject server = iter->toObject(); qDebug() << iter.key(); - qDebug() << iter.value().toMap()["hostname"].toString(); - qDebug() << iter.value().toMap()["modname"].toString(); - qDebug() << iter.value().toMap()["players"].toInt(); - qDebug() << iter.value().toMap()["max_players"].toInt(); + qDebug() << server["hostname"].toString(); + qDebug() << server["modname"].toString(); + qDebug() << server["players"].toInt(); + qDebug() << server["max_players"].toInt(); QVector::Iterator value = std::find_if(model->myData.begin(), model->myData.end(), pattern(iter.key())); if(value == model->myData.end()) model->insertRow(0); - QModelIndex mi = model->index(0, ServerData::ADDR); - model->setData(mi, iter.key()); - - mi = model->index(0, ServerData::PLAYERS); - model->setData(mi, iter.value().toMap()["players"].toInt()); - - mi = model->index(0, ServerData::MAX_PLAYERS); - model->setData(mi, iter.value().toMap()["max_players"].toInt()); - - mi = model->index(0, ServerData::HOSTNAME); - model->setData(mi, iter.value().toMap()["hostname"].toString()); - - mi = model->index(0, ServerData::MODNAME); - model->setData(mi, iter.value().toMap()["modname"].toString()); - - mi = model->index(0, ServerData::PING); - - QStringList addr = iter.key().split(":"); - model->setData(mi, PingRakNetServer(addr[0].toLatin1().data(), addr[1].toUShort())); + setData(iter.key(), server, model); } return true; @@ -128,16 +162,11 @@ bool NetController::downloadInfo(QAbstractItemModel *pModel, QModelIndex index) bool NetController::updateInfo(QAbstractItemModel *pModel, QModelIndex index) { - qDebug() << "TODO: \"NetController::updateInfo(QAbstractItemModel *, QModelIndex)\" is not completed"; ServerModel *model = ((ServerModel*)pModel); bool result; if (index.isValid() && index.row() >= 0) - { - //ServerData &sd = model->myData[index.row()]; - //qDebug() << sd.addr; result = downloadInfo(pModel, index); - } else { for (QVector::Iterator iter = model->myData.begin(); iter != model->myData.end(); iter++) diff --git a/apps/browser/NetController.hpp b/apps/browser/NetController.hpp index 40d45fbb0..2dd508068 100644 --- a/apps/browser/NetController.hpp +++ b/apps/browser/NetController.hpp @@ -30,6 +30,7 @@ protected: private: NetController(const NetController &controller); bool downloadInfo(QAbstractItemModel *pModel, QModelIndex index); + void setData(QString addr, QJsonObject server, ServerModel *model); static NetController *mThis; ServerData *sd;