Skip to main content

Put it all together - Weekly leaderboard - (Unreal Engine module)

Last updated on October 24, 2024

Connect the UI to get weekly periodic leaderboard rankings

  1. Open the LeaderboardWeeklyWidget_Starter class CPP file, navigate to the GetWeeklyRankings() function, and replace the current implementation with the code below. It will get the weekly leaderboard rankings using the PeriodicBoardSubsystem_Starter and then display it upon completion.

    void ULeaderboardWeeklyWidget_Starter::GetWeeklyRankings()
    {
    FUniqueNetIdRepl PlayerNetId = GetOwningPlayer()->GetLocalPlayer()->GetPreferredUniqueNetId();
    if (!PlayerNetId.IsValid())
    {
    return;
    }

    Ws_Leaderboard->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);

    PeriodicLeaderboardSubsystem->GetPeriodicRankings(
    GetOwningPlayer(),
    LeaderboardCode,
    CycleId,
    ResultLimit,
    FOnGetLeaderboardRankingComplete::CreateWeakLambda(this, [this, PlayerNetId](bool bWasSuccessful, const TArray<ULeaderboardRank*> Rankings)
    {
    if (!bWasSuccessful)
    {
    Ws_Leaderboard->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Empty);
    return;
    }

    // Add rankings to the leaderboard weekly ranking list.
    Lv_Leaderboard->SetListItems(Rankings);

    // Get the logged-in player's weekly rank if it is not included in the leaderboard.
    const TArray<ULeaderboardRank*> FilteredRank = Rankings.FilterByPredicate([PlayerNetId](const ULeaderboardRank* Temp) { return Temp && Temp->UserId == PlayerNetId; });
    const ULeaderboardRank* PlayerRank = FilteredRank.IsEmpty() ? nullptr : FilteredRank[0];
    if (!PlayerRank)
    {
    GetPlayerWeeklyRanking();
    }
    // Display the weekly rankings if it is not empty.
    else
    {
    DisplayPlayerWeeklyRank(PlayerRank);
    Ws_Leaderboard->SetWidgetState(
    Lv_Leaderboard->GetNumItems() <= 0 ?
    EAccelByteWarsWidgetSwitcherState::Empty :
    EAccelByteWarsWidgetSwitcherState::Not_Empty);
    }
    }
    ));
    }
  2. Navigate to the GetPlayerWeeklyRanking() function and replace the current implementation with the code below. It will only be called if the currently logged-in player is not included in the rankings retrieved by the GetWeeklyRankings() function.

    void ULeaderboardWeeklyWidget_Starter::GetPlayerWeeklyRanking()
    {
    PeriodicLeaderboardSubsystem->GetPlayerPeriodicRanking(
    GetOwningPlayer(),
    LeaderboardCode,
    CycleId,
    FOnGetLeaderboardRankingComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, const TArray<ULeaderboardRank*> Rankings)
    {
    // Get and display the logged-in player's weekly rank.
    DisplayPlayerWeeklyRank((!bWasSuccessful || Rankings.IsEmpty()) ? nullptr : Rankings[0]);

    // Display the weekly rankings if it is not empty.
    Ws_Leaderboard->SetWidgetState(
    Lv_Leaderboard->GetNumItems() <= 0 ?
    EAccelByteWarsWidgetSwitcherState::Empty :
    EAccelByteWarsWidgetSwitcherState::Not_Empty);
    }
    ));
    }
  3. Modify the NativeOnActivated() by replacing it with the code below. In this code, you pass the leaderboard code based on what you configured in the Admin Portal and pass the statistics cycle name.

    void ULeaderboardWeeklyWidget_Starter::NativeOnActivated()
    {
    // Set leaderboard code based on board-unreal-highestscore-{gamemode} format.
    LeaderboardCode = FString::Printf(TEXT("board-unreal-highestscore-%s"), *ULeaderboardsWidget::GetLeaderboardGameMode());

    // Reset widgets.
    PlayerRankPanel->SetVisibility(ESlateVisibility::Collapsed);
    Lv_Leaderboard->ClearListItems();
    Ws_Leaderboard->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);

    PeriodicLeaderboardSubsystem->GetLeaderboardCycleIdByName(FString("unreal-weekly"), EAccelByteCycle::WEEKLY,
    FOnGetLeaderboardsCycleIdComplete::CreateWeakLambda(this, [this] (bool IsSuccessful, const FString& ResultCycleId)
    {
    if (IsSuccessful && !ResultCycleId.IsEmpty())
    {
    // Set cycle id to the weekly leaderboard's cycle id.
    CycleId = ResultCycleId;

    if (FFTUEDialogueModel* FTUELeaderboard = FFTUEDialogueModel::GetMetadataById("ftue_weekly_leaderboard", FTUEDialogues))
    {
    FTUELeaderboard->Button1.URLArguments[1].Argument = LeaderboardCode;
    FTUELeaderboard->Button1.URLArguments[2].Argument = CycleId;
    }

    Super::NativeOnActivated();

    // Reset widgets.
    PlayerRankPanel->SetVisibility(ESlateVisibility::Collapsed);
    Ws_Leaderboard->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Empty);
    Lv_Leaderboard->ClearListItems();

    // Get leaderboard weekly rankings.
    GetWeeklyRankings();
    }
    else
    {
    // Error if cycle ID is not found
    Ws_Leaderboard->ErrorMessage = FText(CYCLE_ID_NOT_FOUND_TEXT);
    Ws_Leaderboard->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
    }
    })
    );
    }

Resources