2026-03-31 07:42:56 -04:00
|
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "UI/Portal/PortalManager.h"
|
2026-03-31 08:15:28 -04:00
|
|
|
|
2026-04-02 22:01:25 -04:00
|
|
|
#include "HttpModule.h"
|
2026-04-03 20:34:49 -04:00
|
|
|
#include "JsonObjectConverter.h"
|
2026-04-02 22:01:25 -04:00
|
|
|
#include "Data/API/APIData.h"
|
|
|
|
|
#include "GameplayTags/DedicatedServerTags.h"
|
2026-04-03 20:34:49 -04:00
|
|
|
#include "Interfaces/IHttpResponse.h"
|
|
|
|
|
#include "UI/HTTP/HTTPRequestTypes.h"
|
2026-04-02 22:01:25 -04:00
|
|
|
|
2026-03-31 08:15:28 -04:00
|
|
|
void UPortalManager::JoinGameSession()
|
|
|
|
|
{
|
2026-04-03 20:34:49 -04:00
|
|
|
BroadcastJoinGameSessionMessage.Broadcast(TEXT("Searching for Game Session..."), false);
|
2026-04-02 22:01:25 -04:00
|
|
|
|
|
|
|
|
check(APIData);
|
|
|
|
|
|
|
|
|
|
TSharedPtr<IHttpRequest> Request = FHttpModule::Get().CreateRequest();
|
|
|
|
|
Request->OnProcessRequestComplete().BindUObject(this, &UPortalManager::FindOrCreateGameSession_Response);
|
|
|
|
|
|
|
|
|
|
const FString APIUrl = APIData->GetAPIEndPoint(DedicatedServersTags::GameSessionsAPI::FindOrCreateGameSession);
|
|
|
|
|
|
|
|
|
|
Request->SetURL(APIUrl);
|
|
|
|
|
Request->SetVerb("POST");
|
|
|
|
|
Request->SetHeader("Content-Type", "application/json");
|
|
|
|
|
Request->ProcessRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UPortalManager::FindOrCreateGameSession_Response(FHttpRequestPtr Request, FHttpResponsePtr Response,
|
|
|
|
|
bool bWasSuccessful)
|
|
|
|
|
{
|
|
|
|
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Find or Create Game Session Response Received"));
|
2026-04-03 20:34:49 -04:00
|
|
|
|
|
|
|
|
if (!bWasSuccessful)
|
|
|
|
|
{
|
|
|
|
|
BroadcastJoinGameSessionMessage.Broadcast(HTTPStatusMessages::SomethingWentWrong, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSharedPtr<FJsonObject> JsonObject;
|
|
|
|
|
TSharedRef<TJsonReader<>> JsonReader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
|
|
|
|
|
if (FJsonSerializer::Deserialize(JsonReader, JsonObject))
|
|
|
|
|
{
|
|
|
|
|
if (ContainsErrors(JsonObject))
|
|
|
|
|
{
|
|
|
|
|
BroadcastJoinGameSessionMessage.Broadcast(HTTPStatusMessages::SomethingWentWrong, true);
|
|
|
|
|
}
|
|
|
|
|
DumpMetadata(JsonObject);
|
|
|
|
|
|
|
|
|
|
if (JsonObject->HasField(TEXT("GameProperties")))
|
|
|
|
|
{
|
|
|
|
|
auto PropertiesArray = JsonObject->GetArrayField(TEXT("GameProperties"));
|
|
|
|
|
TSharedPtr<FJsonObject> FlatMapJson = MakeShareable(new FJsonObject);
|
|
|
|
|
|
|
|
|
|
for (auto& PropJson : PropertiesArray)
|
|
|
|
|
{
|
|
|
|
|
auto PropObj = PropJson->AsObject();
|
|
|
|
|
FString Key = PropObj->GetStringField(TEXT("Key"));
|
|
|
|
|
FString Value = PropObj->GetStringField(TEXT("Value"));
|
|
|
|
|
FlatMapJson->SetStringField(Key, Value);
|
|
|
|
|
}
|
|
|
|
|
// Replace the array with a flat map field
|
|
|
|
|
JsonObject->SetObjectField(TEXT("GameProperties"), FlatMapJson);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FDSGameSession GameSession;
|
|
|
|
|
FJsonObjectConverter::JsonObjectToUStruct(JsonObject.ToSharedRef(), &GameSession);
|
|
|
|
|
GameSession.Dump();
|
|
|
|
|
BroadcastJoinGameSessionMessage.Broadcast(TEXT("Found Game Session."), false);
|
|
|
|
|
}
|
2026-03-31 08:15:28 -04:00
|
|
|
}
|