From 7e38750f2ee7b4c2a5da988317039ca63d8f3985 Mon Sep 17 00:00:00 2001 From: Kyle Kukshtel Date: Tue, 25 Jul 2017 10:09:41 -0700 Subject: [PATCH] changes lobby initialization and makes lobbylist filter easier --- Facepunch.Steamworks.Test/Client/Lobby.cs | 32 ++++++++++ Facepunch.Steamworks/Client/Lobby.cs | 70 +++++++++++++-------- Facepunch.Steamworks/Client/LobbyList.cs | 74 ++++++++++++----------- 3 files changed, 116 insertions(+), 60 deletions(-) create mode 100644 Facepunch.Steamworks.Test/Client/Lobby.cs diff --git a/Facepunch.Steamworks.Test/Client/Lobby.cs b/Facepunch.Steamworks.Test/Client/Lobby.cs new file mode 100644 index 0000000..8f3e7b7 --- /dev/null +++ b/Facepunch.Steamworks.Test/Client/Lobby.cs @@ -0,0 +1,32 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Facepunch.Steamworks.Test +{ + [DeploymentItem("steam_api.dll")] + [DeploymentItem("steam_api64.dll")] + [DeploymentItem("steam_appid.txt")] + [TestClass] + class Lobby + { + [TestMethod] + public void FriendList() + { + /* + using (var client = new Facepunch.Steamworks.Client(252490)) + { + Assert.IsTrue(client.IsValid); + + client.Friends.Refresh(); + + Assert.IsNotNull(client.Friends.All); + + foreach (var friend in client.Friends.All) + { + Console.WriteLine("{0}: {1} (Friend:{2}) (Blocked:{3})", friend.Id, friend.Name, friend.IsFriend, friend.IsBlocked); + } + } + */ + } + } +} diff --git a/Facepunch.Steamworks/Client/Lobby.cs b/Facepunch.Steamworks/Client/Lobby.cs index 72ef977..5748672 100644 --- a/Facepunch.Steamworks/Client/Lobby.cs +++ b/Facepunch.Steamworks/Client/Lobby.cs @@ -18,9 +18,18 @@ namespace Facepunch.Steamworks //TODO: } - + // Create a lobby, auto joins the created lobby + public Lobby CreateLobby(Lobby.Type lobbyType, int maxMembers) + { + var lobby = new Lobby(this, lobbyType); + native.matchmaking.CreateLobby((SteamNative.LobbyType)lobbyType, maxMembers, lobby.OnLobbyCreatedAPI); + return lobby; + } + } + public class Lobby : IDisposable + { //The type of lobby you are creating - public enum LobbyType : int + public enum Type : int { Private = SteamNative.LobbyType.Private, FriendsOnly = SteamNative.LobbyType.FriendsOnly, @@ -28,22 +37,6 @@ namespace Facepunch.Steamworks Invisible = SteamNative.LobbyType.Invisible } - // Create a lobby, auto joins the created lobby - public Lobby CreateLobby(LobbyType lobbyType, int maxMembers, string name) - { - var lobby = new Lobby(this); - native.matchmaking.CreateLobby((SteamNative.LobbyType)lobbyType, maxMembers, lobby.OnLobbyCreated); - if (lobby.IsValid) - { - lobby.Name = name; - lobby.MaxMembers = maxMembers; - lobby.LobbyType = lobbyType; - } - return lobby; - } - } - public class Lobby : IDisposable - { internal Client client; /// @@ -68,16 +61,23 @@ namespace Facepunch.Steamworks /// public string Name { - get { return LobbyData["name"]; } - set { SetLobbyData("name", value); } + get { return _name; } + set { if (_name == value) return; SetLobbyData("name", value); } } + string _name = ""; - public Lobby(Client c) + /// + /// Callback for when lobby is created + /// + public Action OnLobbyCreated; + + public Lobby(Client c, Type type) { client = c; + LobbyType = type; } - internal void OnLobbyCreated(LobbyCreated_t callback, bool error) + internal void OnLobbyCreatedAPI(LobbyCreated_t callback, bool error) { //from SpaceWarClient.cpp 793 if (error || (callback.Result != Result.OK)) @@ -88,6 +88,10 @@ namespace Facepunch.Steamworks Owner = client.SteamId; //this is implicitly set on creation but need to cache it here LobbyID = callback.SteamIDLobby; + MaxMembers = client.native.matchmaking.GetLobbyMemberLimit(LobbyID); + SetLobbyData("appid", client.AppId.ToString()); + + if (OnLobbyCreated != null) { OnLobbyCreated(); } } Dictionary LobbyData = new Dictionary(); @@ -118,12 +122,21 @@ namespace Facepunch.Steamworks client.native.matchmaking.DeleteLobbyData(LobbyID, key); } - public Client.LobbyType LobbyType + public Type LobbyType { get { return _lobbyType; } - set { if (_lobbyType == value) return; client.native.matchmaking.SetLobbyType(LobbyID, (SteamNative.LobbyType)value); _lobbyType = value; } //returns bool + set + { + if (_lobbyType == value) return; + //only call the proper method if the lobby is valid, otherwise cache the value + if(IsValid) + { + client.native.matchmaking.SetLobbyType(LobbyID, (SteamNative.LobbyType)value); //returns bool? + } + _lobbyType = value; + } } - Client.LobbyType _lobbyType; + Type _lobbyType; //Must be the owner to change the owner @@ -158,6 +171,13 @@ namespace Facepunch.Steamworks } int _maxMembers = 0; + //How many people are currently in the lobby + public int NumMembers + { + get { return client.native.matchmaking.GetNumLobbyMembers(LobbyID);} + } + + //leave the current lobby public void Leave() { client.native.matchmaking.LeaveLobby(LobbyID); diff --git a/Facepunch.Steamworks/Client/LobbyList.cs b/Facepunch.Steamworks/Client/LobbyList.cs index b4e7fd4..1019c4e 100644 --- a/Facepunch.Steamworks/Client/LobbyList.cs +++ b/Facepunch.Steamworks/Client/LobbyList.cs @@ -15,36 +15,42 @@ namespace Facepunch.Steamworks this.client = client; } - public void Refresh ( LobbyFilter filter = null) + public void Refresh ( Filter filter = null) { - if(filter != null) + if(filter == null) { - client.native.matchmaking.AddRequestLobbyListDistanceFilter((SteamNative.LobbyDistanceFilter)filter.DistanceFilter); - client.native.matchmaking.AddRequestLobbyListFilterSlotsAvailable(filter.SlotsAvailable); - client.native.matchmaking.AddRequestLobbyListResultCountFilter(filter.MaxResults); - foreach (KeyValuePair fil in filter.StringFilters) - { - client.native.matchmaking.AddRequestLobbyListStringFilter(fil.Key, fil.Value, SteamNative.LobbyComparison.Equal); - } - foreach (KeyValuePair fil in filter.NearFilters) - { - client.native.matchmaking.AddRequestLobbyListNearValueFilter(fil.Key, fil.Value); - } - foreach (KeyValuePair> fil in filter.NumericalFilters) - { - client.native.matchmaking.AddRequestLobbyListNumericalFilter(fil.Key, fil.Value.Value, (SteamNative.LobbyComparison)fil.Value.Key); - } + filter = new Filter(); + filter.StringFilters.Add("appid", client.AppId.ToString()); } + client.native.matchmaking.AddRequestLobbyListDistanceFilter((SteamNative.LobbyDistanceFilter)filter.DistanceFilter); + + if(filter.SlotsAvailable != null) + { + client.native.matchmaking.AddRequestLobbyListFilterSlotsAvailable((int)filter.SlotsAvailable); + } + + if (filter.MaxResults != null) + { + client.native.matchmaking.AddRequestLobbyListResultCountFilter((int)filter.MaxResults); + } + + foreach (KeyValuePair fil in filter.StringFilters) + { + client.native.matchmaking.AddRequestLobbyListStringFilter(fil.Key, fil.Value, SteamNative.LobbyComparison.Equal); + } + foreach (KeyValuePair fil in filter.NearFilters) + { + client.native.matchmaking.AddRequestLobbyListNearValueFilter(fil.Key, fil.Value); + } + foreach (KeyValuePair> fil in filter.NumericalFilters) + { + client.native.matchmaking.AddRequestLobbyListNumericalFilter(fil.Key, fil.Value.Value, (SteamNative.LobbyComparison)fil.Value.Key); + } + + // this will never return lobbies that are full (via the actual api) client.native.matchmaking.RequestLobbyList(OnLobbyList); - - /* - if (filter == null) may need this if we are getting too many lobbies - { - filter = new Filter(); - //filter.Add("appid", client.AppId.ToString()); - }*/ } @@ -69,14 +75,19 @@ namespace Facepunch.Steamworks } } + + if (OnLobbiesRefreshed != null) { OnLobbiesRefreshed(); } } + + public Action OnLobbiesRefreshed; + public void Dispose() { client = null; } - public class LobbyFilter + public class Filter { // Filters that match actual metadata keys exactly public Dictionary StringFilters = new Dictionary(); @@ -84,9 +95,9 @@ namespace Facepunch.Steamworks public Dictionary NearFilters = new Dictionary(); //Filters that are of string key and int value, with a comparison filter to say how we should relate to the value public Dictionary> NumericalFilters = new Dictionary>(); - public Distance DistanceFilter { get; set; } - public int SlotsAvailable { get; set; } - public int MaxResults { get; set; } + public Distance DistanceFilter = Distance.Worldwide; + public int? SlotsAvailable { get; set; } + public int? MaxResults { get; set; } public enum Distance : int { @@ -105,13 +116,6 @@ namespace Facepunch.Steamworks EqualToOrGreaterThan = SteamNative.LobbyComparison.EqualToOrGreaterThan, NotEqual = SteamNative.LobbyComparison.NotEqual } - - public LobbyFilter(Distance distanceFilter, int slotsAvailable, int maxResults) - { - DistanceFilter = distanceFilter; - SlotsAvailable = slotsAvailable; - MaxResults = maxResults; - } } } }