Documentation for the Unreal C++ Plugin
Loading...
Searching...
No Matches
ApexReportingManager.cpp
Go to the documentation of this file.
1// Copyright(c) Pixo Group. All Rights Reserved.
2
4#include "Kismet/KismetMathLibrary.h"
10#include "GameFramework/GameModeBase.h"
12
21
23{
24 if (!ApexSubSystem)
25 {
26 return;
27 }
28
29 int32 CompletedStepsCount = 0;
30 int32 TotalNumberOfSteps = 0;
31
32 if (GetWorld()->GetAuthGameMode()->GetClass()->ImplementsInterface(UPixoCoreInterface::StaticClass()))
33 {
34 if (UStoryManager* StoryManagerRef = Cast<UStoryManager>(IPixoCoreInterface::Execute_GetManagerRef(GetWorld()->GetAuthGameMode(), UStoryManager::StaticClass())))
35 {
36 TArray<FPassedExperiences> PassedExperiences = StoryManagerRef->PassedExperiencesArray;
37 CompletedStepsCount = GetAmountOfCompletedSteps(PassedExperiences);
38 for (const auto & PassExpElement : PassedExperiences)
39 {
40 TotalNumberOfSteps += PassExpElement.PassedStepData.Num();
41 }
42 }
43 }
44
45 FSessionData NewSessionData;
46 NewSessionData.Complete = Completed;
47 NewSessionData.Success = Success;
48 NewSessionData.Score = CompletedStepsCount * ScoreFactor;
49 NewSessionData.ScoreScaled = UKismetMathLibrary::SafeDivide((float)CompletedStepsCount, (float)TotalNumberOfSteps);
50 NewSessionData.ScoreMin = 0.0f;
51 NewSessionData.ScoreMax = TotalNumberOfSteps * ScoreFactor;
52 NewSessionData.Duration = GetApexSessionDuration();
53 ApexSubSystem->CompleteSession(NewSessionData);
54}
55
56void UApexReportingManager::EarlyExitSession(float Score, float MaxScore, TMap<FString, FString> ExtensionParameters)
57{
58 if (!ApexSubSystem)
59 {
60 return;
61 }
62
63 FXAPIExtension Extension;
64 TArray<FString> ExtensionParameterKeys;
65 ExtensionParameters.GetKeys(ExtensionParameterKeys);
66 for (auto ExtensionKey : ExtensionParameterKeys)
67 {
68 FString Key = ExtensionKey;
69 FString Value = ExtensionParameters.FindRef(ExtensionKey);
70 Extension.AddString(Key, Value);
71 }
72
73 FSessionData NewSessionData;
74 NewSessionData.Complete = false;
75 NewSessionData.Success = false;
76 NewSessionData.Score = Score;
77 NewSessionData.ScoreScaled = MaxScore > 0.f? Score/MaxScore : 0.f;
78 NewSessionData.ScoreMin = 0.0f;
79 NewSessionData.ScoreMax = MaxScore;
80 NewSessionData.Duration = GetApexSessionDuration();
81 NewSessionData.AdditionalResultData = Extension;
82
83 ApexSubSystem->CompleteSession(NewSessionData);
84}
85
86void UApexReportingManager::OnEndStoryReached_Implementation()
87{
88}
89
90void UApexReportingManager::OnInitialized_Implementation()
91{
92}
93
94FXAPIStatement UApexReportingManager::GetStatementData_Implementation()
95{
96 FXAPIStatement StatementData;
97
98 StatementData.Verb.ID = FString("https://pixovr.com/xapi/verbs/completed");
99 StatementData.Target.ID = FString("https://pixovr.com/xapi/objects/") +
100 FString::FromInt(ApexSubSystem->LoadedModuleId) + FString("/") + CurrentStep.ToString();
101
102 return StatementData;
103}
104
105int32 UApexReportingManager::GetAmountOfCompletedSteps(const TArray<FPassedExperiences> PassedExperiences) const
106{
107 int32 Counter = 0;
108 for (const auto & PassExpElement : PassedExperiences)
109 {
110 for (const auto & Step : PassExpElement.PassedStepData)
111 {
112 if (Step.Step.Completed)
113 {
114 Counter++;
115 }
116 }
117 }
118 return Counter;
119}
120
122{
123 return (UKismetMathLibrary::Now() - ApexStartTime).GetTotalSeconds();
124}
125
127{
128 ApexSubSystem = GetWorld()->GetGameInstance()->GetSubsystem<UApexAPI>();
129 if (ApexSubSystem)
130 {
131 ApexStartTime = UKismetMathLibrary::Now();
133 {
134 const FXAPIExtension Extension;
135 ApexSubSystem->JoinSession(FString(FApp::GetProjectName()), Extension);
136 }
137 }
138 else
139 UE_LOG(LogTemp, Error, TEXT ("Apex SubSystem is not valid"));
140
142}
143
145{
147}
148
150{
152 const UStoryManager* StoryManager = Cast<UStoryManager>(IPixoCoreInterface::Execute_GetManagerRef(GetWorld()->GetAuthGameMode(), UStoryManager::StaticClass()));
153 LoadedStoryIndexesHistory.AddUnique(StoryManager->StoryIndex);
154}
155
157{
159 {
160 if (ApexSubSystem)
161 {
162 ApexSubSystem->SendSessionEvent(GetStatementData());
163 }
164 else
165 UE_LOG(LogTemp, Error, TEXT("ApexSubSystem reference is not valid!"));
166 }
167}
168
170{
172 {
173 CompleteApexSession(true, true);
174 }
176}
#define DEFINE_EVENT_HANDLER(TEventType)
Definition BaseManager.h:13
UApexAPI * ApexSubSystem
UPROPERTY(BlueprintReadOnly)
FXAPIStatement GetStatementData()
This function can be implemented in blueprint to send custom FXAPIStatement.
virtual void CompleteApexSession(bool Completed, bool Success)
This function is responsible for completing the session.
bool UseReportingEachStep
Indicates whether reporting should occur after completing each step.
int32 GetApexSessionDuration()
Retrieves the duration of the Apex session.
void OnInitialized()
UFUNCTION(BlueprintNativeEvent)
FText CurrentStep
UPROPERTY(BlueprintReadOnly)
int32 ScoreFactor
The value by which the score reported in 'CompleteApexSession' will be multiplied.
void OnEndStoryReached()
UFUNCTION(BlueprintNativeEvent)
virtual void EarlyExitSession(float Score, float MaxScore, TMap< FString, FString > ExtensionParameters)
This function is responsible for quitting the session without completing it.
TArray< int32 > LoadedStoryIndexesHistory
Array storing the history of loaded story indexes.
bool UseDefaultSessionEnd
Indicates whether the default session end should be used on UEndOfStoryReached.
int32 GetAmountOfCompletedSteps(const TArray< FPassedExperiences > PassedExperiences) const
bool UseDefaultSessionStart
Indicates whether the default session start should be used on UInitializationEvent.
void HandleEvent_Impl(class UBaseEvent *Event)
Event called when we complete last step.
Event called when we on init experience.
Event called when new step is loaded.
Event called when new story is loaded.
FStoryStep CurrentStep
Event called when step is completed.
Manager that provides handling story-related operations and data.
int StoryIndex
UPROPERTY(BlueprintReadWrite, Replicated)
FText StepName
UPROPERTY(BlueprintReadWrite, EditAnywhere)