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

@@ -1,6 +1,11 @@
#include "UI/HTTP/HTTPRequestTypes.h"
#include "DedicatedServers/DedicatedServers.h"
namespace HTTPStatusMessages
{
const FString SomethingWentWrong{TEXT("Something went wrong...")};
}
void FDSMetaData::Dump() const
{
UE_LOGFMT(LogDedicatedServers, Log, "MetaData:");
@@ -21,6 +26,39 @@ void FDSListFleetsResponse::Dump() const
if (!NextToken.IsEmpty())
{
UE_LOGFMT(LogDedicatedServers, Log, "NextToken: {NextToken}", NextToken);
UE_LOGFMT(LogDedicatedServers, Log, "NextToken: {NextToken}", *NextToken);
}
}
void FDSGameSession::Dump() const
{
UE_LOGFMT(LogDedicatedServers, Log, "GameSession:");
UE_LOGFMT(LogDedicatedServers, Log, "GameSessionId: {GameSessionId}", *GameSessionId);
UE_LOGFMT(LogDedicatedServers, Log, " Name: {Name}", *Name);
UE_LOGFMT(LogDedicatedServers, Log, " FleetArn: {FleetArn}", *FleetArn);
UE_LOGFMT(LogDedicatedServers, Log, " CreationTime: {CreationTime}", CreationTime); // need to write a conversion function
UE_LOGFMT(LogDedicatedServers, Log, " TerminationTime: {TerminationTime}", TerminationTime); // need to write a conversion function
UE_LOGFMT(LogDedicatedServers, Log, " CurrentPlayerSessionCount: {CurrentPlayerSessionCount}", CurrentPlayerSessionCount);
UE_LOGFMT(LogDedicatedServers, Log, " MaximumPlayerSessionCount: {MaximumPlayerSessionCount}", MaximumPlayerSessionCount);
UE_LOGFMT(LogDedicatedServers, Log, " Status: {Status}", *Status);
UE_LOGFMT(LogDedicatedServers, Log, " StatusReason: {StatusReason}", *StatusReason);
UE_LOGFMT(LogDedicatedServers, Log, " GameProperties:");
for (const TTuple<FString, FString> GameProperty : GameProperties)
{
UE_LOGFMT(LogDedicatedServers, Log, " {Key} : {Value}", *GameProperty.Key, *GameProperty.Value);
}
UE_LOGFMT(LogDedicatedServers, Log, " IpAddress: {IpAddress}", *IpAddress);
UE_LOGFMT(LogDedicatedServers, Log, " DnsName: {DnsName}", *DnsName);
UE_LOGFMT(LogDedicatedServers, Log, " Port: {Port}", Port);
UE_LOGFMT(LogDedicatedServers, Log, " PlayerSessionCreationPolicy: {PlayerSessionCreationPolicy}", *PlayerSessionCreationPolicy);
UE_LOGFMT(LogDedicatedServers, Log, " CreatorId: {CreatorId}", *CreatorId);
UE_LOGFMT(LogDedicatedServers, Log, " GameSessionData: {GameSessionData}", *GameSessionData);
UE_LOGFMT(LogDedicatedServers, Log, " MatchmakerData: {MatchmakerData}", *MatchmakerData);
UE_LOGFMT(LogDedicatedServers, Log, " Location: {Location}", *Location);
UE_LOGFMT(LogDedicatedServers, Log, " ComputeName: {ComputeName}", *ComputeName);
UE_LOGFMT(LogDedicatedServers, Log, " PlayerGatewayStatus: {PlayerGatewayStatus}", *PlayerGatewayStatus);
}

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);
}
}

View File

@@ -30,8 +30,14 @@ void USignInOverlay::OnJoinGameButtonClicked()
JoinGameWidget->Button_JoinGame->SetIsEnabled(false);
}
void USignInOverlay::UpdateJoinGameStatusMessage(const FString& StatusMessage)
void USignInOverlay::UpdateJoinGameStatusMessage(const FString& StatusMessage, const bool bShouldResetJoinGameButton)
{
check(IsValid(JoinGameWidget));
check(IsValid(JoinGameWidget->Button_JoinGame));
JoinGameWidget->SetStatusMessage(StatusMessage);
if (bShouldResetJoinGameButton)
{
JoinGameWidget->Button_JoinGame->SetIsEnabled(true);
}
}

View File

@@ -7,7 +7,7 @@
#include "UI/HTTP/HTTPRequestManager.h"
#include "APITestManager.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnListFleetsResponseReceived, const FDSListFleetsResponse&, ListFleetsResponse, bool, bWasSuccessful);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnListFleetsResponseReceived, const FDSListFleetsResponse&, ListFleetsResponse, const bool, bWasSuccessful);
/**

View File

@@ -2,6 +2,11 @@
#include "HTTPRequestTypes.generated.h"
namespace HTTPStatusMessages
{
extern DEDICATEDSERVERS_API const FString SomethingWentWrong;
}
USTRUCT()
struct FDSMetaData
{
@@ -33,5 +38,73 @@ struct FDSListFleetsResponse
UPROPERTY()
FString NextToken;
void Dump() const;
};
USTRUCT()
struct FDSGameSession
{
GENERATED_BODY()
UPROPERTY()
FString GameSessionId{};
UPROPERTY()
FString Name{};
UPROPERTY()
FString FleetArn{};
UPROPERTY()
double CreationTime{};
UPROPERTY()
double TerminationTime{};
UPROPERTY()
int32 CurrentPlayerSessionCount{};
UPROPERTY()
int32 MaximumPlayerSessionCount{};
UPROPERTY()
FString Status{};
UPROPERTY()
FString StatusReason{};
UPROPERTY()
TMap<FString, FString> GameProperties{};
UPROPERTY()
FString IpAddress{};
UPROPERTY()
FString DnsName{};
UPROPERTY()
int32 Port{};
UPROPERTY()
FString PlayerSessionCreationPolicy{};
UPROPERTY()
FString CreatorId{};
UPROPERTY()
FString GameSessionData{};
UPROPERTY()
FString MatchmakerData{};
UPROPERTY()
FString Location{};
UPROPERTY()
FString ComputeName{};
UPROPERTY()
FString PlayerGatewayStatus{};
void Dump() const;
};

View File

@@ -7,7 +7,7 @@
#include "UI/HTTP/HTTPRequestManager.h"
#include "PortalManager.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FBroadcastJoinGameSessionMessage, const FString&, StatusMessage);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FBroadcastJoinGameSessionMessage, const FString&, StatusMessage, bool, bShouldResetJoinGameButton);
/**
*

View File

@@ -39,5 +39,5 @@ private:
void OnJoinGameButtonClicked();
UFUNCTION()
void UpdateJoinGameStatusMessage(const FString& StatusMessage);
void UpdateJoinGameStatusMessage(const FString& StatusMessage, const bool bShouldResetJoinGameButton);
};