Added Leaderboard.AddScore overload that accepts a callback

This commit is contained in:
James King 2017-05-15 14:05:36 +01:00
parent d3ebe4e2f2
commit 906cff4416
2 changed files with 96 additions and 10 deletions

View File

@ -68,9 +68,6 @@ public void GetLeaderboardCallback()
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;
@ -79,7 +76,7 @@ public void GetLeaderboardCallback()
{
Assert.IsTrue( success );
foreach ( var entry in board.Results )
foreach ( var entry in results )
{
Console.WriteLine( $"{entry.GlobalRank}: {entry.SteamId} ({entry.Name}) with {entry.Score}" );
@ -135,5 +132,40 @@ public void AddScores()
client.Update();
}
}
[TestMethod]
public void AddScoresCallback()
{
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 );
var done = false;
const int score = 5678;
board.AddScore( false, score, null, ( success, result ) =>
{
Assert.IsTrue( success );
Assert.IsTrue( result.ScoreChanged );
Assert.AreEqual( result.Score, score );
} );
while ( !done )
{
Thread.Sleep( 10 );
client.Update();
}
}
}
}
}

View File

@ -99,14 +99,66 @@ internal void OnBoardCreated( LeaderboardFindResult_t result, bool error )
/// If onlyIfBeatsOldScore is true, the score will only be updated if it beats the existing score, else it will always
/// be updated.
/// </summary>
public void AddScore( bool onlyIfBeatsOldScore, int score, params int[] subscores )
public bool AddScore( bool onlyIfBeatsOldScore, int score, params int[] subscores )
{
if ( !IsValid ) return;
if ( !IsValid ) return false;
var flags = LeaderboardUploadScoreMethod.ForceUpdate;
if ( onlyIfBeatsOldScore ) flags = LeaderboardUploadScoreMethod.KeepBest;
client.native.userstats.UploadLeaderboardScore( BoardId, flags, score, subscores, subscores.Length );
return true;
}
/// <summary>
/// Callback invoked by <see cref="AddScore(bool, int, int[], AddScoreCallback)"/> when score submission
/// is complete.
/// </summary>
/// <param name="success">If true, the score was submitted</param>
/// <param name="result">If successful, information about the new entry</param>
public delegate void AddScoreCallback( bool success, AddScoreResult result );
/// <summary>
/// Information about a newly submitted score.
/// </summary>
public struct AddScoreResult
{
public int Score;
public bool ScoreChanged;
public int GlobalRankNew;
public int GlobalRankPrevious;
}
/// <summary>
/// Add a score to this leaderboard.
/// Subscores are totally optional, and can be used for other game defined data such as laps etc.. although
/// they have no bearing on sorting at all
/// If onlyIfBeatsOldScore is true, the score will only be updated if it beats the existing score, else it will always
/// be updated.
/// Information about the newly submitted score is passed to the optional <paramref name="callback"/>.
/// </summary>
public bool AddScore( bool onlyIfBeatsOldScore, int score, int[] subscores = null, AddScoreCallback callback = null )
{
if ( !IsValid ) return false;
if ( subscores == null ) subscores = new int[0];
var flags = LeaderboardUploadScoreMethod.ForceUpdate;
if ( onlyIfBeatsOldScore ) flags = LeaderboardUploadScoreMethod.KeepBest;
client.native.userstats.UploadLeaderboardScore( BoardId, flags, score, subscores, subscores.Length, callback != null ? (Action<LeaderboardScoreUploaded_t, bool>) (( result, error ) =>
{
callback( !error && result.Success != 0, new AddScoreResult
{
Score = result.Score,
ScoreChanged = result.ScoreChanged != 0,
GlobalRankNew = result.GlobalRankNew,
GlobalRankPrevious = result.GlobalRankPrevious
} );
}) : null );
return true;
}
/// <summary>
@ -145,6 +197,10 @@ private unsafe void ReadScores( LeaderboardScoresDownloaded_t result, List<Entry
[ThreadStatic] private static List<Entry> _sEntryBuffer;
/// <summary>
/// Callback invoked by <see cref="FetchScores(RequestType, int, int, FetchScoresCallback)"/> when
/// a query is complete.
/// </summary>
public delegate void FetchScoresCallback( bool success, Entry[] results );
/// <summary>
@ -174,14 +230,12 @@ public bool FetchScores( RequestType RequestType, int start, int end, FetchScore
return true;
}
private unsafe void OnScores( LeaderboardScoresDownloaded_t result, bool error )
private void OnScores( LeaderboardScoresDownloaded_t result, bool error )
{
IsQuerying = false;
if ( client == null ) return;
if ( error )
return;
if ( error ) return;
var list = new List<Entry>();
ReadScores( result, list );