Leaderboard, more entries and submit scores

This commit is contained in:
Garry Newman 2019-04-17 10:34:47 +01:00
parent ebcd96326a
commit ddd5cc03f3
4 changed files with 174 additions and 4 deletions

View File

@ -53,26 +53,102 @@ namespace Steamworks
[TestMethod] [TestMethod]
public async Task CreateLeaderboard() public async Task CreateLeaderboard()
{ {
var leaderboard = await SteamUserStats.FindOrCreateLeaderboard( "Testleaderboard", Data.LeaderboardSort.Descending, Data.LeaderboardDisplay.Numeric ); var leaderboard = await SteamUserStats.FindOrCreateLeaderboard( "Testleaderboard", Data.LeaderboardSort.Ascending, Data.LeaderboardDisplay.Numeric );
Assert.IsTrue( leaderboard.HasValue ); Assert.IsTrue( leaderboard.HasValue );
} }
[TestMethod] [TestMethod]
public async Task FindLeaderboard() public async Task FindLeaderboard()
{
var leaderboard = await SteamUserStats.FindLeaderboard( "Testleaderboard" );
Assert.IsTrue( leaderboard.HasValue );
}
[TestMethod]
public async Task SubmitScore()
{
var leaderboard = await SteamUserStats.FindLeaderboard( "Testleaderboard" );
Assert.IsTrue( leaderboard.HasValue );
var result = await leaderboard.Value.SubmitScore( 576 );
Assert.IsTrue( result.HasValue );
Console.WriteLine( $"result.Changed: {result?.Changed}" );
Console.WriteLine( $"result.OldGlobalRank: {result?.OldGlobalRank}" );
Console.WriteLine( $"result.NewGlobalRank: {result?.NewGlobalRank}" );
Console.WriteLine( $"result.RankChange: {result?.RankChange}" );
Console.WriteLine( $"result.Score: {result?.Score}" );
}
[TestMethod]
public async Task ReplaceScore()
{
var leaderboard = await SteamUserStats.FindLeaderboard( "Testleaderboard" );
Assert.IsTrue( leaderboard.HasValue );
var result = await leaderboard.Value.ReplaceScore( 576 );
Assert.IsTrue( result.HasValue );
Console.WriteLine( $"result.Changed: {result?.Changed}" );
Console.WriteLine( $"result.OldGlobalRank: {result?.OldGlobalRank}" );
Console.WriteLine( $"result.NewGlobalRank: {result?.NewGlobalRank}" );
Console.WriteLine( $"result.RankChange: {result?.RankChange}" );
Console.WriteLine( $"result.Score: {result?.Score}" );
}
[TestMethod]
public async Task GetScoresFromFriends()
{
var leaderboard = await SteamUserStats.FindLeaderboard( "Testleaderboard" );
Assert.IsTrue( leaderboard.HasValue );
// Get entries around user
var friendScores = await leaderboard.Value.GetScoresFromFriends();
Assert.IsNotNull( friendScores );
Console.WriteLine( $"" );
Console.WriteLine( $"Friend Scores:" );
foreach ( var e in friendScores )
{
Console.WriteLine( $"{e.GlobalRank}: {e.Score} {e.User}" );
}
}
[TestMethod]
public async Task GetScoresAroundUserAsync()
{
var leaderboard = await SteamUserStats.FindLeaderboard( "Testleaderboard" );
Assert.IsTrue( leaderboard.HasValue );
// Get entries around user
var relativeScores = await leaderboard.Value.GetScoresAroundUserAsync( -5, 5 );
Assert.IsNotNull( relativeScores );
Console.WriteLine( $"" );
Console.WriteLine( $"Relative Scores:" );
foreach ( var e in relativeScores )
{
Console.WriteLine( $"{e.GlobalRank}: {e.Score} {e.User}" );
}
}
[TestMethod]
public async Task GetScoresAsync()
{ {
var leaderboard = await SteamUserStats.FindLeaderboard( "Testleaderboard" ); var leaderboard = await SteamUserStats.FindLeaderboard( "Testleaderboard" );
Assert.IsTrue( leaderboard.HasValue ); Assert.IsTrue( leaderboard.HasValue );
// Get top 20 global scores // Get top 20 global scores
var globalsScores = await leaderboard.Value.GetGlobalEntriesAsync( 20 ); var globalsScores = await leaderboard.Value.GetScoresAsync( 20 );
Assert.IsNotNull( globalsScores ); Assert.IsNotNull( globalsScores );
Console.WriteLine( $"" );
Console.WriteLine( $"Global Scores:" );
foreach ( var e in globalsScores ) foreach ( var e in globalsScores )
{ {
Console.WriteLine( $"{e.GlobalRank}: {e.Score} {e.User}" ); Console.WriteLine( $"{e.GlobalRank}: {e.Score} {e.User}" );
} }
} }
} }

View File

