-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include "network_manager.hpp" | ||
|
||
FileDownloader::FileDownloader(const QUrl &url, QObject *parent) : QObject(parent), m_WebCtrl(this) { | ||
connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)), this, SLOT(fileDownloaded(QNetworkReply*))); | ||
|
||
const QNetworkRequest request(url); | ||
m_WebCtrl.get(request); | ||
} | ||
|
||
FileDownloader::~FileDownloader() { | ||
} | ||
|
||
void FileDownloader::fileDownloaded(QNetworkReply *pReply) { | ||
if (pReply->error() != QNetworkReply::NoError) { | ||
qDebug() << "Network error: " << pReply->errorString(); | ||
} else { | ||
m_DownloadedData = pReply->readAll(); | ||
emit downloaded(); | ||
} | ||
pReply->deleteLater(); | ||
} | ||
|
||
QByteArray FileDownloader::downloadedData() const { | ||
return m_DownloadedData; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef NETWORK_MANAGER_H | ||
#define NETWORK_MANAGER_H | ||
|
||
#include <QObject> | ||
#include <QByteArray> | ||
#include <QNetworkAccessManager> | ||
#include <QNetworkRequest> | ||
#include <QNetworkReply> | ||
|
||
class FileDownloader final : public QObject { | ||
Q_OBJECT | ||
|
||
public: | ||
explicit FileDownloader(const QUrl &url, QObject *parent = nullptr); | ||
|
||
~FileDownloader() override; | ||
|
||
QByteArray downloadedData() const; | ||
|
||
signals: | ||
void downloaded(); | ||
|
||
private slots: | ||
void fileDownloaded(QNetworkReply *pReply); | ||
|
||
private: | ||
QNetworkAccessManager m_WebCtrl; | ||
QByteArray m_DownloadedData; | ||
}; | ||
|
||
#endif // NETWORK_MANAGER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include "browsemapscreen.hpp" | ||
|
||
|
||
/** | ||
* Construct map browser screen. | ||
*/ | ||
BrowseMapScreen::BrowseMapScreen(QWidget *parent): QWidget(parent) { | ||
const auto layout = new QVBoxLayout(this); | ||
|
||
const auto backButton = new QPushButton("Back", this); | ||
mapList = new QListWidget(this); | ||
loadButton = new QPushButton("Play", this); | ||
|
||
layout->addWidget(backButton); | ||
layout->addWidget(mapList); | ||
layout->addWidget(loadButton); | ||
|
||
connect(backButton, &QPushButton::clicked, this, &BrowseMapScreen::backClicked); | ||
connect(loadButton, &QPushButton::clicked, this, &BrowseMapScreen::loadClicked); | ||
connect(mapList, &QListWidget::itemSelectionChanged, this, &BrowseMapScreen::mapSelectionChanged); | ||
|
||
// Load maps | ||
QDirIterator it("maps", QDir::Files); | ||
QPixmap transparentPixmap(24, 24); | ||
transparentPixmap.fill(Qt::transparent); | ||
const QIcon spacerIcon(transparentPixmap); | ||
while (it.hasNext()) { | ||
it.next(); | ||
const auto item = new QListWidgetItem(it.fileName()); | ||
item->setIcon(spacerIcon); | ||
item->setData(Qt::UserRole, true); | ||
mapList->addItem(item); | ||
} | ||
|
||
loadOnlineMaps(); | ||
} | ||
|
||
void BrowseMapScreen::loadOnlineMaps() { | ||
downloader = new FileDownloader(QUrl("https://snakeqt.denisd3d.fr/maps"), this); | ||
connect(downloader, &FileDownloader::downloaded, this, &BrowseMapScreen::onlineMapListDownloaded); | ||
} | ||
|
||
void BrowseMapScreen::backClicked() { | ||
emit back(); | ||
} | ||
|
||
void BrowseMapScreen::loadClicked() { | ||
if (mapList->currentItem() != nullptr) { | ||
if (!mapList->currentItem()->data(Qt::UserRole).toBool()) { | ||
auto mapName = mapList->currentItem()->text().replace(".skm", ""); | ||
downloader = new FileDownloader(QUrl("https://snakeqt.denisd3d.fr/maps/" + mapName), | ||
this); | ||
connect(downloader, &FileDownloader::downloaded, [this, mapName]() { | ||
QFile file("maps/" + mapName + ".skm"); | ||
file.open(QIODevice::WriteOnly); | ||
file.write(downloader->downloadedData()); | ||
file.close(); | ||
emit load("maps/" + mapName + ".skm"); | ||
}); | ||
} else { | ||
emit load("maps/" + mapList->currentItem()->text()); | ||
} | ||
} | ||
} | ||
|
||
void BrowseMapScreen::mapSelectionChanged() { | ||
if (mapList->currentItem() != nullptr) { | ||
loadButton->setText(mapList->currentItem()->data(Qt::UserRole).toBool() ? "Play" : "Download and Play"); | ||
} | ||
} | ||
|
||
void BrowseMapScreen::onlineMapListDownloaded() { | ||
const QByteArray data = downloader->downloadedData(); | ||
const QJsonDocument doc = QJsonDocument::fromJson(data); | ||
const QIcon cloudIcon(":/images/cloud.png"); | ||
QJsonArray array = doc.array(); | ||
for (const auto &value: array) { | ||
QString mapName = value.toObject().value("id").toString(); | ||
bool found = false; | ||
for (int i = 0; i < mapList->count(); i++) { | ||
if (mapList->item(i)->text() == mapName) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!found) { | ||
const auto item = new QListWidgetItem(mapName); | ||
item->setData(Qt::UserRole, false); | ||
item->setIcon(cloudIcon); | ||
mapList->addItem(item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#ifndef BROWSEMAPSCREEN_HPP | ||
#define BROWSEMAPSCREEN_HPP | ||
|
||
#include <QtCore/QtCore> | ||
#include <QtWidgets/QtWidgets> | ||
|
||
#include "../network_manager.hpp" | ||
|
||
class BrowseMapScreen final : public QWidget { | ||
Q_OBJECT | ||
QListWidget *mapList; | ||
QPushButton *loadButton; | ||
FileDownloader *downloader; | ||
|
||
void loadOnlineMaps(); | ||
|
||
void updateMapList(); | ||
|
||
public: | ||
explicit BrowseMapScreen(QWidget *parent = nullptr); | ||
|
||
public slots: | ||
void backClicked(); | ||
|
||
void loadClicked(); | ||
|
||
void onlineMapListDownloaded(); | ||
|
||
void mapSelectionChanged(); | ||
|
||
signals: | ||
void back(); | ||
|
||
void load(const QString &mapName); | ||
}; | ||
|
||
#endif //BROWSEMAPSCREEN_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters