Advertisement
BojanNovakovic

Untitled

May 31st, 2023
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. void UEosHelper::SubmitLeaderboardScore(APlayerController* PlayerController, FName StatName, int32 StatValue)
  2. {
  3.     UE_LOG(LogTemp, Warning, TEXT("Submitting score for %s. Value: %d"), *StatName.ToString(), StatValue);
  4.     FOnlineLeaderboardWrite WriteObject;
  5.     WriteObject.LeaderboardNames.Add(StatName);
  6.     WriteObject.RatedStat = StatName;
  7.     WriteObject.DisplayFormat = ELeaderboardFormat::Milliseconds;
  8.     WriteObject.SortMethod = ELeaderboardSort::Ascending;
  9.     WriteObject.UpdateMethod = ELeaderboardUpdateMethod::KeepBest;
  10.     WriteObject.SetIntStat(StatName, StatValue);
  11.     bool Successful = DoWriteLeaderboardObject(PlayerController, WriteObject);
  12. }
  13.  
  14. void UEosHelper::QueryTopPlayersLeaderboard(FName LeaderboardName, int32 Range)
  15. {
  16.     if (IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::IsLoaded() ? IOnlineSubsystem::Get() : nullptr)
  17.     {
  18.         IOnlineLeaderboardsPtr Leaderboards = OnlineSub->GetLeaderboardsInterface();
  19.  
  20.         if (Leaderboards.IsValid())
  21.         {
  22.             FOnlineLeaderboardReadRef ReadRef = MakeShared<FOnlineLeaderboardRead, ESPMode::ThreadSafe>();
  23.             ReadRef->LeaderboardName = FName(LeaderboardName);
  24.             ReadRef->SortedColumn = LeaderboardName;
  25.             new (ReadRef->ColumnMetadata) FColumnMetaData(LeaderboardName, EOnlineKeyValuePairDataType::Int32);
  26.            
  27.             Leaderboards->AddOnLeaderboardReadCompleteDelegate_Handle(
  28.                 FOnLeaderboardReadComplete::FDelegate::CreateUObject(
  29.                     this,
  30.                     &UEosHelper::TopLeaderboardReadComplete,
  31.                     ReadRef));
  32.             Leaderboards->ReadLeaderboardsAroundRank(1, Range, ReadRef);
  33.         }
  34.     }
  35. }
  36.  
  37. void UEosHelper::QueryLeaderboardAroundPlayer(FName LeaderboardName, const APlayerController* PlayerController, int32 Range)
  38. {
  39.     if (APlayerState* PlayerState = (PlayerController != NULL) ? PlayerController->PlayerState : NULL)
  40.     {
  41.         FUniqueNetIdPtr LocalUserNetId = PlayerState->GetUniqueId().GetUniqueNetId();
  42.         if (LocalUserNetId.IsValid())
  43.         {
  44.             if (IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::IsLoaded() ? IOnlineSubsystem::Get() : nullptr)
  45.             {
  46.                 IOnlineLeaderboardsPtr Leaderboards = OnlineSub->GetLeaderboardsInterface();
  47.  
  48.                 if (Leaderboards.IsValid())
  49.                 {
  50.                     FOnlineLeaderboardReadRef ReadRef = MakeShared<FOnlineLeaderboardRead, ESPMode::ThreadSafe>();
  51.                     ReadRef->LeaderboardName = FName(LeaderboardName);
  52.                     Leaderboards->AddOnLeaderboardReadCompleteDelegate_Handle(
  53.                         FOnLeaderboardReadComplete::FDelegate::CreateUObject(
  54.                             this,
  55.                             &UEosHelper::TopLeaderboardReadComplete,
  56.                             ReadRef));
  57.                     Leaderboards->ReadLeaderboardsAroundUser(LocalUserNetId.ToSharedRef(), Range, ReadRef);
  58.                 }
  59.             }
  60.         }
  61.     }
  62. }
  63.  
  64. void UEosHelper::TopLeaderboardReadComplete(bool bWasSuccessful, FOnlineLeaderboardReadRef ReadRef)
  65. {
  66.     TArray<FLeaderboardUiData> LeaderboardUiData;
  67.     ClearTopLeaderboardReadDelegates();
  68.     if (bWasSuccessful)
  69.     {
  70.         for (const FOnlineStatsRow& Row: ReadRef->Rows)
  71.         {
  72.             const FVariantData* Variant = Row.Columns.Find(ReadRef->LeaderboardName);
  73.             int32 Value = 0;
  74.             if (Variant != nullptr)
  75.             {
  76.                 // Get player rank
  77.                 Variant->GetValue(Value);
  78.  
  79.                 // Create list item
  80.                 FLeaderboardUiData DataToWrite;
  81.                 DataToWrite.Rank = Row.Rank;
  82.                 FUniqueNetIdPtr PlayerId = Row.PlayerId;
  83.                 if (PlayerId != nullptr)
  84.                 {
  85.                     DataToWrite.PlayerId = PlayerId->ToString();
  86.                 }
  87.                 DataToWrite.PlayerName = Row.NickName;
  88.                 DataToWrite.PlayerScore = Value;
  89.  
  90.                 // Add to list
  91.                 LeaderboardUiData.Add(DataToWrite);
  92.             } else
  93.             {
  94.                 UE_LOG(LogTemp, Warning, TEXT("Leaderboard value is not found for Leaderboard %s"), *ReadRef->LeaderboardName.ToString());             
  95.             }
  96.         }
  97.     }
  98.     LeaderboardLoadComplete.Broadcast(LeaderboardUiData);
  99. }
  100.  
  101. void UEosHelper::ClearTopLeaderboardReadDelegates()
  102. {
  103.     if (IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::IsLoaded() ? IOnlineSubsystem::Get() : nullptr)
  104.     {
  105.         IOnlineLeaderboardsPtr Leaderboards = OnlineSub->GetLeaderboardsInterface();
  106.  
  107.         if (Leaderboards.IsValid())
  108.         {
  109.             Leaderboards->ClearOnLeaderboardReadCompleteDelegates(this);
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement