Stats callbacks

This commit is contained in:
Garry Newman 2019-04-17 08:26:31 +01:00
parent bc658d0a31
commit d2a3e0b1da
7 changed files with 100 additions and 11 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props')" />
<Import Project="..\packages\MSTest.TestAdapter.2.0.0-beta4\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.2.0.0-beta4\build\net45\MSTest.TestAdapter.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@ -76,10 +76,10 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.4.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
<HintPath>..\packages\MSTest.TestFramework.2.0.0-beta4\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.4.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
<HintPath>..\packages\MSTest.TestFramework.2.0.0-beta4\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.2-beta1\lib\net45\Newtonsoft.Json.dll</HintPath>
@ -114,10 +114,10 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.0.0-beta4\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.0.0-beta4\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.0.0-beta4\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.0.0-beta4\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets')" />
<Import Project="..\packages\MSTest.TestAdapter.2.0.0-beta4\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.2.0.0-beta4\build\net45\MSTest.TestAdapter.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

View File

@ -29,6 +29,27 @@ public void AchievementList()
}
}
[TestMethod]
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 );
}
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSTest.TestAdapter" version="1.4.0" targetFramework="net46" />
<package id="MSTest.TestFramework" version="1.4.0" targetFramework="net46" />
<package id="MSTest.TestAdapter" version="2.0.0-beta4" targetFramework="net46" />
<package id="MSTest.TestFramework" version="2.0.0-beta4" targetFramework="net46" />
<package id="Newtonsoft.Json" version="9.0.2-beta1" targetFramework="net452" />
</packages>

View File

@ -35,15 +35,16 @@ public static void Init( uint appid )
SteamUser.InstallEvents();
SteamFriends.InstallEvents();
SteamScreenshots.InstallEvents();
SteamUserStats.InstallEvents();
RunCallbacks();
RunCallbacksAsync();
}
public static bool IsValid => initialized;
internal static async void RunCallbacks()
internal static async void RunCallbacksAsync()
{
while ( true )
while ( IsValid )
{
await Task.Delay( 16 );
try

View File

@ -21,6 +21,47 @@ internal static ISteamUserStats Internal
}
}
public static bool StatsRecieved { get; internal set; }
internal static void InstallEvents()
{
new Event<UserStatsReceived_t>( x =>
{
if ( x.SteamIDUser == SteamClient.SteamId )
StatsRecieved = true;
OnUserStatsReceived?.Invoke( x.SteamIDUser, x.Result );
} );
new Event<UserStatsStored_t>( x => OnUserStatsStored?.Invoke( x.Result ) );
new Event<UserAchievementStored_t>( x => OnAchievementProgress?.Invoke( x.AchievementName, (int) x.CurProgress, (int)x.MaxProgress ) );
new Event<UserStatsUnloaded_t>( x => OnUserStatsUnloaded?.Invoke( x.SteamIDUser ) );
}
/// <summary>
/// called when the latests stats and achievements have been received
/// from the server
/// </summary>
public static event Action<SteamId, Result> OnUserStatsReceived;
/// <summary>
/// result of a request to store the user stats for a game
/// </summary>
public static event Action<Result> OnUserStatsStored;
/// <summary>
/// result of a request to store the achievements for a game, or an
/// "indicate progress" call. If both m_nCurProgress and m_nMaxProgress
/// are zero, that means the achievement has been fully unlocked
/// </summary>
public static event Action<string, int, int> OnAchievementProgress;
/// <summary>
/// Callback indicating that a user's stats have been unloaded
/// </summary>
public static event Action<SteamId> OnUserStatsUnloaded;
/// <summary>
/// Get the available achievements
/// </summary>
@ -34,5 +75,31 @@ public static IEnumerable<Achievement> Achievements
}
}
}
/// <summary>
/// Send the changed stats and achievements data to the server for permanent storage.
/// If this fails then nothing is sent to the server. It's advisable to keep trying until the call is successful.
/// This call can be rate limited. Call frequency should be on the order of minutes, rather than seconds.You should only be calling this during major state changes such as the end of a round, the map changing, or the user leaving a server. This call is required to display the achievement unlock notification dialog though, so if you have called SetAchievement then it's advisable to call this soon after that.
/// If you have stats or achievements that you have saved locally but haven't uploaded with this function when your application process ends then this function will automatically be called.
/// You can find additional debug information written to the %steam_install%\logs\stats_log.txt file.
/// This function returns true upon success if :
/// RequestCurrentStats has completed and successfully returned its callback AND
/// the current game has stats associated with it in the Steamworks Partner backend, and those stats are published.
/// </summary>
public static bool StoreStats()
{
return Internal.StoreStats();
}
/// <summary>
/// Asynchronously request the user's current stats and achievements from the server.
/// You must always call this first to get the initial status of stats and achievements.
/// Only after the resulting callback comes back can you start calling the rest of the stats
/// and achievement functions for the current user.
/// </summary>
public static bool RequestCurrentStats()
{
return Internal.RequestCurrentStats();
}
}
}