UserStats (known bug in GlobalUnlockedPercentage)

This commit is contained in:
Garry Newman 2019-04-16 17:22:56 +01:00
parent f4e2efaca1
commit 7e02ad736d
5 changed files with 174 additions and 0 deletions

View File

@ -89,6 +89,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="UserTest.cs" />
<Compile Include="UserStatsTest.cs" />
<Compile Include="UtilsTest.cs" />
<Compile Include="AppTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -0,0 +1,32 @@
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Steamworks
{
[TestClass]
[DeploymentItem( "steam_api64.dll" )]
public class UserStatsTest
{
[TestMethod]
public void AchievementList()
{
foreach ( var a in SteamUserStats.Achievements )
{
Console.WriteLine( $"{a.Value}" );
Console.WriteLine( $" a.State: {a.State}" );
Console.WriteLine( $" a.UnlockTime: {a.UnlockTime}" );
Console.WriteLine( $" a.GlobalUnlockedPercentage: {a.GlobalUnlockedPercentage}" );
var icon = a.GetIcon();
Console.WriteLine( $" a.Icon: {icon}" );
}
}
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Steamworks.Data;
namespace Steamworks
{
public static class SteamUserStats
{
static ISteamUserStats _internal;
internal static ISteamUserStats Internal
{
get
{
if ( _internal == null )
_internal = new ISteamUserStats();
return _internal;
}
}
/// <summary>
/// Get the available achievements
/// </summary>
public static IEnumerable<Achievement> Achievements
{
get
{
for( int i=0; i< Internal.GetNumAchievements(); i++ )
{
yield return new Achievement( Internal.GetAchievementName( (uint) i ) );
}
}
}
}
}

View File

@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Steamworks.Data
{
public struct Achievement
{
public string Value;
public Achievement( string name )
{
Value = name;
}
public override string ToString() => Value;
/// <summary>
/// True if unlocked
/// </summary>
public bool State
{
get
{
var state = false;
SteamUserStats.Internal.GetAchievement( Value, ref state );
return state;
}
}
/// <summary>
/// Should hold the unlock time if State is true
/// </summary>
public DateTime? UnlockTime
{
get
{
var state = false;
uint time = 0;
if ( !SteamUserStats.Internal.GetAchievementAndUnlockTime( Value, ref state, ref time ) || !state )
return null;
return Epoch.ToDateTime( time );
}
}
/// <summary>
/// Gets the icon of the achievement
/// </summary>
public Image? GetIcon()
{
return SteamUtils.GetImage( SteamUserStats.Internal.GetAchievementIcon( Value ) );
}
/// <summary>
/// Returns the percentage of users who have unlocked the specified achievement, or -1 if no data available.
/// </summary>
public float GlobalUnlockedPercentage
{
get
{
float pct = 0;
if ( !SteamUserStats.Internal.GetAchievementAchievedPercent( Value, ref pct ) )
return -1.0f;
return pct;
}
}
/// <summary>
/// Make this achievement earned
/// </summary>
public bool Trigger( bool apply = true )
{
var r = SteamUserStats.Internal.SetAchievement( Value );
if ( apply && r )
{
SteamUserStats.Internal.StoreStats();
}
return r;
}
/// <summary>
/// Reset this achievement to not achieved
/// </summary>
public bool Clear()
{
return SteamUserStats.Internal.ClearAchievement( Value );
}
}
}

View File

@ -23,6 +23,11 @@ namespace Steamworks.Data
return c;
}
public override string ToString()
{
return $"{Width}x{Height} ({Data.Length}bytes)";
}
}
public struct Color