@ -21,8 +21,25 @@ namespace Steamworks
return $"{Name} ({Id.ToString()})"; return $"{Name} ({Id.ToString()})";
} }
/// <summary>
/// Returns true if this is the local user
/// </summary>
public bool IsMe => Id == SteamClient.SteamId;
/// <summary>
/// Return true if this is a friend
/// </summary>
public bool IsFriend => Relationship == Relationship.Friend; public bool IsFriend => Relationship == Relationship.Friend;
/// <summary>
/// Returns true if you have this user blocked
/// </summary>
public bool IsBlocked => Relationship == Relationship.Blocked; public bool IsBlocked => Relationship == Relationship.Blocked;
/// <summary>
/// Return true if this user is playing the game we're running
/// </summary>
public bool IsPlayingThisGame => GameInfo?.GameID == SteamUtils.AppId; public bool IsPlayingThisGame => GameInfo?.GameID == SteamUtils.AppId;
/// <summary> /// <summary>

View File

@ -19,11 +19,39 @@ namespace Steamworks.Data
public LeaderboardDisplay Display => SteamUserStats.Internal.GetLeaderboardDisplayType( Id ); public LeaderboardDisplay Display => SteamUserStats.Internal.GetLeaderboardDisplayType( Id );
static int[] detailsBuffer = new int[64]; static int[] detailsBuffer = new int[64];
static int[] noDetails = new int[0];
/// <summary>
/// Submit your score and replace your old score even if it was better
/// </summary>
public async Task<LeaderboardUpdate?> ReplaceScore( int score, int[] details = null )
{
if ( details == null ) details = noDetails;
var r = await SteamUserStats.Internal.UploadLeaderboardScore( Id, LeaderboardUploadScoreMethod.ForceUpdate, score, details, details.Length );
if ( !r.HasValue ) return null;
return LeaderboardUpdate.From( r.Value );
}
/// <summary>
/// Submit your new score, but won't replace your high score if it's lower
/// </summary>
public async Task<LeaderboardUpdate?> SubmitScore( int score, int[] details = null )
{
if ( details == null ) details = noDetails;
var r = await SteamUserStats.Internal.UploadLeaderboardScore( Id, LeaderboardUploadScoreMethod.KeepBest, score, details, details.Length );
if ( !r.HasValue ) return null;
return LeaderboardUpdate.From( r.Value );
}
/// <summary> /// <summary>
/// Used to query for a sequential range of leaderboard entries by leaderboard Sort. /// Used to query for a sequential range of leaderboard entries by leaderboard Sort.
/// </summary> /// </summary>
public async Task<LeaderboardEntry[]> GetGlobalEntriesAsync( int count, int offset = 1 ) public async Task<LeaderboardEntry[]> GetScoresAsync( int count, int offset = 1 )
{ {
if ( offset <= 0 ) throw new System.ArgumentException( "Should be 1+", nameof( offset ) ); if ( offset <= 0 ) throw new System.ArgumentException( "Should be 1+", nameof( offset ) );
@ -34,6 +62,33 @@ namespace Steamworks.Data
return await LeaderboardResultToEntries( r.Value ); return await LeaderboardResultToEntries( r.Value );
} }
/// <summary>
/// Used to retrieve leaderboard entries relative a user's entry. If there are not enough entries in the leaderboard
/// before or after the user's entry, Steam will adjust the range to try to return the number of entries requested.
/// For example, if the user is #1 on the leaderboard and start is set to -2, end is set to 2, Steam will return the first
/// 5 entries in the leaderboard. If The current user has no entry, this will return null.
/// </summary>
public async Task<LeaderboardEntry[]> GetScoresAroundUserAsync( int start = -10, int end = 10 )
{
var r = await SteamUserStats.Internal.DownloadLeaderboardEntries( Id, LeaderboardDataRequest.GlobalAroundUser, start, end );
if ( !r.HasValue )
return null;
return await LeaderboardResultToEntries( r.Value );
}
/// <summary>
/// Used to retrieve all leaderboard entries for friends of the current user
/// </summary>
public async Task<LeaderboardEntry[]> GetScoresFromFriends()
{
var r = await SteamUserStats.Internal.DownloadLeaderboardEntries( Id, LeaderboardDataRequest.Friends, 0, 0 );
if ( !r.HasValue )
return null;
return await LeaderboardResultToEntries( r.Value );
}
#region util #region util
internal async Task<LeaderboardEntry[]> LeaderboardResultToEntries( LeaderboardScoresDownloaded_t r ) internal async Task<LeaderboardEntry[]> LeaderboardResultToEntries( LeaderboardScoresDownloaded_t r )
{ {

View File

@ -0,0 +1,22 @@
using System.Linq;
namespace Steamworks.Data
{
public struct LeaderboardUpdate
{
public int Score;
public bool Changed;
public int NewGlobalRank;
public int OldGlobalRank;
public int RankChange => NewGlobalRank - OldGlobalRank;
internal static LeaderboardUpdate From( LeaderboardScoreUploaded_t e ) =>
new LeaderboardUpdate
{
Score = e.Score,
Changed = e.ScoreChanged == 1,
NewGlobalRank = e.GlobalRankNew,
OldGlobalRank = e.GlobalRankPrevious
};
}
}