Added overload of Leaderboard.FetchScores that takes a callback

This commit is contained in:
James King 2017-05-15 13:23:19 +01:00
parent 8fa8da4939
commit d3ebe4e2f2
2 changed files with 97 additions and 15 deletions

View File

@ -51,6 +51,53 @@ namespace Facepunch.Steamworks.Test
}
}
[TestMethod]
public void GetLeaderboardCallback()
{
using ( var client = new Steamworks.Client( 252490 ) )
{
var board = client.GetLeaderboard( "TestLeaderboard", Steamworks.Client.LeaderboardSortMethod.Ascending, Steamworks.Client.LeaderboardDisplayType.Numeric );
while ( !board.IsValid )
{
Thread.Sleep( 10 );
client.Update();
}
Assert.IsTrue( board.IsValid );
Assert.IsFalse( board.IsError );
Assert.IsNotNull( board.Name );
Console.WriteLine( $"Board name is \"{board.Name}\"" );
Console.WriteLine( $"Board has \"{board.TotalEntries}\" entries" );
board.AddScore( true, 86275309, 7, 8, 9 );
var done = false;
board.FetchScores( Steamworks.Leaderboard.RequestType.Global, 0, 20, ( success, results ) =>
{
Assert.IsTrue( success );
foreach ( var entry in board.Results )
{
Console.WriteLine( $"{entry.GlobalRank}: {entry.SteamId} ({entry.Name}) with {entry.Score}" );
if ( entry.SubScores != null )
Console.WriteLine( " - " + string.Join( ";", entry.SubScores.Select( x => x.ToString() ).ToArray() ) );
}
done = true;
} );
while ( !done )
{
Thread.Sleep( 10 );
client.Update();
}
}
}
[TestMethod]
public void AddScores()
{

View File

@ -125,6 +125,55 @@ namespace Facepunch.Steamworks
return true;
}
private unsafe void ReadScores( LeaderboardScoresDownloaded_t result, List<Entry> dest )
{
for ( var i = 0; i < result.CEntryCount; i++ )
fixed ( int* ptr = subEntriesBuffer )
{
var entry = new LeaderboardEntry_t();
if ( client.native.userstats.GetDownloadedLeaderboardEntry( result.SteamLeaderboardEntries, i, ref entry, (IntPtr) ptr, subEntriesBuffer.Length ) )
dest.Add( new Entry
{
GlobalRank = entry.GlobalRank,
Score = entry.Score,
SteamId = entry.SteamIDUser,
SubScores = entry.CDetails == 0 ? null : subEntriesBuffer.Take( entry.CDetails ).ToArray(),
Name = client.Friends.GetName( entry.SteamIDUser )
} );
}
}
[ThreadStatic] private static List<Entry> _sEntryBuffer;
public delegate void FetchScoresCallback( bool success, Entry[] results );
/// <summary>
/// Fetch a subset of scores. The scores are passed to <paramref name="callback"/>.
/// </summary>
/// <returns>Returns true if we have started the query</returns>
public bool FetchScores( RequestType RequestType, int start, int end, FetchScoresCallback callback )
{
if ( !IsValid ) return false;
client.native.userstats.DownloadLeaderboardEntries( BoardId, (LeaderboardDataRequest) RequestType, start, end, ( result, error ) =>
{
if ( error )
{
callback( false, null );
}
else
{
if ( _sEntryBuffer == null ) _sEntryBuffer = new List<Entry>();
else _sEntryBuffer.Clear();
ReadScores( result, _sEntryBuffer );
callback( true, _sEntryBuffer.ToArray() );
}
} );
return true;
}
private unsafe void OnScores( LeaderboardScoresDownloaded_t result, bool error )
{
IsQuerying = false;
@ -135,21 +184,7 @@ namespace Facepunch.Steamworks
return;
var list = new List<Entry>();
for ( var i = 0; i < result.CEntryCount; i++ )
fixed ( int* ptr = subEntriesBuffer )
{
var entry = new LeaderboardEntry_t();
if ( client.native.userstats.GetDownloadedLeaderboardEntry( result.SteamLeaderboardEntries, i, ref entry, (IntPtr) ptr, subEntriesBuffer.Length ) )
list.Add( new Entry
{
GlobalRank = entry.GlobalRank,
Score = entry.Score,
SteamId = entry.SteamIDUser,
SubScores = entry.CDetails == 0 ? null : subEntriesBuffer.Take( entry.CDetails ).ToArray(),
Name = client.Friends.GetName( entry.SteamIDUser )
} );
}
ReadScores( result, list );
Results = list.ToArray();
}