Skip to content

Commit 85c30a9

Browse files
committed
added registartion screen, and termination of game session when there is zero players
1 parent 050286b commit 85c30a9

13 files changed

Lines changed: 228 additions & 10 deletions

Source/ActionRPGGame/Private/ARGameInstance.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ void UARGameInstance::AttemptLogin(const FString& UserName, const FString& Passw
7373

7474
}
7575

76+
void UARGameInstance::RegisterNewPlayer(const FString& UserName, const FString& DisplayName, const FString& Password)
77+
{
78+
#if GAMESPARKS_CLIENT
79+
GameSparks::Core::GS& gs = UGameSparksModule::GetModulePtr()->GetGSInstance();
80+
GameSparks::Api::Requests::RegistrationRequest regRequest(gs);
81+
regRequest.SetUserName(TCHAR_TO_UTF8(*UserName));
82+
regRequest.SetDisplayName(TCHAR_TO_UTF8(*DisplayName));
83+
regRequest.SetPassword(TCHAR_TO_UTF8(*DisplayName));
84+
85+
auto RegRequestResponse = [&](GameSparks::Core::GS&, const GameSparks::Api::Responses::RegistrationResponse& Response)
86+
{
87+
GEngine->AddOnScreenDebugMessage(-1, 20.f, FColor::Red, Response.GetJSONString().c_str());
88+
};
89+
regRequest.Send(RegRequestResponse);
90+
#endif
91+
}
92+
7693
void UARGameInstance::Init()
7794
{
7895
Super::Init();

Source/ActionRPGGame/Private/ARGameMode.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Abilities/ARAbilityBase.h"
99
#include "ARGameInstance.h"
1010
#include "SDraggableWindowWidget.h"
11+
#include "ARPlayerController.h"
1112

1213

1314

@@ -19,4 +20,33 @@ void AARGameMode::BeginPlay()
1920
{
2021
Super::BeginPlay();
2122

23+
}
24+
25+
FString AARGameMode::InitNewPlayer(APlayerController* NewPlayerController
26+
, const FUniqueNetIdRepl& UniqueId
27+
, const FString& Options
28+
, const FString& Portal)
29+
{
30+
FString ReturnString = Super::InitNewPlayer(NewPlayerController, UniqueId, Options, Portal);
31+
32+
TArray<FString> OutParams;
33+
Options.ParseIntoArray(OutParams, TEXT("?"));
34+
35+
TArray<FString> OutPlayerId;
36+
FString Left;
37+
FString PlayerId;
38+
for (FString& str : OutParams)
39+
{
40+
if (str.Split("PlayerId=", &Left, &PlayerId))
41+
{
42+
break;
43+
}
44+
}
45+
46+
if (AARPlayerController* PC = Cast<AARPlayerController>(NewPlayerController))
47+
{
48+
PC->GameLiftId = PlayerId;
49+
}
50+
51+
return ReturnString;
2252
}

Source/ActionRPGGame/Private/ARGameSession.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Fill out your copyright notice in the Description page of Project Settings.
22

33
#include "ARGameSession.h"
4-
5-
4+
#include "ARGameInstance.h"
5+
#include "ARGameMode.h"
6+
#include "ARPlayerController.h"
67

78

89
FString AARGameSession::ApproveLogin(const FString& Options)
@@ -12,9 +13,6 @@ FString AARGameSession::ApproveLogin(const FString& Options)
1213
TArray<FString> OutParams;
1314
Options.ParseIntoArray(OutParams, TEXT("?"));
1415

15-
int32 Idx = Options.Find("PlayerId=");
16-
int32 KeyIdx = Idx + 9;
17-
1816
TArray<FString> OutPlayerId;
1917
FString Left;
2018
FString PlayerId;
@@ -37,4 +35,33 @@ FString AARGameSession::ApproveLogin(const FString& Options)
3735

3836

3937
return Output;
38+
}
39+
40+
void AARGameSession::UnregisterPlayer(const APlayerController* ExitingPlayer)
41+
{
42+
#if LINUX_GAMELIFT
43+
if (const AARPlayerController* PC = Cast<AARPlayerController>(ExitingPlayer))
44+
{
45+
FString GLId = PC->GameLiftId;
46+
FGameLiftServerSDKModule* gameLiftSdkModule = &FModuleManager::LoadModuleChecked<FGameLiftServerSDKModule>(FName("GameLiftServerSDK"));
47+
FGameLiftGenericOutcome out = gameLiftSdkModule->RemovePlayerSession(GLId);
48+
49+
FString ErrorName = out.GetError().m_errorName;
50+
FString ErrorMessage = out.GetError().m_errorMessage;
51+
}
52+
#endif
53+
54+
Super::UnregisterPlayer(ExitingPlayer);
55+
56+
/*AARGameMode* GM = Cast<AARGameMode>(GetOuter());
57+
int32 NumPlayer = GM->GetNumPlayers();
58+
if (NumPlayer <= 0)
59+
{
60+
#if LINUX_GAMELIFT
61+
FString GLId = PC->GameLiftId;
62+
FGameLiftServerSDKModule* gameLiftSdkModule = &FModuleManager::LoadModuleChecked<FGameLiftServerSDKModule>(FName("GameLiftServerSDK"));
63+
gameLiftSdkModule->TerminateGameSession();
64+
#endif
65+
}*/
66+
4067
}

