Calls on an unresolved leaderboard are deferred

Applies to:
* AddScore( bool onlyIfBeatsOldScore, int score, params int[] subscores )
* AddScore( bool onlyIfBeatsOldScore, int score, int[] subscores = null, AddScoreCallback onSuccess = null, FailureCallback onFailure = null )
* AttachRemoteFile( RemoteFile file, AttachRemoteFileCallback onSuccess = null, FailureCallback onFailure = null )
* FetchScores( RequestType RequestType, int start, int end, FetchScoresCallback onSuccess, FailureCallback onFailure = null )
This commit is contained in:
James King 2017-07-28 15:38:07 +01:00
parent 7eb4a88cee
commit 1c04459cdb

View File

@ -35,6 +35,8 @@ namespace Facepunch.Steamworks
internal ulong BoardId; internal ulong BoardId;
internal Client client; internal Client client;
private readonly Queue<Action> _onCreated = new Queue<Action>();
/// <summary> /// <summary>
/// The results from the last query. Can be null. /// The results from the last query. Can be null.
/// </summary> /// </summary>
@ -77,14 +79,35 @@ namespace Facepunch.Steamworks
client = null; client = null;
} }
private void DispatchOnCreatedCallbacks()
{
while ( _onCreated.Count > 0 )
{
_onCreated.Dequeue()();
}
}
private bool DeferOnCreated( Action onValid, FailureCallback onFailure = null )
{
if ( IsValid || IsError ) return false;
_onCreated.Enqueue( () =>
{
if ( IsValid ) onValid();
else onFailure?.Invoke( Callbacks.Result.Fail );
} );
return true;
}
internal void OnBoardCreated( LeaderboardFindResult_t result, bool error ) internal void OnBoardCreated( LeaderboardFindResult_t result, bool error )
{ {
if ( error || ( result.LeaderboardFound == 0 ) ) if ( error || ( result.LeaderboardFound == 0 ) )
{ {
IsError = true; IsError = true;
return;
} }
else
{
BoardId = result.SteamLeaderboard; BoardId = result.SteamLeaderboard;
if ( IsValid ) if ( IsValid )
@ -94,6 +117,9 @@ namespace Facepunch.Steamworks
} }
} }
DispatchOnCreatedCallbacks();
}
/// <summary> /// <summary>
/// Add a score to this leaderboard. /// Add a score to this leaderboard.
/// Subscores are totally optional, and can be used for other game defined data such as laps etc.. although /// Subscores are totally optional, and can be used for other game defined data such as laps etc.. although
@ -103,7 +129,8 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
public bool AddScore( bool onlyIfBeatsOldScore, int score, params int[] subscores ) public bool AddScore( bool onlyIfBeatsOldScore, int score, params int[] subscores )
{ {
if ( !IsValid ) return false; if ( IsError ) return false;
if ( !IsValid ) return DeferOnCreated( () => AddScore( onlyIfBeatsOldScore, score, subscores ) );
var flags = LeaderboardUploadScoreMethod.ForceUpdate; var flags = LeaderboardUploadScoreMethod.ForceUpdate;
if ( onlyIfBeatsOldScore ) flags = LeaderboardUploadScoreMethod.KeepBest; if ( onlyIfBeatsOldScore ) flags = LeaderboardUploadScoreMethod.KeepBest;
@ -142,7 +169,8 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
public bool AddScore( bool onlyIfBeatsOldScore, int score, int[] subscores = null, AddScoreCallback onSuccess = null, FailureCallback onFailure = null ) public bool AddScore( bool onlyIfBeatsOldScore, int score, int[] subscores = null, AddScoreCallback onSuccess = null, FailureCallback onFailure = null )
{ {
if ( !IsValid ) return false; if ( IsError ) return false;
if ( !IsValid ) return DeferOnCreated( () => AddScore( onlyIfBeatsOldScore, score, subscores, onSuccess, onFailure ), onFailure );
if ( subscores == null ) subscores = new int[0]; if ( subscores == null ) subscores = new int[0];
@ -182,7 +210,8 @@ namespace Facepunch.Steamworks
/// <returns>True if the file attachment process has started</returns> /// <returns>True if the file attachment process has started</returns>
public bool AttachRemoteFile( RemoteFile file, AttachRemoteFileCallback onSuccess = null, FailureCallback onFailure = null ) public bool AttachRemoteFile( RemoteFile file, AttachRemoteFileCallback onSuccess = null, FailureCallback onFailure = null )
{ {
if ( !IsValid ) return false; if ( IsError ) return false;
if ( !IsValid ) return DeferOnCreated( () => AttachRemoteFile( file, onSuccess, onFailure ), onFailure );
if ( file.IsShared ) if ( file.IsShared )
{ {
@ -260,7 +289,8 @@ namespace Facepunch.Steamworks
/// <returns>Returns true if we have started the query</returns> /// <returns>Returns true if we have started the query</returns>
public bool FetchScores( RequestType RequestType, int start, int end, FetchScoresCallback onSuccess, FailureCallback onFailure = null ) public bool FetchScores( RequestType RequestType, int start, int end, FetchScoresCallback onSuccess, FailureCallback onFailure = null )
{ {
if ( !IsValid ) return false; if ( IsError ) return false;
if ( !IsValid ) return DeferOnCreated( () => FetchScores( RequestType, start, end, onSuccess, onFailure ), onFailure );
client.native.userstats.DownloadLeaderboardEntries( BoardId, (LeaderboardDataRequest) RequestType, start, end, ( result, error ) => client.native.userstats.DownloadLeaderboardEntries( BoardId, (LeaderboardDataRequest) RequestType, start, end, ( result, error ) =>
{ {