From 4b8e1e33affe0b6cd3b8fc4902cdcd02159769b1 Mon Sep 17 00:00:00 2001 From: thesupersoup <40295603+thesupersoup@users.noreply.github.com> Date: Mon, 24 Jun 2019 20:13:48 -0700 Subject: [PATCH] LobbyQuery Numerical Filters, NearValFilter, SteamClient and Friend OwnsLobby methods Added LobbyQuery numerical filters for WithLower, WithHigher, WithEqual, and WithNotEqual; added OrderByNear for near value filters. Added OwnsLobby method to SteamClient and Friend to test for ownership. --- Facepunch.Steamworks/SteamClient.cs | 8 +- Facepunch.Steamworks/Structs/Friend.cs | 5 + Facepunch.Steamworks/Structs/LobbyQuery.cs | 106 +++++++++++++++++++-- 3 files changed, 112 insertions(+), 7 deletions(-) diff --git a/Facepunch.Steamworks/SteamClient.cs b/Facepunch.Steamworks/SteamClient.cs index d7154e6..a19dc59 100644 --- a/Facepunch.Steamworks/SteamClient.cs +++ b/Facepunch.Steamworks/SteamClient.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; +using Steamworks.Data; namespace Steamworks { @@ -105,7 +106,7 @@ namespace Steamworks SteamParties.Shutdown(); SteamNetworkingUtils.Shutdown(); SteamNetworkingSockets.Shutdown(); - + ServerList.Base.Shutdown(); SteamAPI.Shutdown(); @@ -169,6 +170,11 @@ namespace Steamworks /// public static AppId AppId { get; internal set; } + /// + /// Check if this SteamClient owns the specified lobby + /// + public static bool OwnsLobby( Lobby k ) => k.Owner.Id == SteamId; + /// /// Checks if your executable was launched through Steam and relaunches it through Steam if it wasn't /// this returns true then it starts the Steam client if required and launches your game again through it, diff --git a/Facepunch.Steamworks/Structs/Friend.cs b/Facepunch.Steamworks/Structs/Friend.cs index 5a3da5e..bf717a5 100644 --- a/Facepunch.Steamworks/Structs/Friend.cs +++ b/Facepunch.Steamworks/Structs/Friend.cs @@ -71,6 +71,11 @@ namespace Steamworks /// public bool IsSnoozing => State == FriendState.Snooze; + /// + /// Check if this Friend owns the specified lobby + /// + public bool OwnsLobby( Lobby k ) => k.Owner.Id == this.Id; + public Relationship Relationship => SteamFriends.Internal.GetFriendRelationship( Id ); diff --git a/Facepunch.Steamworks/Structs/LobbyQuery.cs b/Facepunch.Steamworks/Structs/LobbyQuery.cs index 8a84b9e..14dc8d5 100644 --- a/Facepunch.Steamworks/Structs/LobbyQuery.cs +++ b/Facepunch.Steamworks/Structs/LobbyQuery.cs @@ -7,11 +7,7 @@ namespace Steamworks.Data { // TODO FILTERS // AddRequestLobbyListStringFilter - // - WithKeyValue, WithoutKeyValue - // AddRequestLobbyListNumericalFilter - // - WithLower, WithHigher, WithEqual, WithNotEqual - // AddRequestLobbyListNearValueFilter - // - OrderByNear + // - WithoutKeyValue #region Distance Filter internal LobbyDistanceFilter? distance; @@ -67,6 +63,89 @@ namespace Steamworks.Data } #endregion + #region Numerical filters + internal Dictionary, LobbyComparison> numericalFilters; + + /// + /// Numerical filter where value is less than the value provided + /// + public LobbyQuery WithLower( string key, int value ) + { + AddNumericalFilter( key, value, LobbyComparison.LessThan ); + return this; + } + + /// + /// Numerical filter where value is greater than the value provided + /// + public LobbyQuery WithHigher( string key, int value ) + { + AddNumericalFilter( key, value, LobbyComparison.GreaterThan ); + return this; + } + + /// + /// Numerical filter where value must be equal to the value provided + /// + public LobbyQuery WithEqual( string key, int value ) + { + AddNumericalFilter( key, value, LobbyComparison.Equal ); + return this; + } + + /// + /// Numerical filter where value must not equal the value provided + /// + public LobbyQuery WithNotEqual( string key, int value ) + { + AddNumericalFilter( key, value, LobbyComparison.NotEqual ); + return this; + } + + /// + /// Test key, initialize numerical filter dictionary if necessary, then add new numerical filter + /// + internal LobbyQuery AddNumericalFilter( string key, int value, LobbyComparison compare ) + { + if ( string.IsNullOrEmpty( key ) ) + throw new System.ArgumentException( "Key string provided for LobbyQuery filter is null or empty", nameof( key ) ); + + if ( key.Length > SteamMatchmaking.MaxLobbyKeyLength ) + throw new System.ArgumentException( $"Key length is longer than {SteamMatchmaking.MaxLobbyKeyLength}", nameof( key ) ); + + if ( numericalFilters == null ) + numericalFilters = new Dictionary, LobbyComparison>(); + + numericalFilters.Add( new KeyValuePair( key, value ), compare ); + + return this; + } + #endregion + + #region Near value filter + internal Dictionary nearValFilters; + + /// + /// Order filtered results according to key/values nearest the provided key/value pair. + /// Can specify multiple near value filters; each successive filter is lower priority than the previous. + /// + public LobbyQuery OrderByNear( string key, int value ) + { + if ( string.IsNullOrEmpty( key ) ) + throw new System.ArgumentException( "Key string provided for LobbyQuery filter is null or empty", nameof( key ) ); + + if ( key.Length > SteamMatchmaking.MaxLobbyKeyLength ) + throw new System.ArgumentException( $"Key length is longer than {SteamMatchmaking.MaxLobbyKeyLength}", nameof( key ) ); + + if ( nearValFilters == null ) + nearValFilters = new Dictionary(); + + nearValFilters.Add( key, value ); + + return this; + } + #endregion + #region Slots Filter internal int? slotsAvailable; @@ -95,7 +174,6 @@ namespace Steamworks.Data #endregion - void ApplyFilters() { if ( distance.HasValue ) @@ -120,6 +198,22 @@ namespace Steamworks.Data SteamMatchmaking.Internal.AddRequestLobbyListStringFilter( k.Key, k.Value, LobbyComparison.Equal ); } } + + if( numericalFilters != null ) + { + foreach ( var n in numericalFilters ) + { + SteamMatchmaking.Internal.AddRequestLobbyListNumericalFilter( n.Key.Key, n.Key.Value, n.Value ); + } + } + + if( nearValFilters != null ) + { + foreach (var v in nearValFilters ) + { + SteamMatchmaking.Internal.AddRequestLobbyListNearValueFilter( v.Key, v.Value ); + } + } } ///