2017-02-08 16:33:37 +03:00
|
|
|
|
using System;
|
2017-05-16 18:35:37 +03:00
|
|
|
|
using System.Diagnostics;
|
2017-02-08 16:33:37 +03:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
|
|
|
|
|
namespace Facepunch.Steamworks.Test
|
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
[DeploymentItem( "steam_api64.dll" )]
|
|
|
|
|
public class Leaderboard
|
|
|
|
|
{
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void GetLeaderboard()
|
|
|
|
|
{
|
|
|
|
|
using ( var client = new Steamworks.Client( 252490 ) )
|
|
|
|
|
{
|
|
|
|
|
var board = client.GetLeaderboard( "TestLeaderboard", Steamworks.Client.LeaderboardSortMethod.Ascending, Steamworks.Client.LeaderboardDisplayType.Numeric );
|
|
|
|
|
|
2018-01-23 15:33:49 +03:00
|
|
|
|
var time = Stopwatch.StartNew();
|
2017-02-08 16:33:37 +03:00
|
|
|
|
while ( !board.IsValid )
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep( 10 );
|
|
|
|
|
client.Update();
|
2018-01-23 15:33:49 +03:00
|
|
|
|
|
|
|
|
|
if (time.Elapsed.TotalSeconds > 10 )
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("board.IsValid took too long");
|
|
|
|
|
}
|
2017-02-08 16:33:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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" );
|
|
|
|
|
|
2017-04-02 13:29:06 +03:00
|
|
|
|
board.AddScore( true, 86275309, 7, 8, 9 );
|
2017-02-08 16:33:37 +03:00
|
|
|
|
|
|
|
|
|
board.FetchScores( Steamworks.Leaderboard.RequestType.Global, 0, 20 );
|
|
|
|
|
|
2018-01-23 15:33:49 +03:00
|
|
|
|
time = Stopwatch.StartNew();
|
2017-02-08 16:33:37 +03:00
|
|
|
|
while ( board.IsQuerying )
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep( 10 );
|
|
|
|
|
client.Update();
|
2018-01-23 15:33:49 +03:00
|
|
|
|
|
|
|
|
|
if (time.Elapsed.TotalSeconds > 10)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("board.IsQuerying took too long");
|
|
|
|
|
}
|
2017-02-08 16:33:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 13:59:18 +03:00
|
|
|
|
Assert.IsFalse( board.IsError );
|
|
|
|
|
Assert.IsNotNull( board.Results );
|
|
|
|
|
|
2017-02-08 16:33:37 +03:00
|
|
|
|
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() ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 15:23:19 +03:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void GetLeaderboardCallback()
|
|
|
|
|
{
|
|
|
|
|
using ( var client = new Steamworks.Client( 252490 ) )
|
|
|
|
|
{
|
|
|
|
|
var board = client.GetLeaderboard( "TestLeaderboard", Steamworks.Client.LeaderboardSortMethod.Ascending, Steamworks.Client.LeaderboardDisplayType.Numeric );
|
|
|
|
|
|
2018-01-31 14:28:47 +03:00
|
|
|
|
var time = Stopwatch.StartNew();
|
2017-05-15 15:23:19 +03:00
|
|
|
|
while ( !board.IsValid )
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep( 10 );
|
|
|
|
|
client.Update();
|
2018-01-31 14:28:47 +03:00
|
|
|
|
|
|
|
|
|
if (time.Elapsed.TotalSeconds > 10)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("board.IsValid took too long");
|
|
|
|
|
}
|
2017-05-15 15:23:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue( board.IsValid );
|
|
|
|
|
Assert.IsFalse( board.IsError );
|
|
|
|
|
Assert.IsNotNull( board.Name );
|
|
|
|
|
|
|
|
|
|
board.AddScore( true, 86275309, 7, 8, 9 );
|
|
|
|
|
|
|
|
|
|
var done = false;
|
|
|
|
|
|
2017-06-12 13:59:18 +03:00
|
|
|
|
board.FetchScores( Steamworks.Leaderboard.RequestType.Global, 0, 20, results =>
|
2017-05-15 15:23:19 +03:00
|
|
|
|
{
|
2017-05-15 16:05:36 +03:00
|
|
|
|
foreach ( var entry in results )
|
2017-05-15 15:23:19 +03:00
|
|
|
|
{
|
|
|
|
|
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;
|
2017-06-12 13:59:18 +03:00
|
|
|
|
}, error => Assert.Fail( error.ToString() ) );
|
2017-05-15 15:23:19 +03:00
|
|
|
|
|
|
|
|
|
while ( !done )
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep( 10 );
|
|
|
|
|
client.Update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-08 16:33:37 +03:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void AddScores()
|
|
|
|
|
{
|
|
|
|
|
using ( var client = new Steamworks.Client( 252490 ) )
|
|
|
|
|
{
|
|
|
|
|
var board = client.GetLeaderboard( "TestLeaderboard", Steamworks.Client.LeaderboardSortMethod.Ascending, Steamworks.Client.LeaderboardDisplayType.Numeric );
|
|
|
|
|
|
2018-01-31 14:28:47 +03:00
|
|
|
|
var time = Stopwatch.StartNew();
|
|
|
|
|
while (!board.IsValid)
|
2017-02-08 16:33:37 +03:00
|
|
|
|
{
|
2018-01-31 14:28:47 +03:00
|
|
|
|
Thread.Sleep(10);
|
2017-02-08 16:33:37 +03:00
|
|
|
|
client.Update();
|
2018-01-31 14:28:47 +03:00
|
|
|
|
|
|
|
|
|
if (time.Elapsed.TotalSeconds > 10)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("board.IsValid took too long");
|
|
|
|
|
}
|
2017-02-08 16:33:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue( board.IsValid );
|
|
|
|
|
Assert.IsFalse( board.IsError );
|
|
|
|
|
|
2017-04-02 13:29:06 +03:00
|
|
|
|
board.AddScore( true, 1234 );
|
2017-02-08 16:33:37 +03:00
|
|
|
|
|
|
|
|
|
Thread.Sleep( 10 );
|
|
|
|
|
client.Update();
|
|
|
|
|
|
2017-04-02 13:29:06 +03:00
|
|
|
|
board.AddScore( true, 34566 );
|
2017-02-08 16:33:37 +03:00
|
|
|
|
|
|
|
|
|
Thread.Sleep( 10 );
|
|
|
|
|
client.Update();
|
|
|
|
|
|
2017-04-02 13:29:06 +03:00
|
|
|
|
board.AddScore( true, 86275309, 7, 8, 9, 7, 4, 7, 98, 24, 5, 76, 124, 6 );
|
2017-02-08 16:33:37 +03:00
|
|
|
|
|
|
|
|
|
Thread.Sleep( 10 );
|
|
|
|
|
client.Update();
|
|
|
|
|
|
2017-04-02 13:29:06 +03:00
|
|
|
|
board.AddScore( false, 86275309, 7, 8, 9, 7, 4, 7, 98, 24, 5, 76, 124, 6 );
|
2017-02-08 16:33:37 +03:00
|
|
|
|
|
|
|
|
|
Thread.Sleep( 10 );
|
|
|
|
|
client.Update();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-15 16:05:36 +03:00
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void AddScoresCallback()
|
|
|
|
|
{
|
|
|
|
|
using ( var client = new Steamworks.Client( 252490 ) )
|
|
|
|
|
{
|
|
|
|
|
var board = client.GetLeaderboard( "TestLeaderboard", Steamworks.Client.LeaderboardSortMethod.Ascending, Steamworks.Client.LeaderboardDisplayType.Numeric );
|
|
|
|
|
|
2018-01-31 14:28:47 +03:00
|
|
|
|
var time = Stopwatch.StartNew();
|
|
|
|
|
while (!board.IsValid)
|
2017-05-15 16:05:36 +03:00
|
|
|
|
{
|
2018-01-31 14:28:47 +03:00
|
|
|
|
Thread.Sleep(10);
|
2017-05-15 16:05:36 +03:00
|
|
|
|
client.Update();
|
2018-01-31 14:28:47 +03:00
|
|
|
|
|
2018-02-14 17:15:02 +03:00
|
|
|
|
if ( board.IsError )
|
|
|
|
|
{
|
|
|
|
|
throw new Exception( "Board is Error" );
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 14:28:47 +03:00
|
|
|
|
if (time.Elapsed.TotalSeconds > 10)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("board.IsValid took too long");
|
|
|
|
|
}
|
2017-05-15 16:05:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue( board.IsValid );
|
|
|
|
|
Assert.IsFalse( board.IsError );
|
|
|
|
|
|
|
|
|
|
var done = false;
|
|
|
|
|
|
|
|
|
|
const int score = 5678;
|
|
|
|
|
|
2017-06-12 13:59:18 +03:00
|
|
|
|
board.AddScore( false, score, null, result =>
|
2017-05-15 16:05:36 +03:00
|
|
|
|
{
|
|
|
|
|
Assert.IsTrue( result.ScoreChanged );
|
|
|
|
|
Assert.AreEqual( result.Score, score );
|
2017-05-15 16:13:28 +03:00
|
|
|
|
|
|
|
|
|
done = true;
|
2017-06-12 13:59:18 +03:00
|
|
|
|
}, error => Assert.Fail( error.ToString() ) );
|
2017-05-15 16:05:36 +03:00
|
|
|
|
|
|
|
|
|
while ( !done )
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep( 10 );
|
|
|
|
|
client.Update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-16 18:35:37 +03:00
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void AddFileAttachment()
|
|
|
|
|
{
|
|
|
|
|
using ( var client = new Steamworks.Client( 252490 ) )
|
|
|
|
|
{
|
|
|
|
|
var board = client.GetLeaderboard( "TestLeaderboard", Steamworks.Client.LeaderboardSortMethod.Ascending, Steamworks.Client.LeaderboardDisplayType.Numeric );
|
|
|
|
|
|
2018-01-31 14:28:47 +03:00
|
|
|
|
var time = Stopwatch.StartNew();
|
|
|
|
|
while (!board.IsValid)
|
2017-05-16 18:35:37 +03:00
|
|
|
|
{
|
2018-01-31 14:28:47 +03:00
|
|
|
|
Thread.Sleep(10);
|
2017-05-16 18:35:37 +03:00
|
|
|
|
client.Update();
|
2018-01-31 14:28:47 +03:00
|
|
|
|
|
|
|
|
|
if (time.Elapsed.TotalSeconds > 10)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("board.IsValid took too long");
|
|
|
|
|
}
|
2017-05-16 18:35:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue( board.IsValid );
|
|
|
|
|
Assert.IsFalse( board.IsError );
|
|
|
|
|
|
|
|
|
|
var done = false;
|
|
|
|
|
|
|
|
|
|
const int score = 5678;
|
|
|
|
|
const string attachment = "Hello world!";
|
|
|
|
|
|
|
|
|
|
var file = client.RemoteStorage.CreateFile( "score/example.txt" );
|
|
|
|
|
file.WriteAllText( attachment );
|
|
|
|
|
|
2017-06-12 13:59:18 +03:00
|
|
|
|
Assert.IsTrue( board.AddScore( false, score, null, result =>
|
2017-05-16 18:35:37 +03:00
|
|
|
|
{
|
|
|
|
|
Assert.IsTrue( result.ScoreChanged );
|
|
|
|
|
|
2017-06-12 13:59:18 +03:00
|
|
|
|
Assert.IsTrue( board.AttachRemoteFile( file, () =>
|
2017-05-16 18:35:37 +03:00
|
|
|
|
{
|
|
|
|
|
done = true;
|
2017-06-12 13:59:18 +03:00
|
|
|
|
}, error => Assert.Fail( error.ToString() ) ) );
|
|
|
|
|
}, error => Assert.Fail( error.ToString() ) ) );
|
2017-05-16 18:35:37 +03:00
|
|
|
|
|
|
|
|
|
while ( !done )
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep( 10 );
|
|
|
|
|
client.Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
done = false;
|
|
|
|
|
|
2017-06-12 13:59:18 +03:00
|
|
|
|
Assert.IsTrue( board.FetchScores( Steamworks.Leaderboard.RequestType.GlobalAroundUser, 0, 0, entries =>
|
2017-05-16 18:35:37 +03:00
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual( 1, entries.Length );
|
|
|
|
|
Assert.IsNotNull( entries[0].AttachedFile );
|
|
|
|
|
|
2017-06-12 13:59:18 +03:00
|
|
|
|
Assert.IsTrue( entries[0].AttachedFile.Download( () =>
|
2017-05-16 18:35:37 +03:00
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual( attachment, entries[0].AttachedFile.ReadAllText() );
|
|
|
|
|
|
|
|
|
|
done = true;
|
2017-06-12 13:59:18 +03:00
|
|
|
|
}, error => Assert.Fail( error.ToString() ) ) );
|
|
|
|
|
}, error => Assert.Fail( error.ToString() ) ) );
|
2017-05-16 18:35:37 +03:00
|
|
|
|
|
|
|
|
|
while ( !done )
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep( 10 );
|
|
|
|
|
client.Update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-08 16:33:37 +03:00
|
|
|
|
}
|
|
|
|
|
}
|