Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

alpha version for locator system

  • Loading branch information...
KoBeWi committed Oct 21, 2016
1 parent 0a6a2c0 commit af359f59843f709bad711582d75f8f71c31dcf3f
@@ -96,6 +96,8 @@
<file>res/panelbg.png</file>
<file>res/lightbulb_on.png</file>
<file>res/lightbulb_off.png</file>
<file>res/download.png</file>
<file>res/btnOverlay@2x.png</file>
<file>res/spin_up.png</file>
<file>res/spin_down.png</file>
<file>res/PlaySound.png</file>
@@ -1256,7 +1256,8 @@ void HWForm::_NetConnect(const QString & hostName, quint16 port, QString nick)
ui.pageNetGame->chatWidget, SLOT(adminAccess(bool)), Qt::QueuedConnection);
connect(ui.pageNetGame->chatWidget, SIGNAL(chatLine(const QString&)),
hwnet, SLOT(chatLineToNetWithEcho(const QString&)));
connect(ui.pageNetGame->BtnGo, SIGNAL(clicked()), hwnet, SLOT(ToggleReady()));
connect(ui.pageNetGame->BtnGo, SIGNAL(clicked()), ui.pageNetGame, SLOT(btnGoClicked()));
connect(ui.pageNetGame, SIGNAL(toggleReady()), hwnet, SLOT(ToggleReady()));
connect(hwnet, SIGNAL(setMyReadyStatus(bool)),
ui.pageNetGame, SLOT(setReadyStatus(bool)), Qt::QueuedConnection);

@@ -1366,6 +1367,16 @@ void HWForm::_NetConnect(const QString & hostName, quint16 port, QString nick)
ui.pageNetGame->setUser(nickname);

hwnet->Connect(hostName, port, nickname);

// locators
connect(ui.pageNetGame->pGameCFG->pMapContainer, SIGNAL(locatorRequest(const QString &)), hwnet, SLOT(locatorRequest(const QString &)));
connect(ui.pageNetGame->pGameCFG->pMapContainer, SIGNAL(locatorRequest(const QString &)), ui.pageNetGame, SLOT(resourceMissing(const QString &)));
connect(ui.pageNetGame->pGameCFG->pMapContainer, SIGNAL(locatorReply(const QString &, const QString &, const QString &)), hwnet, SLOT(locatorReply(const QString &, const QString &, const QString &)));
connect(ui.pageNetGame->pGameCFG->pMapContainer, SIGNAL(resourceUpdate(const QString &)), ui.pageNetGame, SLOT(resourceUpdate(const QString &)));

//CHATHACK
connect(ui.pageNetGame->chatWidget, SIGNAL(hackMessage1(const QString &, const QString &)), ui.pageNetGame->pGameCFG->pMapContainer, SLOT(handleLocatorRequest(const QString &, const QString &)));
connect(ui.pageNetGame->chatWidget, SIGNAL(hackMessage2(const QString &, const QString &, const QString &)), ui.pageNetGame, SLOT(loadLocator(const QString &, const QString &, const QString &)));
}

int HWForm::AskForNickAndPwd(void)
@@ -231,3 +231,7 @@ QStandardItem * MapModel::infoToItem(

return item;
}

void MapModel::reset() {
m_loaded = false;
}
@@ -105,6 +105,7 @@ class MapModel : public QStandardItemModel

/// Loads the maps
bool loadMaps();
void reset();

/// returns this model but excluding DLC themes
QSortFilterProxyModel * withoutDLC();
@@ -30,9 +30,9 @@ ThemeModel::ThemeModel(QObject *parent) :
{
m_data = QList<QMap<int, QVariant> >();

m_themesLoaded = false;

m_filteredNoDLC = NULL;

reset();
}

QSortFilterProxyModel * ThemeModel::withoutDLC()
@@ -81,12 +81,6 @@ void ThemeModel::loadThemes() const

m_themesLoaded = true;


DataManager & datamgr = DataManager::instance();

QStringList themes =
datamgr.entryList("Themes", QDir::AllDirs | QDir::NoDotAndDotDot);

m_data.clear();

#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
@@ -124,3 +118,10 @@ void ThemeModel::loadThemes() const
m_data.append(dataset);
}
}

void ThemeModel::reset() {
m_themesLoaded = false;

DataManager & datamgr = DataManager::instance();
themes = datamgr.entryList("Themes", QDir::AllDirs | QDir::NoDotAndDotDot);
}
@@ -40,12 +40,14 @@ class ThemeModel : public QAbstractListModel
Q_OBJECT

public:
void reset();
enum Roles { ActualNameRole = Qt::UserRole, IsDlcRole, IconPathRole };
explicit ThemeModel(QObject *parent = 0);

int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
QSortFilterProxyModel * withoutDLC();
QStringList themes;

