Facepunch.Steamworks/Facepunch.Steamworks.Test/UserStatsTest.cs

223 lines
6.0 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
2019-04-17 15:20:32 +03:00
using Steamworks.Data;
namespace Steamworks
{
[TestClass]
[DeploymentItem( "steam_api64.dll" )]
2020-02-22 23:29:37 +03:00
[DeploymentItem( "steam_api.dll" )]
public class UserStatsTest
{
[TestMethod]
public async Task AchievementList()
{
foreach ( var a in SteamUserStats.Achievements )
{
2019-04-16 22:57:17 +03:00
Console.WriteLine( $"{a.Identifier}" );
Console.WriteLine( $" a.State: {a.State}" );
Console.WriteLine( $" a.UnlockTime: {a.UnlockTime}" );
2019-04-16 22:57:17 +03:00
Console.WriteLine( $" a.Name: {a.Name}" );
Console.WriteLine( $" a.Description: {a.Description}" );
Console.WriteLine( $" a.GlobalUnlocked: {a.GlobalUnlocked}" );
var icon = await a.GetIconAsync();
Console.WriteLine( $" a.Icon: {icon}" );
}
}
2019-04-16 23:22:46 +03:00
[TestMethod]
public async Task PlayerCountAsync()
{
var players = await SteamUserStats.PlayerCountAsync();
Assert.AreNotEqual( players, -1 );
Console.WriteLine( $"players: {players}" );
}
2019-04-17 10:26:31 +03:00
public async Task StoreStats()
{
var result = Result.NotSettled;
SteamUserStats.OnUserStatsStored += ( r ) =>
{
result = r;
};
SteamUserStats.StoreStats();
while ( result == Result.NotSettled )
{
await Task.Delay( 10 );
}
Assert.AreEqual( result, Result.OK );
}
2019-04-17 11:31:28 +03:00
[TestMethod]
public async Task CreateLeaderboard()
{
var leaderboard = await SteamUserStats.FindOrCreateLeaderboardAsync( "Testleaderboard", Data.LeaderboardSort.Ascending, Data.LeaderboardDisplay.Numeric );
2019-04-17 11:31:28 +03:00
Assert.IsTrue( leaderboard.HasValue );
}
[TestMethod]
public async Task FindLeaderboard()
{
var leaderboard = await SteamUserStats.FindLeaderboardAsync( "Testleaderboard" );
Assert.IsTrue( leaderboard.HasValue );
}
[TestMethod]
public async Task SubmitScore()
{
var leaderboard = await SteamUserStats.FindLeaderboardAsync( "Testleaderboard" );
Assert.IsTrue( leaderboard.HasValue );
var result = await leaderboard.Value.SubmitScoreAsync( 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.FindLeaderboardAsync( "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.FindLeaderboardAsync( "Testleaderboard" );
var friendScores = await leaderboard.Value.GetScoresFromFriendsAsync();
foreach ( var e in friendScores )
{
Console.WriteLine( $"{e.GlobalRank}: {e.Score} {e.User}" );
}
}
[TestMethod]
public async Task GetScoresAroundUserAsync()
{
var leaderboard = await SteamUserStats.FindLeaderboardAsync( "Testleaderboard" );
Assert.IsTrue( leaderboard.HasValue );
for ( int i = 1; i < 10; i++ )
{
// Get entries around user
var relativeScores = await leaderboard.Value.GetScoresAroundUserAsync( -i, i );
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()
2019-04-17 11:31:28 +03:00
{
var leaderboard = await SteamUserStats.FindLeaderboardAsync( "Testleaderboard" );
2019-04-17 11:31:28 +03:00
Assert.IsTrue( leaderboard.HasValue );
// Get top 20 global scores
var globalsScores = await leaderboard.Value.GetScoresAsync( 20 );
2019-04-17 11:31:28 +03:00
Assert.IsNotNull( globalsScores );
Console.WriteLine( $"" );
Console.WriteLine( $"Global Scores:" );
2019-04-17 11:31:28 +03:00
foreach ( var e in globalsScores )
{
Console.WriteLine( $"{e.GlobalRank}: {e.Score} {e.User}" );
}
}
2019-04-17 15:20:32 +03:00
[TestMethod]
public void GetStatInt()
{
2019-04-27 18:12:16 +03:00
var deaths = new Stat( "deaths" );
Console.WriteLine( $"{deaths.Name} {deaths.GetInt()} times" );
Console.WriteLine( $"{deaths.Name} {deaths.GetFloat()} times" );
2019-04-17 15:20:32 +03:00
2019-04-27 18:12:16 +03:00
Assert.AreNotEqual( 0, deaths.GetInt() );
2019-04-17 15:20:32 +03:00
}
2020-02-24 15:00:13 +03:00
[TestMethod]
public async Task GetFriendStats()
{
var friend = new Friend( 76561197965732579 ); // Hezzy
// Download stats
var status = await friend.RequestUserStatsAsync();
2020-02-24 15:00:13 +03:00
Assert.AreNotEqual( false, status );
var deaths = friend.GetStatInt( "deaths" );
Console.WriteLine( $"Hezzy has died {deaths} times" );
Assert.AreNotEqual( 0, deaths );
var unlocked = friend.GetAchievement( "COLLECT_100_WOOD" );
Assert.AreNotEqual( false, unlocked );
var when = friend.GetAchievementUnlockTime( "COLLECT_100_WOOD" );
Assert.AreNotEqual( when, DateTime.MinValue );
Console.WriteLine( $"Hezzy unlocked COLLECT_100_WOOD {when}" );
}
2019-04-17 15:20:32 +03:00
[TestMethod]
public async Task GetStatGlobalInt()
{
2019-04-27 18:12:16 +03:00
var deaths = new Stat( "deaths" );
await deaths.GetGlobalIntDaysAsync( 5 );
2019-04-17 15:20:32 +03:00
2019-04-27 18:12:16 +03:00
var totalStartups = deaths.GetGlobalInt();
2019-04-17 15:20:32 +03:00
Assert.AreNotEqual( 0, totalStartups );
2019-04-27 18:12:16 +03:00
Console.WriteLine( $"Rust has had {totalStartups} deaths" );
2019-04-17 15:20:32 +03:00
}
[TestMethod]
public async Task GetStatGlobalHistoryInt()
{
2019-04-27 18:12:16 +03:00
var deaths = new Stat( "deaths" );
2019-04-17 15:20:32 +03:00
var history = await deaths.GetGlobalIntDaysAsync( 10 );
2019-04-17 15:33:38 +03:00
Assert.AreNotEqual( 0, history.Length );
2019-04-17 15:20:32 +03:00
2019-04-17 15:33:38 +03:00
for ( int i=0; i< history.Length; i++ )
2019-04-17 15:20:32 +03:00
{
Console.WriteLine( $"{i} : {history[i]}" );
}
}
2019-04-16 23:22:46 +03:00
}
}