From e50a8addaf06090ada40cdeb24b81b8ee0b97945 Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Fri, 8 Dec 2023 00:06:49 +0000 Subject: [PATCH] Update Game Instance for Getting Lobby Players --- .../.idea.CorruptedMemory.dir/.idea/vcs.xml | 1 + .../Content/Levels/ClientEntry.umap | 4 +- .../Content/Server/BP_LobbyPlayer.uasset | 4 +- .../Content/Server/BP_ServerMenu.uasset | 4 +- .../Source/CorruptedMemory/CMGameInstance.cpp | 97 ++++++++++++++++--- .../Source/CorruptedMemory/CMGameInstance.h | 19 +++- 6 files changed, 106 insertions(+), 23 deletions(-) diff --git a/CorruptedMemory/.idea/.idea.CorruptedMemory.dir/.idea/vcs.xml b/CorruptedMemory/.idea/.idea.CorruptedMemory.dir/.idea/vcs.xml index 6c0b863..c7b7900 100644 --- a/CorruptedMemory/.idea/.idea.CorruptedMemory.dir/.idea/vcs.xml +++ b/CorruptedMemory/.idea/.idea.CorruptedMemory.dir/.idea/vcs.xml @@ -1,6 +1,7 @@ + \ No newline at end of file diff --git a/CorruptedMemory/Content/Levels/ClientEntry.umap b/CorruptedMemory/Content/Levels/ClientEntry.umap index 2eb03de..e00cc5c 100644 --- a/CorruptedMemory/Content/Levels/ClientEntry.umap +++ b/CorruptedMemory/Content/Levels/ClientEntry.umap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:14f450331c03198d77da600afab5083c2a67a4a29c42eefa392499a8785fc5e7 -size 45651 +oid sha256:237937226f0528cf5b214af84fca969814ebd5a9bb0ace21f9619ae505084f7e +size 42837 diff --git a/CorruptedMemory/Content/Server/BP_LobbyPlayer.uasset b/CorruptedMemory/Content/Server/BP_LobbyPlayer.uasset index dceb0bd..938ab1e 100644 --- a/CorruptedMemory/Content/Server/BP_LobbyPlayer.uasset +++ b/CorruptedMemory/Content/Server/BP_LobbyPlayer.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c04c195257bcdbae6fc5dac85dbc71078434e04fd0a0ec60811a22509f9c5517 -size 113176 +oid sha256:44980cb55dcf0f576a61c7cb15800fc9b3fbe722500af8f796855a2a7befe79b +size 120991 diff --git a/CorruptedMemory/Content/Server/BP_ServerMenu.uasset b/CorruptedMemory/Content/Server/BP_ServerMenu.uasset index bddbbd5..50184a4 100644 --- a/CorruptedMemory/Content/Server/BP_ServerMenu.uasset +++ b/CorruptedMemory/Content/Server/BP_ServerMenu.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:9c065c47cb680b72dbb951d997957674b88bd1fc79813ad11e8fa86b0049269c -size 593178 +oid sha256:9252a90a0432d8c2c3759c93104ce14780763bb53e4d09a23c7148ea40fef6dc +size 851581 diff --git a/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.cpp b/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.cpp index 76ec0c1..e5807bb 100644 --- a/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.cpp +++ b/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.cpp @@ -1,5 +1,6 @@ #include "CMGameInstance.h" #include "SocketIOClient.h" +#include "Kismet/GameplayStatics.h" void UCMGameInstance::Login(const FString& Username, const FString& Password) { @@ -48,7 +49,7 @@ void UCMGameInstance::CreateLobby(const int MaxPlayers) Request->ProcessRequest(); } -void UCMGameInstance::JoinLobby() +void UCMGameInstance::JoinLobby(const FString LobbyID) { // if (APlayerController* PC = GetWorld()->GetFirstPlayerController()) // { @@ -56,7 +57,7 @@ void UCMGameInstance::JoinLobby() // } const auto Request = FHttpModule::Get().CreateRequest(); - Request->SetURL("/join"); + Request->SetURL(LobbyAPI + "/join"); Request->SetVerb("POST"); Request->SetHeader("Authorization", "Bearer " + JwtToken); Request->SetHeader("Content-Type", "application/json"); @@ -72,19 +73,30 @@ void UCMGameInstance::JoinLobby() void UCMGameInstance::LeaveLobby() { const auto Request = FHttpModule::Get().CreateRequest(); - Request->SetURL("/leave"); + Request->SetURL(LobbyAPI + "/leave"); Request->SetVerb("POST"); Request->SetHeader("Authorization", "Bearer " + JwtToken); - Request->SetHeader("Accept", "application/json"); + Request->SetHeader("Content-Type", "application/json"); Request->SetHeader("Accept", "application/json"); const TSharedPtr JoinData = MakeShareable(new FJsonObject); - JoinData->SetStringField("lobbyID", LobbyID); + JoinData->SetStringField("lobbyID", JoinedLobbyID); JoinData->SetStringField("username", AccountUsername); Request->SetContentAsString(USIOJConvert::ToJsonString(JoinData)); Request->OnProcessRequestComplete().BindUObject(this, &UCMGameInstance::OnRequestToLeaveLobbyComplete); 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() { Super::Init(); @@ -97,11 +109,11 @@ void UCMGameInstance::Init() UE_LOG(LogTemp, Log, TEXT("Connected: %s %s"), *SocketID, *SessionID); if (IsDedicatedServerInstance()) { - FParse::Value(FCommandLine::Get(), TEXT("lobbyID="), LobbyID); + FParse::Value(FCommandLine::Get(), TEXT("lobbyID="), JoinedLobbyID); FString CMServerSecret; FParse::Value(FCommandLine::Get(), TEXT("CMServerSecret="), CMServerSecret); const TSharedPtr AuthorityData = MakeShareable(new FJsonObject); - AuthorityData->SetStringField("lobbyID", LobbyID); + AuthorityData->SetStringField("lobbyID", JoinedLobbyID); AuthorityData->SetStringField("secret", CMServerSecret); Socket->Emit(TEXT("authority"), AuthorityData); } @@ -125,10 +137,6 @@ void UCMGameInstance::Init() } }); - Socket->OnEvent(LobbyID + "/A", [this](const FString& Event, const TSharedPtr& Message) - { - UE_LOG(LogTemp, Log, TEXT("Received: %s"), *USIOJConvert::ToJsonString(Message)); - }); Socket->OnEvent("join", [this](const FString& Event, const TSharedPtr& Message) { @@ -149,6 +157,20 @@ void UCMGameInstance::Init() OnPlayerLeave.Broadcast(JsonUsername); } }); + + Socket->OnEvent("close", [this](const FString& Event, const TSharedPtr& Message) + { + const auto JsonReader = TJsonReaderFactory<>::Create(USIOJConvert::ToJsonString(Message)); + if (TSharedPtr 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() @@ -220,10 +242,8 @@ void UCMGameInstance::OnRequestToCreateLobbyComplete(FHttpRequestPtr Request, FH { const auto JsonLobbyID = JsonObject->GetStringField("lobbyID"); const auto JsonPort = JsonObject->GetIntegerField("port"); - LobbyID = JsonLobbyID; - ServerPort = JsonPort; OnCreateLobbyComplete.Broadcast(true); - JoinLobby(); + JoinLobby(JsonLobbyID); } else { @@ -240,7 +260,23 @@ void UCMGameInstance::OnRequestToJoinLobbyComplete(FHttpRequestPtr Request, FHtt { if (bWasSuccessful) { - OnJoinLobbyComplete.Broadcast(true); + const auto JsonReader = TJsonReaderFactory<>::Create(Response->GetContentAsString()); + if (TSharedPtr JsonObject; FJsonSerializer::Deserialize(JsonReader, JsonObject)) + { + const auto JsonLobbyID = JsonObject->GetStringField("lobbyID"); + const auto JsonPort = JsonObject->GetIntegerField("port"); + JoinedLobbyID = JsonLobbyID; + ServerPort = JsonPort; + const TSharedPtr 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 { @@ -252,6 +288,10 @@ void UCMGameInstance::OnRequestToLeaveLobbyComplete(FHttpRequestPtr Request, FHt { if (bWasSuccessful) { + const TSharedPtr LeaveData = MakeShareable(new FJsonObject); + LeaveData->SetStringField("lobbyID", JoinedLobbyID); + LeaveData->SetStringField("username", AccountUsername); + Socket->Emit(TEXT("leave"), LeaveData); OnLeaveLobbyComplete.Broadcast(true); } else @@ -259,3 +299,30 @@ void UCMGameInstance::OnRequestToLeaveLobbyComplete(FHttpRequestPtr Request, FHt OnLeaveLobbyComplete.Broadcast(false); } } + +void UCMGameInstance::OnRequestToGetLobbyPlayersComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) +{ + if (bWasSuccessful) + { + const auto JsonReader = TJsonReaderFactory<>::Create(Response->GetContentAsString()); + if (TArray> 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); + } +} diff --git a/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.h b/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.h index 55acf24..7c0875b 100644 --- a/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.h +++ b/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.h @@ -4,6 +4,7 @@ #include "Engine/GameInstance.h" #include "Http.h" #include "SocketIONative.h" +#include "GenericStructs.h" #include "CMGameInstance.generated.h" /** @@ -27,10 +28,14 @@ public: 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(FOnPlayerLeave, const FString, PlayerName); + DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnServerClose, const FString, Message); + UPROPERTY(BlueprintAssignable, Category = "Account") FOnLoginComplete OnLoginComplete; UPROPERTY(BlueprintAssignable, Category = "Account") @@ -44,9 +49,13 @@ public: UPROPERTY(BlueprintAssignable, Category = "Lobby") FOnLeaveLobbyComplete OnLeaveLobbyComplete; UPROPERTY(BlueprintAssignable, Category = "Lobby") + FOnGetPlayersInLobbyComplete OnGetPlayersInLobbyComplete; + UPROPERTY(BlueprintAssignable, Category = "Lobby") FOnPlayerJoin OnPlayerJoin; UPROPERTY(BlueprintAssignable, Category = "Lobby") FOnPlayerLeave OnPlayerLeave; + UPROPERTY(BlueprintAssignable, Category = "Lobby") + FOnServerClose OnServerClose; UPROPERTY(BlueprintReadOnly, Category = "Account") FString AccountUsername; @@ -56,9 +65,11 @@ public: UPROPERTY(BlueprintReadOnly, Category = "Lobby") FString ServerFQDN = "cm.api.philipwhite.dev"; UPROPERTY(BlueprintReadOnly, Category = "Lobby") - FString LobbyID; + FString JoinedLobbyID; UPROPERTY(BlueprintReadOnly, Category = "Lobby") int ServerPort; + UPROPERTY(BlueprintReadOnly, Category = "Lobby") + TArray PlayersInLobby; UFUNCTION(BlueprintCallable, Category = "Account") void Login(const FString& Username, const FString& Password); @@ -69,9 +80,12 @@ public: UFUNCTION(BlueprintCallable, Category = "Lobby") void CreateLobby(const int MaxPlayers); UFUNCTION(BlueprintCallable, Category = "Lobby") - void JoinLobby(); + void JoinLobby(const FString LobbyID); UFUNCTION(BlueprintCallable, Category = "Lobby") void LeaveLobby(); + UFUNCTION(BlueprintCallable, Category = "Lobby") + void GetPlayerLobbyList(); + virtual void Init() override; virtual void Shutdown() override; @@ -85,4 +99,5 @@ private: void OnRequestToCreateLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); void OnRequestToJoinLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); void OnRequestToLeaveLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); + void OnRequestToGetLobbyPlayersComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); };