diff --git a/apps/browser/CMakeLists.txt b/apps/browser/CMakeLists.txt index dc8b2b9ed..95f09bfec 100644 --- a/apps/browser/CMakeLists.txt +++ b/apps/browser/CMakeLists.txt @@ -11,6 +11,7 @@ set(BROWSER ServerModel.cpp NetController.cpp ServerInfoDialog.cpp + MySortFilterProxyModel.cpp netutils/HTTPNetwork.cpp netutils/Utils.cpp ${CMAKE_SOURCE_DIR}/files/tes3mp/browser.rc @@ -20,6 +21,7 @@ set(BROWSER_HEADER_MOC MainWindow.hpp ServerModel.hpp ServerInfoDialog.hpp + MySortFilterProxyModel.hpp ) set(BROWSER_HEADER diff --git a/apps/browser/MainWindow.cpp b/apps/browser/MainWindow.cpp index c6b5a7564..3d5abe7d4 100644 --- a/apps/browser/MainWindow.cpp +++ b/apps/browser/MainWindow.cpp @@ -23,7 +23,7 @@ MainWindow::MainWindow(QWidget *parent) browser = new ServerModel; favorites = new ServerModel; - proxyModel = new QSortFilterProxyModel; + proxyModel = new MySortFilterProxyModel(this); proxyModel->setSourceModel(browser); tblServerBrowser->setModel(proxyModel); tblFavorites->setModel(proxyModel); @@ -39,7 +39,12 @@ MainWindow::MainWindow(QWidget *parent) connect(actionPlay, SIGNAL(triggered(bool)), this, SLOT(play())); connect(tblServerBrowser, SIGNAL(clicked(QModelIndex)), this, SLOT(serverSelected())); connect(tblFavorites, SIGNAL(clicked(QModelIndex)), this, SLOT(serverSelected())); - + connect(tblFavorites, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(play())); + connect(tblServerBrowser, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(play())); + connect(cBoxNotFully, SIGNAL(toggled(bool)), this, SLOT(notFullySwitch(bool))); + connect(cBoxWithPlayers, SIGNAL(toggled(bool)), this, SLOT(havePlayersSwitch(bool))); + connect(comboLatency, SIGNAL(currentIndexChanged(int)), this, SLOT(maxLatencyChanged(int))); + connect(leGamemode, SIGNAL(textChanged(const QString &)), this, SLOT(gamemodeChanged(const QString &))); loadFavorites(); } @@ -184,4 +189,27 @@ void MainWindow::loadFavorites() addServerAndUpdate(server.toString()); file.close(); -} \ No newline at end of file +} + +void MainWindow::notFullySwitch(bool state) +{ + proxyModel->filterFullServer(state); +} + +void MainWindow::havePlayersSwitch(bool state) +{ + proxyModel->filterEmptyServers(state); +} + +void MainWindow::maxLatencyChanged(int index) +{ + int maxLatency = index * 50; + proxyModel->pingLessThan(maxLatency); + +} + +void MainWindow::gamemodeChanged(const QString &text) +{ + proxyModel->setFilterFixedString(text); + proxyModel->setFilterKeyColumn(ServerData::MODNAME); +} diff --git a/apps/browser/MainWindow.hpp b/apps/browser/MainWindow.hpp index 74e882c6d..8f67236f9 100644 --- a/apps/browser/MainWindow.hpp +++ b/apps/browser/MainWindow.hpp @@ -8,7 +8,7 @@ #include "ui_Main.h" #include "ServerModel.hpp" -#include +#include "MySortFilterProxyModel.hpp" #include class MainWindow : public QMainWindow, private Ui::MainWindow @@ -29,10 +29,14 @@ protected slots: void deleteServer(); void play(); void serverSelected(); + void notFullySwitch(bool state); + void havePlayersSwitch(bool state); + void maxLatencyChanged(int index); + void gamemodeChanged(const QString &text); private: Process::ProcessInvoker *mGameInvoker; ServerModel *browser, *favorites; - QSortFilterProxyModel *proxyModel; + MySortFilterProxyModel *proxyModel; void loadFavorites(); }; diff --git a/apps/browser/MySortFilterProxyModel.cpp b/apps/browser/MySortFilterProxyModel.cpp new file mode 100644 index 000000000..3d50174a1 --- /dev/null +++ b/apps/browser/MySortFilterProxyModel.cpp @@ -0,0 +1,52 @@ +// +// Created by koncord on 30.01.17. +// + +#include "MySortFilterProxyModel.hpp" +#include "ServerModel.hpp" + +#include + +bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const +{ + + QModelIndex pingIndex = sourceModel()->index(sourceRow, ServerData::PING, sourceParent); + QModelIndex plIndex = sourceModel()->index(sourceRow, ServerData::PLAYERS, sourceParent); + QModelIndex maxPlIndex = sourceModel()->index(sourceRow, ServerData::MAX_PLAYERS, sourceParent); + + int ping = sourceModel()->data(pingIndex).toInt(); + int players = sourceModel()->data(plIndex).toInt(); + int maxPlayers = sourceModel()->data(maxPlIndex).toInt(); + + if(maxPing > 0 && (ping == -1 || ping > maxPing)) + return false; + if(filterEmpty && players == 0) + return false; + if(filterFull && players >= maxPlayers) + return false; + + return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); +} + +MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent) +{ + +} + +void MySortFilterProxyModel::filterEmptyServers(bool state) +{ + filterEmpty = state; + invalidateFilter(); +} + +void MySortFilterProxyModel::filterFullServer(bool state) +{ + filterFull = state; + invalidateFilter(); +} + +void MySortFilterProxyModel::pingLessThan(int maxPing) +{ + this->maxPing = maxPing; + invalidateFilter(); +} diff --git a/apps/browser/MySortFilterProxyModel.hpp b/apps/browser/MySortFilterProxyModel.hpp new file mode 100644 index 000000000..c53de3a90 --- /dev/null +++ b/apps/browser/MySortFilterProxyModel.hpp @@ -0,0 +1,27 @@ +// +// Created by koncord on 30.01.17. +// + +#ifndef OPENMW_MYSORTFILTERPROXYMODEL_HPP +#define OPENMW_MYSORTFILTERPROXYMODEL_HPP + + +#include + +class MySortFilterProxyModel : public QSortFilterProxyModel +{ + Q_OBJECT +protected: + bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const Q_DECL_FINAL; +public: + MySortFilterProxyModel(QObject *parent); + void filterFullServer(bool state); + void filterEmptyServers(bool state); + void pingLessThan(int maxPing); +private: + bool filterEmpty, filterFull; + int maxPing; +}; + + +#endif //OPENMW_MYSORTFILTERPROXYMODEL_HPP diff --git a/apps/browser/NetController.cpp b/apps/browser/NetController.cpp index 3182c84ef..e07f74f9f 100644 --- a/apps/browser/NetController.cpp +++ b/apps/browser/NetController.cpp @@ -217,7 +217,10 @@ void NetController::updateInfo() QStringList addr = sd->addr.split(":"); sd->ping = PingRakNetServer(addr[0].toLatin1(), addr[1].toUShort()); - sed = getExtendedData(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() diff --git a/apps/browser/main.cpp b/apps/browser/main.cpp index 16f9833c4..ad54962c1 100644 --- a/apps/browser/main.cpp +++ b/apps/browser/main.cpp @@ -43,7 +43,6 @@ int main(int argc, char *argv[]) atexit(NetController::Destroy); QApplication app(argc, argv); MainWindow d; - d.setWindowTitle("tes3mp Server Browser"); if (d.refresh()) { diff --git a/apps/browser/netutils/Utils.cpp b/apps/browser/netutils/Utils.cpp index 60e009cca..a876cc9c3 100644 --- a/apps/browser/netutils/Utils.cpp +++ b/apps/browser/netutils/Utils.cpp @@ -20,7 +20,7 @@ unsigned int PingRakNetServer(const char *addr, unsigned short port) RakNet::Packet *packet; bool done = false; int attempts = 0; - RakNet::TimeMS time = 999; + RakNet::TimeMS time = PING_UNREACHABLE; RakNet::SocketDescriptor socketDescriptor {0, ""}; RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance(); diff --git a/apps/browser/netutils/Utils.hpp b/apps/browser/netutils/Utils.hpp index 99e3e49a6..1be5b788b 100644 --- a/apps/browser/netutils/Utils.hpp +++ b/apps/browser/netutils/Utils.hpp @@ -9,6 +9,8 @@ #include +#define PING_UNREACHABLE 999 + unsigned int PingRakNetServer(const char *addr, unsigned short port); struct ServerExtendedData diff --git a/files/tes3mp/ui/Main.ui b/files/tes3mp/ui/Main.ui index e05dbc51e..14464f306 100644 --- a/files/tes3mp/ui/Main.ui +++ b/files/tes3mp/ui/Main.ui @@ -11,10 +11,10 @@ - MainWindow + tes3mp Server Browser - + @@ -68,6 +68,122 @@ + + + + Filters + + + + + + + + + + Max latency: + + + + + + + + All + + + + + <50 + + + + + <100 + + + + + <150 + + + + + <200 + + + + + <250 + + + + + + + + + + + + Game mode + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + Not fully + + + + + + + With players + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + +