Update Game Instance for Getting Lobby Players

This commit is contained in:
Philip W 2023-12-08 00:06:49 +00:00
parent 10d39a80b6
commit e50a8addaf
6 changed files with 106 additions and 23 deletions

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$USER_HOME$/Downloads/Ue5/UnrealEngine" vcs="Git" />
<mapping directory="$PROJECT_DIR$/.." vcs="Git" /> <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component> </component>
</project> </project>

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:14f450331c03198d77da600afab5083c2a67a4a29c42eefa392499a8785fc5e7 oid sha256:237937226f0528cf5b214af84fca969814ebd5a9bb0ace21f9619ae505084f7e
size 45651 size 42837

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:c04c195257bcdbae6fc5dac85dbc71078434e04fd0a0ec60811a22509f9c5517 oid sha256:44980cb55dcf0f576a61c7cb15800fc9b3fbe722500af8f796855a2a7befe79b
size 113176 size 120991

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:9c065c47cb680b72dbb951d997957674b88bd1fc79813ad11e8fa86b0049269c oid sha256:9252a90a0432d8c2c3759c93104ce14780763bb53e4d09a23c7148ea40fef6dc
size 593178 size 851581

View File

@ -1,5 +1,6 @@
#include "CMGameInstance.h" #include "CMGameInstance.h"
#include "SocketIOClient.h" #include "SocketIOClient.h"
#include "Kismet/GameplayStatics.h"
void UCMGameInstance::Login(const FString& Username, const FString& Password) void UCMGameInstance::Login(const FString& Username, const FString& Password)
{ {
@ -48,7 +49,7 @@ void UCMGameInstance::CreateLobby(const int MaxPlayers)
Request->ProcessRequest(); Request->ProcessRequest();
} }
void UCMGameInstance::JoinLobby() void UCMGameInstance::JoinLobby(const FString LobbyID)
{ {
// if (APlayerController* PC = GetWorld()->GetFirstPlayerController()) // if (APlayerController* PC = GetWorld()->GetFirstPlayerController())
// { // {
@ -56,7 +57,7 @@ void UCMGameInstance::JoinLobby()
// } // }
const auto Request = FHttpModule::Get().CreateRequest(); const auto Request = FHttpModule::Get().CreateRequest();
Request->SetURL("/join"); Request->SetURL(LobbyAPI + "/join");
Request->SetVerb("POST"); Request->SetVerb("POST");
Request->SetHeader("Authorization", "Bearer " + JwtToken); Request->SetHeader("Authorization", "Bearer " + JwtToken);
Request->SetHeader("Content-Type", "application/json"); Request->SetHeader("Content-Type", "application/json");
@ -72,19 +73,30 @@ void UCMGameInstance::JoinLobby()
void UCMGameInstance::LeaveLobby() void UCMGameInstance::LeaveLobby()
{ {
const auto Request = FHttpModule::Get().CreateRequest(); const auto Request = FHttpModule::Get().CreateRequest();
Request->SetURL("/leave"); Request->SetURL(LobbyAPI + "/leave");
Request->SetVerb("POST"); Request->SetVerb("POST");
Request->SetHeader("Authorization", "Bearer " + JwtToken); Request->SetHeader("Authorization", "Bearer " + JwtToken);
Request->SetHeader("Accept", "application/json"); Request->SetHeader("Content-Type", "application/json");
Request->SetHeader("Accept", "application/json"); Request->SetHeader("Accept", "application/json");
const TSharedPtr<FJsonObject> JoinData = MakeShareable(new FJsonObject); const TSharedPtr<FJsonObject> JoinData = MakeShareable(new FJsonObject);
JoinData->SetStringField("lobbyID", LobbyID); JoinData->SetStringField("lobbyID", JoinedLobbyID);
JoinData->SetStringField("username", AccountUsername); JoinData->SetStringField("username", AccountUsername);
Request->SetContentAsString(USIOJConvert::ToJsonString(JoinData)); Request->SetContentAsString(USIOJConvert::ToJsonString(JoinData));
Request->OnProcessRequestComplete().BindUObject(this, &UCMGameInstance::OnRequestToLeaveLobbyComplete); Request->OnProcessRequestComplete().BindUObject(this, &UCMGameInstance::OnRequestToLeaveLobbyComplete);
Request->ProcessRequest(); Request->ProcessRequest();
} }
void UCMGameInstance::GetPlayerLobbyList()
{
const auto Request = FHttpModule::Get().CreateRequest();
Request->SetURL(LobbyAPI + "/lobby/players/" + JoinedLobbyID);
Request->SetVerb("GET");
Request->SetHeader("Authorization", "Bearer " + JwtToken);
Request->SetHeader("Accept", "application/json");
Request->OnProcessRequestComplete().BindUObject(this, &UCMGameInstance::OnRequestToGetLobbyPlayersComplete);
Request->ProcessRequest();
}
void UCMGameInstance::Init() void UCMGameInstance::Init()
{ {
Super::Init(); Super::Init();
@ -97,11 +109,11 @@ void UCMGameInstance::Init()
UE_LOG(LogTemp, Log, TEXT("Connected: %s %s"), *SocketID, *SessionID); UE_LOG(LogTemp, Log, TEXT("Connected: %s %s"), *SocketID, *SessionID);
if (IsDedicatedServerInstance()) if (IsDedicatedServerInstance())
{ {
FParse::Value(FCommandLine::Get(), TEXT("lobbyID="), LobbyID); FParse::Value(FCommandLine::Get(), TEXT("lobbyID="), JoinedLobbyID);
FString CMServerSecret; FString CMServerSecret;
FParse::Value(FCommandLine::Get(), TEXT("CMServerSecret="), CMServerSecret); FParse::Value(FCommandLine::Get(), TEXT("CMServerSecret="), CMServerSecret);
const TSharedPtr<FJsonObject> AuthorityData = MakeShareable(new FJsonObject); const TSharedPtr<FJsonObject> AuthorityData = MakeShareable(new FJsonObject);
AuthorityData->SetStringField("lobbyID", LobbyID); AuthorityData->SetStringField("lobbyID", JoinedLobbyID);
AuthorityData->SetStringField("secret", CMServerSecret); AuthorityData->SetStringField("secret", CMServerSecret);
Socket->Emit(TEXT("authority"), AuthorityData); Socket->Emit(TEXT("authority"), AuthorityData);
} }
@ -125,10 +137,6 @@ void UCMGameInstance::Init()
} }
}); });
Socket->OnEvent(LobbyID + "/A", [this](const FString& Event, const TSharedPtr<FJsonValue>& Message)
{
UE_LOG(LogTemp, Log, TEXT("Received: %s"), *USIOJConvert::ToJsonString(Message));
});
Socket->OnEvent("join", [this](const FString& Event, const TSharedPtr<FJsonValue>& Message) Socket->OnEvent("join", [this](const FString& Event, const TSharedPtr<FJsonValue>& Message)
{ {
@ -149,6 +157,20 @@ void UCMGameInstance::Init()
OnPlayerLeave.Broadcast(JsonUsername); OnPlayerLeave.Broadcast(JsonUsername);
} }
}); });
Socket->OnEvent("close", [this](const FString& Event, const TSharedPtr<FJsonValue>& Message)
{
const auto JsonReader = TJsonReaderFactory<>::Create(USIOJConvert::ToJsonString(Message));
if (TSharedPtr<FJsonObject> JsonObject; FJsonSerializer::Deserialize(JsonReader, JsonObject))
{
const auto JsonMessage = JsonObject->GetStringField("message");
if (GetWorld()->GetName() != "ClientEntry")
{
UGameplayStatics::OpenLevel(GetWorld(), "ClientEntry");
OnServerClose.Broadcast(JsonMessage);
}
}
});
} }
void UCMGameInstance::Shutdown() void UCMGameInstance::Shutdown()
@ -220,10 +242,8 @@ void UCMGameInstance::OnRequestToCreateLobbyComplete(FHttpRequestPtr Request, FH
{ {
const auto JsonLobbyID = JsonObject->GetStringField("lobbyID"); const auto JsonLobbyID = JsonObject->GetStringField("lobbyID");
const auto JsonPort = JsonObject->GetIntegerField("port"); const auto JsonPort = JsonObject->GetIntegerField("port");
LobbyID = JsonLobbyID;
ServerPort = JsonPort;
OnCreateLobbyComplete.Broadcast(true); OnCreateLobbyComplete.Broadcast(true);
JoinLobby(); JoinLobby(JsonLobbyID);
} }
else else
{ {
@ -240,7 +260,23 @@ void UCMGameInstance::OnRequestToJoinLobbyComplete(FHttpRequestPtr Request, FHtt
{ {
if (bWasSuccessful) if (bWasSuccessful)
{ {
OnJoinLobbyComplete.Broadcast(true); const auto JsonReader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
if (TSharedPtr<FJsonObject> JsonObject; FJsonSerializer::Deserialize(JsonReader, JsonObject))
{
const auto JsonLobbyID = JsonObject->GetStringField("lobbyID");
const auto JsonPort = JsonObject->GetIntegerField("port");
JoinedLobbyID = JsonLobbyID;
ServerPort = JsonPort;
const TSharedPtr<FJsonObject> JoinData = MakeShareable(new FJsonObject);
JoinData->SetStringField("lobbyID", JoinedLobbyID);
JoinData->SetStringField("username", AccountUsername);
Socket->Emit(TEXT("join"), JoinData);
OnJoinLobbyComplete.Broadcast(true);
}
else
{
OnJoinLobbyComplete.Broadcast(false);
}
} }
else else
{ {
@ -252,6 +288,10 @@ void UCMGameInstance::OnRequestToLeaveLobbyComplete(FHttpRequestPtr Request, FHt
{ {
if (bWasSuccessful) if (bWasSuccessful)
{ {
const TSharedPtr<FJsonObject> LeaveData = MakeShareable(new FJsonObject);
LeaveData->SetStringField("lobbyID", JoinedLobbyID);
LeaveData->SetStringField("username", AccountUsername);
Socket->Emit(TEXT("leave"), LeaveData);
OnLeaveLobbyComplete.Broadcast(true); OnLeaveLobbyComplete.Broadcast(true);
} }
else else
@ -259,3 +299,30 @@ void UCMGameInstance::OnRequestToLeaveLobbyComplete(FHttpRequestPtr Request, FHt
OnLeaveLobbyComplete.Broadcast(false); OnLeaveLobbyComplete.Broadcast(false);
} }
} }
void UCMGameInstance::OnRequestToGetLobbyPlayersComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful)
{
const auto JsonReader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
if (TArray<TSharedPtr<FJsonValue>> JsonObject; FJsonSerializer::Deserialize(JsonReader, JsonObject))
{
PlayersInLobby.Empty();
for (const auto& Player : JsonObject)
{
const auto JsonUsername = Player->AsObject()->GetStringField("user");
const auto JsonIsOwner = Player->AsObject()->GetBoolField("isOwner");
PlayersInLobby.Add(FPlayerInLobby(JsonUsername, JsonIsOwner));
}
OnGetPlayersInLobbyComplete.Broadcast(true);
}
else
{
OnGetPlayersInLobbyComplete.Broadcast(false);
}
}
else
{
OnGetPlayersInLobbyComplete.Broadcast(false);
}
}

View File

@ -4,6 +4,7 @@
#include "Engine/GameInstance.h" #include "Engine/GameInstance.h"
#include "Http.h" #include "Http.h"
#include "SocketIONative.h" #include "SocketIONative.h"
#include "GenericStructs.h"
#include "CMGameInstance.generated.h" #include "CMGameInstance.generated.h"
/** /**
@ -27,10 +28,14 @@ public:
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnLeaveLobbyComplete, const bool, bWasSuccessful); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnLeaveLobbyComplete, const bool, bWasSuccessful);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnGetPlayersInLobbyComplete, const bool, bWasSuccessful);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPlayerJoin, const FString, PlayerName); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPlayerJoin, const FString, PlayerName);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPlayerLeave, const FString, PlayerName); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPlayerLeave, const FString, PlayerName);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnServerClose, const FString, Message);
UPROPERTY(BlueprintAssignable, Category = "Account") UPROPERTY(BlueprintAssignable, Category = "Account")
FOnLoginComplete OnLoginComplete; FOnLoginComplete OnLoginComplete;
UPROPERTY(BlueprintAssignable, Category = "Account") UPROPERTY(BlueprintAssignable, Category = "Account")
@ -44,9 +49,13 @@ public:
UPROPERTY(BlueprintAssignable, Category = "Lobby") UPROPERTY(BlueprintAssignable, Category = "Lobby")
FOnLeaveLobbyComplete OnLeaveLobbyComplete; FOnLeaveLobbyComplete OnLeaveLobbyComplete;
UPROPERTY(BlueprintAssignable, Category = "Lobby") UPROPERTY(BlueprintAssignable, Category = "Lobby")
FOnGetPlayersInLobbyComplete OnGetPlayersInLobbyComplete;
UPROPERTY(BlueprintAssignable, Category = "Lobby")
FOnPlayerJoin OnPlayerJoin; FOnPlayerJoin OnPlayerJoin;
UPROPERTY(BlueprintAssignable, Category = "Lobby") UPROPERTY(BlueprintAssignable, Category = "Lobby")
FOnPlayerLeave OnPlayerLeave; FOnPlayerLeave OnPlayerLeave;
UPROPERTY(BlueprintAssignable, Category = "Lobby")
FOnServerClose OnServerClose;
UPROPERTY(BlueprintReadOnly, Category = "Account") UPROPERTY(BlueprintReadOnly, Category = "Account")
FString AccountUsername; FString AccountUsername;
@ -56,9 +65,11 @@ public:
UPROPERTY(BlueprintReadOnly, Category = "Lobby") UPROPERTY(BlueprintReadOnly, Category = "Lobby")
FString ServerFQDN = "cm.api.philipwhite.dev"; FString ServerFQDN = "cm.api.philipwhite.dev";
UPROPERTY(BlueprintReadOnly, Category = "Lobby") UPROPERTY(BlueprintReadOnly, Category = "Lobby")
FString LobbyID; FString JoinedLobbyID;
UPROPERTY(BlueprintReadOnly, Category = "Lobby") UPROPERTY(BlueprintReadOnly, Category = "Lobby")
int ServerPort; int ServerPort;
UPROPERTY(BlueprintReadOnly, Category = "Lobby")
TArray<FPlayerInLobby> PlayersInLobby;
UFUNCTION(BlueprintCallable, Category = "Account") UFUNCTION(BlueprintCallable, Category = "Account")
void Login(const FString& Username, const FString& Password); void Login(const FString& Username, const FString& Password);
@ -69,9 +80,12 @@ public:
UFUNCTION(BlueprintCallable, Category = "Lobby") UFUNCTION(BlueprintCallable, Category = "Lobby")
void CreateLobby(const int MaxPlayers); void CreateLobby(const int MaxPlayers);
UFUNCTION(BlueprintCallable, Category = "Lobby") UFUNCTION(BlueprintCallable, Category = "Lobby")
void JoinLobby(); void JoinLobby(const FString LobbyID);
UFUNCTION(BlueprintCallable, Category = "Lobby") UFUNCTION(BlueprintCallable, Category = "Lobby")
void LeaveLobby(); void LeaveLobby();
UFUNCTION(BlueprintCallable, Category = "Lobby")
void GetPlayerLobbyList();
virtual void Init() override; virtual void Init() override;
virtual void Shutdown() override; virtual void Shutdown() override;
@ -85,4 +99,5 @@ private:
void OnRequestToCreateLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); void OnRequestToCreateLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
void OnRequestToJoinLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); void OnRequestToJoinLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
void OnRequestToLeaveLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); void OnRequestToLeaveLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
void OnRequestToGetLobbyPlayersComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
}; };