forked from aws-samples/amazon-gamelift-unreal-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OfflineMainMenuWidget.cpp
84 lines (62 loc) · 3.36 KB
/
OfflineMainMenuWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
#include "OfflineMainMenuWidget.h"
#include "Json.h"
#include "JsonUtilities.h"
#include "Kismet/GameplayStatics.h"
UOfflineMainMenuWidget::UOfflineMainMenuWidget(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) {
Http = &FHttpModule::Get();
//TODO update with your API Gateway endpoint
ApiGatewayEndpoint = FString::Printf(TEXT("https://your-endpoint.execute-api.us-west-2.amazonaws.com/test"));
LoginURI = FString::Printf(TEXT("/login"));
StartSessionURI = FString::Printf(TEXT("/startsession"));
}
void UOfflineMainMenuWidget::OnLoginClicked() {
LoginRequest(user, pass);
}
void UOfflineMainMenuWidget::LoginRequest(FString usr, FString pwd) {
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
JsonObject->SetStringField(TEXT("username"), *FString::Printf(TEXT("%s"), *usr));
JsonObject->SetStringField(TEXT("password"), *FString::Printf(TEXT("%s"), *pwd));
FString JsonBody;
TSharedRef<TJsonWriter<TCHAR>> JsonWriter = TJsonWriterFactory<>::Create(&JsonBody);
FJsonSerializer::Serialize(JsonObject.ToSharedRef(), JsonWriter);
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> LoginHttpRequest = Http->CreateRequest();
LoginHttpRequest->SetVerb("POST");
LoginHttpRequest->SetURL(ApiGatewayEndpoint + LoginURI);
LoginHttpRequest->SetHeader("Content-Type", "application/json");
LoginHttpRequest->SetContentAsString(JsonBody);
LoginHttpRequest->OnProcessRequestComplete().BindUObject(this, &UOfflineMainMenuWidget::OnLoginResponse);
LoginHttpRequest->ProcessRequest();
}
void UOfflineMainMenuWidget::OnLoginResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) {
if (bWasSuccessful) {
TSharedPtr<FJsonObject> JsonObject;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
if (FJsonSerializer::Deserialize(Reader, JsonObject)) {
FString IdToken = JsonObject->GetObjectField("tokens")->GetStringField("IdToken");
StartSessionRequest(IdToken);
}
}
}
void UOfflineMainMenuWidget::StartSessionRequest(FString idt) {
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> StartSessionHttpRequest = Http->CreateRequest();
StartSessionHttpRequest->SetVerb("GET");
StartSessionHttpRequest->SetURL(ApiGatewayEndpoint + StartSessionURI);
StartSessionHttpRequest->SetHeader("Content-Type", "application/json");
StartSessionHttpRequest->SetHeader("Authorization", idt);
StartSessionHttpRequest->OnProcessRequestComplete().BindUObject(this, &UOfflineMainMenuWidget::OnStartSessionResponse);
StartSessionHttpRequest->ProcessRequest();
}
void UOfflineMainMenuWidget::OnStartSessionResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) {
if (bWasSuccessful) {
TSharedPtr<FJsonObject> JsonObject;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
if (FJsonSerializer::Deserialize(Reader, JsonObject)) {
FString IpAddress = JsonObject->GetObjectField("PlayerSession")->GetStringField("IpAddress");
FString Port = JsonObject->GetObjectField("PlayerSession")->GetStringField("Port");
FString LevelName = IpAddress + ":" + Port;
UGameplayStatics::OpenLevel(GetWorld(), FName(*LevelName), false);
}
}
}