private:
mutable QList<QMap<int, QVariant> > m_data;
@@ -1172,3 +1172,15 @@ void HWNewNet::maybeSendPassword()

RawSendNet(QString("PASSWORD%1%2%1%3").arg(delimiter).arg(hash).arg(m_clientSalt));
}

void HWNewNet::locatorRequest(const QString &type)
{
// RawSendNet(QString("LOCATOR_REQUEST%1%2").arg(delimiter).arg(type)); //CHATHACK: should be something like this, but there's no handler for server (also needs to pass nick somehow)
RawSendNet(QString("CHAT%1hackyMessage,,LOCATOR_REQUEST,,%2").arg(delimiter).arg(type));
}

void HWNewNet::locatorReply(const QString &where, const QString &type, const QString &location)
{
// RawSendNet(QString("LOCATOR%1%2").arg(delimiter).arg(type)); //CHATHACK: as above
RawSendNet(QString("CHAT%1hackyMessage,,LOCATOR,,%2,,%3,,%4").arg(delimiter).arg(where).arg(type).arg(location));
}
@@ -141,6 +141,8 @@ class HWNewNet : public QObject
void setMyReadyStatus(bool isReady);

void messageProcessed();
void handleLocatorRequest(const QString &);
void fetchLocator(const QString &);

public slots:
void ToggleReady();
@@ -182,6 +184,9 @@ class HWNewNet : public QObject
void banIP(const QString & ip, const QString & reason, int seconds);
void banNick(const QString & nick, const QString & reason, int seconds);
void roomPasswordEntered(const QString & password);

void locatorRequest(const QString &);
void locatorReply(const QString &, const QString &, const QString &);

private slots:
void ClientRead();
@@ -23,11 +23,20 @@
#include <QMenu>
#include <QMessageBox>
#include <QSettings>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QProgressBar>
#include <QTime>
#include <QDate>
#include <QProgressBar>

#include "pagenetgame.h"
#include "gamecfgwidget.h"
#include "teamselect.h"
#include "chatwidget.h"
#include "ThemeModel.h"
#include "FileEngine.h"

const int cutoffHeight = 688; /* Don't make this number below 605, or else it'll
let the GameCFGWidget shrink too much before switching to tabbed mode. */
@@ -124,6 +133,12 @@ QLayout * PageNetGame::footerLayoutLeftDefinition()
QLayout * PageNetGame::footerLayoutDefinition()
{
QHBoxLayout * bottomLayout = new QHBoxLayout;

// Transfer Bars

progressBarsLayout = new QVBoxLayout();
bottomLayout->addLayout(progressBarsLayout);
bottomLayout->addStretch();

// Ready button

@@ -287,3 +302,146 @@ void PageNetGame::setSettings(QSettings *settings)
{
m_gameSettings = settings;
}

void PageNetGame::btnGoClicked() {
if (resourcesMissing.isEmpty())
emit toggleReady();
else {
QHashIterator<QString, QString> i(resourcesMissing);
while (i.hasNext()) {
i.next();
if (i.value() != QString("")) {
fetchLocator(i.key(), i.value());
resourcesMissing.insert(i.key(), "");
}
}
}
}

void PageNetGame::resourceMissing(const QString & type) {
BtnGo->setIcon(QIcon(":/res/btnOverlay@2x.png"));
resourcesMissing.insert(type, "");

if (amReady)
emit toggleReady();
}

void PageNetGame::loadLocator(const QString & nick, const QString & type, const QString & location) {
if (location == QString("?") || nick != chatWidget->getUser())
return;
BtnGo->setIcon(QIcon(":/res/download.png"));
resourcesMissing.insert(type, location);
}

void PageNetGame::fetchLocator(const QString & type, const QString & location)
{
QString finalLocation(location);

if (finalLocation.contains("dropbox") && !finalLocation.endsWith("?dl=1") && !finalLocation.endsWith("?raw=1")) {
if (finalLocation.endsWith("?dl=0"))
finalLocation = finalLocation.left(finalLocation.length() - 5);
finalLocation.append("?dl=1");
}

qWarning() << "Download Request" << finalLocation;

QUrl url(finalLocation);
QNetworkRequest newRequest(url);

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(locatorDone(QNetworkReply*)));

QNetworkReply *reply = manager->get(newRequest);
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64)));

QProgressBar *progressBar = new QProgressBar(this);
progressBarsLayout->addWidget(progressBar);
progressBars.insert(reply, progressBar);
resourceLocators.insert(reply, type);
}

void PageNetGame::locatorDone(QNetworkReply* reply) {
QProgressBar *progressBar = progressBars.value(reply, 0);

if(progressBar)
{
progressBars.remove(reply);
progressBar->deleteLater();
resourcesMissing.remove(resourceLocators.value(reply));
resourceLocators.remove(reply);
}

if (!reply->attribute(QNetworkRequest::RedirectionTargetAttribute).isNull()) {
QUrl redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
qWarning() << "Request Redirect" << redirect;

QNetworkRequest newRequest = QNetworkRequest(redirect);
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(locatorDone(QNetworkReply*)));

QNetworkReply *reply = manager->get(newRequest);
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64)));

