请求新权限的同意
(这是一个路线图功能,并在API文档中标记为实验性)
在孩子被授予玩游戏的权限后,如果某个功能的权限被禁用,并且managedBy
字段设置为GUARDIAN
,则玩家可以请求该功能的同意。在这种情况下,应调用/session/upgrade
API来创建同意挑战,并显示一个接受父母电子邮件地址、二维码和一次性密码(OTP)的挑战窗口。父母将在家庭门户中收到对该功能的访问请求。由于玩家只是请求启用某个功能,但仍被允许玩游戏,因此一种选择是在请求同意期间,允许通过浮动窗口或小部件继续游戏,该窗口或小部件包含二维码、OTP和电子邮件地址字段。另一种选择是阻止进一步游戏,但允许玩家关闭对话框,而不是寻求父母的同意。
用Unreal C++升级会话
// 如果权限由GUARDIAN管理,则创建新的同意挑战,如果成功,
// 调用EnableFeature回调参数,然后可以在游戏中启用该功能
void UKidWorkflow::UpgradeSession(const FString &FeatureName, TFunction<void()> EnableFeature)
{
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
JsonObject->SetStringField(TEXT("sessionId"), SessionInfo->GetStringField(TEXT("sessionId")));
TSharedPtr<FJsonObject> PermJsonObject = MakeShareable(new FJsonObject());
PermJsonObject->SetStringField(TEXT("name"), FeatureName);
TArray<TSharedPtr<FJsonValue>> JsonArray;
JsonArray.Add(MakeShareable(new FJsonValueObject(PermJsonObject)));
JsonObject->SetArrayField(TEXT("requestedPermissions"), JsonArray);
FString ContentJsonString;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&ContentJsonString);
FJsonSerializer::Serialize(JsonObject.ToSharedRef(), Writer);
FString Url = TEXT("https://api.k-id.com/session/upgrade");
TSharedRef<IHttpRequest> Request = FHttpModule::Get().CreateRequest();
Request->SetURL(Url);
Request->SetVerb(TEXT("POST"));
Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
Request->SetContentAsString(ContentJsonString);
Request->OnProcessRequestComplete().BindLambda([this, EnableFeature, FeatureName](FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful)
{
TSharedPtr<FJsonObject> JsonResponse;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
if (FJsonSerializer::Deserialize(Reader, JsonResponse))
{
if (JsonResponse->HasField(TEXT("challenge")))
{
TSharedPtr<FJsonObject> Challenge = JsonResponse->GetObjectField(TEXT("challenge"));
FString ChallengeId = Challenge->GetStringField(TEXT("challengeId"));
FString OneTimePassword = Challenge->GetStringField(TEXT("oneTimePassword"));
FString QRCodeUrl = Challenge->GetStringField(TEXT("url"));
SaveChallengeId(ChallengeId);
ShowConsentChallenge(ChallengeId, ConsentTimeoutSeconds, OneTimePassword, QRCodeUrl,
[this, EnableFeature, FeatureName](bool bConsentGranted, const FString &SessionId)
{
ClearChallengeId();
if (bConsentGranted)
{
GetSessionPermissions(SessionId, TEXT(""));
EnableFeature();
}
else
{
// 请 求启用功能被拒绝,不执行任何操作
UE_LOG(LogTemp, Warning, TEXT("Feature request denied for feature %s."), *FeatureName);
}
});
}
else if (JsonResponse->HasField(TEXT("session")))
{
SessionInfo = JsonResponse->GetObjectField(TEXT("session"));
SaveSessionInfo();
EnableFeature();
}
}
}
});
}