2023-08-05 22:14:05 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2021-11-01 09:54:58 -04:00
|
|
|
#include "sapphirelauncher.h"
|
|
|
|
|
|
|
|
#include <QJsonDocument>
|
2022-08-15 11:14:37 -04:00
|
|
|
#include <QJsonObject>
|
2021-11-01 09:54:58 -04:00
|
|
|
#include <QNetworkReply>
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
SapphireLauncher::SapphireLauncher(LauncherCore &window, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, window(window)
|
|
|
|
{
|
|
|
|
}
|
2021-11-01 09:54:58 -04:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
void SapphireLauncher::login(const QString &lobbyUrl, const LoginInformation &info)
|
|
|
|
{
|
2022-08-15 11:14:37 -04:00
|
|
|
QJsonObject data{{"username", info.username}, {"pass", info.password}};
|
2021-11-01 09:54:58 -04:00
|
|
|
|
2023-01-15 16:39:36 +00:00
|
|
|
QUrl url(lobbyUrl + "/sapphire-api/lobby/login");
|
2021-11-01 09:54:58 -04:00
|
|
|
|
|
|
|
QNetworkRequest request(url);
|
2022-08-15 11:14:37 -04:00
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
2021-11-01 09:54:58 -04:00
|
|
|
|
|
|
|
auto reply = window.mgr->post(request, QJsonDocument(data).toJson(QJsonDocument::JsonFormat::Compact));
|
2023-08-18 22:05:38 -04:00
|
|
|
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()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-01 09:54:58 -04:00
|
|
|
QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
|
2022-08-15 11:14:37 -04:00
|
|
|
if (!document.isEmpty()) {
|
2021-11-01 09:54:58 -04:00
|
|
|
LoginAuth auth;
|
|
|
|
auth.SID = document["sId"].toString();
|
|
|
|
auth.lobbyhost = document["lobbyHost"].toString();
|
|
|
|
auth.frontierHost = document["frontierHost"].toString();
|
|
|
|
auth.region = 3;
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
window.launchGame(*info.profile, auth);
|
2021-11-01 09:54:58 -04:00
|
|
|
} else {
|
2023-08-18 22:05:38 -04:00
|
|
|
Q_EMIT window.loginError("Invalid username or password.");
|
2021-11-01 09:54:58 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
void SapphireLauncher::registerAccount(const QString &lobbyUrl, const LoginInformation &info)
|
|
|
|
{
|
2022-08-15 11:14:37 -04:00
|
|
|
QJsonObject data{{"username", info.username}, {"pass", info.password}};
|
2023-01-15 16:39:36 +00:00
|
|
|
QUrl url(lobbyUrl + "/sapphire-api/lobby/createAccount");
|
2021-11-01 09:54:58 -04:00
|
|
|
|
|
|
|
QNetworkRequest request(url);
|
2022-08-15 11:14:37 -04:00
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
2021-11-01 09:54:58 -04:00
|
|
|
|
|
|
|
auto reply = window.mgr->post(request, QJsonDocument(data).toJson(QJsonDocument::JsonFormat::Compact));
|
2022-06-08 13:55:15 -04:00
|
|
|
connect(reply, &QNetworkReply::finished, [&] {
|
2021-11-01 09:54:58 -04:00
|
|
|
QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
|
|
|
|
|
|
|
|
LoginAuth auth;
|
|
|
|
auth.SID = document["sId"].toString();
|
|
|
|
auth.lobbyhost = document["lobbyHost"].toString();
|
|
|
|
auth.frontierHost = document["frontierHost"].toString();
|
|
|
|
auth.region = 3;
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
window.launchGame(*info.profile, auth);
|
2021-11-01 09:54:58 -04:00
|
|
|
});
|
2023-01-15 16:39:36 +00:00
|
|
|
}
|