QProgressBar *progressBar = new QProgressBar(this);
progressBarsLayout->addWidget(progressBar);
progressBars.insert(reply, progressBar);
return;
}

QDir extractDir(*cfgdir);
extractDir.cd("Data");

QString fileName = extractDir.filePath(QFileInfo(reply->request().url().path()).fileName());
if (fileName.endsWith(".zip"))
fileName = fileName.left(fileName.length() - 4) + ".hwp";
else if (!fileName.endsWith(".hwp")) { //file is without extension, so assuming a non-unique name
QDate timestamp = QDate::currentDate();
QTime timestamp2 = QTime::currentTime();
fileName = extractDir.filePath(QString("CachedData") + QString::number(timestamp.day()) + QString::number(timestamp.month()) + QString::number(timestamp.year()) + QString::number(timestamp2.hour()) + QString::number(timestamp2.minute()) + QString::number(timestamp2.second()) + QString(".hwp"));
}

QFile out(fileName);
if(!out.open(QFile::WriteOnly))
{
qWarning() << "out.open():" << out.errorString();
return ;
}

QByteArray data(reply->readAll());
out.write(data);

out.close();

FileEngineHandler::mount(fileName);

DataManager::instance().themeModel()->reset(); //should probably check if theme was just download, not map etc.
//also, should update gamecfg widget if there are icons missing or such
DataManager::instance().staticMapModel()->reset();
DataManager::instance().missionMapModel()->reset();

if (resourcesMissing.isEmpty())
BtnGo->setIcon(QIcon(":/res/lightbulb_off.png"));
}

void PageNetGame::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal)
{
QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());

if(reply)
{
QProgressBar *progressBar = progressBars.value(reply, 0);

if(progressBar)
{
progressBar->setValue(bytesRecieved);
progressBar->setMaximum(bytesTotal);
}
}
}

void PageNetGame::resourceUpdate(const QString & type) {
resourcesMissing.remove(type);

if (resourcesMissing.isEmpty())
BtnGo->setIcon(QIcon(":/res/lightbulb_off.png"));
}
@@ -19,6 +19,8 @@
#ifndef PAGE_NETGAME_H
#define PAGE_NETGAME_H

#include <QNetworkReply>
#include <QProgressBar>
#include "HistoryLineEdit.h"

#include "AbstractPage.h"
@@ -62,13 +64,21 @@ class PageNetGame : public AbstractPage
void setUser(const QString & nickname);
void onUpdateClick();
void setMasterMode(bool isMaster);

void btnGoClicked();
void resourceUpdate(const QString &);
void resourceMissing(const QString &);
void loadLocator(const QString &, const QString &, const QString &);
void locatorDone(QNetworkReply*);
void downloadProgress(qint64, qint64);

private slots:
void onRoomNameEdited();

signals:
void SetupClicked();
void askForUpdateRoomName(const QString &);
void toggleReady();

protected:
void resizeEvent(QResizeEvent * event);
@@ -82,6 +92,13 @@ class PageNetGame : public AbstractPage
QSettings * m_gameSettings;
QPushButton * btnSetup;
QLabel * lblRoomNameReadOnly;

void fetchLocator(const QString &, const QString &);
bool amReady;
QVBoxLayout *progressBarsLayout;
QHash<QNetworkReply*, QProgressBar *> progressBars;
QHash<QNetworkReply*, QString> resourceLocators;
QHash<QString, QString> resourcesMissing;
};

#endif
@@ -412,6 +412,15 @@ void HWChatWidget::onChatAction(const QString & nick, const QString & action)

void HWChatWidget::onChatMessage(const QString & nick, const QString & message)
{
if (message.startsWith(QString("hackyMessage,,"))) { //CHATHACK: chat hack should be replaced with a proper server commands (note that on chat we can get nick for free)
QStringList command = message.split(",,");

if (command.at(1) == QString("LOCATOR_REQUEST"))
emit hackMessage1(nick, command.at(2));
else if (command.at(1) == QString("LOCATOR"))
emit hackMessage2(command.at(2), command.at(3), command.at(4));
return;
}
printChatString(nick, linkedNick(nick) + ": " + messageToHTML(message), "Chat", containsHighlight(nick, message));
}

@@ -987,3 +996,8 @@ void HWChatWidget::showEvent(QShowEvent * event)

afterContentAdd();
}

QString HWChatWidget::getUser()
{
return m_userNick;
}

0 comments on commit af359f5

Please sign in to comment.
You can’t perform that action at this time.