Lesson 76 - Parsing the Game Session Response

This commit is contained in:
Norman Lansing
2026-04-03 20:34:49 -04:00
parent 3ed338054f
commit ce6472a687
8 changed files with 166 additions and 6 deletions

View File

@@ -4,12 +4,15 @@
#include "UI/Portal/PortalManager.h"
#include "HttpModule.h"
#include "JsonObjectConverter.h"
#include "Data/API/APIData.h"
#include "GameplayTags/DedicatedServerTags.h"
#include "Interfaces/IHttpResponse.h"
#include "UI/HTTP/HTTPRequestTypes.h"
void UPortalManager::JoinGameSession()
{
BroadcastJoinGameSessionMessage.Broadcast(TEXT("Searching for Game Session..."));
BroadcastJoinGameSessionMessage.Broadcast(TEXT("Searching for Game Session..."), false);
check(APIData);
@@ -28,4 +31,42 @@ void UPortalManager::FindOrCreateGameSession_Response(FHttpRequestPtr Request, F
bool bWasSuccessful)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Find or Create Game Session Response Received"));
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);
}
}