void UTestCloudDeploymentMenuWidget::OnGetGameConnectionResponse()

in GameLiftPlugin/Source/GameLiftClient/Private/UI/TestCloudDeploymentMenuWidget.cpp [257:340]


void UTestCloudDeploymentMenuWidget::OnGetGameConnectionResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
#if WITH_GAMELIFT_CLIENT
	if (bWasSuccessful)
	{
		auto& WorldTimerManager = GetWorld()->GetTimerManager();

		auto PollReset = [this, &WorldTimerManager]()
		{
			WorldTimerManager.ClearTimer(PollGameConnectionHandle);
			WorldTimerManager.ClearTimer(PollGameConnectionEndHandle);
			MatchmakingInProgress = false;
			MatchmakingIsTimedOut = false;
		};

		auto ResponseCode = static_cast<ServerHttpStatusCode>(Response->GetResponseCode());

		if (ResponseCode == ServerHttpStatusCode::GetGameConnection_Ready)
		{
			TSharedPtr<FJsonObject> JsonObject;
			TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());

			if (FJsonSerializer::Deserialize(Reader, JsonObject))
			{
				FString IpAddress = JsonObject->GetStringField("IpAddress");
				FString Port = JsonObject->GetStringField("Port");
				const FString& PlayerSessionId = JsonObject->GetStringField("PlayerSessionId");
				const FString& PlayerId = JsonObject->GetStringField("PlayerId");
				const FString& Options = "?PlayerSessionId=" + PlayerSessionId + "?PlayerId=" + PlayerId;

				UE_LOG(TestCloudDeployment, Log, TEXT("Game connection: GameSessionArn '%s', PlayerSessionId '%s', PlayerId '%s'"), *(JsonObject->GetStringField("GameSessionArn")), *PlayerSessionId, *PlayerId);

				if (MatchmakingInProgress)
				{
					PollReset();
				}

				OpenLevel(IpAddress, Port, Options);
			}
		}
		else if (ResponseCode == ServerHttpStatusCode::GetGameConnection_NotFound)
		{
			UE_LOG(TestCloudDeployment, Log, TEXT("No game is found, starting new game..."));
			StartGame(IdToken);
		}
		else if (ResponseCode == ServerHttpStatusCode::GetGameConnection_MatchmakingInProgress)
		{
			UE_LOG(TestCloudDeployment, Log, TEXT("Wating for the other players to join the game..."));

			if (!MatchmakingInProgress)
			{
				MatchmakingInProgress = true;
				MatchmakingIsTimedOut = false;
				WorldTimerManager.SetTimer(PollGameConnectionHandle, this, &UTestCloudDeploymentMenuWidget::PollGameConnection, GetGameConnectionRetryDelayMs / 1000.0f, true);
				WorldTimerManager.SetTimer(PollGameConnectionEndHandle, this, &UTestCloudDeploymentMenuWidget::PollGameConnectionEnd, (float)MatchmakingTimeoutInSecondsParameter, false);
			}
			else
			{
				if (MatchmakingIsTimedOut)
				{
					PollReset();
					LatestError = TEXT("Game connection is timed out. Try to connect again later");
					UE_LOG(TestCloudDeployment, Error, TEXT("%s"), *LatestError);
				}
			}
		}
		else if (ResponseCode == ServerHttpStatusCode::GetGameConnection_NoServerError)
		{
			LatestError = TEXT("Server is not deployed, please use another deployment scenario");
			UE_LOG(TestCloudDeployment, Error, TEXT("%s. Error code: %d"), *LatestError, ResponseCode);
		} 
		else
		{
			LatestError = TEXT("Failed to get game connection");
			UE_LOG(TestCloudDeployment, Error, TEXT("%s. Error code: %d"), *LatestError, ResponseCode);

			if (MatchmakingInProgress)
			{
				PollReset();
			}
		}
	}
#endif
}