Source/ActionRPGGame/Private/Menu/ARMenuHUD.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "UI/Menu/ARLoginScreenView.h"
66
#include "ARGameInstance.h"
77
#include "ARMainMenuView.h"
8+
#include "ARRegisterView.h"
89

910

1011
void AARMenuHUD::BeginPlay()
@@ -27,6 +28,14 @@ void AARMenuHUD::BeginPlay()
2728
MainMenuScreen->SetVisibility(ESlateVisibility::Collapsed);
2829
}
2930

31+
if (RegisterViewClass)
32+
{
33+
RegisterView = CreateWidget<UARRegisterView>(PC, RegisterViewClass);
34+
RegisterView->AddToViewport();
35+
36+
RegisterView->SetVisibility(ESlateVisibility::Collapsed);
37+
}
38+
3039
if (UARGameInstance* GI = Cast<UARGameInstance>(PC->GetGameInstance()))
3140
{
3241
GI->OnLoginSuccess.AddDynamic(this, &AARMenuHUD::OnLoginSuccess);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Fill out your copyright notice in the Description page of Project Settings.
2+
3+
#include "ARRegisterView.h"
4+
#include "ARGameInstance.h"
5+
6+
7+
8+
void UARRegisterView::NativeConstruct()
9+
{
10+
Super::NativeConstruct();
11+
12+
RegisterButton->OnClicked.AddDynamic(this, &UARRegisterView::OnRegisterClicked);
13+
}
14+
15+
16+
void UARRegisterView::OnRegisterClicked()
17+
{
18+
FString UserName = UserNameBox->GetText().ToString();
19+
FString DisplayName = DisplayNameBox->GetText().ToString();
20+
FString Password = PasswordBox->GetText().ToString();
21+
22+
if (UserName.Len() <= 0)
23+
{
24+
WarrningText->SetText(FText::FromString("Invalid Username"));
25+
return;
26+
}
27+
if (DisplayName.Len() <= 0)
28+
{
29+
WarrningText->SetText(FText::FromString("Invalid DisplayName"));
30+
return;
31+
}
32+
if (Password.Len() <= 0)
33+
{
34+
WarrningText->SetText(FText::FromString("Invalid Password"));
35+
return;
36+
}
37+
38+
if (UARGameInstance* GI = Cast<UARGameInstance>(GetOwningPlayer()->GetGameInstance()))
39+
{
40+
RegisterButton->SetVisibility(ESlateVisibility::HitTestInvisible);
41+
GI->RegisterNewPlayer(UserName, DisplayName, Password);
42+
}
43+
}
44+
45+
void UARRegisterView::OnRegisterSuccess()
46+
{
47+
48+
}
49+
50+
void UARRegisterView::OnRegisterFailed()
51+
{
52+
53+
}

Source/ActionRPGGame/Private/UI/Menu/ARLoginScreenView.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
#include "ARLoginScreenView.h"
44
#include "ARGameInstance.h"
5-
5+
#include "Menu/ARRegisterView.h"
6+
#include "ARMenuHUD.h"
67

78

89
void UARLoginScreenView::NativeConstruct()
910
{
1011
Super::NativeConstruct();
1112

1213
LoginButton->OnClicked.AddDynamic(this, &UARLoginScreenView::OnLoginClicked);
14+
RegisterButton->OnClicked.AddDynamic(this, &UARLoginScreenView::OnRegisterClicked);
1315
}
1416

1517
void UARLoginScreenView::OnLoginClicked()
@@ -47,4 +49,16 @@ void UARLoginScreenView::OnLoginSuccess()
4749
void UARLoginScreenView::OnLoginFailed()
4850
{
4951

52+
}
53+
54+
void UARLoginScreenView::OnRegisterClicked()
55+
{
56+
if (!GetOwningPlayer())
57+
return;
58+
59+
AARMenuHUD* Hud = Cast<AARMenuHUD>(GetOwningPlayer()->GetHUD());
60+
if (!Hud)
61+
return;
62+
63+
Hud->GetRegisterView()->SetVisibility(ESlateVisibility::Visible);
5064
}

Source/ActionRPGGame/Public/ARGameInstance.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class ACTIONRPGGAME_API UARGameInstance : public UGameInstance
9898

9999

100100
void AttemptLogin(const FString& UserName, const FString& Password);
101+
void RegisterNewPlayer(const FString& UserName, const FString& DisplayName, const FString& Password);
101102
//Function used to determine what happens if GameSparks connects or fails to (Needs to be UFUNCTION)
102103
UFUNCTION()
103104
void OnGameSparksAvailable(bool bAvailable);
@@ -116,12 +117,11 @@ class ACTIONRPGGAME_API UARGameInstance : public UGameInstance
116117
void OnGameSessionStarted(Aws::GameLift::Server::Model::GameSession InGameSession);
117118
#endif
118119

119-
120120
UFUNCTION(BlueprintCallable, Category = "GameLift|Test")
121121
void ConnectToHub();
122-
123122
UFUNCTION(BlueprintCallable, Category = "GameLift|Test")
124123
void ConnectToWorld();
124+
125125
#if GAMELIFT_CLIENT
126126
void OnSessionsSearched(const Aws::GameLift::GameLiftClient* Client, const Aws::GameLift::Model::SearchGameSessionsRequest& Request, const Aws::GameLift::Model::SearchGameSessionsOutcome& Outcome, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& Context);
127127
void OnSessionCreated(const Aws::GameLift::GameLiftClient* Client, const Aws::GameLift::Model::CreateGameSessionRequest& Request, const Aws::GameLift::Model::CreateGameSessionOutcome& Outcome, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& Context);

Source/ActionRPGGame/Public/ARGameMode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class AARGameMode : public AGameModeBase
1414
AARGameMode();
1515

1616
virtual void BeginPlay() override;
17+
18+
virtual FString InitNewPlayer(APlayerController* NewPlayerController, const FUniqueNetIdRepl& UniqueId, const FString& Options, const FString& Portal = TEXT("")) override;
1719
};
1820

1921

Source/ActionRPGGame/Public/ARGameSession.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ class ACTIONRPGGAME_API AARGameSession : public AGameSession
2626
GENERATED_BODY()
2727

2828
virtual FString ApproveLogin(const FString& Options) override;
29-
29+
virtual void UnregisterPlayer(const APlayerController* ExitingPlayer) override;
3030

3131
};

Source/ActionRPGGame/Public/ARPlayerController.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class ACTIONRPGGAME_API AARPlayerController : public APlayerController, public I
5252
TSoftClassPtr<UGAAbilityBase> SetAbilityGroup02;
5353

5454
bool bInputBount;
55+
56+
UPROPERTY()
57+
FString GameLiftId;
5558
public:
5659
AARPlayerController(const FObjectInitializer& ObjectInitializer);
5760
virtual void BeginPlay() override;

0 commit comments

Comments
 (0)