1
Fork 0
mirror of https://github.com/redstrate/Astra.git synced 2025-04-20 19:57:45 +00:00

Modernize SapphireLauncher

This commit is contained in:
Joshua Goins 2023-09-17 08:55:25 -04:00
parent f990b9dd75
commit 28b0fe8537

View file

@ -3,6 +3,7 @@
#include "sapphirelauncher.h"
#include <KLocalizedString>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
@ -15,51 +16,51 @@ SapphireLauncher::SapphireLauncher(LauncherCore &window, QObject *parent)
void SapphireLauncher::login(const QString &lobbyUrl, const LoginInformation &info)
{
QJsonObject data{{"username", info.username}, {"pass", info.password}};
const QJsonObject data{{QStringLiteral("username"), info.username}, {QStringLiteral("pass"), info.password}};
QUrl url(lobbyUrl + "/sapphire-api/lobby/login");
const QUrl url(lobbyUrl + QStringLiteral("/sapphire-api/lobby/login"));
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setHeader(QNetworkRequest::ContentTypeHeader, QByteArrayLiteral("application/x-www-form-urlencoded"));
auto reply = window.mgr->post(request, QJsonDocument(data).toJson(QJsonDocument::JsonFormat::Compact));
const auto reply = window.mgr->post(request, QJsonDocument(data).toJson(QJsonDocument::JsonFormat::Compact));
connect(reply, &QNetworkReply::finished, [this, reply, &info] {
if (reply->error() != QNetworkReply::NetworkError::NoError) {
Q_EMIT window.loginError(QStringLiteral("Could not contact lobby server.\n\n%1").arg(reply->errorString()));
Q_EMIT window.loginError(i18n("Could not contact lobby server.\n\n%1", reply->errorString()));
return;
}
QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
const QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
if (!document.isEmpty()) {
LoginAuth auth;
auth.SID = document["sId"].toString();
auth.lobbyhost = document["lobbyHost"].toString();
auth.frontierHost = document["frontierHost"].toString();
auth.SID = document[QLatin1String("sId")].toString();
auth.lobbyhost = document[QLatin1String("lobbyHost")].toString();
auth.frontierHost = document[QLatin1String("frontierHost")].toString();
auth.region = 3;
window.launchGame(*info.profile, auth);
} else {
Q_EMIT window.loginError("Invalid username or password.");
Q_EMIT window.loginError(i18n("Invalid username or password."));
}
});
}
void SapphireLauncher::registerAccount(const QString &lobbyUrl, const LoginInformation &info)
{
QJsonObject data{{"username", info.username}, {"pass", info.password}};
QUrl url(lobbyUrl + "/sapphire-api/lobby/createAccount");
const QJsonObject data{{QStringLiteral("username"), info.username}, {QStringLiteral("pass"), info.password}};
const QUrl url(lobbyUrl + QStringLiteral("/sapphire-api/lobby/createAccount"));
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setHeader(QNetworkRequest::ContentTypeHeader, QByteArrayLiteral("application/x-www-form-urlencoded"));
auto reply = window.mgr->post(request, QJsonDocument(data).toJson(QJsonDocument::JsonFormat::Compact));
const auto reply = window.mgr->post(request, QJsonDocument(data).toJson(QJsonDocument::JsonFormat::Compact));
connect(reply, &QNetworkReply::finished, [&] {
QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
const QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
LoginAuth auth;
auth.SID = document["sId"].toString();
auth.lobbyhost = document["lobbyHost"].toString();
auth.frontierHost = document["frontierHost"].toString();
auth.SID = document[QLatin1String("sId")].toString();
auth.lobbyhost = document[QLatin1String("lobbyHost")].toString();
auth.frontierHost = document[QLatin1String("frontierHost")].toString();
auth.region = 3;
window.launchGame(*info.profile, auth);