Skip to content

Commit ada60b7

Browse files
committed
Add http request error
1 parent 0f8b3ef commit ada60b7

File tree

8 files changed

+75
-3
lines changed

8 files changed

+75
-3
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ To demonstrate the Unreal SDK capabilities the game can trigger different types
66

77
In `Project Settings > Plugins > Sentry` set `DSN` and configure [Debug Symbols Uploading](https://docs.sentry.io/platforms/unreal/configuration/debug-symbols/)
88

9-
## In-game crashes
9+
## In-game crashes/errors
1010

1111
### Null pointer dereference
1212

@@ -54,6 +54,10 @@ void HeavyComputeLoop(uint3 DispatchThreadId : SV_DispatchThreadID, uint GroupIn
5454

5555
In order to be able to capture GPU crashes game has to be built with Unreal Engine version that addops this [change](https://github.com/EpicGames/UnrealEngine/pull/12648).
5656

57+
### HTTP request error
58+
59+
Open the in-game shop (button with a cart icon in the upper-right corner of the screen) to purchase the tower upgrade. After clicking the `Upgrade Tower` button the game will send an HTTP request to a dummy server which is expected to return an error. Also, this will trigger the `ensure` check in the corresponding request handler to fail (see `USentryTowerGameInstance::BuyUpgrade` for more details).
60+
5761
## Breadcrumbs
5862

5963
The game automatically adds breadcrumbs whenever a player levels up, a tower's attack modifier is changed, or the game is restarted from the "Game Over" popup.

Content/Maps/Main.umap

38.7 KB
Binary file not shown.

Content/Textures/T_Cart.uasset

13.5 KB
Binary file not shown.

Content/UI/W_GameShop.uasset

123 KB
Binary file not shown.

Content/UI/W_HUD.uasset

29.4 KB
Binary file not shown.

Source/SentryTower/SentryTower.Build.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public SentryTower(ReadOnlyTargetRules Target) : base(Target)
88
{
99
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
1010

11-
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "AIModule", "EnhancedInput", "UMG", "Sentry" });
11+
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "AIModule", "EnhancedInput", "UMG", "Sentry", "HTTP", "Json", "JsonUtilities" });
1212

1313
if (Target.Platform == UnrealTargetPlatform.Win64)
1414
{

Source/SentryTower/SentryTowerGameInstance.cpp

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,70 @@
22

33
#include "SentryTowerGameInstance.h"
44

5+
#include "HttpModule.h"
6+
#include "SentrySpan.h"
7+
#include "SentrySubsystem.h"
8+
#include "SentryTransaction.h"
9+
#include "Interfaces/IHttpResponse.h"
10+
511
void USentryTowerGameInstance::Init()
612
{
713
Super::Init();
8-
}
14+
}
15+
16+
void USentryTowerGameInstance::BuyUpgrade(const FOnBuyComplete& OnBuyComplete)
17+
{
18+
USentrySubsystem* Sentry = GEngine->GetEngineSubsystem<USentrySubsystem>();
19+
20+
USentryTransaction* CheckoutTransaction = Sentry->StartTransaction(TEXT("checkout"), TEXT("http.client"));
21+
22+
USentrySpan* ProcessSpan = CheckoutTransaction->StartChildSpan(TEXT("task"), TEXT("process_upgrade_data"));
23+
24+
TSharedPtr<FJsonObject> UpgradeDataJsonObject = MakeShareable(new FJsonObject());
25+
UpgradeDataJsonObject->SetStringField(TEXT("UpgradeName"), TEXT("NewTower"));
26+
UpgradeDataJsonObject->SetStringField(TEXT("PlayerEmail"), TEXT("player@sentry-tower.com"));
27+
28+
FString JsonString;
29+
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&JsonString);
30+
if (!FJsonSerializer::Serialize(UpgradeDataJsonObject.ToSharedRef(), Writer))
31+
{
32+
UE_LOG(LogTemp, Error, TEXT("Failed to serialize JSON object"));
33+
}
34+
35+
FPlatformProcess::Sleep(0.1f);
36+
37+
ProcessSpan->Finish();
38+
39+
FString Domain = TEXT("https://aspnetcore.empower-plant.com");
40+
FString Endpoint = TEXT("/checkout");
41+
FString CheckoutURL = Domain + Endpoint;
42+
43+
FHttpModule* Http = &FHttpModule::Get();
44+
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = Http->CreateRequest();
45+
46+
HttpRequest->SetURL(CheckoutURL);
47+
HttpRequest->SetVerb("POST");
48+
HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
49+
50+
HttpRequest->SetContentAsString(JsonString);
51+
52+
HttpRequest->OnProcessRequestComplete().BindLambda([=](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
53+
{
54+
ensureMsgf(bWasSuccessful && Response.IsValid() && Response->GetResponseCode() == 200, TEXT("Checkout HTTP request failed"));
55+
56+
if (bWasSuccessful && Response.IsValid() && Response->GetResponseCode() == 200)
57+
{
58+
UE_LOG(LogTemp, Log, TEXT("Checkout completed"));
59+
OnBuyComplete.ExecuteIfBound(true);
60+
}
61+
else
62+
{
63+
UE_LOG(LogTemp, Error, TEXT("Checkout failed"));
64+
OnBuyComplete.ExecuteIfBound(false);
65+
}
66+
67+
CheckoutTransaction->Finish();
68+
});
69+
70+
HttpRequest->ProcessRequest();
71+
}

Source/SentryTower/SentryTowerGameInstance.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
#include "Engine/GameInstance.h"
77
#include "SentryTowerGameInstance.generated.h"
88

9+
DECLARE_DYNAMIC_DELEGATE_OneParam(FOnBuyComplete, bool, IsSuccessfull);
10+
911
UCLASS()
1012
class SENTRYTOWER_API USentryTowerGameInstance : public UGameInstance
1113
{
1214
GENERATED_BODY()
1315

1416
public:
1517
virtual void Init() override;
18+
19+
UFUNCTION(BlueprintCallable, Meta = (AutoCreateRefTerm = "OnBuyComplete"))
20+
void BuyUpgrade(const FOnBuyComplete& OnBuyComplete);
1621
};

0 commit comments

Comments
 (0)