Update Game Instance for Getting Lobby Players
This commit is contained in:
parent
10d39a80b6
commit
e50a8addaf
@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$USER_HOME$/Downloads/Ue5/UnrealEngine" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:14f450331c03198d77da600afab5083c2a67a4a29c42eefa392499a8785fc5e7
|
||||
size 45651
|
||||
oid sha256:237937226f0528cf5b214af84fca969814ebd5a9bb0ace21f9619ae505084f7e
|
||||
size 42837
|
||||
|
@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:c04c195257bcdbae6fc5dac85dbc71078434e04fd0a0ec60811a22509f9c5517
|
||||
size 113176
|
||||
oid sha256:44980cb55dcf0f576a61c7cb15800fc9b3fbe722500af8f796855a2a7befe79b
|
||||
size 120991
|
||||
|
@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:9c065c47cb680b72dbb951d997957674b88bd1fc79813ad11e8fa86b0049269c
|
||||
size 593178
|
||||
oid sha256:9252a90a0432d8c2c3759c93104ce14780763bb53e4d09a23c7148ea40fef6dc
|
||||
size 851581
|
||||
|
@ -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<FJsonObject> 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<FJsonObject> 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<FJsonValue>& Message)
|
||||
{
|
||||
UE_LOG(LogTemp, Log, TEXT("Received: %s"), *USIOJConvert::ToJsonString(Message));
|
||||
});
|
||||
|
||||
Socket->OnEvent("join", [this](const FString& Event, const TSharedPtr<FJsonValue>& Message)
|
||||
{
|
||||
@ -149,6 +157,20 @@ void UCMGameInstance::Init()
|
||||
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()
|
||||
@ -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,6 +260,17 @@ void UCMGameInstance::OnRequestToJoinLobbyComplete(FHttpRequestPtr Request, FHtt
|
||||
{
|
||||
if (bWasSuccessful)
|
||||
{
|
||||
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
|
||||
@ -247,11 +278,20 @@ void UCMGameInstance::OnRequestToJoinLobbyComplete(FHttpRequestPtr Request, FHtt
|
||||
OnJoinLobbyComplete.Broadcast(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
OnJoinLobbyComplete.Broadcast(false);
|
||||
}
|
||||
}
|
||||
|
||||
void UCMGameInstance::OnRequestToLeaveLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool 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);
|
||||
}
|
||||
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<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);
|
||||
}
|
||||
}
|
||||
|
@ -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<FPlayerInLobby> 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);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user