From 527ce45d849bac09532bf4f529d4918f96e9d0d1 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Mon, 3 Oct 2016 23:09:11 +0100 Subject: [PATCH] Server Stats --- .../Facepunch.Steamworks.Test.csproj | 1 + Facepunch.Steamworks.Test/Server/Server.cs | 2 +- Facepunch.Steamworks.Test/Server/Stats.cs | 34 +++++++ .../Facepunch.Steamworks.csproj | 1 + Facepunch.Steamworks/Server.cs | 5 + Facepunch.Steamworks/Server/Stats.cs | 96 +++++++++++++++++++ 6 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 Facepunch.Steamworks.Test/Server/Stats.cs create mode 100644 Facepunch.Steamworks/Server/Stats.cs diff --git a/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj b/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj index c87f1b0..9ff3737 100644 --- a/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj +++ b/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj @@ -93,6 +93,7 @@ + diff --git a/Facepunch.Steamworks.Test/Server/Server.cs b/Facepunch.Steamworks.Test/Server/Server.cs index ca7f70c..15c4b55 100644 --- a/Facepunch.Steamworks.Test/Server/Server.cs +++ b/Facepunch.Steamworks.Test/Server/Server.cs @@ -9,7 +9,7 @@ namespace Facepunch.Steamworks.Test [DeploymentItem( "vstdlib_s.dll" )] [DeploymentItem( "steamclient.dll" )] [TestClass] - public class Server + public partial class Server { [TestMethod] public void Init() diff --git a/Facepunch.Steamworks.Test/Server/Stats.cs b/Facepunch.Steamworks.Test/Server/Stats.cs new file mode 100644 index 0000000..5bb69e1 --- /dev/null +++ b/Facepunch.Steamworks.Test/Server/Stats.cs @@ -0,0 +1,34 @@ +using System; +using System.Text; +using System.Threading; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Facepunch.Steamworks.Test +{ + public partial class Server + { + [TestMethod] + public void StatsGet() + { + using ( var server = new Facepunch.Steamworks.Server( 252490, 0, 30002, true, "VersionString" ) ) + { + Assert.IsTrue( server.Valid ); + server.LogOnAnonymous(); + + ulong MySteamId = 76561197960279927; + + server.Stats.Refresh( MySteamId ); + + // TODO - Callback on complete + + Thread.Sleep( 2000 ); + + var deaths = server.Stats.GetInt( MySteamId, "deaths", -1 ); + + Console.WriteLine( "Deaths: {0}", deaths ); + Assert.IsTrue( deaths > 0 ); + } + } + + } +} diff --git a/Facepunch.Steamworks/Facepunch.Steamworks.csproj b/Facepunch.Steamworks/Facepunch.Steamworks.csproj index 29aaec7..ec01503 100644 --- a/Facepunch.Steamworks/Facepunch.Steamworks.csproj +++ b/Facepunch.Steamworks/Facepunch.Steamworks.csproj @@ -135,6 +135,7 @@ + diff --git a/Facepunch.Steamworks/Server.cs b/Facepunch.Steamworks/Server.cs index fa5feaf..751fa51 100644 --- a/Facepunch.Steamworks/Server.cs +++ b/Facepunch.Steamworks/Server.cs @@ -352,5 +352,10 @@ namespace Facepunch.Steamworks { native.gameServer.BUpdateUserData( steamid, name, (uint) score ); } + + public bool LoggedOn + { + get { return native.gameServer.BLoggedOn(); } + } } } diff --git a/Facepunch.Steamworks/Server/Stats.cs b/Facepunch.Steamworks/Server/Stats.cs new file mode 100644 index 0000000..36db54c --- /dev/null +++ b/Facepunch.Steamworks/Server/Stats.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; + +namespace Facepunch.Steamworks +{ + public partial class Server + { + ServerStats _stats; + + public ServerStats Stats + { + get + { + if ( _stats == null ) + _stats = new ServerStats( this ); + + return _stats; + } + } + } + + public class ServerStats + { + internal Server server; + + internal ServerStats( Server s ) + { + server = s; + } + + [StructLayout( LayoutKind.Sequential )] + public struct StatsReceived + { + public int Result; + public ulong SteamId; + } + + /// + /// Retrieve the stats for this user + /// + public void Refresh( ulong steamid ) + { + var handle = server.native.stats.RequestUserStats( steamid ); + } + + public void Commit( ulong steamid ) + { + server.native.stats.StoreUserStats( steamid ); + } + + /// + /// Set the named statistic for this user + /// + public bool Set( ulong steamid, string name, int stat ) + { + return server.native.stats.SetUserStat( steamid, name, stat ); + } + + /// + /// Set the named statistic for this user + /// + public bool Set( ulong steamid, string name, float stat ) + { + return server.native.stats.SetUserStat0( steamid, name, stat ); + } + + /// + /// Set the named stat for this user + /// + public int GetInt( ulong steamid, string name, int defaultValue = 0 ) + { + int data = defaultValue; + + if ( !server.native.stats.GetUserStat( steamid, name, ref data ) ) + return defaultValue; + + return data; + } + + /// + /// Set the named stat for this user + /// + public float GetFloat( ulong steamid, string name, float defaultValue = 0 ) + { + float data = defaultValue; + + if ( !server.native.stats.GetUserStat0( steamid, name, ref data ) ) + return defaultValue; + + return data; + } + } +}