Update Game Instance for Joining & Leaving Lobbies

This commit is contained in:
Philip W 2023-12-06 03:45:52 +00:00
parent abf1be1227
commit 41fa2dd954
2 changed files with 122 additions and 11 deletions

View File

@ -39,19 +39,50 @@ void UCMGameInstance::CreateLobby()
{ {
const auto Request = FHttpModule::Get().CreateRequest(); const auto Request = FHttpModule::Get().CreateRequest();
Request->SetURL(LobbyAPI + "/create"); Request->SetURL(LobbyAPI + "/create");
Request->SetVerb("GET"); Request->SetVerb("POST");
Request->SetHeader("Authorization", "Bearer " + JwtToken); Request->SetHeader("Authorization", "Bearer " + JwtToken);
Request->SetHeader("Content-Type", "application/json");
Request->SetHeader("Accept", "application/json"); Request->SetHeader("Accept", "application/json");
Request->SetContentAsString("{\"maxPlayers\":\"4\"}");
Request->OnProcessRequestComplete().BindUObject(this, &UCMGameInstance::OnRequestToCreateLobbyComplete); Request->OnProcessRequestComplete().BindUObject(this, &UCMGameInstance::OnRequestToCreateLobbyComplete);
Request->ProcessRequest(); Request->ProcessRequest();
} }
void UCMGameInstance::JoinLobby(const int Port) void UCMGameInstance::JoinLobby()
{ {
if (APlayerController* PC = GetWorld()->GetFirstPlayerController()) // if (APlayerController* PC = GetWorld()->GetFirstPlayerController())
{ // {
PC->ConsoleCommand("open " + ServerFQDN + ":" + FString::FromInt(Port)); // 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<FJsonObject> 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<FJsonObject> 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() void UCMGameInstance::Init()
@ -74,14 +105,50 @@ void UCMGameInstance::Init()
AuthorityData->SetStringField("secret", CMServerSecret); AuthorityData->SetStringField("secret", CMServerSecret);
Socket->Emit(TEXT("authority"), AuthorityData); Socket->Emit(TEXT("authority"), AuthorityData);
} }
else
{
const TSharedPtr<FJsonObject> AuthorityData = MakeShareable(new FJsonObject);
AuthorityData->SetStringField("authority", "client");
Socket->Emit(TEXT("authority"), AuthorityData);
}
}; };
Socket->OnEvent("command", [this](const FString& Event, const TSharedPtr<FJsonValue>& 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<FJsonValue>& Message) Socket->OnEvent(LobbyID + "/A", [this](const FString& Event, const TSharedPtr<FJsonValue>& Message)
{ {
UE_LOG(LogTemp, Log, TEXT("Received: %s"), *USIOJConvert::ToJsonString(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<FJsonValue>& Message)
{
const auto JsonReader = TJsonReaderFactory<>::Create(USIOJConvert::ToJsonString(Message));
if (TSharedPtr<FJsonObject> JsonObject; FJsonSerializer::Deserialize(JsonReader, JsonObject))
{
const auto JsonUsername = JsonObject->GetStringField("username");
OnPlayerJoin.Broadcast(JsonUsername);
}
});
Socket->OnEvent("leave", [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 JsonUsername = JsonObject->GetStringField("username");
OnPlayerLeave.Broadcast(JsonUsername);
}
});
} }
void UCMGameInstance::Shutdown() void UCMGameInstance::Shutdown()
@ -155,16 +222,40 @@ void UCMGameInstance::OnRequestToCreateLobbyComplete(FHttpRequestPtr Request, FH
const auto JsonPort = JsonObject->GetIntegerField("port"); const auto JsonPort = JsonObject->GetIntegerField("port");
LobbyID = JsonLobbyID; LobbyID = JsonLobbyID;
ServerPort = JsonPort; ServerPort = JsonPort;
OnJoinLobbyComplete.Broadcast(true); OnCreateLobbyComplete.Broadcast(true);
JoinLobby(ServerPort); JoinLobby();
} }
else else
{ {
OnJoinLobbyComplete.Broadcast(false); OnCreateLobbyComplete.Broadcast(false);
} }
} }
else else
{
OnCreateLobbyComplete.Broadcast(false);
}
}
void UCMGameInstance::OnRequestToJoinLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful)
{
OnJoinLobbyComplete.Broadcast(true);
}
else
{ {
OnJoinLobbyComplete.Broadcast(false); OnJoinLobbyComplete.Broadcast(false);
} }
} }
void UCMGameInstance::OnRequestToLeaveLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful)
{
OnLeaveLobbyComplete.Broadcast(true);
}
else
{
OnLeaveLobbyComplete.Broadcast(false);
}
}

View File

@ -21,8 +21,16 @@ public:
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnLogoutComplete, const bool, bWasSuccessful); 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(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") UPROPERTY(BlueprintAssignable, Category = "Account")
FOnLoginComplete OnLoginComplete; FOnLoginComplete OnLoginComplete;
UPROPERTY(BlueprintAssignable, Category = "Account") UPROPERTY(BlueprintAssignable, Category = "Account")
@ -30,7 +38,15 @@ public:
UPROPERTY(BlueprintAssignable, Category = "Account") UPROPERTY(BlueprintAssignable, Category = "Account")
FOnLogoutComplete OnLogoutComplete; FOnLogoutComplete OnLogoutComplete;
UPROPERTY(BlueprintAssignable, Category = "Lobby") UPROPERTY(BlueprintAssignable, Category = "Lobby")
FOnCreateLobbyComplete OnCreateLobbyComplete;
UPROPERTY(BlueprintAssignable, Category = "Lobby")
FOnJoinLobbyComplete OnJoinLobbyComplete; FOnJoinLobbyComplete OnJoinLobbyComplete;
UPROPERTY(BlueprintAssignable, Category = "Lobby")
FOnLeaveLobbyComplete OnLeaveLobbyComplete;
UPROPERTY(BlueprintAssignable, Category = "Lobby")
FOnPlayerJoin OnPlayerJoin;
UPROPERTY(BlueprintAssignable, Category = "Lobby")
FOnPlayerLeave OnPlayerLeave;
UPROPERTY(BlueprintReadOnly, Category = "Account") UPROPERTY(BlueprintReadOnly, Category = "Account")
FString AccountUsername; FString AccountUsername;
@ -53,7 +69,9 @@ public:
UFUNCTION(BlueprintCallable, Category = "Lobby") UFUNCTION(BlueprintCallable, Category = "Lobby")
void CreateLobby(); void CreateLobby();
UFUNCTION(BlueprintCallable, Category = "Lobby") UFUNCTION(BlueprintCallable, Category = "Lobby")
void JoinLobby(const int Port); void JoinLobby();
UFUNCTION(BlueprintCallable, Category = "Lobby")
void LeaveLobby();
virtual void Init() override; virtual void Init() override;
virtual void Shutdown() override; virtual void Shutdown() override;
@ -65,4 +83,6 @@ private:
void OnRequestToRegisterComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) const; void OnRequestToRegisterComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) const;
void OnRequestToLogoutComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); void OnRequestToLogoutComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
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 OnRequestToLeaveLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
}; };