From 41fa2dd9545feb1638df9179d6342b0e82c0a0b9 Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Wed, 6 Dec 2023 03:45:52 +0000 Subject: [PATCH] Update Game Instance for Joining & Leaving Lobbies --- .../Source/CorruptedMemory/CMGameInstance.cpp | 111 ++++++++++++++++-- .../Source/CorruptedMemory/CMGameInstance.h | 22 +++- 2 files changed, 122 insertions(+), 11 deletions(-) diff --git a/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.cpp b/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.cpp index a6b0687..56bd485 100644 --- a/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.cpp +++ b/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.cpp @@ -39,19 +39,50 @@ void UCMGameInstance::CreateLobby() { const auto Request = FHttpModule::Get().CreateRequest(); Request->SetURL(LobbyAPI + "/create"); - Request->SetVerb("GET"); + Request->SetVerb("POST"); Request->SetHeader("Authorization", "Bearer " + JwtToken); + Request->SetHeader("Content-Type", "application/json"); Request->SetHeader("Accept", "application/json"); + Request->SetContentAsString("{\"maxPlayers\":\"4\"}"); Request->OnProcessRequestComplete().BindUObject(this, &UCMGameInstance::OnRequestToCreateLobbyComplete); Request->ProcessRequest(); } -void UCMGameInstance::JoinLobby(const int Port) +void UCMGameInstance::JoinLobby() { - if (APlayerController* PC = GetWorld()->GetFirstPlayerController()) - { - PC->ConsoleCommand("open " + ServerFQDN + ":" + FString::FromInt(Port)); - } + // if (APlayerController* PC = GetWorld()->GetFirstPlayerController()) + // { + // PC->ConsoleCommand("open " + ServerFQDN + ":" + FString::FromInt(Port)); + // } + + const auto Request = FHttpModule::Get().CreateRequest(); + Request->SetURL("/join"); + Request->SetVerb("POST"); + Request->SetHeader("Authorization", "Bearer " + JwtToken); + Request->SetHeader("Content-Type", "application/json"); + Request->SetHeader("Accept", "application/json"); + const TSharedPtr JoinData = MakeShareable(new FJsonObject); + JoinData->SetStringField("lobbyID", LobbyID); + JoinData->SetStringField("username", AccountUsername); + Request->SetContentAsString(USIOJConvert::ToJsonString(JoinData)); + Request->OnProcessRequestComplete().BindUObject(this, &UCMGameInstance::OnRequestToJoinLobbyComplete); + Request->ProcessRequest(); +} + +void UCMGameInstance::LeaveLobby() +{ + const auto Request = FHttpModule::Get().CreateRequest(); + Request->SetURL("/leave"); + Request->SetVerb("POST"); + Request->SetHeader("Authorization", "Bearer " + JwtToken); + Request->SetHeader("Accept", "application/json"); + Request->SetHeader("Accept", "application/json"); + const TSharedPtr JoinData = MakeShareable(new FJsonObject); + JoinData->SetStringField("lobbyID", LobbyID); + JoinData->SetStringField("username", AccountUsername); + Request->SetContentAsString(USIOJConvert::ToJsonString(JoinData)); + Request->OnProcessRequestComplete().BindUObject(this, &UCMGameInstance::OnRequestToLeaveLobbyComplete); + Request->ProcessRequest(); } void UCMGameInstance::Init() @@ -74,14 +105,50 @@ void UCMGameInstance::Init() AuthorityData->SetStringField("secret", CMServerSecret); Socket->Emit(TEXT("authority"), AuthorityData); } + else + { + const TSharedPtr AuthorityData = MakeShareable(new FJsonObject); + AuthorityData->SetStringField("authority", "client"); + Socket->Emit(TEXT("authority"), AuthorityData); + } }; + Socket->OnEvent("command", [this](const FString& Event, const TSharedPtr& Command) + { + if (IsDedicatedServerInstance()) + { + if (Command->AsString() == "stop") + { + UE_LOG(LogTemp, Log, TEXT("Shutting Down Server")); + FGenericPlatformMisc::RequestExit(false); + } + } + }); + Socket->OnEvent(LobbyID + "/A", [this](const FString& Event, const TSharedPtr& Message) { UE_LOG(LogTemp, Log, TEXT("Received: %s"), *USIOJConvert::ToJsonString(Message)); }); - Socket->Emit(TEXT("MyEmit"), TEXT("hi")); + Socket->OnEvent("join", [this](const FString& Event, const TSharedPtr& Message) + { + const auto JsonReader = TJsonReaderFactory<>::Create(USIOJConvert::ToJsonString(Message)); + if (TSharedPtr JsonObject; FJsonSerializer::Deserialize(JsonReader, JsonObject)) + { + const auto JsonUsername = JsonObject->GetStringField("username"); + OnPlayerJoin.Broadcast(JsonUsername); + } + }); + + Socket->OnEvent("leave", [this](const FString& Event, const TSharedPtr& Message) + { + const auto JsonReader = TJsonReaderFactory<>::Create(USIOJConvert::ToJsonString(Message)); + if (TSharedPtr JsonObject; FJsonSerializer::Deserialize(JsonReader, JsonObject)) + { + const auto JsonUsername = JsonObject->GetStringField("username"); + OnPlayerLeave.Broadcast(JsonUsername); + } + }); } void UCMGameInstance::Shutdown() @@ -155,16 +222,40 @@ void UCMGameInstance::OnRequestToCreateLobbyComplete(FHttpRequestPtr Request, FH const auto JsonPort = JsonObject->GetIntegerField("port"); LobbyID = JsonLobbyID; ServerPort = JsonPort; - OnJoinLobbyComplete.Broadcast(true); - JoinLobby(ServerPort); + OnCreateLobbyComplete.Broadcast(true); + JoinLobby(); } else { - OnJoinLobbyComplete.Broadcast(false); + OnCreateLobbyComplete.Broadcast(false); } } else + { + OnCreateLobbyComplete.Broadcast(false); + } +} + +void UCMGameInstance::OnRequestToJoinLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) +{ + if (bWasSuccessful) + { + OnJoinLobbyComplete.Broadcast(true); + } + else { OnJoinLobbyComplete.Broadcast(false); } } + +void UCMGameInstance::OnRequestToLeaveLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) +{ + if (bWasSuccessful) + { + OnLeaveLobbyComplete.Broadcast(true); + } + else + { + OnLeaveLobbyComplete.Broadcast(false); + } +} diff --git a/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.h b/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.h index 6973df7..58fc84e 100644 --- a/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.h +++ b/CorruptedMemory/Source/CorruptedMemory/CMGameInstance.h @@ -21,8 +21,16 @@ public: DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnLogoutComplete, const bool, bWasSuccessful); + DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnCreateLobbyComplete, const bool, bWasSuccessful); + DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnJoinLobbyComplete, const bool, bWasSuccessful); + DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnLeaveLobbyComplete, const bool, bWasSuccessful); + + DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPlayerJoin, const FString, PlayerName); + + DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPlayerLeave, const FString, PlayerName); + UPROPERTY(BlueprintAssignable, Category = "Account") FOnLoginComplete OnLoginComplete; UPROPERTY(BlueprintAssignable, Category = "Account") @@ -30,7 +38,15 @@ public: UPROPERTY(BlueprintAssignable, Category = "Account") FOnLogoutComplete OnLogoutComplete; UPROPERTY(BlueprintAssignable, Category = "Lobby") + FOnCreateLobbyComplete OnCreateLobbyComplete; + UPROPERTY(BlueprintAssignable, Category = "Lobby") FOnJoinLobbyComplete OnJoinLobbyComplete; + UPROPERTY(BlueprintAssignable, Category = "Lobby") + FOnLeaveLobbyComplete OnLeaveLobbyComplete; + UPROPERTY(BlueprintAssignable, Category = "Lobby") + FOnPlayerJoin OnPlayerJoin; + UPROPERTY(BlueprintAssignable, Category = "Lobby") + FOnPlayerLeave OnPlayerLeave; UPROPERTY(BlueprintReadOnly, Category = "Account") FString AccountUsername; @@ -53,7 +69,9 @@ public: UFUNCTION(BlueprintCallable, Category = "Lobby") void CreateLobby(); UFUNCTION(BlueprintCallable, Category = "Lobby") - void JoinLobby(const int Port); + void JoinLobby(); + UFUNCTION(BlueprintCallable, Category = "Lobby") + void LeaveLobby(); virtual void Init() override; virtual void Shutdown() override; @@ -65,4 +83,6 @@ private: void OnRequestToRegisterComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) const; void OnRequestToLogoutComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); void OnRequestToCreateLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); + void OnRequestToJoinLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); + void OnRequestToLeaveLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); };